À các coi dùm code của mình nha mọi ngày mình làm avr bây gìơ vì vấn đề tài chính lên học 89 không biết code chuẩn chưa mà sai dữ wá
#include <REGX51.H>
#define data_led P0 //P1
#define quet_led P1 //P0
#define un unsigned
//-----------Read-Time-Clock----------
#define SCL P3_0
#define SDA P3_1
#define den P2_7
un char gio,phut,giay;
un char ma_led[]={0xA0,0xF9,0xC4,0xD0,0x99,0x92, 0x82,0xF8,0x80,0x90,255,255};
void ht();
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void tang_so()
{
if(++giay>59)
{ giay=0;
if(++phut>59)
{phut=0;
if(++gio>23)
{gio=0;
}
}
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void delay_ms(un int ms)
{un int n,bb;
for(n=0;n<ms;n++){for(bb=0;bb<125;bb++);}
}
void delay_us(un int us)
{
un int i;
for(i=0;i<us;i++);
}
//*************************************
//**************ds 1307*****************
//*******************************************
void I2C_start() //ham khoi dong I2C
{
SCL = 1;
SCL = 0;
SDA = 1;
SCL = 1;
delay_us(2);
SDA = 0;
delay_us(2);
SCL = 0;
delay_us(2);
}
void I2C_stop()
{
SCL = 1; //keo chan SCL len 1
SCL = 0;
SDA = 0;
delay_us(2);
SCL = 1;
delay_us(2);
SDA = 1;
}
un char I2C_write(un char dat)//ham ghi 1 byte du lieu vao DS1307
{
un char i;
for (i=0;i<8;i++)
{
SDA = (dat & 0x80); ///? 1:0;
SCL=1;SCL=0;
dat<<=1;
}
SCL = 1; delay_us(2);
SCL = 0;
return dat;
}
un char I2C_read(void)
{
bit rd_bit;
un char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
delay_us(2);
SCL = 1;
delay_us(2); /* Set SCL */
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
un char bin2bcd(un char gtri_nhiphan)
{
un char temp,retval;
temp=gtri_nhiphan;
retval=0;
while(1)
{
/*Get the 10 digit by doing multiple subtraction of 10 from the binary value*/
if(temp>=10)
{
temp-=10;
retval+=0x10;
}
else /*Get the 1 digitby assing the memainder*/
{
retval+=temp;
break;
}
}
return(retval);
}
un char bcd2bin(un char gtri_bcd)
{
un char temp;
temp=gtri_bcd;
/*Shifting upper digit right by 1 is same as multiplying by 8*/
temp>>=1;
/*Isolate the bits for the upper digit*/
temp &= 0x78;
/*Bay gio quay lai (10*8)+(10*2)+1 */
return (temp + (temp >>2) + (gtri_bcd & 0x0F));
}
un char DS1307_read(un char addr)
{
un int temp,ret;
I2C_start(); /* Start i2c bus */
I2C_write(0xD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(0XD1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop();
//************************************************** ********
temp = ret; /*BCD to HEX*/
ret = (((ret/16)*10)+ (temp & 0x0f)); /*for Led 7seg*/
//************************************************** ********
return ret;
}
//------------------------------------
void DS1307_Write(un char addr,un char dat)
{
un int temp;
//**********************************************
temp = dat ; /*HEX to BCD*/
dat = (((dat/10)*16)|(temp %10)); /*for Led 7seg*/
//**********************************************
I2C_start(); /* Start i2c bus */
I2C_write(0XD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_write(dat); /* Connect to DS1307 for Read */
I2C_stop();
}
//-----------------------------------
void rtc_set_time(un char h,un char m,un char s)
{
I2C_start();
I2C_write(0xD0);
I2C_write(0);
I2C_write(bin2bcd(s));
I2C_write(bin2bcd(m));
I2C_write(bin2bcd(h));
I2C_stop();
}
void rtc_get_time(un char h,un char m,un char s)
{
I2C_start();
I2C_write(0xD0);
I2C_write(0);
I2C_start();
I2C_write(0xD1);
s=bcd2bin(I2C_read() & 0x7F);
m=bcd2bin(I2C_read() & 0x7F);
h =bcd2bin(I2C_read() & 0x3F);
I2C_stop();
}
//############ngat time_0 dung quet led 7 doan ###############
//void timer0_ISR (void) interrupt 1 //ngat time_0 (tao ngat 2ms)
//{ TR0 = 0;
// TH0 = 0xf0; // 2ms
// TL0 = 0x00;
//
//
//
//
//
// TR0 = 1;
// }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void timer1_ISR (void) interrupt 3
{
static un char t=0;
TR1 = 0;
TH1 = 0x3C;//50ms
TL1 = 0xAF;
if(++t>19){t=0;tang_so();}//sau 1s ngat xay ra 1 lan
TR1 = 1;
}
//############ngat ngoai_0################
void ngat0_ISR (void) interrupt 0 //doc ds 1307
{
giay=DS1307_read(0);
phut=DS1307_read(1);
gio=DS1307_read(2);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void ht()
{
quet_led=0xDF;
data_led=ma_led[giay%10];
delay_ms(1);data_led=0xFF;
quet_led=0xEF;
data_led=ma_led[giay/10];
delay_ms(1);data_led=0xFF;
quet_led=0xF7;
data_led=ma_led[phut%10];
delay_ms(1);data_led=0xFF;
quet_led=0xFB;
data_led=ma_led[phut/10];
delay_ms(1);data_led=0xFF;
quet_led=0xFD;
data_led=ma_led[gio%10];
delay_ms(1);data_led=0xFF;
quet_led=0xFE;
data_led=ma_led[gio/10];
delay_ms(1);data_led=0xFF;
}
//***********************************
void main()
{
/*
gio=20,phut=1,giay=0;
DS1307_Write(0,giay);
DS1307_Write(1,phut);
DS1307_Write(2,gio);
*/
rtc_get_time(gio,phut,giay);
P3_0=1;//input
P3_1=1;//input
P0=0xFF,P1=0xFF,P2=0xFF;
TMOD = 0x11; // Set Mode (16-bit timer_0; 16-bit timer_1)
// TH0 = 0xf8; // 2ms
// TL0 = 0x30;
TH1 = 0x3C; // 50ms
TL1 = 0xAF;
// ET0 = 1; // Enable Timer 0 Interrupts
// TR0 = 1; // Start Timer 0 Running
ET1 = 1; // Enable Timer 1 Interrupts
TR1 = 1; // Start Timer 1 Running
EX0=1; //ngat ngoai_0
IT0=1; //Edge Trigerred
//********cap nhat ds1307**********************
giay=DS1307_read(0);
phut=DS1307_read(1);
gio =DS1307_read(2);
if(giay & 0x80==1) //kiem tra bit CH
{DS1307_Write(0,giay & 0x7f); }
DS1307_Write(7,10);//tao 1hz tai chan 7 ham Write se chuyen sang hex
EA = 1; // Global Interrupt Enable
while(1)
{
ht();
if(gio==18 && phut==0 && giay==0)den=0;
else if(gio==22 && phut==0 && giay==0)den=1;
}
}
#include <REGX51.H>
#define data_led P0 //P1
#define quet_led P1 //P0
#define un unsigned
//-----------Read-Time-Clock----------
#define SCL P3_0
#define SDA P3_1
#define den P2_7
un char gio,phut,giay;
un char ma_led[]={0xA0,0xF9,0xC4,0xD0,0x99,0x92, 0x82,0xF8,0x80,0x90,255,255};
void ht();
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void tang_so()
{
if(++giay>59)
{ giay=0;
if(++phut>59)
{phut=0;
if(++gio>23)
{gio=0;
}
}
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void delay_ms(un int ms)
{un int n,bb;
for(n=0;n<ms;n++){for(bb=0;bb<125;bb++);}
}
void delay_us(un int us)
{
un int i;
for(i=0;i<us;i++);
}
//*************************************
//**************ds 1307*****************
//*******************************************
void I2C_start() //ham khoi dong I2C
{
SCL = 1;
SCL = 0;
SDA = 1;
SCL = 1;
delay_us(2);
SDA = 0;
delay_us(2);
SCL = 0;
delay_us(2);
}
void I2C_stop()
{
SCL = 1; //keo chan SCL len 1
SCL = 0;
SDA = 0;
delay_us(2);
SCL = 1;
delay_us(2);
SDA = 1;
}
un char I2C_write(un char dat)//ham ghi 1 byte du lieu vao DS1307
{
un char i;
for (i=0;i<8;i++)
{
SDA = (dat & 0x80); ///? 1:0;
SCL=1;SCL=0;
dat<<=1;
}
SCL = 1; delay_us(2);
SCL = 0;
return dat;
}
un char I2C_read(void)
{
bit rd_bit;
un char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
delay_us(2);
SCL = 1;
delay_us(2); /* Set SCL */
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
un char bin2bcd(un char gtri_nhiphan)
{
un char temp,retval;
temp=gtri_nhiphan;
retval=0;
while(1)
{
/*Get the 10 digit by doing multiple subtraction of 10 from the binary value*/
if(temp>=10)
{
temp-=10;
retval+=0x10;
}
else /*Get the 1 digitby assing the memainder*/
{
retval+=temp;
break;
}
}
return(retval);
}
un char bcd2bin(un char gtri_bcd)
{
un char temp;
temp=gtri_bcd;
/*Shifting upper digit right by 1 is same as multiplying by 8*/
temp>>=1;
/*Isolate the bits for the upper digit*/
temp &= 0x78;
/*Bay gio quay lai (10*8)+(10*2)+1 */
return (temp + (temp >>2) + (gtri_bcd & 0x0F));
}
un char DS1307_read(un char addr)
{
un int temp,ret;
I2C_start(); /* Start i2c bus */
I2C_write(0xD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(0XD1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop();
//************************************************** ********
temp = ret; /*BCD to HEX*/
ret = (((ret/16)*10)+ (temp & 0x0f)); /*for Led 7seg*/
//************************************************** ********
return ret;
}
//------------------------------------
void DS1307_Write(un char addr,un char dat)
{
un int temp;
//**********************************************
temp = dat ; /*HEX to BCD*/
dat = (((dat/10)*16)|(temp %10)); /*for Led 7seg*/
//**********************************************
I2C_start(); /* Start i2c bus */
I2C_write(0XD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_write(dat); /* Connect to DS1307 for Read */
I2C_stop();
}
//-----------------------------------
void rtc_set_time(un char h,un char m,un char s)
{
I2C_start();
I2C_write(0xD0);
I2C_write(0);
I2C_write(bin2bcd(s));
I2C_write(bin2bcd(m));
I2C_write(bin2bcd(h));
I2C_stop();
}
void rtc_get_time(un char h,un char m,un char s)
{
I2C_start();
I2C_write(0xD0);
I2C_write(0);
I2C_start();
I2C_write(0xD1);
s=bcd2bin(I2C_read() & 0x7F);
m=bcd2bin(I2C_read() & 0x7F);
h =bcd2bin(I2C_read() & 0x3F);
I2C_stop();
}
//############ngat time_0 dung quet led 7 doan ###############
//void timer0_ISR (void) interrupt 1 //ngat time_0 (tao ngat 2ms)
//{ TR0 = 0;
// TH0 = 0xf0; // 2ms
// TL0 = 0x00;
//
//
//
//
//
// TR0 = 1;
// }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void timer1_ISR (void) interrupt 3
{
static un char t=0;
TR1 = 0;
TH1 = 0x3C;//50ms
TL1 = 0xAF;
if(++t>19){t=0;tang_so();}//sau 1s ngat xay ra 1 lan
TR1 = 1;
}
//############ngat ngoai_0################
void ngat0_ISR (void) interrupt 0 //doc ds 1307
{
giay=DS1307_read(0);
phut=DS1307_read(1);
gio=DS1307_read(2);
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
void ht()
{
quet_led=0xDF;
data_led=ma_led[giay%10];
delay_ms(1);data_led=0xFF;
quet_led=0xEF;
data_led=ma_led[giay/10];
delay_ms(1);data_led=0xFF;
quet_led=0xF7;
data_led=ma_led[phut%10];
delay_ms(1);data_led=0xFF;
quet_led=0xFB;
data_led=ma_led[phut/10];
delay_ms(1);data_led=0xFF;
quet_led=0xFD;
data_led=ma_led[gio%10];
delay_ms(1);data_led=0xFF;
quet_led=0xFE;
data_led=ma_led[gio/10];
delay_ms(1);data_led=0xFF;
}
//***********************************
void main()
{
/*
gio=20,phut=1,giay=0;
DS1307_Write(0,giay);
DS1307_Write(1,phut);
DS1307_Write(2,gio);
*/
rtc_get_time(gio,phut,giay);
P3_0=1;//input
P3_1=1;//input
P0=0xFF,P1=0xFF,P2=0xFF;
TMOD = 0x11; // Set Mode (16-bit timer_0; 16-bit timer_1)
// TH0 = 0xf8; // 2ms
// TL0 = 0x30;
TH1 = 0x3C; // 50ms
TL1 = 0xAF;
// ET0 = 1; // Enable Timer 0 Interrupts
// TR0 = 1; // Start Timer 0 Running
ET1 = 1; // Enable Timer 1 Interrupts
TR1 = 1; // Start Timer 1 Running
EX0=1; //ngat ngoai_0
IT0=1; //Edge Trigerred
//********cap nhat ds1307**********************
giay=DS1307_read(0);
phut=DS1307_read(1);
gio =DS1307_read(2);
if(giay & 0x80==1) //kiem tra bit CH
{DS1307_Write(0,giay & 0x7f); }
DS1307_Write(7,10);//tao 1hz tai chan 7 ham Write se chuyen sang hex
EA = 1; // Global Interrupt Enable
while(1)
{
ht();
if(gio==18 && phut==0 && giay==0)den=0;
else if(gio==22 && phut==0 && giay==0)den=1;
}
}
Comment