不知道“中断”怎么用。所以在PWM子程序里直接用的例程。最好有大神讲解下这里面的中断。
这个程序还没有实际运行,刚刚写好,编译没问题。不知道能不能实现要的功能:AD采样(光敏电阻),比较后产生不同占空比的PWM(调光)。谢谢
#include <msp430f5529.h>
int dig,i;
void ad()
{
P6SEL |=0x01;
REFCTL0 &= ~REFMSTR; // Reset REFMSTR to hand over control to
// ADC12_A ref control registers
ADC12CTL0 = ADC12ON+ADC12SHT02+ADC12REFON+ADC12REF2_5V;
// Turn on ADC12, Sampling time
// On Reference Generator and set to
// 2.5V
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12MCTL0 = ADC12SREF_1; // Vr+=Vref+ and Vr-=AVss
for ( i=0; i<0x30; i++); // Delay for reference start-up
ADC12CTL0 |= ADC12ENC; // Enable conversions
while (1)
{
ADC12CTL0 |= ADC12SC; // Start conversion
while (!(ADC12IFG & BIT0));
dig=ADC12MEM0;
}
}
//PWM
void Port_Mapping()
{
__disable_interrupt(); // Disable Interrupts before altering Port Mapping registers
PMAPPWD = 0x02D52; // Enable Write-access to modify port mapping registers
#ifdef PORT_MAP_RECFG
PMAPCTL = PMAPRECFG; // Allow reconfiguration during runtime
#endif
P4MAP0 = PM_TB0CCR1A;
PMAPPWD = 0; // Disable Write-Access to modify port mapping registers
#ifdef PORT_MAP_EINT
__enable_interrupt(); // Re-enable all interrupts
#endif
}
/*
* main.c
*/
int main(void)
{
while(1)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
ad();
Port_Mapping();
P4DIR |= 0x01; // P4.0 output
P4SEL |= 0x01; // P4.0 Port Map functions
TB0CCR0 = 128; // PWM Period/2 不明白
TB0CCTL1 = OUTMOD_6; // CCR1 toggle/set
if (dig>0x00&&dig<0x01) //0x01 0x02 0x03是假设值,需要测试再确定
TB0CCR1 = 48;
else if(dig>0x01&&dig<0x02)
TB0CCR1 = 96;
else if(dig>0x03)
TB0CCR1 = 128;
else TB0CCR1 = 0;
}
}
本文来自论坛,点击查看完整帖子内容。