(Mikro C) Đồng hồ thời gian thực - DS1307 (real time - Mikro C code)
sơ đồ nguyên lý cảm biến thời gian thực DS1307 |
Mạch sử dụng giao tiếp I2C, đọc và hiển thị thời gian lên LCD.
sau đây là code :
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
short readdata(short adrs)
{
short getinp;
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(adrs);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
getinp=I2C1_Rd(0);
I2C1_Stop();
return getinp;
}
void writedata(unsigned short adrs,short writedat)
{
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(adrs);
I2C1_Wr(writedat);
I2C1_Stop();
}
char firstcharofbcd(char bcddata)
{
char tireturn;
tireturn= (bcddata >> 4) +0x30;
return tireturn;
}
char secondcharofbcd(char bcddata)
{
char toret;
toret=(bcddata & 0x0F) + 0x30;
return toret;
}
char time[] = " : : ";
char date[] = " / / ";
short inc;
short chk;
short sec;
short mint;
short hr;
short h, ht;
short day;
short mon;
short yr;
short wk;
char wkd;
void main() {
I2C1_Init(100000);
inc=0;
ANSEL=0;
ANSELH=0;
TRISA = 0x3F;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_out(1,1,"Time:");
while(1){
sec = readdata(0);
mint = readdata(1);
hr = readdata(2);
h=hr;
h= Bcd2Dec(h);
if(h-12>=0){
if(h-12==0) {
ht=12 ;
chk=1; }
if(h-12==1) {
ht=1 ;
chk=1; }
if(h-12==2) {
ht=2 ;
chk=1; }
if(h-12==3) {
ht=3 ;
chk=1; }
if(h-12==4) {
ht=4 ;
chk=1; }
if(h-12==5) {
ht=5 ;
chk=1; }
if(h-12==6) {
ht=6;
chk=1; }
if(h-12==7) {
ht=7;
chk=1; }
if(h-12==8) {
ht=8;
chk=1; }
if(h-12==9) {
ht=9 ;
chk=1; }
if(h-12==10) {
ht=10;
chk=1; }
if(h-12==11) {
ht=11 ;
chk=1; }
} else{
if(h==0){
ht=12;
chk=0;
}
else{
ht=h;
chk=0;
}
}
h=Dec2Bcd(ht);
wk=readdata(3);
day = readdata(4);
mon = readdata(5);
yr = readdata(6);
time[0] = firstcharofbcd(h);
time[1] =secondcharofbcd(h);
time[3] = firstcharofbcd(mint);
time[4] = secondcharofbcd(mint);
time[6] = firstcharofbcd(sec);
time[7] = secondcharofbcd(sec);
date[0] = firstcharofbcd(day);
date[1] = secondcharofbcd(day);
date[3] = firstcharofbcd(mon);
date[4] = secondcharofbcd(mon);
date[6] = firstcharofbcd(yr);
date[7] = secondcharofbcd(yr);
wkd=secondcharofbcd(wk);
switch(wkd){
case '1':
date[9] ='S';
date[10] ='u';
date[11] ='n';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '2':
date[9] ='M';
date[10] ='o';
date[11] ='n';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '3':
date[9] ='T';
date[10] ='u';
date[11] ='e';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '4':
date[9] ='W';
date[10] ='e';
date[11] ='d';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '5':
date[9] ='T';
date[10] ='h';
date[11] ='u';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '6':
date[9] ='F';
date[10] ='r';
date[11] ='i';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '7':
date[9] ='S';
date[10] ='a';
date[11] ='t';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
}
if(chk)
{
time[9] = 'P';
time[10] = 'M';
}
else
{
time[9] = 'A';
time[10] = 'M';
}
Lcd_out(1, 6, time);
Lcd_out(2, 1, date);
if(PORTA.F0==0){
inc++;
delay_ms(300);
mint=Bcd2Dec(mint);
mint=mint+inc;
if(mint>59){
mint=0;
}
mint=Dec2Bcd(mint);
writedata(1,mint);
}
if(PORTA.F1==0){
inc++;
delay_ms(300);
if(inc>23){
inc=0;
}
h=Bcd2Dec(h);
h=h+inc;
if(h>23){
hr=1;
}
h=Dec2Bcd(h);
writedata(2,h);
}
inc=0;
if(PORTA.F2==0){
inc++;
day=Bcd2Dec(day);
day=day+inc;
delay_ms(300);
if(day>31){
day=1;
}
if(day>31){
day=1;
}
day=Dec2Bcd(day);
writedata(4,day);
}
inc=0;
if(PORTA.F3==0){
inc++;
delay_ms(300);
if(inc>12){
inc=0;
}
mon=Bcd2Dec(mon);
mon=mon+inc;
if(mon>12){
mon=1;
}
mon=Dec2Bcd(mon);
writedata(5,mon);
}
inc=0;
if(PORTA.F4==0){
inc++;
delay_ms(300);
if(inc>59){
inc=0;
}
yr=Bcd2Dec(yr);
yr=yr+inc;
if(yr>59){
yr=1;
}
yr=Dec2Bcd(yr);
writedata(6,yr);
}
}
}
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
short readdata(short adrs)
{
short getinp;
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(adrs);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
getinp=I2C1_Rd(0);
I2C1_Stop();
return getinp;
}
void writedata(unsigned short adrs,short writedat)
{
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(adrs);
I2C1_Wr(writedat);
I2C1_Stop();
}
char firstcharofbcd(char bcddata)
{
char tireturn;
tireturn= (bcddata >> 4) +0x30;
return tireturn;
}
char secondcharofbcd(char bcddata)
{
char toret;
toret=(bcddata & 0x0F) + 0x30;
return toret;
}
char time[] = " : : ";
char date[] = " / / ";
short inc;
short chk;
short sec;
short mint;
short hr;
short h, ht;
short day;
short mon;
short yr;
short wk;
char wkd;
void main() {
I2C1_Init(100000);
inc=0;
ANSEL=0;
ANSELH=0;
TRISA = 0x3F;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_out(1,1,"Time:");
while(1){
sec = readdata(0);
mint = readdata(1);
hr = readdata(2);
h=hr;
h= Bcd2Dec(h);
if(h-12>=0){
if(h-12==0) {
ht=12 ;
chk=1; }
if(h-12==1) {
ht=1 ;
chk=1; }
if(h-12==2) {
ht=2 ;
chk=1; }
if(h-12==3) {
ht=3 ;
chk=1; }
if(h-12==4) {
ht=4 ;
chk=1; }
if(h-12==5) {
ht=5 ;
chk=1; }
if(h-12==6) {
ht=6;
chk=1; }
if(h-12==7) {
ht=7;
chk=1; }
if(h-12==8) {
ht=8;
chk=1; }
if(h-12==9) {
ht=9 ;
chk=1; }
if(h-12==10) {
ht=10;
chk=1; }
if(h-12==11) {
ht=11 ;
chk=1; }
} else{
if(h==0){
ht=12;
chk=0;
}
else{
ht=h;
chk=0;
}
}
h=Dec2Bcd(ht);
wk=readdata(3);
day = readdata(4);
mon = readdata(5);
yr = readdata(6);
time[0] = firstcharofbcd(h);
time[1] =secondcharofbcd(h);
time[3] = firstcharofbcd(mint);
time[4] = secondcharofbcd(mint);
time[6] = firstcharofbcd(sec);
time[7] = secondcharofbcd(sec);
date[0] = firstcharofbcd(day);
date[1] = secondcharofbcd(day);
date[3] = firstcharofbcd(mon);
date[4] = secondcharofbcd(mon);
date[6] = firstcharofbcd(yr);
date[7] = secondcharofbcd(yr);
wkd=secondcharofbcd(wk);
switch(wkd){
case '1':
date[9] ='S';
date[10] ='u';
date[11] ='n';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '2':
date[9] ='M';
date[10] ='o';
date[11] ='n';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '3':
date[9] ='T';
date[10] ='u';
date[11] ='e';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '4':
date[9] ='W';
date[10] ='e';
date[11] ='d';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '5':
date[9] ='T';
date[10] ='h';
date[11] ='u';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '6':
date[9] ='F';
date[10] ='r';
date[11] ='i';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '7':
date[9] ='S';
date[10] ='a';
date[11] ='t';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
}
if(chk)
{
time[9] = 'P';
time[10] = 'M';
}
else
{
time[9] = 'A';
time[10] = 'M';
}
Lcd_out(1, 6, time);
Lcd_out(2, 1, date);
if(PORTA.F0==0){
inc++;
delay_ms(300);
mint=Bcd2Dec(mint);
mint=mint+inc;
if(mint>59){
mint=0;
}
mint=Dec2Bcd(mint);
writedata(1,mint);
}
if(PORTA.F1==0){
inc++;
delay_ms(300);
if(inc>23){
inc=0;
}
h=Bcd2Dec(h);
h=h+inc;
if(h>23){
hr=1;
}
h=Dec2Bcd(h);
writedata(2,h);
}
inc=0;
if(PORTA.F2==0){
inc++;
day=Bcd2Dec(day);
day=day+inc;
delay_ms(300);
if(day>31){
day=1;
}
if(day>31){
day=1;
}
day=Dec2Bcd(day);
writedata(4,day);
}
inc=0;
if(PORTA.F3==0){
inc++;
delay_ms(300);
if(inc>12){
inc=0;
}
mon=Bcd2Dec(mon);
mon=mon+inc;
if(mon>12){
mon=1;
}
mon=Dec2Bcd(mon);
writedata(5,mon);
}
inc=0;
if(PORTA.F4==0){
inc++;
delay_ms(300);
if(inc>59){
inc=0;
}
yr=Bcd2Dec(yr);
yr=yr+inc;
if(yr>59){
yr=1;
}
yr=Dec2Bcd(yr);
writedata(6,yr);
}
}
}
hic k hiểu gì hết
Trả lờiXóahạt điều mật ong