View Single Post
Old 18-05-2007, 02:40 PM   #3
An Hiep
Đệ tử 1 túi
 
An Hiep's Avatar
 
Tham gia ngày: May 2007
Bài gửi: 15
:
Nếu bạn chỉ định lưu các giá trị lấy mẫu ADC vào internal eeprom và sau mỗi lần ghi mới sẽ xóa phần ghi cũ đi thì bạn có thể tham khảo đoạn code dưới đây. Ví dụ đo điện áp <=5V trên kênh AN1 hiển thị lên LCD. Sau mỗi lần lấy mẫu mới sẽ xóa giá trị trước đó đi. Bài tập của bạn MOIHOCPIC1985 cũng tương tự thế thôi.

Code:
/*==============================================================================
* Author          :DRAGON(Hungnv0401@gmail.com)
* File Name       :Do ap va hien thi tren LCD
* Data            :10/5/2007
* Hardware        :PIC16F876A
* Compiler        :CCS C 3.249
* Description     :Voltage Measurement & Display on LCD
*=============================================================================*/

#include <16F876A.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP

#device *=16 ADC=8

#use delay(clock=10000000)
#define RS PIN_C4
#define RW PIN_C5
#define CE PIN_C7

#ifndef INTERNAL_EEPROM_UTINITIES
#define INTERNAL_EEPROM_UTINITIES

#ifndef INT_EEPROM_ADDRESS
#define INT_EEPROM_ADDRESS
#endif



int16 adc;
int16 voltage;
int8 nguyen, phan, i;

//* --------------------- Khoi tao ------------------------------------------*//
void lcd_init()
{
      output_low(RS);
      output_low(RW);
      output_low(CE);

      OUTPUT_b(0x0f);
      delay_ms(10);
      output_high(CE);
      delay_ms(1);
      output_low(CE);
}

//*---------------------- Dua byte ra LCD -----------------------------------*//
void lcd_put(byte data)
{
      output_high(RS);
      output_low(RW);
      output_low(CE);

      OUTPUT_b(data);
      delay_ms(10);
      output_high(CE);
      delay_ms(1);
      output_low(CE);
}

//*-------------------------- Chon 2 dong cho LCD ---------------------------*//
void lcd_2lines()
{
      output_low(RS);
      output_low(RW);
      output_low(CE);

      OUTPUT_b(0x38);
      delay_ms(10);
      output_high(CE);
      delay_ms(1);
      output_low(CE);
}


//*----------------------------- Gui dia chi cho LCD ------------------------*//
void lcd_add(byte add)
{
      byte real_add;
      real_add = 0x80 + add;

      output_low(RS);
      output_low(RW);
      output_low(CE);

      OUTPUT_b(real_add);
      delay_ms(10);
      output_high(CE);
      delay_ms(1);
      output_low(CE);
}


//----------------- Ghi 1 byte vao eeprom ------------------------------------//

void write_byte_eeprom(INT_EEPROM_ADDRESS address, int16 data)
{
   int8 i;

   for(i = 0; i < 2; ++i)
   {
     write_eeprom(address + i, *(&data + i));
   }
}


//*---------------- Doc 1 byte tu eeprom ------------------------------------*//

int16 read_byte_eeprom(INT_EEPROM_ADDRESS address)
{
   int8  i;
   int16 data;

   for(i = 0; i < 2; ++i)
   {
     *(&data + i) = read_eeprom(address + i);
   }

   return(data);
}


//*--------------------------- Chuong trinh chinh ---------------------------*//
void main()
{
   int16 t;
   int8 tr,ch,dv;
   delay_ms(10);
   lcd_init();
   lcd_2lines();



//*----------------- Doc du lieu tu eeprom va ghi ra LCD --------------------*//
   nguyen = read_byte_eeprom(0x110); // Tu d/chi 0x110 tro di la vung nho cua
   phan = read_byte_eeprom(0x111);   // thanh ghi da muc dich
   lcd_add(0x08);
   lcd_put(nguyen); lcd_put('.'); lcd_put(phan); lcd_put('V');
   lcd_put(32); lcd_put(32); lcd_put(32);
   delay_ms(1000);
   delay_ms(100);


   tr = read_byte_eeprom(0x112);
   ch = read_byte_eeprom(0x113);
   dv = read_byte_eeprom(0x114);
   lcd_add(0x08);
   lcd_put(tr); lcd_put(ch); lcd_put(dv);
   lcd_put(32); lcd_put(32); lcd_put(32);
   delay_ms(1000);


//*------------------------Chay chu tieu de ---------------------------------*//
   lcd_add(0x00);
   lcd_put('S'); lcd_put('t'); lcd_put('a'); lcd_put('r'); lcd_put('t');
   lcd_put('e'); lcd_put('d'); lcd_put('.'); lcd_put('.'); lcd_put('.');
   lcd_add(0x0F);lcd_put(32);
   delay_ms(500);

   lcd_add(0x07);
   lcd_put('.'); lcd_put('.'); lcd_put('.');
   lcd_add(0x0F);lcd_put(32);
   delay_ms(1000);

   lcd_add(0x00);
   lcd_put('L'); lcd_put('o'); lcd_put('a');
   lcd_put('d'); lcd_put('i'); lcd_put('n'); lcd_put('g');
   lcd_add(0x0F);lcd_put(32);
   delay_ms(5);




   for (i=1;i<=10;++i)
      {
      lcd_add(0x07);
      lcd_put('.'); lcd_put('.'); lcd_put('.');
      lcd_add(0x0F);
      lcd_put(32);
      delay_ms(100);
      lcd_add(0x07);
      lcd_put(32); lcd_put(32); lcd_put(32);
      lcd_add(0x0F);
      lcd_put(32);
      delay_ms(100);
      }

   lcd_add(0x00);
   lcd_put('V'); lcd_put('_'); lcd_put(32);
   lcd_put('M'); lcd_put('e'); lcd_put('s'); lcd_put('u'); lcd_put('r');
   lcd_put('e'); lcd_put('m'); lcd_put('e'); lcd_put('n'); lcd_put('t');
   lcd_put(32);  lcd_put(32);  lcd_put(32);  lcd_put(32);  lcd_put(32);
   lcd_put(32); lcd_put(32); lcd_put(32);
   delay_ms(1000);

   lcd_add(0x00);
   lcd_put('V'); lcd_put('o'); lcd_put('l'); lcd_put('t'); lcd_put('a');
   lcd_put('g'); lcd_put('e'); lcd_put(':');

   lcd_add(0x42);
   lcd_put('D'); lcd_put('a'); lcd_put('t');lcd_put('e');lcd_put(':');
   lcd_put('1');lcd_put('0');lcd_put('/');lcd_put('5');lcd_put('/');
   lcd_put('0');lcd_put('7');

   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(AN0_AN1_AN3);

   while(1)
   {
   set_timer1(0);
   set_adc_channel(1);
   delay_ms(10);
   adc=read_adc();
   voltage=adc*51/256;
   nguyen = voltage/10;
   phan = (voltage%10);
   nguyen = nguyen +48;
   phan = phan +48;
   lcd_add(0x08);
   lcd_put(nguyen); lcd_put('.'); lcd_put(phan); lcd_put('V');
   lcd_put(32); lcd_put(32); lcd_put(32);

      

   //* -------------- Ghi du lieu tuc thoi vao bo nho eeprom ----------------*//
   write_byte_eeprom(0x110,nguyen);
   write_byte_eeprom(0x111,phan);
   

   delay_ms(200);
   }

} // end.

thay đổi nội dung bởi: falleaf, 18-05-2007 lúc 03:42 PM.
An Hiep vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn