ZigBee-CC2530单片机 - 1路硬件PWM控制舵机角度(精度为1us)
程序源码
#include <ioCC2530.h>
typedef unsigned char uchar;
typedef unsigned int uint;
//系统时钟初始化
void SystemClockInit()
{
CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定为32M
CLKCONCMD &= ~0x07; //设置系统主时钟频率为32MHZ
}
//定时器1输出PWM
void Timer1PwmInit()
{
uint value;
CLKCONCMD |= 0x28; //时钟速度32 MHz 定时器标记输出设置[5:3]1MHz
PERCFG |= 0x40; //定时器1 的IO位置 1:备用位置2
P2SEL &= ~0x10; //定时器1优先
P2DIR |= 0xC0; //第1优先级:定时器1通道2-3
//timer1 通道2映射口P1_0
P1DIR |= 0x01;
P1SEL |= 0x01;
//设定周期
value=19999;
T1CC0H = value>>8;
T1CC0L = value;
// 模式选择 通道2比较模式
T1CCTL2 = 0x1c;
//不分频,模计数
T1CTL = 0x02;
}
//定时器通道2PWM输出
void Timer1ch2PwmOut(uint value)
{
value= 19999 - value;
T1CC2H = value>>8;
T1CC2L = value;
}
void main(void)
{
SystemClockInit();
Timer1PwmInit();
Timer1ch2PwmOut(2500-1);// P1_0 输出2.5ms的占空比
while(1)
{
}
}