PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT 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

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 20-07-2010, 12:01 PM   #1
toanfet
Nhập môn đệ tử
 
Tham gia ngày: Feb 2010
Bài gửi: 1
:
Thumbs up Thắc mắc về DS1307,xin tiền bối giúp đỡ!

Em đang làm 1 ví dụ nhỏ với DS1307 là dùng pic 16f877a đọc giá trị của thanh ghi giây (thanh ghi 0x00),dựa vào giá trị đó sẽ làm cho led sáng.Nếu Second=(0,15),led 1 sáng,=(16,30),led2 sáng,=(31,45),led3 sáng, =(46,60)led 4 sáng.Nhưng không hiểu sao PIC lai không thể chuyển trạng thái được.Ví dụ như ở khoảng thời gian từ giây thứ 1->15, led1 sáng nhưng đến giây thứ 16, led 2 không sáng mà led 1 vẫn sáng.Đây là code và mạch của em, mong các tiền bối chỉ giáo!
////////////////////////////////////////////////////////////////////////////////////////////
#include "D:\Electronic and Telecomunication\sendpicvietnam.h"
#use i2c(MASTER,FAST,SDA=PIN_C4,SCL=PIN_C3)
#include <C:\Program Files\PICC\Devices\ds1307.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#byte PORTB=0x06
int8 mode,seconds;
//#int_ext
//void ngat_RB0()
//{
//seconds=read_DS1307(0);
//}
int8 time()
{
int8 s,second;
second=read_DS1307(0);
s=(second/16)*10 +second%16;
if(s<15) return 1;
if((s>=15)&&(s<30)) return 2;
if((s>=30)&&(s<45)) return 3;

if(s>=45) return 4;
}



void main()
{
//enable_interrupts(int_ext);
//ext_int_edge(0,H_TO_L);
//enable_interrupts(global);
set_tris_b(0x00);
init_DS1307();
while(1){mode=time();
if(mode==1){
output_high(pin_b1);output_low(pin_b2);output_low( pin_b3);}
if(mode==2){
output_high(pin_b2);output_low(pin_b1);output_low( pin_b3);}
if(mode==3){
output_high(pin_b3);output_low(pin_b2);output_low( pin_b1);}
if(mode==4){output_high(pin_b3);output_high(pin_b1 );output_high(pin_b2);}

}}
////////////////////////////////////////////////////////////////////////
Còn đây là code ds1307
/////////////////////////////////////////////////////////////////////////
#define DS1307_SDA PIN_C4
#define DS1307_SCL PIN_C3

void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}
BYTE read_DS1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}

void init_DS1307()
{
int8 temp1,temp2,temp3;
output_float(DS1307_SCL);
output_float(DS1307_SDA);
temp1= read_DS1307(0);//giay
temp2= read_DS1307(1);//phut
temp3= read_DS1307(2);//gio
temp1 &= 0x7f;
temp2 &= 0x7f;
temp3 &= 0x3f;
delay_us(5);
write_DS1307(0,temp1);
write_DS1307(1,temp2);
write_DS1307(2,temp3);
}
Hình Kèm Theo
File Type: png simlation.png (226.7 KB, 4 lần tải)
toanfet 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à 12:17 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