由定时器产生pwm方波输出致P1.4,再由S1控制PWM波的占空比
s1---p1.7TA0CCR3----P1.4
(产生pwm波和控制占空比的部分没问题,就是无法用按键中断控制,头文件也没问题)
void GPIO_init();
unsigned int freq=10;
void main()
{
WDTCTL = WDTPW + WDTHOLD;
SPWM_1Way_Init();
SPWM_1Way_Set_Freq(10);
GPIO_init();
_EINT();//开总中断
}
void GPIO_init()
{
P1REN |=BIT7;
P1OUT |=BIT7;
P1IE |=BIT7;
P1IES|=BIT7;
P1IFG&=~BIT7;
}
void SPWM_1Way_Init(void)
{
spwm_i=0;
TA0CCR0 =198;//载波126.26KHZ
TA0CCR3 =Lab1[spwm_i];
TA0CCTL3 =OUTMOD_7;
TA0CTL =TASSEL_2+MC_1+TACLR;//选择时钟为SMCLK,UP模式
TA1CCR0 =2499;//25000000/(100*100)=2500:100Hz,100个点:10KHZ(时间不太准我做了些补偿)
TA1CTL =TASSEL_2+MC_1+TACLR;//选择时钟为SMCLK,UP模式
}
void SPWM_1Way_Set_Freq(unsigned int freq)
{
unsigned long freq_num;
freq_num=250000/(freq)-1;
TA1CCR0 =freq_num;
}
#pragma vector=PORT1_VECTOR
__interrupt void PORT17ISR(void)
{
TA0CCR3 =Lab1[spwm_i++];
if(spwm_i==100)spwm_i=0;
TA1IV=0;
}
此帖出自
小平头技术问答
一周热门 更多>