目录
- 硬知识
- 定时器初始化函数
- 测试
- 基本功能测试
- main.c
- 中断服务函数
- 实验现象
- 模拟PWM信号
- main.c
- 实验现象
STC实验箱4
IAP15W4K58S4
Keil uVision V5.29.0.0
PK51 Prof.Developers Kit Version:9.60.0.0
硬知识
摘自《STC库函数使用参考》
定时器初始化函数
Timer_Inilize
TIM_InitTypeDef的定义见于文件"timer.H".
typedef struct
{u8 TIM_Mode; //工作模式u8 TIM_Polity; //优先级设置u8 TIM_Interrupt; //中断允许u8 TIM_CIkSource; //时钟源u8 TIM CIkOut; //可编程时钟输出u16 TIM_Value; //装载初值u8 TIM_Run; //是否运行
}TIM_InitTypeDef;
TIM_Mode:定时器的工作模式:
TIM_Polity:中断的优先级:
TIM_Interrupt:中断允许或禁止:
TIM_CIkSource:定时器的时钟源选择:
TIM_CIkOut:定时器溢出时取反对应IO输出高速时钟:
TIM_Value:一个16位的初值。
TIM_Run:初始化后是否运行定时器:
测试
基本功能测试
main.c
#include "./Drivers/config.h"
#include "./Drivers/delay.h"#include "./Drivers/timer.h"
#include "./Drivers/GPIO.h"/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ---------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ---------------------*/
/* --- Web: www.GXWMCU.com -----------------------------------------*/
/* --- QQ: 800003751 ----------------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*//************* 功能说明 **************本程序演示3个定时器的使用。定时器0做16位自动重装10us定时,不中断,从P3.5高速输出脉宽10us 50KHZ时钟信号。用户可以修改时间从而修改频率,也可以允许中断,但注意时间不要太短(一般10us以上)。定时器1做16位自动重装1ms定时, 中断,从P3.4高速输出脉宽1ms 500HZ时钟信号。定时器2做16位自动重装20ms定时,中断,从P3.0高速输出脉宽20ms 25HZ时钟信号。同时中断里从P1.0取反输出25HZ时钟信号。******************************************//************************ IO口配置 ****************************/
void GPIO_config(void)
{GPIO_InitTypeDef GPIO_InitStructure; //结构定义GPIO_InitStructure.Mode = GPIO_OUT_PP; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PPGPIO_InitStructure.Pin = GPIO_Pin_4 | GPIO_Pin_5; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作GPIO_Inilize(GPIO_P3,&GPIO_InitStructure); //初始化GPIO_InitStructure.Pin = GPIO_Pin_0; //指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7, 或操作GPIO_Inilize(GPIO_P1,&GPIO_InitStructure); //初始化
}/************************ 定时器配置 ****************************/
void Timer_config(void)
{TIM_InitTypeDef TIM_InitStructure; //结构定义TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMaskTIM_InitStructure.TIM_Polity = PolityLow; //指定中断优先级, PolityHigh,PolityLowTIM_InitStructure.TIM_Interrupt = DISABLE; //中断是否允许, ENABLE或DISABLETIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_ExtTIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLETIM_InitStructure.TIM_Value = 65536UL - ((double)MAIN_Fosc * 10/1000000.); //初值,TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLETimer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMaskTIM_InitStructure.TIM_Polity = PolityLow; //指定中断优先级, PolityHigh,PolityLowTIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLETIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_ExtTIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLETIM_InitStructure.TIM_Value = 65536UL - ((double)MAIN_Fosc * 1/1000.); //初值,TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLETimer_Inilize(Timer1,&TIM_InitStructure); //初始化Timer1 Timer0,Timer1,Timer2TIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLE. (注意: Timer2固定为16位自动重装, 中断固定为低优先级)TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_ExtTIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLETIM_InitStructure.TIM_Value = 65536UL - ((double)MAIN_Fosc/12 * 20/1000.); //初值TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLETimer_Inilize(Timer2,&TIM_InitStructure); //初始化Timer2 Timer0,Timer1,Timer2
}/******************** 主函数**************************/
void main(void)
{GPIO_config();Timer_config();EA = 1;while (1){}
}
中断服务函数
/********************* Timer0中断函数************************/
void timer0_int (void) interrupt TIMER0_VECTOR
{}/********************* Timer1中断函数************************/
void timer1_int (void) interrupt TIMER1_VECTOR
{}/********************* Timer2中断函数************************/
void timer2_int (void) interrupt TIMER2_VECTOR
{P10 = ~P10;
}
实验现象
模拟PWM信号
修改timer.c中的timer0_int函数:
/********************* Timer0中断函数************************/
void timer0_int (void) interrupt TIMER0_VECTOR
{extern u16 PWM_high,PWM_low; //中间变量,用户请勿修改。if(P35){TH0 = (u8)(PWM_low >> 8); //如果是输出高电平,则装载低电平时间。TL0 = (u8)PWM_low;}else{TH0 = (u8)(PWM_high >> 8); //如果是输出低电平,则装载高电平时间。TL0 = (u8)PWM_high;}
}
main.c
#include "./Drivers/config.h"
#include "./Drivers/delay.h"#include "./Drivers/GPIO.h"
#include "./Drivers/timer.h"/************* 功能说明 **************本程序演示使用定时器做软件PWM。定时器0做16位自动重装,中断,从T0CLKO高速输出PWM。本例程是使用STC15F/L系列MCU的定时器T0做模拟PWM的例程。PWM可以是任意的量程。但是由于软件重装需要一点时间,所以PWM占空比最小为32T/周期,最大为(周期-32T)/周期, T为时钟周期。PWM频率为周期的倒数。假如周期为6000, 使用24MHZ的主频,则PWM频率为4000HZ。******************************************//************* 本地常量声明 **************/#define PWM_DUTY 6000 //定义PWM的周期,数值为时钟周期数,假如使用24MHZ的主频,则PWM频率为6000HZ。#define PWM_HIGH_MIN 32 //限制PWM输出的最小占空比。用户请勿修改。
#define PWM_HIGH_MAX (PWM_DUTY-PWM_HIGH_MIN) //限制PWM输出的最大占空比。用户请勿修改。/************* 本地变量声明 **************/u16 PWM_high,PWM_low; //中间变量,用户请勿修改。
u16 pwm; //定义PWM输出高电平的时间的变量。用户操作PWM的变量。/************* 本地函数声明 **************//************* 外部函数和变量声明 *****************/void GPIO_config(void)
{GPIO_InitTypeDef GPIO_InitStructure; //结构定义GPIO_InitStructure.Mode = GPIO_OUT_PP; //指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PPGPIO_InitStructure.Pin = GPIO_Pin_5; //指定要初始化的IO, 或操作GPIO_Inilize(GPIO_P3, &GPIO_InitStructure); //P3.5初始化为推挽输出
}/************************ 定时器配置 ****************************/
void Timer_config(void)
{TIM_InitTypeDef TIM_InitStructure; //结构定义TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMaskTIM_InitStructure.TIM_Polity = PolityHigh; //指定中断优先级, PolityHigh,PolityLowTIM_InitStructure.TIM_Interrupt = ENABLE; //中断是否允许, ENABLE或DISABLETIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_ExtTIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLETIM_InitStructure.TIM_Value = 65536UL - PWM_HIGH_MIN; //初值,TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLETimer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2
}/**************** 计算PWM重装值函数 *******************/
void LoadPWM(u16 i)
{u16 j;if(i > PWM_HIGH_MAX) i = PWM_HIGH_MAX; //如果写入大于最大占空比数据,则强制为最大占空比。if(i < PWM_HIGH_MIN) i = PWM_HIGH_MIN; //如果写入小于最小占空比数据,则强制为最小占空比。j = 65536UL - PWM_DUTY + i; //计算PWM低电平时间i = 65536UL - i; //计算PWM高电平时间EA = 0;PWM_high = i; //装载PWM高电平时间PWM_low = j; //装载PWM低电平时间EA = 1;
}/******************** 主函数**************************/
void main(void)
{GPIO_config();Timer_config();EA = 1;P35 = 0; //对于STC15W408S系列,要输出低,或者设置成推挽或OD,否则不正常pwm = (u16)(PWM_DUTY * 0.25); //给PWM一个初值,这里为25%占空比LoadPWM(pwm); //计算PWM重装值while (1){}
}
实验现象
PWM频率 = 定时器时钟频率 / PWM_DUTY =30MHz/6000=5000Hz= 30MHz / 6000 = 5000 Hz=30MHz/6000=5000Hz