📄 timer.c~
字号:
#include <mega128.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "typedef.h"
#include "app.h"
#include "cc2420.h"
#include "rtc.h"
#include "serialdrv.h"
#include "timer.h"
TIMERTICK timer1;
TIMERTICK timer2;
TIMERTICK timer3;
TIMERTICK timer4;
TIMERTICK timer5;
TIMERTICK timer6; // sp
TIMERTICK timer7; // g20_sp
TIMERTICK timer8; // usart0
//unsigned char naaa[10];
bit b_set_sp = FALSE;
bit b_set_g20_sp = FALSE;
bit b_set_usart0 = FALSE;
bit all_sending = FALSE;
bit reset_cc2420 = FALSE;
/*
uchar CLK_MIN = 0;
uchar CLK_SEC = 0;
uchar CLK_HOUR = 0;
uchar CLK_DAY = 0;
uchar CLK_MONTH = 0;
uchar CLK_YEAR = 0;
*/
UINT call_time = 0;
UINT free_time = 0;
void timer_set_sp()
{
b_set_sp = TRUE;
timer6.bUsed = TRUE;
timer6.ltimes = SPTIMEOUT;
}
void timer_set_g20_sp()
{
b_set_g20_sp = TRUE;
timer7.bUsed = TRUE;
timer7.ltimes = SPTIMEOUT;
}
void timer_set_usart0()
{
b_set_usart0 = TRUE;
timer8.bUsed = TRUE;
timer8.ltimes = USART0TIMEOUT;
}
unsigned char timer_get_sp_timeout()
{
wdr();
if(b_set_sp == TRUE)
{
if(timer6.bUsed == FALSE)
{
b_set_sp = FALSE;
return TRUE;
}
else
return FALSE;
}
return FALSE;
}
unsigned char timer_get_g20_sp_timeout()
{
wdr();
if(b_set_g20_sp == TRUE)
{
if(timer7.bUsed == FALSE)
{
b_set_g20_sp = FALSE;
return TRUE;
}
else
return FALSE;
}
return FALSE;
}
unsigned char timer_get_usart0_timeout()
{
wdr();
if(b_set_usart0 == TRUE)
{
if(timer8.bUsed == FALSE)
{
b_set_usart0 = FALSE;
return TRUE;
}
else
return FALSE;
}
return FALSE;
}
void timer0_init()
{
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 115.200 kHz
// Mode: CTC top=OCR0
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x0C;
TCNT0=0x00;
OCR0=0x73;
}
void timer2_init()
{
/*
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: CTC top=OCR2
// OC2 output: Disconnected
TCCR2=0x09;
TCNT2=0x00;
OCR2=0xD0;
*/
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
}
void timer1_init()
{
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 115.200 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C 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
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x03; //0x04
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
}
void timer3_initY()
{
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: 7372.800 kHz
// Mode: Ph. & fr. cor. PWM top=ICR3
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Non-Inv.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x08;
TCCR3B=0x11;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x61;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00; //0x30 //0x00
}
void timer3_initN()
{
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// Noise Canceler: Off
// Input Capture on Falling Edge
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Timer 3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;
}
void timer_init()
{
timer0_init();
timer1_init();
timer2_init();
timer3_initN();
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x02;
ETIMSK=0x00;
timer1.bUsed = FALSE;
timer2.bUsed = FALSE;
timer3.bUsed = FALSE;
timer4.bUsed = FALSE;
timer5.bUsed = FALSE;
timer6.bUsed = FALSE;
timer7.bUsed = FALSE;
timer8.bUsed = FALSE;
}
TIMERTICK * timer_settimer(DWORD ltimes)
{
wdr();
if(timer1.bUsed != TRUE)
{
timer1.bUsed = TRUE;
timer1.ltimes = ltimes;
return &timer1;
}
if(timer2.bUsed != TRUE)
{
timer2.bUsed = TRUE;
timer2.ltimes = ltimes;
return &timer2;
}
if(timer3.bUsed != TRUE)
{
timer3.bUsed = TRUE;
timer3.ltimes = ltimes;
return &timer3;
}
if(timer4.bUsed != TRUE)
{
timer4.bUsed = TRUE;
timer4.ltimes = ltimes;
return &timer4;
}
return NULL;
}
unsigned char timer_checktimeout(TIMERTICK * ptimer)
{
wdr();
if(ptimer == NULL)
return TRUE;
if(ptimer->bUsed != TRUE)
return TRUE;
if(ptimer->ltimes <=0)
return TRUE;
else
return FALSE;
}
void timer_delay(DWORD ltimes)
{
timer5.bUsed = TRUE;
timer5.ltimes = ltimes;
wdr();
while(timer_checktimeout(&timer5) != TRUE);
wdr();
}
// Timer 2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
// Place your code here
// call_time++;
}
// Timer 0 output compare interrupt service routine
interrupt [TIM0_COMP] void timer0_comp_isr(void)
{
/* uchar tempsec = 0;
uchar tempmin = 0;
uchar temphour = 0;
uchar tempday = 0;
uchar tempmonth = 0;
uchar tempyear = 0; */
if(timer1.bUsed == TRUE){
if(timer1.ltimes >0)
timer1.ltimes --;
else
timer1.bUsed = FALSE;}
if(timer2.bUsed == TRUE){
if(timer2.ltimes >0)
timer2.ltimes --;
else
timer2.bUsed = FALSE;}
if(timer3.bUsed == TRUE){
if(timer3.ltimes >0)
timer3.ltimes --;
else
timer3.bUsed = FALSE;}
if(timer4.bUsed == TRUE){
if(timer4.ltimes >0)
timer4.ltimes --;
else
timer4.bUsed = FALSE;}
if(timer5.bUsed == TRUE){
if(timer5.ltimes >0)
timer5.ltimes --;
else
timer5.bUsed = FALSE;}
if(timer6.bUsed == TRUE){
if(timer6.ltimes > 0)
timer6.ltimes --;
else
timer6.bUsed = FALSE;}
if(timer7.bUsed == TRUE){
if(timer7.ltimes > 0)
timer7.ltimes --;
else
timer7.bUsed = FALSE;}
if(timer8.bUsed == TRUE){
if(timer8.ltimes > 0)
timer8.ltimes --;
else
timer8.bUsed = FALSE;}
if (call_time++ >= CALL_TIMER) //60000
{
call_time = 0;
all_sending = TRUE;
// WORK_LED = !WORK_LED;
/* if (free_time++ >= 10) //120
{
free_time = 0;
reset_cc2420 = TRUE;
}*/
}
/*
if (free_time++ > 100) //3000
{
free_time = 0;
if(system_initial_flag != TRUE)
return;
if(rtc_set_flag == TRUE)
return;
v_Get1302(naaa);
tempsec = CLK_SEC;
tempmin = CLK_MIN;
temphour = CLK_HOUR;
tempday = CLK_DAY;
tempmonth = CLK_MONTH;
tempyear = CLK_YEAR;
CLK_SEC = bcd2hex(naaa[0]);
CLK_MIN = bcd2hex(naaa[1]);
CLK_HOUR = bcd2hex(naaa[2]);
CLK_DAY = bcd2hex(naaa[3]);
CLK_MONTH = bcd2hex(naaa[4]);
CLK_YEAR = bcd2hex(naaa[6]);
}*/
}
/* bcd to hex */
unsigned int bcd2hex(BYTE bdata)
{
unsigned int tmp;
// tmp = (((unsigned int)(bdata >> 4) * 10) + (unsigned int)(bdata & 0x0f));
tmp = (unsigned int)bdata / 16 * 10 + (unsigned int)bdata % 16;
return tmp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -