?? avr134.c
字號:
/**** A V R A P P L I C A T I O N NOTE 1 3 4 **************************
*
* Title: Real Time Clock
* Version: 1.01
* Last Updated: 12.10.98
*
* ***** ADAPTED FOR THE CodeVisionAVR C Compiler *****
* ***** BY HP InfoTech s.r.l. www.hpinfotech.ro *****
* ***** MAY BE TESTED WITH THE STK300 STARTER KIT *****
*
* Target: ATmega103 (All AVR Devices with secondary external oscillator)
*
* Support E-mail: avr@atmel.com
*
* Description
* This application note shows how to implement a Real Time Clock utilizing a secondary
* external oscilator. Included a test program that performs this function, which keeps
* track of time, date, month, and year with auto leap-year configuration. 8 LEDs are used
* to display the RTC. The 1st LED flashes every second, the next six represents the
* minute, and the 8th LED represents the hour.
*
******************************************************************************************/
// I/O register definitions for the ATmega103
#include <mega103.h>
// bit definitions for the TIMSK register
#define TOIE0 0
#define OCIE0 1
// bit definitions for the ASSR register
#define AS0 3
char not_leap(void);
typedef struct{
unsigned char second; //enter the current time, date, month, and year
unsigned char minute;
unsigned char hour;
unsigned char date;
unsigned char month;
unsigned int year;
}time;
time t;
void main(void)
{
// init_rtc();
int temp0,temp1;
for(temp0=0;temp0<0x0040;temp0++) // Wait for external clock crystal to stabilize
{
for(temp1=0;temp1<0xFFFF;temp1++);
}
DDRB=0xFF;
TIMSK &=~((1<<TOIE0)|(1<<OCIE0)); //Disable TC0 interrupt
ASSR |= (1<<AS0); //set Timer/Counter0 to be asynchronous from the CPU clock
//with a second external clock(32,768kHz)driving it.
TCNT0 = 0x00;
TCCR0 = 0x05; //prescale the timer to be clock source / 128 to make it
//exactly 1 second for every overflow to occur
while(ASSR&0x07); //Wait until TC0 is updated
TIMSK |= (1<<TOIE0); //set 8-bit Timer/Counter0 Overflow Interrupt Enable
#asm("sei") //set the Global Interrupt Enable Bit
while(1)
{
MCUCR = 0x38; //entering sleeping mode: power save mode
#asm("sleep") //will wake up from time overflow interrupt
#asm("nop")
OCR0 = 0; //Write dummy value to Output Compare register
while(ASSR&0x02); //Wait until OCR0 is updated
}
}
interrupt [TIM0_OVF] void counter(void) //overflow interrupt vector
{
if (++t.second==60) //keep track of time, date, month, and year
{
t.second=0;
if (++t.minute==60)
{
t.minute=0;
if (++t.hour==24)
{
t.hour=0;
if (++t.date==32)
{
t.month++;
t.date=1;
}
else if (t.date==31)
{
if ((t.month==4) || (t.month==6) || (t.month==9) || (t.month==11))
{
t.month++;
t.date=1;
}
}
else if (t.date==30)
{
if(t.month==2)
{
t.month++;
t.date=1;
}
}
else if (t.date==29)
{
if((t.month==2) && (not_leap()))
{
t.month++;
t.date=1;
}
}
if (t.month==13)
{
t.month=1;
t.year++;
}
}
}
}
PORTB=~(((t.second&0x01)|t.minute<<1)|t.hour<<7);
}
char not_leap(void) //check for leap year
{
if (!(t.year%100))
return (char)(t.year%400);
else
return (char)(t.year%4);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -