产品:液晶电视冰箱洗衣机抽油烟机热水器燃气灶微波炉除湿器面包机电风扇手机豆浆机

avr基于ATmega128的采用片内定时器的LCD电子时钟设计

常见问题

#include // DS1302 Real Time Clock functions#asm.equ __ds1302_port=0x18 ;PORTB.equ __ds1302_io=0.equ __ds1302_sclk=1.equ __ds1302_rst=2#endasm#include #include #include "led.h"#include "key.h"unsigned char Dout[8]={0,1,2,3,4,5,6,7};unsigned char LED_N=0;//led dispalyinterrupt [TIM1_COMPA] void timer1_compa_isr(void){DisplayBit(LED_N,Dout[LED_N]);LED_N++;if(LED_N>7) LED_N=0;}void PortInit(void){PORTB = 0xFF;DDRB = 0xFF;PORTD = 0x00; //displayDDRD = 0xFF;PORTE = 0x00; //displayDDRE = 0xFF;PORTF = 0xFF; //input,pull upDDRF = 0x00;}void Timer1Init(void){TCCR1B = 0x00; //stopOCR1AH = 0x06;OCR1AL = 0x81;TCCR1A = 0x00;TCCR1B = 0x0A; //start Timer}void DataChange(unsigned char x,unsigned char y,unsigned char z){Dout[0]=x/10;Dout[1]=x%10;Dout[2]=0x80;Dout[3]=y/10;Dout[4]=y%10;Dout[5]=0x80;Dout[6]=z/10;Dout[7]=z%10;}void main(void){unsigned char hour=13,min=25,sec=35;unsigned char date=10,month=6,year=6;PortInit();Timer1Init();TIMSK=0x10;ETIMSK=0x00;// DS1302 Real Time Clock initialization// Trickle charger: Offrtc_init(0,0,0);rtc_set_time(hour,min,sec);rtc_set_date(date,month,year);#asm("sei")while (1){if(!TimeSwitch){while(!TimeSwitch);if(TimeSetFlag){if(TimeSFlag) rtc_set_time(hour,min,sec);else rtc_set_date(date,month,year);TimeSetFlag=0;TimeSetTmp=0;}else TimeSFlag++; //date,time switch}if(TimeSFlag&&!TimeSetFlag) //TimeSFlag=1,time;TimeSFlag=0,date{rtc_get_time(&hour,&min,&sec);DataChange(hour,min,sec);}else if(!TimeSetFlag){rtc_get_date(&date,&month,&year);DataChange(date,month,year);}if(!TimeSet){while(!TimeSet);TimeSetFlag=1;switch(TimeSetTmp){case 0: //hour or dateif(TimeSFlag) DataChange(0,min,sec);else DataChange(0,month,year);break;case 1: //min or yearif(TimeSFlag) DataChange(hour,0,sec);else DataChange(date,0,year);break;case 2: //sec or yearif(TimeSFlag) DataChange(hour,min,0);else DataChange(date,month,0);break;}TimeSetTmp++;if(TimeSetTmp>2) TimeSetTmp=0;}if(!TimeAdd){while(!TimeAdd);if(TimeSetFlag){switch(TimeSetTmp){case 1: //hour or dateif(TimeSFlag){hour++;if(hour>23) hour=0;DataChange(hour,min,sec);}else{date++;if(month==2&&((year%4==0 && year%100!=0) || (year%400==0))&&date>29)//闰年二月29天date=1;else if(month==2&&date>28)date=1;else if((month==4||month==6||month==9||month==11)&&date>30)date=1;else if(date>31) date=1;DataChange(date,month,year);}break;case 2: //min or monthif(TimeSFlag){min++;if(min>59) min=0;DataChange(hour,min,sec);}else{month++;if(month>12) month=1;DataChange(date,month,year);}break;case 0: //sec or yearif(TimeSFlag){sec++;if(sec>59) sec=0;DataChange(hour,min,sec);}else{year++;if(year>20) year=0;DataChange(date,month,year);}break;}}}delay_ms(100);};}#ifndef KEY_H#define KEY_H#define TimeSwitch PINF.2#define TimeSet PINF.1#define TimeAdd PINF.0bit TimeSFlag=0; //time=1,date=0;bit TimeSetFlag=0; //set enable=0,able=1;unsigned char TimeSetTmp=0;#endif//8位共阳#ifndef LED_H#define LED_H#define LedData PORTD#define LedBit PORTE#define uchar unsigned char#define uint unsigned intuchar LedTable[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//在第x个显示void DisplayBit(uchar x,uchar data){LedBit=0x01<LedData=LedTable[data];}#endif

标签:3计步器   

相关问题

热门问题

0