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 22-12-2008, 12:26 PM   #1
lequocbao
Đệ tử 6 túi
 
lequocbao's Avatar
 
Tham gia ngày: Oct 2008
Nơi Cư Ngụ: HCM
Bài gửi: 137
:
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]

thay đổi nội dung bởi: lequocbao, 23-12-2008 lúc 05:27 PM.
lequocbao 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à 11:37 PM.


Đượ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