MỌI NGƯỜI XEM DÙM MÌNH CHƯƠNG TRÌNH NÀY VỚI KHÔNG HIỂU TẠI SAO BAN ĐẦU CHẠY OK NHƯNG CHỈ ĐƯỢC LÚC THÌ LẠI BỊ ĐƠ VÀ HIỂN THỊ LINH TINH ,THANK
//===========================================
//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;
}
dongho.rar
mọi người tải về xem hộ tớ cái nha
//===========================================
//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;
}
dongho.rar
mọi người tải về xem hộ tớ cái nha
Comment