当前位置 - 股票行情交易網 - 國際漫評 - avr基於ATmega128的采用片內定時器的LCD電子時鐘設計

avr基於ATmega128的采用片內定時器的LCD電子時鐘設計

#include <mega128.h>

// 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 <ds1302.h>

#include <delay.h>

#include "led.h"

#include "key.h"

unsigned char Dout[8]={0,1,2,3,4,5,6,7};

unsigned char LED_N=0;

//led dispaly

interrupt [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; //display

DDRD = 0xFF;

PORTE = 0x00; //display

DDRE = 0xFF;

PORTF = 0xFF; //input,pull up

DDRF = 0x00;

}

void Timer1Init(void)

{

TCCR1B = 0x00; //stop

OCR1AH = 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: Off

rtc_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 date

if(TimeSFlag) DataChange(0,min,sec);

else DataChange(0,month,year);

break;

case 1: //min or year

if(TimeSFlag) DataChange(hour,0,sec);

else DataChange(date,0,year);

break;

case 2: //sec or year

if(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 date

if(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 month

if(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 year

if(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.0

bit 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 int

uchar LedTable[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

//在第x個顯示

void DisplayBit(uchar x,uchar data)

{

LedBit=0x01<<x;

LedData=LedTable[data];

}

#endif