回复 楼主 yidaoyizu 的帖子
自己写的程序,不太好用,只能输入两位数的占空比。误差很大,最大的时候能差5%呢。什么原因??
我也在找这个程序 你找到了嘛?求交流 QQ479358469
#include
volatile unsigned char temp=10;
void Timer_A1_2_init() //TA1.2输出PWM
{
TA1CTL|= TASSEL_1+MC_1;//ACLK,增计数
TA1CCTL2=OUTMOD_7;//输出模式为复位/置位,注意CCTL2要写为TA1CCTL2
TA1CCR0=655;//时钟频率为32768HZ,波形32768/CCR0=199HZ
TA1CCR2=30;//占空比CCR2/CCR0=1/4,注意CCR2要写成TA1CCR2 TA1.2由P2.4 P2.5输出
}
void IO_init()
{
P2SEL|=BIT5;
P2DIR|=BIT4+BIT5;//P2.4 P2.5输出 TA1.2 OUT2
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
IO_init();
Timer_A1_2_init(); //TA1.2输出PWM
P1OUT = 0x00; // P1.0/6 setup for LED output
P1DIR = BIT0 + BIT6;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2;
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32kHz/9600 = 3.41
UCA0BR1 = 0x00;
UCA0MCTL = UCBRS1 + UCBRS0; // Modulation UCBRSx = 3
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; //+ UCA0TXIE; // Enable USCI_A0 TX/RX interrupt
__bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/ interrupts enabled
while(1)
{
}
}
// USCI A0/B0 Transmit ISR
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
unsigned char TxByte=0;
if (P1IN & BIT3)
TxByte |= BIT6;
if (P1IN & BIT4)
TxByte |= BIT0;
UCA0TXBUF = TxByte; // Read, justify, and transmit
}
// USCI A0/B0 Receive ISR
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
temp = UCA0RXBUF; // Display RX'ed charater
TA1CCR2=temp;
//UCA0TXBUF = P1OUT;
}
对了,我输出50HZ。用来调舵机的。串口调占空比。