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

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
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
 


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à 04:55 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