PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

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

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 21-06-2008, 05:42 AM   #6
cuong86vt
Đệ tử 1 túi
 
Tham gia ngày: Jul 2007
Bài gửi: 22
:
Bây giờ mình xin được post toàn bộ code lập trình của mình lên đây, xin được các bạn tham khảo và góp ý...

Nội dung cần khởi tạo hiển thị lên LCD ban đầu:
Hàng 1 LCD: SATURDAY, 21-06-2008
Hàng 1 LCD: 02:25:00 AM
(Tức có nghĩa là thời điểm hiện tại ban đầu sẽ được cài đặt là như vậy (bằng nút nhấn ngắt ngoài RB0 trên mạch), sau khi set xong thời gian bắt đầu đếm, sau đó nếu bạn muốn xem giờ trên đồng hồ lúc này là bao nhiêu thì bật nguồn cho vdk chạy (gọi là bật đồng hồ), không muốn xem nữa thì tắt nguồn vdk (gọi là tắt đồng hồ), bạn sẽ thấy là thời gian tại bất cứ lúc nào chúng ta bất đồng hồ lên đều chính xác ý như đồng hồ thật vậy !

Code:
#include <16f877a.h>
#include <def_877a.h>
#use delay(clock = 20000000)
#fuses HS, NOPUT, NOWDT, NOBROWNOUT, NOLVP, NOPROTECT
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3, force_hw)
#include <lcd.c>

int8 second, minute, hour, date, day, month, year;
int16 year1;
int i;

/*------------ Chuyển dữ liệu mã BIN của MASTER -> dữ liệu mã BCD cho DS1307---------*/
int write(int data)                  // MASTER -> DS1307.
   {                                 //      x -> y.
      int x,y;                       //vd:   7 -> 7  = 0x07.
      x = data;                      //vd:  12 -> 18 = 0x12.
      if(x<10)
         {
            y = x;
         }
      else if(x>=10)
         {
            y = (x/10 * 6) + x;            //vd:  29 -> 41 = 0x29.
         }
      return(y);                     //vd:  35 -> 53 = 0x35.
   }
   
   
/*------------- Chuyển dữ liệu mã BCD của DS1307 -> dữ liệu mã BIN cho MASTER--------*/
int read(int data)
   {                                 // MASTER <- DS1307.
      int x,y,z;                     //      x <- y.
      y = data;                      //vd:   5 <-  5 = 0x05.
      i=0;                           //vd:  10 <- 16 = 0x10.
      if(y<10)                       //vd:  20 <- 32 = 0x20.
         {                           //vd:  30 <- 48 = 0x30.
            x = y;                   //vd:  40 <- 64 = 0x40.
            z = x;                   //vd:  50 <- 80 = 0x50.
         }                           //..... 
      else if(y>=10)
         {
            do {
                  x = y - (6 * i);        
                  z = (x/10 * 6) + x;
                  i++;
               }
           while(z!=y);
         }
      return(x);    
   }

#INT_EXT
void khoitao()              
   {           
      /*--------- khoi tao hien thi ban dau : SAT, 21-06-2008, 02:25:00 AM --------*/ 
      second = 0;                   //giay:  00.
      minute = 25;                   //phut:  25.
      hour   = 2;                   //gio:   02 (che do 24h).
      day    =  6;                   //thu 7:  SAT (SATUDAY).
      date   = 21;                   //ngay:  21. 
      month  =  6;                   //thang: 06. 
      year   =  8;                   //nam:   08.
      
      i2c_start();
      i2c_write(0xD0);               //den dia chi ds1307.
      i2c_write(0x00);               //den dia chi thanh ghi 00H.
      i2c_write(write(second));      
      i2c_write(write(minute));      
      i2c_write(write(hour));        
      i2c_write(write(day));         
      i2c_write(write(date));        
      i2c_write(write(month));       
      i2c_write(write(year));        
      i2c_start();
      i2c_write(0xD0);
      i2c_write(0x07);               //den thanh ghi dieu khien.
      i2c_write(0x10);               //tao xung vuong 1Hz.
      i2c_stop();
   }
   
