/************************************************************************************
* Copyright (c), 2006-2007, C8051F网络
* All rights reserved.
*
* Http: www.c8051f.com
* Email: mite@vip.qq.com
*
* File name: timer.c
* File name: iic.c
* Project : C8051F040DK
* Processor: C8051F040
* Compiler : Keil C51 Compiler
*
* Author : EmbedARM
* Version: 1.00
* Date : 2009.8.5
* Email : EmbedARM@126.com
* Modification: none
*
* Description: 本文件是timer硬件驱动程序文件;
*
* Others: none;
*
* Function List:
* 1. void Timer0_Init(unsigned long freq)
* 2. void Timer0_Enable(void)
* 3. void Timer0_Disable(void)
* 4. void Timer0_SetValue(unsigned int value)
* 5. void Timer1_Init(unsigned long freq)
* 6. void Timer1_Enable(void)
* 7. void Timer1_Disable(void)
* 8. void Timer1_SetValue(unsigned int value)
* 9. void Timer2_Init(unsigned long freq)
* 10. void Timer2_Enable(void)
* 11. void Timer2_Disable(void)
* 12. void Timer2_SetValue(unsigned int value)
* 13. void Timer3_Init(unsigned long freq)
* 14. void Timer3_Enable(void)
* 15. void Timer3_Disable(void)
* 16. void Timer3_SetValue(unsigned int value)
*
* History:
* 1. Author: 李林利
* Version: 0.9
* Date: 2009.1.1
* Modification: none
*
*************************************************************************************/
/************************************************************************************/
// 引用外部头文件
#include "..\config\c8051f040.h"
#include "..\config\const.h"
#include "timer.h"
unsigned int timer0_10ms;
unsigned int timer2_10ms;
unsigned int timer3_10ms;
unsigned int timer4_10ms;
unsigned char T0h,T0l;
extern unsigned long code sysclk;
/*************************************************************************************/
//
// Timer0 参数及函数定义
//
// Note: Timer0未被占用,以下函数用户可自由修改使用
//
/*************************************************************************************/
/***********************************************************************************
* Function: Timer0_Init;
*
* Description: Timer0 初始化函数;
*
* Input: freq,定时或计数溢出频率;
*
* Return: none;
*
* Note: 该定时器未被占用用户可自由使用;
************************************************************************************/
void Timer0_Init(unsigned long freq)
{
unsigned int count;
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE; // set SFR page 0
CKCON = 0;
TMOD &= ~0x0f;
TMOD|=0x01;
count = -(sysclk/freq/12);
TH0 = (count>>8)&0x00ff;
TL0 = count&0x00ff;
T0h = TH0;
T0l = TL0;
ET0 = 1; // 允许中断
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer0_Enable;
*
* Description: Timer0使能函数, Timer0开始工作;
*
* Input: none;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer0_Enable(void)
{
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE; // set SFR page 0
TR0 = 1;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer0_Disable;
*
* Description: Timer0关闭函数, Timer0停止工作;
*
* Input: none;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer0_Disable(void)
{
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE; // set SFR page 0
TR0 = 0;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer0_SetValue;
*
* Description: 设置定时或计数寄存器值;
*
* Input: value,定时或计数寄存器值;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer0_SetValue(unsigned int value)
{
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE; // set SFR page 0
TL0 = value&0x00ff;
TH0 = (value>>8)&0x00ff;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer0_ISR;
*
* Description: 定时器0中断响应;
*
* Input: none;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer0_ISR (void) interrupt 1 using 3
{
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE; // set SFR page 0
TR0 = 0;
TH0 = T0h;
TL0 = T0l;
TR0 = 1;
timer0_10ms++;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/*************************************************************************************/
//
// Timer1 参数及函数定义
//
// Note: Timer1已被UART0占用,在不应用UART0的情况下
// 以下函数用户可自由修改使用
//
/*************************************************************************************/
/***********************************************************************************
* Function: Timer1_Init;
*
* Description: Timer1 初始化函数;
*
* Input: freq, 定时或计数溢出频率;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer1_Init(unsigned long freq)
{
unsigned char count,n;
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE;
TMOD&=0x0f;
TMOD|=0;
//PCON |= 0x90; // SMOD1 = 1, 禁止Uart1的波特率/2功能
n = sysclk/freq/16/256; // 计算判断变量
if (n < 1)
{
count = -(sysclk/freq/16); // 计算定时器计数值
CKCON |= 0x10; // T1M = 1,系统时钟
}
else if (n < 12)
{
count = -(sysclk/freq/16/12); // 计算定时器计数值
CKCON &= ~0x10; // T1M = 1,系统时钟/12
}
else
{
PCON &= ~0x10; // SMOD1 = 1, 允许Uart1的波特率/2功能
count = -(sysclk/freq/16/12); // 计算定时器计数值
CKCON &= ~0x10; // T1M = 1,系统时钟/12
}
TL1 = count;
TH1 = count;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer1_Enable;
*
* Description: Timer1使能函数,Timer1开始工作;
*
* Input: none;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer1_Enable(void)
{
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE;
TR1 = 1;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer1_Disable;
*
* Description: Timer1关闭函数,Timer0停止工作;
*
* Input: none;
*
* Return: none;
*
* Note: none;
************************************************************************************/
void Timer1_Disable(void)
{
char data SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TIMER01_PAGE;
TR1 = 0;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************************************************************
* Function: Timer1_SetValue;
*
* Description: 设置定时或计数寄存器值;
*
* Input: value,定时或计数寄存器值;
*
* Return: none;
*
* Note: none;
*