View Single Post
Old 26-03-2009, 05:33 PM   #3
thientaisodo
Đệ tử 4 túi
 
Tham gia ngày: Dec 2008
Nơi Cư Ngụ: Hà Tĩnh - Hà Nội
Bài gửi: 61
:
Send a message via Yahoo to thientaisodo
Đo 8 kênh nhiệt độ ghi vào 24C64

Đây là code đo 8 kênh nhiệt độ bằng ADC của 877A, ghi vào 16 byte của ROM 24C64. Các kênh lần lượt được lấy mẫu sau 50ms, ghi lần lượt vào ROM. Do đo rất nhanh nên PIC phải lấy lần lượt đọc giá trị nhiệt độ từ 24C64 sau 1s và hiển thị ra LCD. Đã làm mạch thật và chạy OK rồi

Code:
#include <16F877A.h>
#include <def_877a.h>
#device *=16 ADC=10
#fuses NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP,
#use delay(clock=20000000)
#use i2c(master,sda=PIN_C4,scl=PIN_C3,force_hw,Slow)
#include <lcd_lib_4bit.c>


int16 radd,wadd,count,temp;
int8 rdata,wdata,low,high,nam_phut,chanel_adc,thutu;

void write_ext_eeprom(int16 wadd, BYTE wdata)
{
   short int status;   
   i2c_start(); // bao hieu bat dau giao tiep i2c
   i2c_write(0xa0);  // dia chi thiet bi nhan
   i2c_write(wadd>>8); // gui den dia chi cao cua thiet bi nhan (dia chi cot)
   i2c_write(wadd);  // gui den dia chi thap cua thiet bi nhan (dia chi hang)
   i2c_write(wdata);  // ghi data vao dia chi tren cua thiet bi nhan
   i2c_stop();    // bao hieu ngung ghi
   i2c_start();   // bao hieu bat dau giao tiep
   status=i2c_write(0xa0); // kiem tra trang thai cua thiet bi nhan
   while(status==1)  // lap lai cho den khi thiet bi da nhan xong (No Ack)
   {
      i2c_start();
      status=i2c_write(0xa0);
   }
   i2c_stop();
}

BYTE read_ext_eeprom(int16 radd)
{
   BYTE rdata;
   i2c_start();
   i2c_write(0xa0);
   i2c_write(radd>>8);
   i2c_write(radd);
   i2c_start();
   i2c_write(0xa1);
   rdata=i2c_read(0); // No Ack
   i2c_stop();
   return(rdata);
}

void convert_to_bcd(int8 x)
  {
   low=x%10;  //chia lay phan du, so hang don vi
   high=x/10;   //tach hang tram va hang chuc
   low = low + 0x30;
   high = high + 0x30;
  }
  
  
//Chuong trinh ngat TMR0 tinh thoi gian
  
#int_timer0
void interrupt_timer0()
   {
      ++count;
      if(count == 500)                                     // 500*100us = 50000us =50ms
         {  count=0;
            ++nam_phut;
            if(nam_phut==1)                                 // 50ms*1=50m   50ms ghi nhiet do vao ROM mot lan
               {
                   nam_phut=0;
                   disable_interrupts(int_timer0);                          // Cam ngat ngat timer0
                   
                   set_adc_channel(chanel_adc);                                     // Do lan luot cac kenh ADC
                   Temp=read_adc();
                   Temp=(Temp-558.5)/2.048;
   
   // ------------------------ Ghi vao eeprom ------------------------
                   
                   wdata=(int8)Temp;                                                 // ghi gia tri nhiet do vao rom ngoai
                   write_ext_eeprom(wadd,wdata);                   
                   ++wadd;
                   ++chanel_adc;
                   if(chanel_adc==8) chanel_adc=0;
                   
                   if(wadd==16)wadd=0;
                   enable_interrupts(int_timer0);                          //Cho phep ngat timer0
               }

         }

   }

main()
{  
    enable_interrupts (GLOBAL);
    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
    set_timer0(6);                                                        // T_dinhthi = 2*(256 - 6)*0.2us = 100us
 

    setup_adc_ports(ALL_ANALOG);                            // Khoi tao che do cho bo ADC
    setup_adc(ADC_CLOCK_INTERNAL);
    delay_us(10);
   
   radd=0x00;
   wadd=0;
   count=0x00;
   rdata=0x00;
   wdata=0;
   nam_phut=0;
   chanel_adc=0;
   thutu=1;
   
   LCD_init();           // Khoi tao LCD
   printf(LCD_putchar," Dieu Nhat Tuan ");
   lcd_putcmd(0xC0);
   printf(lcd_putchar, " thientai_sodo ");
   delay_ms(2000);

   enable_interrupts(int_timer0);                          // Khoi tao ngat timer0
   
  while(1)
   {
      rdata=read_ext_eeprom(radd);
      lcd_putcmd(0xC0);
      printf(lcd_putchar, "  Sensor ");  
      convert_to_bcd(thutu);
      LCD_putchar(low);
      printf(lcd_putchar, " : ");
      convert_to_bcd(rdata);
      LCD_putchar(high);
      LCD_putchar(low);
      delay_ms(1000);
      
      ++radd;
      ++thutu;
      if(thutu==9) thutu=1;
      
      if(radd==16) radd=0;
   
   }   
}
thientaisodo vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn