我正在调试cc2540的Timer1,用通道1,2,3,4来输出PWM波,并且可以实时改变占空比。通道1和2(即P03,P04口)输出正常,可以实现变比。但是通道3和4(即P05,P06口)无法正常输出,有两个问题:1,无法实时改变占空比(不是我改占空比的程序问题),2,P05最高输出2.5V。不知道这是为什么。附上我的相关程序段,希望老师们可以帮忙解决!谢谢!
#define MODE_2 0x02 //0x0000 -> T1CC0
#define SET0_REn 0x30 //set when cc0, clear when ccn
#define CLR_IM 0x00 //IM = 0
#define CLR 0x00
#define RFIRQ 0x80 //When set, use RF interrupt for capture instead of regular capture input.
#define IM 0x40 //Channel 0 interrupt mask. Enables interrupt request when set.
#define CLR_Output 0x08 //Clear output on compare
void InitIO(void) {
P0DIR |= BIT3 + BIT4 + BIT5 + BIT6;
P0SEL |= BIT3 + BIT4 + BIT5 + BIT6;
}
void InitT1() {
InitIO();
PERCFG |= 0x03;
T1CTL |= DIV_128 + MODE_2; //128分频,模式2
T1CCTL1 |= CLR_IM + Compare_Mode + SET0_REn;// IM = 0;set when cc0, clear when ccn; Mode = Compare
T1CCTL2 |= CLR_IM + Compare_Mode + SET0_REn;// IM = 0;set when cc0, clear when ccn; Mode = Compare
T1CCTL3 |= CLR_IM + Compare_Mode + SET0_REn;// IM = 0;set when cc0, clear when ccn; Mode = Compare
T1CCTL4 |= CLR_IM + Compare_Mode + SET0_REn;// IM = 0;set when cc0, clear when ccn; Mode = Compare
T1CNTL = 0; // Reset timer to 0;
T1CCTL0 |= IM + CLR_Output + Compare_Mode;//0x4c
T1CC0L = 0xE8;
T1CC0H = 0x03; //计数1000次,即PWM周期为1000
T1CC1L = (char)500; T1CC1H = 500>>8;
T1CC2L = (char)500; T1CC2H = 500>>8;
T1CC3L = (char)500; T1CC3H = 500>>8;
T1CC4L = (char)500; T1CC4H = 500>>8;
EA = Start; //开总中断
IEN1 |= T1IE; //开定时器1中断
}
void Change_duty(int LF, int RF, int LB, int RB) {
T1CC1L = (char)LF; T1CC1H = (char)(LF >> 8);
T1CC2L = (char)RF; T1CC2H = (char)(RF >> 8);
T1CC3L = (char)LB; T1CC3H = (char)(LB >> 8);
T1CC4L = (char)RB; T1CC4H = (char)(RB >> 8);
}
#pragma vector = T1_VECTOR
__interrupt void T1_ISR(void) {
EA = 0;
unsigned char flag = T1STAT;
Count_Period++;
if(flag&CH0IF && Count_Period >= Sample_Period) {
Count_Period = 0;
get_DMP();
PID();
Change_duty(PWM_LF,PWM_RF,PWM_LB,PWM_RB); }
T1STAT = ~flag;
EA = 1;
}
cedar_xuesong:
https://github.com/cedar-renjun/RemoteControl-Car
参考我的开源代码,4个通道都可以用