ZigBee-CC2530单片机 - 4路硬件定时器PWM输出
程序源码(如需定制源码私聊即可!)
#include <ioCC2530.h>
typedef unsigned char u8;
typedef unsigned int u16;
#define TimerClock_32MHZ 1
#define TimerClock_16MHZ 2
#define TimerClock_8MHZ 3
#define TimerClock_4MHZ 4
#define TimerClock_2MHZ 5
#define TimerClock_1MHZ 6
#define TimerClock_500KHZ 7
#define TimerClock_250KHZ 8
#define T1_CH1 1
#define T1_CH2 2
#define T1_CH3 3
#define T1_CH4 4
void SystemClockInit()
{
CLKCONCMD &= ~0x40;
while(CLKCONSTA & 0x40);
CLKCONCMD &= ~0x07;
}
void Timer1PwmConfig(u8 Clock,u16 count)
{
if(Clock==TimerClock_32MHZ)
{
CLKCONCMD &= ~(1<<5);
CLKCONCMD &= ~(1<<4);
CLKCONCMD &= ~(1<<3);
}
else if(Clock==TimerClock_16MHZ)
{
CLKCONCMD &= ~(1<<5);
CLKCONCMD &= ~(1<<4);
CLKCONCMD |= (1<<3);
}
else if(Clock==TimerClock_8MHZ)
{
CLKCONCMD &= ~(1<<5);
CLKCONCMD |= (1<<4);
CLKCONCMD &= ~(1<<3);
}
else if(Clock==TimerClock_4MHZ)
{
CLKCONCMD &= ~(1<<5);
CLKCONCMD |= (1<<4);
CLKCONCMD |= (1<<3);
}
else if(Clock==TimerClock_2MHZ)
{
CLKCONCMD |= (1<<5);
CLKCONCMD &= ~(1<<4);
CLKCONCMD &= ~(1<<3);
}
else if(Clock==TimerClock_1MHZ)
{
CLKCONCMD |= (1<<5);
CLKCONCMD &= ~(1<<4);
CLKCONCMD |= (1<<3);
}
else if(Clock==TimerClock_500KHZ)
{
CLKCONCMD |= (1<<5);
CLKCONCMD |= (1<<4);
CLKCONCMD &= ~(1<<3);
}
else if(Clock==TimerClock_250KHZ)
{
CLKCONCMD |= (1<<5);
CLKCONCMD |= (1<<4);
CLKCONCMD |= (1<<3);
}
PERCFG |= 0x40;
P2SEL &= ~0x10;
P2DIR |= 0x80;
P1DIR |= 0x03;
P1SEL |= 0x03;
P0DIR |= 0xC0;
P0SEL |= 0xC0;
count--;
T1CC0H = count>>8;
T1CC0L = count;
T1CTL = 0x02;
}
void Timer1PwmOut(u8 channel,u16 value)
{
if(channel==T1_CH1)
{
T1CCTL1 = 0x1c;
T1CC1H = value>>8;
T1CC1L = value;
}
else if(channel==T1_CH2)
{
T1CCTL2 = 0x1c;
T1CC2H = value>>8;
T1CC2L = value;
}
else if(channel==T1_CH3)
{
T1CCTL3 = 0x1c;
T1CC3H = value>>8;
T1CC3L = value;
}
else if(channel==T1_CH4)
{
T1CCTL4 = 0x1c;
T1CC4H = value>>8;
T1CC4L = value;
}
}
void main(void)
{
u16 count,CH1PWM,CH2PWM,CH3PWM,CH4PWM;
SystemClockInit();
count=20000;
CH1PWM=500;
CH2PWM=1000;
CH3PWM=1500;
CH4PWM=2500;
Timer1PwmConfig(TimerClock_1MHZ,count);
Timer1PwmOut(T1_CH1,(count-CH1PWM));
Timer1PwmOut(T1_CH2,(count-CH2PWM));
Timer1PwmOut(T1_CH3,(count-CH3PWM));
Timer1PwmOut(T1_CH4,(count-CH4PWM));
while(1)
{
}
}