源代码:
/**************************************************
** 本程序只供学习使用,未经作者许可,不得用于其它任何用途
** 作者: shizhe
** 创始时间:2014-05-08
** 修改人:shizhe
** 修改时间:2014-05-08
** 修改人:shizhe
** 修改时间:2014-05-08
** 描述:
** 定时器1比较输出PWM
** 版权所有,盗版必究。
** Copyright(C) 联控智能 2014-2020
** All rights reserved
*************************************************/
#include
#define uint unsigned int
#define uchar unsigned char
#define BV(n) (1 << (n))
void T1_Init(void)//系统默认为16M
{
//设置pwm端口为输出
P0DIR|= BV(3)|BV(4)|BV(5);
//设置pwm端口为外设端口,非gpio
P0SEL|= BV(3)|BV(4)|BV(5);
//由于uart等会占用我们当前使用的pwm端口,因此需要将uart等重映射到别的端口去。
PERCFG |= 0x33; // Move USART1&2 to alternate2 location so that T1 is visible
T1CTL = 0x0e; // Div = 128, CLR, MODE = module
T1CCTL1 = 0x1c; // IM = 0; CMP = Clear output on compare; Mode = Compare
T1CCTL2 = 0x1c; // IM = 0; CMP = Clear output on compare; Mode = Compare
T1CCTL3 = 0x1c; // IM = 0, CMP = Clear output on compare; Mode = Compare
T1CNTL = 0; // Reset timer to 0;
T1CNTH = 0; // Reset timer to 0;
//必须设置,否则定时器不工作
T1CCTL0 = 0x1C; // IM = 1, CMP = Clear output on compare; Mode = Compare
T1CC0H = 0x07; // Ticks = 375 (2.4ms)
T1CC0L = 0XF8; // Ticks = 375 (2.4ms)
T1CC1H = 0x03; // Ticks = 375 (1,5ms initial duty cycle)
T1CC1L = 0Xc0;
T1CC2H = 0x03; // Ticks = 375 (1,5ms initial duty cycle)
T1CC2L = 0Xc0;
T1CC3H = 0x03; // Ticks = 375 (1,5ms initial duty cycle)
T1CC3L = 0Xc0;
EA=1;
IEN1 |= 0x02; // Enable T1 cpu interrupt
}
void main(void)
{
T1_Init();
while(1)
{
}
}