void update_time()
   {
      i2c_start();
      i2c_write(0xD0);
      i2c_write(0x00);
      i2c_start();
      i2c_write(0xD1);
      second = read(i2c_read());
      minute = read(i2c_read());
      hour   = read(i2c_read());
      day    = read(i2c_read());
      date   = read(i2c_read());
      month  = read(i2c_read());
      year   = read(i2c_read(0));    
      i2c_stop();
   }
   
void hienthi_LCD()
   {
   
      /*--------------- Hàng 1 cua LCD : hien thi thu, ngay - thang - nam ------------*/ 
      
      lcd_gotoxy(1,1);
      //printf(lcd_putc,"%s",day);           //thu trong tuan.
      if(day==1)       lcd_putc("Mon");
      else if(day==2)  lcd_putc("Tue");
      else if(day==3)  lcd_putc("Wed");
      else if(day==4)  lcd_putc("Thu");
      else if(day==5)  lcd_putc("Fri");
      else if(day==6)  lcd_putc("Sat");
      else if(day==7)  lcd_putc("Sun");
                    
      lcd_gotoxy(4,1);
      lcd_putc(".");    
           
      lcd_gotoxy(6,1);
      if(date<10)
         {
            lcd_putc("0");
            lcd_gotoxy(7,1);
            printf(lcd_putc,"%d",date);            //ngay trong thang.
         }
      else if(date>=10)
         {
            printf(lcd_putc,"%d",date);            
         }
         
      lcd_gotoxy(8,1);                     
      lcd_putc("-");      
      
      lcd_gotoxy(9,1);
      //printf(lcd_putc,"%s",month);               //thang trong nam.
      if(month==1)       lcd_putc("Jan");
      else if(month==2)  lcd_putc("Feb");
      else if(month==3)  lcd_putc("Mar"); 
      else if(month==4)  lcd_putc("Apr");
      else if(month==5)  lcd_putc("May");
      else if(month==6)  lcd_putc("Jun");
      else if(month==7)  lcd_putc("Jul");
      else if(month==8)  lcd_putc("Aug");
      else if(month==9)  lcd_putc("Sep"); 
      else if(month==10) lcd_putc("Oct");
      else if(month==11) lcd_putc("Nov");
      else if(month==12) lcd_putc("Dec");
             
      lcd_gotoxy(12,1);
      lcd_putc("-");
      
      lcd_gotoxy(13,1);                        //nam.
      year1 = 2000 + year;
      printf(lcd_putc,"%ld",year1);            
      
      
      /*--------------- Hàng 2 cua LCD : hien thi gio : phut : giay ,AM/PM ------------*/
      
      lcd_gotoxy(6,2);                        //gio.  
      if(hour<10)
         {
            lcd_putc("0");
            lcd_gotoxy(7,2);
            printf(lcd_putc,"%d",hour);       
         }
      else if(hour>=10)
         {
            printf(lcd_putc,"%d",hour);            
         }
                  
      lcd_gotoxy(8,2);
      lcd_putc(":");
      
      lcd_gotoxy(9,2);                        //phut.  
      if(minute<10)
         {
            lcd_putc("0"); 
            lcd_gotoxy(10,2);
            printf(lcd_putc,"%d",minute);    
         }
      else if(minute>=10)
         {
            printf(lcd_putc,"%d",minute);   
         }
         
      lcd_gotoxy(11,2);
      lcd_putc(":");
      
      lcd_gotoxy(12,2);                     //giay.
      if(second<10)
         {
            lcd_putc("0"); 
            lcd_gotoxy(13,2);
            printf(lcd_putc,"%d",second);    
         }
      else if(second>=10)
         {
            printf(lcd_putc,"%d",second);   
         }
        
      lcd_gotoxy(15,2);
      lcd_putc("AM");               
   }
   
void main()
   {
      enable_interrupts(GLOBAL);
      enable_interrupts(INT_EXT);
      enable_interrupts(INT_RB);
      ext_int_edge(H_to_L);
      lcd_init();
      delay_ms(10);
      
      while(TRUE)
         {
            update_time(); 
            hienthi_LCD();  
         }
   }
File Kèm Theo
File Type: rar DS1307_1.rar (1.7 KB, 907 lần tải)

thay đổi nội dung bởi: cuong86vt, 21-06-2008 lúc 06:34 AM.
cuong86vt 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:06 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