还说好好安下心调我的PID 结果又被某人喊调一个占空比为10%的PWM波
这不,直接上代码吧:
But first,还是要先解释一下端口:按键改变PWM的占空比
按键IO口如下:
然后就是用P2.3和P2,4口输出两路PWM信号
#include<msp430x14x.h>
#define uchar unsigned char
#define uint unsigned int
#define keyin (P1IN & 0x0f)
uchar T[]={10,20,40,100,150,200}; //高电平持续
void delay(void)
{
uint tmp;
for(tmp = 12000;tmp > 0;tmp--);
}
void main( void )
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
uchar temp,keyval = 0;
char i=0;
TACTL = TASSEL0 + TACLR; // ACLK, 清除 TAR
CCR0 = 201-1; // PWM周期
CCTL1 = OUTMOD_7;
CCTL2 = OUTMOD_7;
TACTL |= MC0; // Timer_A 增计数模式
P2DIR |= 0x08; // P2.3 输出
P2SEL |= 0x08; // P2.3 TA1
P2DIR |= 0x10; // P2.4 输出
P2SEL |= 0x10; // P2.4 TA2
P2DIR |= 0x24;
P2OUT &= (~BIT5) +(~BIT2); //P2.2与P2.5点亮
while(1)
{
if(keyin != 0x0f) //如果有键被按下
{
delay(); //延时消抖
if(keyin != 0x0f) //再次检测按键状态
{
temp=keyin;
while(keyin != 0x0f); //等待按键被放开
switch(temp) //转换键值
{
case 0x0e:
keyval = 1;break;
case 0x0d:
keyval = 2;break;
default:
keyval = 0;break;
}
if(keyval==1) //按键加
{
P2DIR |= 0x80;
P2OUT^=BIT7; //按键指示
i++;
if(i>5)
i=0;
}
if(keyval==2) //按键减
{
if(i==0)
i=5;
else
i--;
P2DIR |= 0x80;
P2OUT^=BIT7; //按键指示
}
}
}
CCR1 = T; //占空比: T / 200
CCR2 = T; //占空比: T / 200
}
}