Nguyên văn bởi quangtung112
Xem bài viết
#include <mega16.h>
#include <delay.h>
//==========================================
//Cac chan dieu khien
#define led_port PORTA
#define ic_counter PORTD
#define reg_sec 0x0
#define reg_min 0x1
#define reg_hour 0x2
#define reg_day 0x3
#define reg_date 0x4
#define reg_month 0x5
#define reg_year 0x6
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#define sda PORTC.0;
#define scl PORTC.1
#define ic_time PORTD.4;
#define ic_date PORTD.5;
#define menu PORTB.0;
#define plus PORTB.1;
#define minus PORTB.2;
#define al_cancel PORTB.3;
//==========================================
//Cac bien toan cuc dung trong chuong trinh
unsigned char led_code[13]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0,0xff,0xfe,0x00};
unsigned char led_buffer[15];
unsigned char scan_led,counter,var_menu;
long ngayam,thangam,ngayduong;
unsigned char sec,min,hour,day,date,month,year,date_1,month_1; //Date_1 la ngay am , month_1 thang am
bit var_arlam;
//Quet led 7 doan dung timer 0
void quet_led ();
interrupt[TIM0_OVF]void timer0_ovf_isr(void)
{
TCNT0=0xBB;
//quet 15 LED Sau 12 lan Timer0 ngat thi thay doi cac led sang
void quet_led(void) //dia chi vecto ngat timer0
{
if(scan_led<=6)//chan enb cua IC_COUNTER =>0 khong co led nao duoc hien thi
{
ic_counter=scan_led;//hien thi cac led o hang 1 (gio, phut,giay,thu)
//chan enb cua IC_COUNTER =>0 khong co led nao duoc hien thi
ic_time=1;//hien thi gio phut giay
}else
{
ic_counter=scan_led-7;// hien thi led hang 2 (ngay thang nam)(tru di so led da hien thi o hang 1)
ic_date=1;//hien thi ngay thang nam
} // ic_counter=scan_led-0x7+0xf0 led thu 7 la led thu 0 cua hang 2
led_port=led_code[(led_buffer[scan_led])];
//scan_led++;
if(++scan_led>=15) //vuot qua 15 led thi tro ve ban dau
{
scan_led=0;
}
}
}
// Ham con
//-----------***************Chuyen doi tu DEC sang BCD************-----------------
unsigned char dec_2_bcd(unsigned char value1)//chuyen doi tu he decima sang BCD
{
return ((value1/10)<<4)|(value1%10 ); //((value1/10)*16+value1%10);
}
//----------**************Chuyen doi tu BCD sang DEC**********---------------------
unsigned char bcd_2_dec(unsigned char value2)//chuyen doi tu he BCD sang DECIMA
{
return ((value2/16)*10+value2%16);
}
//--------***********tao thoi gian tre **********-------------
void delay_1 (int time_delay)
{
unsigned int temp;
for (temp=0;temp<time_delay;temp++)
{
delay_us(1);
}
}
void delay_2(int time_delay)
{
unsigned int temp;
for (temp=0;temp<time_delay;temp++)
{
delay_ms(1);
}
}
//------******Khoi dong I2C******---------
void i2c_start(void)
{
scl=1;
sda=1;
delay_us(1);
sda=0;
scl=0;
}
//-----******Ket thuc I2C*******---------
void i2c_stop(void)
{
sda=0;
scl=1;
delay_us(1);
sda=1;
scl=0;
}
//-------*******Gui du lieu len duong I2C******------
void write_i2c(unsigned char data_2_send)
{
unsigned char temp;
for(temp=0;temp<8;temp++)
{
sda=(data_2_send&0x80)? 1:0; //dua bit du lieu ra chan SDA
scl=1; //
delay_us(1); //
scl=0;
data_2_send<<=1; //bit co trong so lon hon duoc truyen truoc
}
scl=1; //nhan bit ACK tu SLAVER
delay_us(1);
scl=0;
}
//------------**********Doc di lieu tren duong I2C******---------
unsigned char read_i2c(void)
{
unsigned char temp,i;
for(i=0;i<8;i++) // nhan vao 8 bit
{
scl=1;
temp<<=1;
temp=temp|sda;
scl=0;
}
sda=1;//gui bit ACK
scl=1;//master nhan/gui bit du lieu(sda) khi scl o muc cao
delay_us(1);
scl=0;//du lieu(sda) thay doi khi scl muc thap*/
return temp;//tra gia tri cho ham
}
//------*****Ghi du lieu len RTC qua duong I2C*****----------
void write_rtc(char value,addr)
{
i2c_start();//start i2c
write_i2c(0xd0);//0xd0 la ID cua DS1307
write_i2c(addr);
write_i2c(value);
i2c_stop();
}
//---------********Doc du lieu tu RTC qua duong I2C*****--------
char read_rtc(char addr)
{
unsigned char temp;
i2c_start();
write_i2c(0xd0);//
write_i2c(addr); //bat dau doc du lieu tu thanh ghi co dia chi 0x0
i2c_start();
write_i2c(0xd1); //0xd0 + 0x01 la ID cua ds107 va che do doc du lieu
temp=read_i2c();//con tro dang tro toi dia chi 0x0 chua du lieu second
i2c_stop();//Stop I2C
return temp;
}
//-----------***********Lay du lieu thoi gian tu cac thanh ghi trong RTC*******---------
void get_time()
{
//sec=read_rtc(reg_sec);
min=read_rtc(reg_min);
hour=read_rtc(reg_hour);
day=read_rtc(reg_day);
date=read_rtc(reg_date);
month=read_rtc(reg_month);
year=read_rtc(reg_year);
}
///////---------------***********Ghi du lieu va bo dem****-----------
void write_led_buffer(void)
{
unsigned char temp;
//*******Hien thi phut**********
led_buffer[0]=min&0x0f; //Lay 4 bit thap
led_buffer[1]=(min&0x70)>>4; //lay 3 bit cao
//*******Hien thi gio*************
if((temp=(hour&0x30)>>4)==0)//khong hien thi so 0 o hang chuc
{
led_buffer[3]=0x0a;
}else
{
led_buffer[3]=temp;//Lay 2 bit cao
}
led_buffer[2]=(hour&0x0f); //Lay 4 bit thap
//Hien thi thu trong tuan
led_buffer[4]=(day&0x7);
//*******Hien thi ngay AL**********
led_buffer[5]=date_1%10;
if((temp=(date_1/10))==0)//khong hien thi so 0 o hang chuc
{
led_buffer[6]=0x0a;
}else
{
led_buffer[6]=temp;
}
//*******Hien thi Thang AL*********
led_buffer[7]=month_1%10;
if((temp=(month_1/10))==0)//khong hien thi so 0 o hang chuc
{
led_buffer[8]=0x0a;
}else
{
led_buffer[8]=temp;
}
//******Hien thi nam***********
led_buffer[9]=(year&0x70)>>4;
led_buffer[10]=year&0x0f;
//*********Hien thi thang************
led_buffer[11]=month&0x0f;
if(((month&0x30)>>4)==0)//khong hien thi so 0 o hang chuc
{
led_buffer[12]=0x0a;
}else
{
led_buffer[12]=(month&0x30)>>4;
}
//Hien thi Ngay duong lich
led_buffer[13]=date&0x0f;
if((temp=(date&0x30)>>4)==0)//khong hien thi so 0 o hang chuc
{
led_buffer[14]=0x0a;
}else
{
led_buffer[14]=temp;
}
}
//-----------------**********Cai dat che do cho RTC****------------------
void setup_rtc(void)
{
unsigned char temp;
/*i2c_start();
write_i2c(0xd0);//che do ghi du lieu vao RTC
write_i2c(0x08);//thanh ghi control
write_i2c(0x10);//xung ra 1Hz muc thap khi khong dc kich hoat
i2c_stop();*/
write_rtc(0x10,0x07);
temp=read_rtc(0x02);
write_rtc(0x02,temp&0x7f);
}
//-----------------**************Do phim**********----------------------
void do_phim(void)
{
if(menu==0)//phim menu duoc an
{
delay_2(50);
if(var_arlam==0)//Neu khong thuc hien chuc nang hen gio
{
if(var_menu<=7)
{
var_menu++; //thuc hien menu tiep
}else
{
var_menu=1;//quay tro ve menu dau tien
}
}else
{
var_arlam=0;//hen gio xong
}
delay_2(50);
}
if(al_cancel==0)
{
var_menu=0;
var_arlam=1;
delay_1(10000);
}
}
//-----------******Chinh Giay********---------
void menu1(void)
{
if((plus==0)||(minus==0))
{
write_rtc(0x0,reg_sec); //Reset GIAY
delay_2(50);
}
}
//----------*********Chinh PHUT*********------------
void menu2(void)
{
unsigned char temp;
temp=bcd_2_dec(min);
if(plus==0)
{
if(temp<59)
{
temp++;
}else
{
temp=0;
}
write_rtc(dec_2_bcd(temp),reg_min);
delay_2(50);
}
if(minus==0)
{
if(temp>0)
{
temp--;
}else
{
temp=59;
}
write_rtc(dec_2_bcd(temp),reg_min);
delay_2(50);
}
}
//----------*********Chinh gio***********------------
void menu3(void)
{
unsigned char temp;
temp=bcd_2_dec(hour);
if(plus==0)
{
if(temp>=23)
{
temp=0;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_hour);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp<=0)
{
temp=23;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_hour);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//----------------********Chinh Ngay Trong Tuan*******----------------------
void menu4(void)
{
unsigned char temp;
temp=bcd_2_dec(day);
if(plus==0)
{
if(temp>=7)
{
temp=1;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_day);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp<=1)
{
temp=7;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_day);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//-----------********Chinh Ngay********----------------
void menu5()
{
unsigned char temp,temp1,temp2;
temp=bcd_2_dec(date);
temp1=bcd_2_dec(month);
temp2=bcd_2_dec(year);
if(plus==0) //******An PLUS***----
{
if((temp1==4)||(temp1==6)||(temp1==9)||(temp1==11) )//Cac thang co 30 ngay
{
if(temp>=30)
{
temp=1;
}
}else
{
if(temp1==2)//thang 2
{
if(temp2%4==0)//nam nhuan
{
if(temp>=29)
{
temp=1;
}else
{
temp++;
}
}else //nam thuong
{
if(temp>=28)
{
temp=1;
}else
{
temp++;
}
}
}else //cac thang co 31 ngay
{
if(temp>=31)
{
temp=1;
}else
{
temp++;
}
}
}
write_rtc(dec_2_bcd(temp),reg_date);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0) //-********AN MINUS****--------
{
if(temp>2)
{
temp--;
}else
{
if((temp1==4)||(temp1==6)||(temp1==9)||(temp1==11) )//Cac thang co 30 ngay
{
temp=30;
}else
{
if(temp1==2)//thang 2
{
if(temp2%4==0)//nam nhuan
{
temp=29;
}else //nam thuong
{
temp=28;
}
}else //cac thang co 31 ngay
{
temp=31;
}
}
}
write_rtc(dec_2_bcd(temp),reg_date);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
void menu6(void)//-------*********Chinh Thang-*******---------
{
unsigned char temp;
temp=bcd_2_dec(month);
if(plus==0)
{
if(temp>=12)
{
temp=1;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_month);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp==0)
{
temp=12;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_month);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//---------**********Chinh Nam**********--------------
void menu7(void)
{ unsigned char temp;
temp=bcd_2_dec(year);
if(plus==0)
{
if(temp>=50)//Chinh thoi gian tu nam 2011 den Nam 2050
{
temp=11;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_year);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp<=11)
{
temp=50;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_year);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//-----------***********Thuc Hien MENU*********---------------
void xu_ly_menu(void)
{
while(var_menu!=0)
{
get_time();
write_led_buffer();
switch(var_menu)
{
case 1:
menu1();
break;
case 2:
menu2();
break;
case 3:
menu3();
break;
case 4:
menu4();
break;
case 5:
menu5();
break;
case 6:
menu6();
break;
case 7:
menu7();
break;
}
do_phim();
}
}
//-------********Dua du lieu ra bo nho dem LED******---------
void start_system()
{
var_menu=0;
//var_arlam=0;
}
//-- chuyen ngay duong sang ngay am------
void da(int m,d)
{
ngayduong=(m-1)*31+d;//so ngay duong so voi 1.1
if ((m==3)|(m==4)) ngayduong=ngayduong-3;//do thang 2 co 28 ngay
else
if ((m==5)|(m==6)) ngayduong=ngayduong-4;// thang 4 co 30 ngay
else
if ((m==7)|(m==8)|(m==9)) ngayduong=ngayduong-5;//thang 6 co 30 ngay
else
if ((m==10)|(m==11)) ngayduong=ngayduong-6;//thang 9 co 30 ngay
else
if (m==12) ngayduong=ngayduong-7;// thang 11 co 30 ngay
ngayam=ngayduong+26;//mung 1.1 la ngay 27.11
thangam=11;
if (ngayam>=30)
{
ngayam=ngayam-29;
thangam=12;
}
while (ngayam>=30)
{
if ((thangam==2)|(thangam==5)|(thangam==8)|(thangam== 7)|(thangam==10))
{
if (ngayam==29) break;
ngayam=ngayam-29;
thangam++;
if (thangam==13) thangam=1;
}
else
if (ngayam==30) break;
else
{
ngayam=ngayam-30;
thangam++;
if (thangam==13) thangam=1;
}
}
}
//---------------**************Chuong trinh chinh**********----------------------
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x04;
TCNT0=0xBB;
OCR0=0x01;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// khoi khoi tao i2c
i2c_init();
//khoi khoi tao ds1307
rtc_init(0,0,0);
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 32768Hz
rtc_init(3,1,0); u
#asm {"sei"}
#endasm
start_system();
setup_rtc();
while(1)
{
//Dat ngay Am lich o day
//////////
get_time();
da(bcd_2_dec(month),bcd_2_dec(date));//dau vao la thang duong ngay duong dau ra la thangam ngayam hien thi ngayam thangam
date_1=thangam;
month_1=ngayam;
write_led_buffer();
do_phim();
xu_ly_menu();
}
}
//-----------------**************Ket thuc chuong trinh********--------------------
#include <delay.h>
//==========================================
//Cac chan dieu khien
#define led_port PORTA
#define ic_counter PORTD
#define reg_sec 0x0
#define reg_min 0x1
#define reg_hour 0x2
#define reg_day 0x3
#define reg_date 0x4
#define reg_month 0x5
#define reg_year 0x6
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#define sda PORTC.0;
#define scl PORTC.1
#define ic_time PORTD.4;
#define ic_date PORTD.5;
#define menu PORTB.0;
#define plus PORTB.1;
#define minus PORTB.2;
#define al_cancel PORTB.3;
//==========================================
//Cac bien toan cuc dung trong chuong trinh
unsigned char led_code[13]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0,0xff,0xfe,0x00};
unsigned char led_buffer[15];
unsigned char scan_led,counter,var_menu;
long ngayam,thangam,ngayduong;
unsigned char sec,min,hour,day,date,month,year,date_1,month_1; //Date_1 la ngay am , month_1 thang am
bit var_arlam;
//Quet led 7 doan dung timer 0
void quet_led ();
interrupt[TIM0_OVF]void timer0_ovf_isr(void)
{
TCNT0=0xBB;
//quet 15 LED Sau 12 lan Timer0 ngat thi thay doi cac led sang
void quet_led(void) //dia chi vecto ngat timer0
{
if(scan_led<=6)//chan enb cua IC_COUNTER =>0 khong co led nao duoc hien thi
{
ic_counter=scan_led;//hien thi cac led o hang 1 (gio, phut,giay,thu)
//chan enb cua IC_COUNTER =>0 khong co led nao duoc hien thi
ic_time=1;//hien thi gio phut giay
}else
{
ic_counter=scan_led-7;// hien thi led hang 2 (ngay thang nam)(tru di so led da hien thi o hang 1)
ic_date=1;//hien thi ngay thang nam
} // ic_counter=scan_led-0x7+0xf0 led thu 7 la led thu 0 cua hang 2
led_port=led_code[(led_buffer[scan_led])];
//scan_led++;
if(++scan_led>=15) //vuot qua 15 led thi tro ve ban dau
{
scan_led=0;
}
}
}
// Ham con
//-----------***************Chuyen doi tu DEC sang BCD************-----------------
unsigned char dec_2_bcd(unsigned char value1)//chuyen doi tu he decima sang BCD
{
return ((value1/10)<<4)|(value1%10 ); //((value1/10)*16+value1%10);
}
//----------**************Chuyen doi tu BCD sang DEC**********---------------------
unsigned char bcd_2_dec(unsigned char value2)//chuyen doi tu he BCD sang DECIMA
{
return ((value2/16)*10+value2%16);
}
//--------***********tao thoi gian tre **********-------------
void delay_1 (int time_delay)
{
unsigned int temp;
for (temp=0;temp<time_delay;temp++)
{
delay_us(1);
}
}
void delay_2(int time_delay)
{
unsigned int temp;
for (temp=0;temp<time_delay;temp++)
{
delay_ms(1);
}
}
//------******Khoi dong I2C******---------
void i2c_start(void)
{
scl=1;
sda=1;
delay_us(1);
sda=0;
scl=0;
}
//-----******Ket thuc I2C*******---------
void i2c_stop(void)
{
sda=0;
scl=1;
delay_us(1);
sda=1;
scl=0;
}
//-------*******Gui du lieu len duong I2C******------
void write_i2c(unsigned char data_2_send)
{
unsigned char temp;
for(temp=0;temp<8;temp++)
{
sda=(data_2_send&0x80)? 1:0; //dua bit du lieu ra chan SDA
scl=1; //
delay_us(1); //
scl=0;
data_2_send<<=1; //bit co trong so lon hon duoc truyen truoc
}
scl=1; //nhan bit ACK tu SLAVER
delay_us(1);
scl=0;
}
//------------**********Doc di lieu tren duong I2C******---------
unsigned char read_i2c(void)
{
unsigned char temp,i;
for(i=0;i<8;i++) // nhan vao 8 bit
{
scl=1;
temp<<=1;
temp=temp|sda;
scl=0;
}
sda=1;//gui bit ACK
scl=1;//master nhan/gui bit du lieu(sda) khi scl o muc cao
delay_us(1);
scl=0;//du lieu(sda) thay doi khi scl muc thap*/
return temp;//tra gia tri cho ham
}
//------*****Ghi du lieu len RTC qua duong I2C*****----------
void write_rtc(char value,addr)
{
i2c_start();//start i2c
write_i2c(0xd0);//0xd0 la ID cua DS1307
write_i2c(addr);
write_i2c(value);
i2c_stop();
}
//---------********Doc du lieu tu RTC qua duong I2C*****--------
char read_rtc(char addr)
{
unsigned char temp;
i2c_start();
write_i2c(0xd0);//
write_i2c(addr); //bat dau doc du lieu tu thanh ghi co dia chi 0x0
i2c_start();
write_i2c(0xd1); //0xd0 + 0x01 la ID cua ds107 va che do doc du lieu
temp=read_i2c();//con tro dang tro toi dia chi 0x0 chua du lieu second
i2c_stop();//Stop I2C
return temp;
}
//-----------***********Lay du lieu thoi gian tu cac thanh ghi trong RTC*******---------
void get_time()
{
//sec=read_rtc(reg_sec);
min=read_rtc(reg_min);
hour=read_rtc(reg_hour);
day=read_rtc(reg_day);
date=read_rtc(reg_date);
month=read_rtc(reg_month);
year=read_rtc(reg_year);
}
///////---------------***********Ghi du lieu va bo dem****-----------
void write_led_buffer(void)
{
unsigned char temp;
//*******Hien thi phut**********
led_buffer[0]=min&0x0f; //Lay 4 bit thap
led_buffer[1]=(min&0x70)>>4; //lay 3 bit cao
//*******Hien thi gio*************
if((temp=(hour&0x30)>>4)==0)//khong hien thi so 0 o hang chuc
{
led_buffer[3]=0x0a;
}else
{
led_buffer[3]=temp;//Lay 2 bit cao
}
led_buffer[2]=(hour&0x0f); //Lay 4 bit thap
//Hien thi thu trong tuan
led_buffer[4]=(day&0x7);
//*******Hien thi ngay AL**********
led_buffer[5]=date_1%10;
if((temp=(date_1/10))==0)//khong hien thi so 0 o hang chuc
{
led_buffer[6]=0x0a;
}else
{
led_buffer[6]=temp;
}
//*******Hien thi Thang AL*********
led_buffer[7]=month_1%10;
if((temp=(month_1/10))==0)//khong hien thi so 0 o hang chuc
{
led_buffer[8]=0x0a;
}else
{
led_buffer[8]=temp;
}
//******Hien thi nam***********
led_buffer[9]=(year&0x70)>>4;
led_buffer[10]=year&0x0f;
//*********Hien thi thang************
led_buffer[11]=month&0x0f;
if(((month&0x30)>>4)==0)//khong hien thi so 0 o hang chuc
{
led_buffer[12]=0x0a;
}else
{
led_buffer[12]=(month&0x30)>>4;
}
//Hien thi Ngay duong lich
led_buffer[13]=date&0x0f;
if((temp=(date&0x30)>>4)==0)//khong hien thi so 0 o hang chuc
{
led_buffer[14]=0x0a;
}else
{
led_buffer[14]=temp;
}
}
//-----------------**********Cai dat che do cho RTC****------------------
void setup_rtc(void)
{
unsigned char temp;
/*i2c_start();
write_i2c(0xd0);//che do ghi du lieu vao RTC
write_i2c(0x08);//thanh ghi control
write_i2c(0x10);//xung ra 1Hz muc thap khi khong dc kich hoat
i2c_stop();*/
write_rtc(0x10,0x07);
temp=read_rtc(0x02);
write_rtc(0x02,temp&0x7f);
}
//-----------------**************Do phim**********----------------------
void do_phim(void)
{
if(menu==0)//phim menu duoc an
{
delay_2(50);
if(var_arlam==0)//Neu khong thuc hien chuc nang hen gio
{
if(var_menu<=7)
{
var_menu++; //thuc hien menu tiep
}else
{
var_menu=1;//quay tro ve menu dau tien
}
}else
{
var_arlam=0;//hen gio xong
}
delay_2(50);
}
if(al_cancel==0)
{
var_menu=0;
var_arlam=1;
delay_1(10000);
}
}
//-----------******Chinh Giay********---------
void menu1(void)
{
if((plus==0)||(minus==0))
{
write_rtc(0x0,reg_sec); //Reset GIAY
delay_2(50);
}
}
//----------*********Chinh PHUT*********------------
void menu2(void)
{
unsigned char temp;
temp=bcd_2_dec(min);
if(plus==0)
{
if(temp<59)
{
temp++;
}else
{
temp=0;
}
write_rtc(dec_2_bcd(temp),reg_min);
delay_2(50);
}
if(minus==0)
{
if(temp>0)
{
temp--;
}else
{
temp=59;
}
write_rtc(dec_2_bcd(temp),reg_min);
delay_2(50);
}
}
//----------*********Chinh gio***********------------
void menu3(void)
{
unsigned char temp;
temp=bcd_2_dec(hour);
if(plus==0)
{
if(temp>=23)
{
temp=0;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_hour);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp<=0)
{
temp=23;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_hour);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//----------------********Chinh Ngay Trong Tuan*******----------------------
void menu4(void)
{
unsigned char temp;
temp=bcd_2_dec(day);
if(plus==0)
{
if(temp>=7)
{
temp=1;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_day);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp<=1)
{
temp=7;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_day);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//-----------********Chinh Ngay********----------------
void menu5()
{
unsigned char temp,temp1,temp2;
temp=bcd_2_dec(date);
temp1=bcd_2_dec(month);
temp2=bcd_2_dec(year);
if(plus==0) //******An PLUS***----
{
if((temp1==4)||(temp1==6)||(temp1==9)||(temp1==11) )//Cac thang co 30 ngay
{
if(temp>=30)
{
temp=1;
}
}else
{
if(temp1==2)//thang 2
{
if(temp2%4==0)//nam nhuan
{
if(temp>=29)
{
temp=1;
}else
{
temp++;
}
}else //nam thuong
{
if(temp>=28)
{
temp=1;
}else
{
temp++;
}
}
}else //cac thang co 31 ngay
{
if(temp>=31)
{
temp=1;
}else
{
temp++;
}
}
}
write_rtc(dec_2_bcd(temp),reg_date);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0) //-********AN MINUS****--------
{
if(temp>2)
{
temp--;
}else
{
if((temp1==4)||(temp1==6)||(temp1==9)||(temp1==11) )//Cac thang co 30 ngay
{
temp=30;
}else
{
if(temp1==2)//thang 2
{
if(temp2%4==0)//nam nhuan
{
temp=29;
}else //nam thuong
{
temp=28;
}
}else //cac thang co 31 ngay
{
temp=31;
}
}
}
write_rtc(dec_2_bcd(temp),reg_date);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
void menu6(void)//-------*********Chinh Thang-*******---------
{
unsigned char temp;
temp=bcd_2_dec(month);
if(plus==0)
{
if(temp>=12)
{
temp=1;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_month);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp==0)
{
temp=12;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_month);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//---------**********Chinh Nam**********--------------
void menu7(void)
{ unsigned char temp;
temp=bcd_2_dec(year);
if(plus==0)
{
if(temp>=50)//Chinh thoi gian tu nam 2011 den Nam 2050
{
temp=11;
}else
{
temp++;
}
write_rtc(dec_2_bcd(temp),reg_year);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
if(minus==0)
{
if(temp<=11)
{
temp=50;
}else
{
temp--;
}
write_rtc(dec_2_bcd(temp),reg_year);//ghi du lieu BCD vao thanh ghi GiO cua RTC
delay_2(50);
}
}
//-----------***********Thuc Hien MENU*********---------------
void xu_ly_menu(void)
{
while(var_menu!=0)
{
get_time();
write_led_buffer();
switch(var_menu)
{
case 1:
menu1();
break;
case 2:
menu2();
break;
case 3:
menu3();
break;
case 4:
menu4();
break;
case 5:
menu5();
break;
case 6:
menu6();
break;
case 7:
menu7();
break;
}
do_phim();
}
}
//-------********Dua du lieu ra bo nho dem LED******---------
void start_system()
{
var_menu=0;
//var_arlam=0;
}
//-- chuyen ngay duong sang ngay am------
void da(int m,d)
{
ngayduong=(m-1)*31+d;//so ngay duong so voi 1.1
if ((m==3)|(m==4)) ngayduong=ngayduong-3;//do thang 2 co 28 ngay
else
if ((m==5)|(m==6)) ngayduong=ngayduong-4;// thang 4 co 30 ngay
else
if ((m==7)|(m==8)|(m==9)) ngayduong=ngayduong-5;//thang 6 co 30 ngay
else
if ((m==10)|(m==11)) ngayduong=ngayduong-6;//thang 9 co 30 ngay
else
if (m==12) ngayduong=ngayduong-7;// thang 11 co 30 ngay
ngayam=ngayduong+26;//mung 1.1 la ngay 27.11
thangam=11;
if (ngayam>=30)
{
ngayam=ngayam-29;
thangam=12;
}
while (ngayam>=30)
{
if ((thangam==2)|(thangam==5)|(thangam==8)|(thangam== 7)|(thangam==10))
{
if (ngayam==29) break;
ngayam=ngayam-29;
thangam++;
if (thangam==13) thangam=1;
}
else
if (ngayam==30) break;
else
{
ngayam=ngayam-30;
thangam++;
if (thangam==13) thangam=1;
}
}
}
//---------------**************Chuong trinh chinh**********----------------------
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x04;
TCNT0=0xBB;
OCR0=0x01;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// khoi khoi tao i2c
i2c_init();
//khoi khoi tao ds1307
rtc_init(0,0,0);
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 32768Hz
rtc_init(3,1,0); u
#asm {"sei"}
#endasm
start_system();
setup_rtc();
while(1)
{
//Dat ngay Am lich o day
//////////
get_time();
da(bcd_2_dec(month),bcd_2_dec(date));//dau vao la thang duong ngay duong dau ra la thangam ngayam hien thi ngayam thangam
date_1=thangam;
month_1=ngayam;
write_led_buffer();
do_phim();
xu_ly_menu();
}
}
//-----------------**************Ket thuc chuong trinh********--------------------
Comment