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 02-05-2012, 10:14 AM   #1
thanhdtk6
Nhập môn đệ tử
 
Tham gia ngày: Aug 2009
Bài gửi: 5
:
Red face đồng hồ sử dụng ds1307

mình mới lập trình với pic 16f877a và định làm chiếc đồng hồ sử dung ic ds307 nhưng không hiểu sao nó chạy được 1 lúc thì lại bị đơ ,mọi người giúp mình với
//===========================================
//ten chuong trinh : tao dong ho
//nguoi thuc hien : tdtmt
//ngay thuc hien : 21/4/2012
//dung pic 16f877a ---thach anh 20MHz
//===========================================
#include <16f877a.h> //KHAI BAO THU VIEN PIC16F877A
#device *=16 adc=10 //KHAI BAO CON TRO VA ADC
#FUSES NOWDT, HS, NOPUT, NOPROTECT //THIET LAP CAU HINH CHO PIC
#use delay(clock=20000000) //KHAI BAO TAN SO DAO DONG LA 20MHZ
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3) //BAT CHE DO GIAO TIEP I2C
//=======KHAI BAO DIA CHI CUA CAC CONG=======
#byte PORTA =0x05
#byte PORTB =0x06
#byte PORTC =0x07
#byte PORTD =0x08
#byte PORTE =0X09
//===========================================
#byte TRISA =0x85
#byte TRISB =0x86
#byte TRISC =0x87
#byte TRISD =0x88
#byte TRISE =0x89
//===========================================
//KHAI BAO HANG =============================
#BIT led1 =PORTA.0
#BIT led2 =PORTA.1
#BIT led3 =PORTA.2
#BIT led4 =PORTA.3
#BIT led5 =PORTA.4
#BIT led6 =PORTA.5
//============================================
//KHAI BAO BIEN/////////////////
int8 sec,min,hour,dow,date,month,year,i;
int8 sec1,sec2,min1,min2,hour1,hour2,day,date1,date2,mo nth1,month2,year1,year2;
INT8 CONST led_code[ ]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0};
//cac chuong trinh con ///////////////////////
void display();
void convert_bcd();
void update_time();
//============================================
void main()
{
TRISA =0X00;
TRISD =0X00;
TRISC =0X00;
i=0;
while(true)
{
update_time();
convert_bcd();
display();
}
}
void update_time()
{
i2c_start(); //phat bit start I2C
i2c_write(0xD0); //gui dia chi cua slave
i2c_write(0x00); //thiet lap lai con tro - set register pointer
i2c_stop(); //phat bit stop I2C
I2C_start(); //phat bit start I2C
I2C_write(0xD1); //gui lenh doc du lieu
sec = i2c_read(1); //doc du lieu tai dia chi 0x00
min = i2c_read(1); //doc du lieu tai dia chi 0x01
hour = i2c_read(1); //doc du lieu tai dia chi 0x02
dow = i2c_read(1);
date = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);
i2c_stop(); //phat bit stop I2C
delay_us(100);
}
void convert_bcd()
{
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4; //convert to BCD SEC
min1=min & 0x0F;
min2=(min & 0x70)>>4; //convert to BCD MIN
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4; //convert to BCD HOUR
day=(dow & 0x07);
date1=date & 0x0F;
date2=(date & 0x30)>>4; //convert to BCD DATE
month1=month & 0x0F;
month2=(month & 0x10)>>4;//convert to BCD MONTH
year1=year & 0x0F;
year2=(year & 0xF0)>>4; //convert to BCD YEAR
}
void display()
{
do
{
// sec - min - hour
i=i+1;
PortD=led_code[sec1];
led6=0;//SEC1
delay_ms(20);
led6=1;
PortD=led_code[sec2];
led5=0;//SEC2
delay_ms(20);
led5=1;
//=================================
PortD=led_code[min1];
led4=0;//MIN1
delay_ms(20);
led4=1;
PortD=Led_code[min2];
led3=0;//MIN2
delay_ms(20);
led3=1;
//==================================
PortD=led_code[hour1];
led2=0;//HOUR1
delay_ms(20);
led2=1;
PortD=led_code[hour2];
led1=0;
delay_ms(20);
led1=1;
//==================================
}
while(i<=5);
i=0;
}
Hình Kèm Theo
File Type: gif DONGHO.GIF (114.4 KB, 204 lần tải)

thay đổi nội dung bởi: thanhdtk6, 02-05-2012 lúc 10:37 AM.
thanhdtk6 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 

Tags
ds1307


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à 02:58 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