PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Giao tiếp USB, CAN, I2C, SPI, USART... (http://www.picvietnam.com/forum/forumdisplay.php?f=45)
-   -   Dùng modul i2c giao tiếp ds1307 cần giúp đỡ! (http://www.picvietnam.com/forum/showthread.php?t=3340)

lequocbao 22-12-2008 12:26 PM

Dùng modul i2c giao tiếp ds1307 cần giúp đỡ!
 
mình sử dụng modul i2c giao tiếp giữa pic16f877a và ds1307 hiển thị giờ phút giây.Code như bên dưới nhưng khi chạy thì chỉ hiện toàn số 0 trên led 7 đoạn.Nhờ các bạn xem giúp đoạn code xem có sai chổ nào không?
[code/]#include<16f877a.h>
#fuses HS,NOWDT,NOLVP,NOPROTECT
#use delay(clock=4000000)
#use i2c(master,slow,sda=pin_c4,scl=pin_c3)
#use fast_io(a)
#use fast_io(b)
#use fast_io(d)
#use fast_io(e)

#define so0 0x00
#define so1 0x10
#define so2 0x20//00100000
#define so3 0x30
#define so4 0x40
#define so5 0x50
#define so6 0x60
#define so7 0x70
#define so8 0x80
#define so9 0x90
byte const digit[] = {so0,so1,so2,so3,so4,so5,so6,so7,so8,so9};

#define led1 0xfe//0xf7
#define led2 0xfd//0xfb
#define led3 0xfb//0xfd
#define led4 0xf7//0xfe
#define led5 0xef
#define led6 0xdf
#define led7 0xbf
#define led8 0x7f
void led_scan(int value,int address);
int8 hour;
int8 minute;
int8 second;
int8 dv_second;
int8 chuc_second;
int8 dv_minute;
int8 chuc_minute;
int8 dv_hour;
int8 chuc_hour;

void main()
{
set_tris_a(0x00);//cau hinh portA la cong xuat
set_tris_b(0x00);//cau hinh portB la cong nhap
set_tris_e(0x70);//cau hinh portE la cong nhap
set_tris_d(0x0f);

do
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_stop();
i2c_start();
i2c_write(0xD1);
second=i2c_read(1);
minute=i2c_read(1);
hour=i2c_read(0);
i2c_stop();

//khu vuc convert BCD to Binary--------------
dv_second=second & 0x0f;//lay 4 bit thap ra phut hang don vi
chuc_second=(second & 0x70)>>4;//lay 3 bit cao roi dich phai 4 bit thi ra hang chuc

dv_minute=minute & 0x0f;//lay 4 bit thap ra phut hang don vi
chuc_minute=(minute & 0x70)>>4;//lay 3 bit cao roi dich phai 4 bit thi ra hang chuc

dv_hour=hour & 0x0f;//lay 4 bit thap ra phut hang don vi
chuc_hour=(hour & 0x30)>>4;//lay 2 bit cao roi dich phai 4 bit thi ra hang chuc
//ket thuc khu vuc convert BCD to Binary-------------------

led_scan(chuc_hour,led1);
led_scan(dv_hour,led2);
led_scan(chuc_minute,led3);
led_scan(dv_minute,led4);
led_scan(chuc_second,led5);
led_scan(dv_second,led6);
}while(true);
}//ket thuc ham main()----------------------------------------------------------

void led_scan(int value,int address)//------------------------------------------
{
output_b(address);
output_d(digit[value]);
delay_ms(1);
output_b(0xff);
}//ket thuc ham led_scan()------------------------------------------------------[/code]

vudt2412 13-04-2012 08:45 PM

Trích:

Nguyên văn bởi lequocbao (Post 21594)
mình sử dụng modul i2c giao tiếp giữa pic16f877a và ds1307 hiển thị giờ phút giây.Code như bên dưới nhưng khi chạy thì chỉ hiện toàn số 0 trên led 7 đoạn.Nhờ các bạn xem giúp đoạn code xem có sai chổ nào không?
[code/]#include<16f877a.h>
#fuses HS,NOWDT,NOLVP,NOPROTECT
#use delay(clock=4000000)
#use i2c(master,slow,sda=pin_c4,scl=pin_c3)
#use fast_io(a)
#use fast_io(b)
#use fast_io(d)
#use fast_io(e)

#define so0 0x00
#define so1 0x10
#define so2 0x20//00100000
#define so3 0x30
#define so4 0x40
#define so5 0x50
#define so6 0x60
#define so7 0x70
#define so8 0x80
#define so9 0x90
byte const digit[] = {so0,so1,so2,so3,so4,so5,so6,so7,so8,so9};

#define led1 0xfe//0xf7
#define led2 0xfd//0xfb
#define led3 0xfb//0xfd
#define led4 0xf7//0xfe
#define led5 0xef
#define led6 0xdf
#define led7 0xbf
#define led8 0x7f
void led_scan(int value,int address);
int8 hour;
int8 minute;
int8 second;
int8 dv_second;
int8 chuc_second;
int8 dv_minute;
int8 chuc_minute;
int8 dv_hour;
int8 chuc_hour;

void main()
{
set_tris_a(0x00);//cau hinh portA la cong xuat
set_tris_b(0x00);//cau hinh portB la cong nhap
set_tris_e(0x70);//cau hinh portE la cong nhap
set_tris_d(0x0f);

do
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_stop();
i2c_start();
i2c_write(0xD1);
second=i2c_read(1);
minute=i2c_read(1);
hour=i2c_read(0);
i2c_stop();

//khu vuc convert BCD to Binary--------------
dv_second=second & 0x0f;//lay 4 bit thap ra phut hang don vi
chuc_second=(second & 0x70)>>4;//lay 3 bit cao roi dich phai 4 bit thi ra hang chuc

dv_minute=minute & 0x0f;//lay 4 bit thap ra phut hang don vi
chuc_minute=(minute & 0x70)>>4;//lay 3 bit cao roi dich phai 4 bit thi ra hang chuc

dv_hour=hour & 0x0f;//lay 4 bit thap ra phut hang don vi
chuc_hour=(hour & 0x30)>>4;//lay 2 bit cao roi dich phai 4 bit thi ra hang chuc
//ket thuc khu vuc convert BCD to Binary-------------------

led_scan(chuc_hour,led1);
led_scan(dv_hour,led2);
led_scan(chuc_minute,led3);
led_scan(dv_minute,led4);
led_scan(chuc_second,led5);
led_scan(dv_second,led6);
}while(true);
}//ket thuc ham main()----------------------------------------------------------

void led_scan(int value,int address)//------------------------------------------
{
output_b(address);
output_d(digit[value]);
delay_ms(1);
output_b(0xff);
}//ket thuc ham led_scan()------------------------------------------------------[/code]

Bạn có thể tham khảo bài code Giao tiếp giữa PIC và DS1307 hiển thị lên LCD, RS232 sau:
http://www.dientumaytinh.com/2012/04...i-ic-thoi.html
Hi vọng giúp ích cho bạn :)


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

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam