📄 timer.c
字号:
/**** A V R A P P L I C A T I O N TIMER **************************
*
* Title: Real Time Clock
* Version: 1.00
* Last Updated: 09.05.2006
*
* ***** ADAPTED FOR THE CodeVisionAVR C Compiler *****
* ***** MAY BE TESTED WITH THE STK300 STARTER KIT *****
*
* Target: ATmega16 (All AVR Devices with secondary external oscillator)
* 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 ATmega16
#include <mega8.h>
#include "util.h"
#include "time.h"
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;
// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here
printf("Timer2");
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
printf("Timer1");
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
unsigned int i;
gPeriod++;
gPeriodNum++;
// if(gPeriodNum >200)
// {
// power_led_on(1);
// gPeriodNum =0;
// }
/*
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++;
}
}
}
}
//printf("year=%x,month=%d,date=%d,hour=%d,min=%d,sec=%d",t.year,t.month,t.date,t.hour,t.minute,t.second);
*/
// printf("gPeriod=%x",gPeriod);
}
char not_leap(void) //check for leap year
{
if (!(t.year%100))
return (char)(t.year%400);
else
return (char)(t.year%4);
}
void clear_time(void)
{
t.year =0x2006;
t.month =1;
t.date =1;
t.hour = 0;
t.minute = 0;
t.second = 0;
}
// Timer/Counter 0 initialization
// Clock source: TOSC1 pin
// Clock value: TOSC1/128
// Mode: Normal top=FFh
// OC0 output: Disconnected
void _timer0_init(void)
{
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
TCCR0=0x05;
TCNT0=0x00;
}
void _timer2_init(void)
{
// 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;
}
void _timer1_init(void)
{
// 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
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -