PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 26-03-2009, 05:04 PM   #1
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
Unhappy Các bác cho code lởm !

Em làm DS1307, lấy code của các bác về nhúng vào chip, lúc thì ra toàn "!", lúc thì ra mã ASCII, mày mò gần một buổi, tính lại công thức thì chạy đúng. (
Đây là code giao tiếp giữa 877A, LCD và DS1307

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>
#include <1307.c>

int8 day,mth,year,dow,hr,min,sec;

void hienthi()
{  int8 so1,so2,so3,so4,so5,so6,so7,so8,so9,so10,so11,so12;
                                                                                              
   LCD_init();           // Khoi tao LCD
   printf(LCD_putchar,"Thu ");                                                                 
   dow=(dow & 0x07);                  
   dow=(dow + 0x30);                                                                           
   LCD_putchar(dow);                                                              
   printf(LCD_putchar,"-");                      
   so1=(day & 0x30)>>4;                                                                               
   so1=so1 + 0x30;
   LCD_putchar(so1);                                                                     
   so2=(day & 0x0F);                                                                            
   so2=so2 + 0x30;
   LCD_putchar(so2);                                                                 
   printf(LCD_putchar,"-");     
   so3=(mth & 0x10)>>4;
   so3=so3 + 0x30;                                                                                
   LCD_putchar(so3);
   so4=mth & 0x0F;
   so4=so4 + 0x30;                                                                                 
   LCD_putchar(so4);
   printf(LCD_putchar,"-");
   so5=(year & 0xF0)>>4;
   so5=so5 + 0x30;                      
   LCD_putchar(so5);
   so6=(year & 0x0F);
   so6=so6 + 0x30;
   LCD_putchar(so6);

   
   lcd_putcmd(0xC0);
   printf(lcd_putchar, "    ");
   so7=(hr & 0x30)>>4;
   so7=so7 + 0x30;
   LCD_putchar(so7);
   so8=(hr & 0x0F);
   so8=so8 + 0x30;
   LCD_putchar(so8);   
   printf(lcd_putchar, ":");
   so9=(min & 0x70)>>4;
   so9=so9 + 0x30;   
   LCD_putchar(so9);                      
   so10=(min & 0x0F);                  
   so10=so10 + 0x30;     
   LCD_putchar(so10);   
   printf(lcd_putchar, ":");
   so11=(sec & 0x70)>>4;   
   so11=so11 + 0x30;    
   LCD_putchar(so11);
   so12=(sec & 0x0F);             
   so12=so12 + 0x30;    
   LCD_putchar(so12);   
   printf(lcd_putchar, "    ");

}


void main()
{     
/* Thoi gian khoi tao cho RTC   
      
      ds1307_init();
                                                                               
      day=0x26;    
      mth=0x03;                       
      year=0x09;                                   
      dow=0x05;
      hr=0x16;                                 
      min=0x45;
      sec=0x00;                                                  
      ds1307_setup(day,mth,year,dow,hr, min,sec);
      
*/  

   hienthi();                                               
   ds1307_init();
   
   while(1)
   {                                                              
      read_date(day,mth,year,dow);
      read_time(hr,min,sec);
      hienthi();
   }
}
Nếu khởi tạo thời gian thì cho dịch cả đoạn này

Code:
      ds1307_init();
                                                                               
      day=0x26;    
      mth=0x03;                       
      year=0x09;                                   
      dow=0x05;
      hr=0x16;                                 
      min=0x45;
      sec=0x00;                                                  
      ds1307_setup(day,mth,year,dow,hr, min,sec);


Còn đây là file 1307.c

Code:

void ds1307_init(void)
{                             
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC                                
   seconds = i2c_read(0); // Read current "seconds" in DS1307
   i2c_stop();
   seconds &= 0x7F;

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(seconds);     // Start oscillator with current "seconds value 
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Register
   i2c_write(0x90);     // Enable squarewave output pin
   i2c_stop();

}

void ds1307_setup(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
 
  sec &= 0x7F;
  hr &= 0x3F;
  

  i2c_start();
  i2c_write(0xD0);              // I2C write address
  i2c_write(0x00);              // Start at REG 0 - Seconds
  i2c_write(sec);      // REG 0        
  i2c_write(min);      // REG 1           
  i2c_write(hr);       // REG 2      
  i2c_write(dow);      // REG 3       
  i2c_write(day);      // REG 4        
  i2c_write(mth);      // REG 5
  i2c_write(year);     // REG 6
  i2c_write(0x90);              // REG 7 - Disable squarewave output pin
  i2c_stop();
}      

void read_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);               // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = i2c_read();      // REG 3
  day  = i2c_read();      // REG 4
  mth  = i2c_read();      // REG 5
  year = i2c_read(0);            // REG 6
  i2c_stop();
}

void read_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = i2c_read();
  min = i2c_read();
  hr  = i2c_read(0);
  i2c_stop();

}
thientaisodo vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 26-03-2009, 05:11 PM   #2
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
Do mạng không cho up files nên không post sơ đồ lên đc, nhưng mà cấu hình phần cứng thì như bình thường rồi, với lại đây là code dạng "thô" nên chưa được tốt, LCD còn giật giật như gà rù
thientaisodo vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
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
Old 26-03-2009, 06:14 PM   #4
lequocbao
Đệ tử 6 túi
 
lequocbao's Avatar
 
Tham gia ngày: Oct 2008
Nơi Cư Ngụ: HCM
Bài gửi: 137
:
bác cho e xin sơ đồ phần cứng thằng 77a giao tiếp với lcd nha!thank trước.
lequocbao vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 27-03-2009, 08:40 AM   #5
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
vậy thì để khi khác mình sẽ up lên cho bạn, bây giờ mạng không thể up file
thientaisodo vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 03:34 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam