Chào các bác,
Em làm mạch đồng hồ tgian thực và đo nhiệt độ + lưu trữ giá trị quá ngưỡng
Đây là code của em
#include <16F877A.h>
#include <def_877a.h>
#device *=16 ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay (clock=20000000)
#use i2c(Master, sda = PIN_C4, scl=PIN_C3)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)
#use fast_io (B)
#include <LCD.c>
#include <ds1307.c>
//Dinh nghia cac nut bam
#define select rb4 //chon che do setup thoi gian
#define up rb5
#define down rb6
#define show rb0 //Hien thi noi dung luu tru trong EEPROM
#define reset rb7 //Reset dia chi bo nho EEPROM
//Khai bao cac bien
int8 t;//Luu tru gia tri nhiet do
int8 sec,min,hour,date,month,year,temp; //Luu tru gia tri thoi gian
unsigned int8 count=0;//Bien luu tru trang thai setup thoi gian
int16 address = 0x01 ; //Thiet lap gia tri ban dau cho EEPROM
/************************************************** ****************************\
* Ham update_time() cap nhat thoi gian tu DS1307
* Thoi gian duoc luu tru trong cac bien sec,min,hour,date,month,year
* Che do hien thi 24h
\************************************************* ****************************/
void update_time(){
sec=bcdToDec(read_DS1307(0));
min=bcdToDec(read_DS1307(1));
hour=bcdToDec(read_DS1307(2));
date=bcdToDec(read_DS1307(4));
month=bcdToDec(read_DS1307(5));
year=bcdToDec(read_DS1307(6));
}
/************************************************** ****************************\
* Ham show_LCD() xuat ra man hinh gia tri nhiet do va thoi gian
\************************************************* *****************************/
void show_LCD(){
//Dua con tro ve vi tri cot 8 dong 1 va xuat ra giay, phut, gio
lcd_putc("\f");//Xoa man hinh
lcd_gotoxy(1,1);
printf(lcd_putc,"%02u %02d:%02d:%02d\n %02d:%02d:%02d",t,hour,min,sec,date,month,year);
}
/************************************************** ****************************\
* Ham ngat ngoai chan RB0 xuat ra du lieu luu tru trong EEPROM noi
* Bo gia tri xuat ra gom co: nhiet do, thoi gian (giay, phut, gio, ngay, thang, nam)
\************************************************* *****************************/
#INT_EXT
void interrupt_EXT(){
if(rb0==0){
delay_ms(20);
if(address>0x01) { //Neu nut bam show duoc nhan va trong bo nho eeprom co du lieu
year= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
month= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
date= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
hour= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
min= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
sec= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
t= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
show_LCD();
}
}
}
/************************************************** ****************************\
*Ham ngat chan RB (RB4-RB7) de chon che do cai dat thoi gian, tang, giam va reset
dia chi cua EEPROM
\************************************************* *****************************/
#int_RB
void ngat_RB()
{
if(rb4==0){
delay_ms(20);//chong nay phim
count++; //Bien luu tru trang thai setup
if(count==7) count=0;
}
if(rb5==0){
delay_ms(20);
switch(count){
case 0: {update_time();break;}
case 1: {sec++; if(sec==60) sec=0;write_ds1307(0,decToBcd(sec)); break;}
case 2: {min++; if(min==60) min=0;write_ds1307(1,decToBcd(min));break;}
case 3: {hour++; if(hour==24) hour=0;write_ds1307(2,decToBcd(hour)); break;}
case 4: {date++; if(date==32) date=1;write_ds1307(4,decToBcd(date)); break;}
case 5: {month++; if(month==13) month=1; write_ds1307(5,decToBcd(month));break;}
case 6: {year++; if(year==100) year=0;write_ds1307(6,decToBcd(year));break;}
}
}
if(rb6==0){
delay_ms(20);
switch(count){
case 0: {update_time();break;}
case 1: {sec--; if(sec==-1) sec=59;write_ds1307(0,decToBcd(sec)); break;}
case 2: {min--; if(min==-1) min=59;write_ds1307(1,decToBcd(min));break;}
case 3: {hour--; if(hour==-1) hour=23;write_ds1307(2,decToBcd(hour)); break;}
case 4: {date--; if(date==0) date=31;write_ds1307(4,decToBcd(date)); break;}
case 5: {month--; if(month==0) month=12;write_ds1307(5,decToBcd(month)); break;}
case 6: {year--; if(year==0) year=99;write_ds1307(6,decToBcd(year));break;}
}
}
if(rb7==0){
delay_ms(20);//Chong rung
address=0x01;
}
}
/************************************************** ****************************\
*Chuong trinh chinh cua he thong
\************************************************* *****************************/
void main(){
set_tris_a(0xFF);
set_tris_d(0); //Tin hieu ra o Port D
set_tris_b(0xff); //tin hieu vao o Port B
portb=0xff; //Khoi tao tin hieu o port B la muc cao
enable_interrupts(global);//Cho phep ngat toan cuc
enable_interrupts(int_RB);//Cho phep ngat tu Rb4-Rb7
enable_interrupts(INT_EXT);//kich hoat ngat ngoai
ext_int_edge(H_to_L); //Ngat xay ra khi chuyen xung nhip chuyen tu trang thai cao xuong thap
setup_adc_ports(AN0); //Chan lay tin hieu analog o chan AN0
setup_adc(ADC_CLOCK_INTERNAL); //Thiet lao che do ADC
delay_ms(10);
lcd_init();
while(true){
t=read_adc();
//ADC su dung 10 bit :1V=>ADC=1023/5=204.6
//1oC~10mV =>ADC=10*204.6/1000=2.046
//ToC=>ADC=T*2.046=>T=ADC/2.046
t=t/2.046;
update_time();
show_LCD();
if(t>71.61||t<40.92){
if(address<0xFC){ //Neu con du bo nho de ghi day du du lieu ve nhiet do va thoi gian thi ghi
write_eeprom(address++,t); //khong thi bo qua khong lam gi ca
write_eeprom(address++,sec); //Moi lan ghi 7 bytes du lieu, eeprom su dung co bo nho 256 nen co the ghi toi da 256/7~36 lan
write_eeprom(address++,min); //36 lan tuong ung voi dia chi toi da cua eeprom la 0xFC
write_eeprom(address++,hour);
write_eeprom(address++,date);
write_eeprom(address++,month);
write_eeprom(address++,year);
}
else{}
}
}
}
Dưới đây là mạch mô phỏng Protues
Em mô phỏng thấy thời gian nó cứ quét lần lượt từ trái sang phải mà không hiển thị như bình thường. Mong các bác giúp em gấp.
Em xin chân thành cảm ơn.
Em làm mạch đồng hồ tgian thực và đo nhiệt độ + lưu trữ giá trị quá ngưỡng
Đây là code của em
#include <16F877A.h>
#include <def_877a.h>
#device *=16 ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay (clock=20000000)
#use i2c(Master, sda = PIN_C4, scl=PIN_C3)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bi ts=8)
#use fast_io (B)
#include <LCD.c>
#include <ds1307.c>
//Dinh nghia cac nut bam
#define select rb4 //chon che do setup thoi gian
#define up rb5
#define down rb6
#define show rb0 //Hien thi noi dung luu tru trong EEPROM
#define reset rb7 //Reset dia chi bo nho EEPROM
//Khai bao cac bien
int8 t;//Luu tru gia tri nhiet do
int8 sec,min,hour,date,month,year,temp; //Luu tru gia tri thoi gian
unsigned int8 count=0;//Bien luu tru trang thai setup thoi gian
int16 address = 0x01 ; //Thiet lap gia tri ban dau cho EEPROM
/************************************************** ****************************\
* Ham update_time() cap nhat thoi gian tu DS1307
* Thoi gian duoc luu tru trong cac bien sec,min,hour,date,month,year
* Che do hien thi 24h
\************************************************* ****************************/
void update_time(){
sec=bcdToDec(read_DS1307(0));
min=bcdToDec(read_DS1307(1));
hour=bcdToDec(read_DS1307(2));
date=bcdToDec(read_DS1307(4));
month=bcdToDec(read_DS1307(5));
year=bcdToDec(read_DS1307(6));
}
/************************************************** ****************************\
* Ham show_LCD() xuat ra man hinh gia tri nhiet do va thoi gian
\************************************************* *****************************/
void show_LCD(){
//Dua con tro ve vi tri cot 8 dong 1 va xuat ra giay, phut, gio
lcd_putc("\f");//Xoa man hinh
lcd_gotoxy(1,1);
printf(lcd_putc,"%02u %02d:%02d:%02d\n %02d:%02d:%02d",t,hour,min,sec,date,month,year);
}
/************************************************** ****************************\
* Ham ngat ngoai chan RB0 xuat ra du lieu luu tru trong EEPROM noi
* Bo gia tri xuat ra gom co: nhiet do, thoi gian (giay, phut, gio, ngay, thang, nam)
\************************************************* *****************************/
#INT_EXT
void interrupt_EXT(){
if(rb0==0){
delay_ms(20);
if(address>0x01) { //Neu nut bam show duoc nhan va trong bo nho eeprom co du lieu
year= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
month= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
date= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
hour= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
min= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
sec= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
t= read_eeprom(address--); // Hien thi gia tri lay ra tu eeprom
show_LCD();
}
}
}
/************************************************** ****************************\
*Ham ngat chan RB (RB4-RB7) de chon che do cai dat thoi gian, tang, giam va reset
dia chi cua EEPROM
\************************************************* *****************************/
#int_RB
void ngat_RB()
{
if(rb4==0){
delay_ms(20);//chong nay phim
count++; //Bien luu tru trang thai setup
if(count==7) count=0;
}
if(rb5==0){
delay_ms(20);
switch(count){
case 0: {update_time();break;}
case 1: {sec++; if(sec==60) sec=0;write_ds1307(0,decToBcd(sec)); break;}
case 2: {min++; if(min==60) min=0;write_ds1307(1,decToBcd(min));break;}
case 3: {hour++; if(hour==24) hour=0;write_ds1307(2,decToBcd(hour)); break;}
case 4: {date++; if(date==32) date=1;write_ds1307(4,decToBcd(date)); break;}
case 5: {month++; if(month==13) month=1; write_ds1307(5,decToBcd(month));break;}
case 6: {year++; if(year==100) year=0;write_ds1307(6,decToBcd(year));break;}
}
}
if(rb6==0){
delay_ms(20);
switch(count){
case 0: {update_time();break;}
case 1: {sec--; if(sec==-1) sec=59;write_ds1307(0,decToBcd(sec)); break;}
case 2: {min--; if(min==-1) min=59;write_ds1307(1,decToBcd(min));break;}
case 3: {hour--; if(hour==-1) hour=23;write_ds1307(2,decToBcd(hour)); break;}
case 4: {date--; if(date==0) date=31;write_ds1307(4,decToBcd(date)); break;}
case 5: {month--; if(month==0) month=12;write_ds1307(5,decToBcd(month)); break;}
case 6: {year--; if(year==0) year=99;write_ds1307(6,decToBcd(year));break;}
}
}
if(rb7==0){
delay_ms(20);//Chong rung
address=0x01;
}
}
/************************************************** ****************************\
*Chuong trinh chinh cua he thong
\************************************************* *****************************/
void main(){
set_tris_a(0xFF);
set_tris_d(0); //Tin hieu ra o Port D
set_tris_b(0xff); //tin hieu vao o Port B
portb=0xff; //Khoi tao tin hieu o port B la muc cao
enable_interrupts(global);//Cho phep ngat toan cuc
enable_interrupts(int_RB);//Cho phep ngat tu Rb4-Rb7
enable_interrupts(INT_EXT);//kich hoat ngat ngoai
ext_int_edge(H_to_L); //Ngat xay ra khi chuyen xung nhip chuyen tu trang thai cao xuong thap
setup_adc_ports(AN0); //Chan lay tin hieu analog o chan AN0
setup_adc(ADC_CLOCK_INTERNAL); //Thiet lao che do ADC
delay_ms(10);
lcd_init();
while(true){
t=read_adc();
//ADC su dung 10 bit :1V=>ADC=1023/5=204.6
//1oC~10mV =>ADC=10*204.6/1000=2.046
//ToC=>ADC=T*2.046=>T=ADC/2.046
t=t/2.046;
update_time();
show_LCD();
if(t>71.61||t<40.92){
if(address<0xFC){ //Neu con du bo nho de ghi day du du lieu ve nhiet do va thoi gian thi ghi
write_eeprom(address++,t); //khong thi bo qua khong lam gi ca
write_eeprom(address++,sec); //Moi lan ghi 7 bytes du lieu, eeprom su dung co bo nho 256 nen co the ghi toi da 256/7~36 lan
write_eeprom(address++,min); //36 lan tuong ung voi dia chi toi da cua eeprom la 0xFC
write_eeprom(address++,hour);
write_eeprom(address++,date);
write_eeprom(address++,month);
write_eeprom(address++,year);
}
else{}
}
}
}
Dưới đây là mạch mô phỏng Protues
Em mô phỏng thấy thời gian nó cứ quét lần lượt từ trái sang phải mà không hiển thị như bình thường. Mong các bác giúp em gấp.
Em xin chân thành cảm ơn.
Comment