#include <msp430.h>
#define UART_TXD 0x02 // TXD on P1.1 (Timer0_A.OUT0)
#define UART_TBIT (1000000 / 9600) // 9600 Baud, SMCLK = 1MHz
// Globals for transmit UART communication
unsigned int txData; // UART internal variable for TX
unsigned int capture1,capture2;
unsigned period;
void TimerA_UART_tx(unsigned char byte); // Function prototypes
void TimerA_UART_print(char *string);
void DelayNms(int n);
void Delay1ms(void);
void UART0_send_byte(unsigned char data) //发送一位
{
int j;
// for( j=10;j>0;j--);
UCA0TXBUF=data;
}
void UART0()
{
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 |= BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void BCSplus_init(void)
{
BCSCTL2 = SELM_0 + DIVM_0 + DIVS_3;//SELM_0选择DCO,DIVM_0,MCLK不分频,DIVS_3,SMCLK 8分频
if (CALBC1_8MHZ != 0xFF) {
DCOCTL = 0x00;
BCSCTL1 = CALBC1_8MHZ; /* 设置 DCO to 8MHz */
DCOCTL = CALDCO_8MHZ;
}
BCSCTL1 |= XT2OFF + DIVA_0; //关闭XT2,ACLK不分频
BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1;//XT2S_0:XT2 0.4~1Mhz,LFXT1S_2:VCLOCK做ACLK,XCAP_1:6pf电容
}
//P1口的初始化程序
void gpio_init(void)
{
P1SEL |=BIT0;
P1SEL2 |=BIT0; //P1.3做CCI2A信号,定时器0的捕获输入
}
//定时器的初始化程序
void timer0_init(void)
{
TA0CTL |=TASSEL_2+ID_0+MC_2;//选择SMCLK为定时器时钟,不分频,连续计数方式。
TA0CCR2 |=CM_1+CCIS_0+SCS+CAP+CCIE;//上升沿捕获,CCI2A做捕获输入,同步方式,捕获方式,允许捕获中断。
}
void ini_usart() // rs232接受
{
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
}
int send(char c) //232发送一个字符
{
if(c<1||c>126) c='0';
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = c; // TX -> RXed character
// for(i=0;i<500;i++);
return(c);
}
void UART0_send_str(char *s) //发送字符串
{
while(*s != '\0')
{
send(*s++);
}
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
int j;
P3DIR |=BIT0;//输出模式
// gpio_init();
// timer0_init();
// BCSplus_init();
ini_usart();
CCTL0 = CCIE; // 时钟中断
CCR0 = 12700;//15624;//15625; // 中断周期1秒
TACTL = TASSEL0+TACLR+MC0; // 时钟为ACLK:MC1:连续增计数模式
_EINT();//开全局中断
while(1)
{
//for(j=10000;j>0;j--);
UART0_send_str("hello world!\n");
}
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
UART0_send_str("JJJJ!\n");
}
void TimerA_UART_tx(unsigned char byte) // Outputs one byte using the Timer_A UART
{
while (TACCTL0 & CCIE); // Ensure last char got TX'd
TA0CCR0 = TAR; // Current state of TA counter
TA0CCR0 += UART_TBIT; // One bit time till first bit
txData = byte; // Load transmit data, e.g. 'A'=01000001
txData |= 0x100; // Add mark stop bit, e.g. 101000001
txData <<= 1; // Add space start bit, e.g. 1010000010
TA0CCTL0 = OUTMOD0 + CCIE; // Set TXD on, enable counter interrupt
}
void TimerA_UART_print(char *string)
{ // Prints a string using the Timer_A UART
while (*string)
TimerA_UART_tx(*string++);
}
/*******************************************
函数名称:Delay1ms
功 能:延时约1ms的时间
参 数:无
返回值 :无
********************************************/
void Delay1ms(void)
{
char i;
for(i = 190;i > 0;i--) _NOP();
}
/*******************************************
函数名称:DelayNms
功 能:延时N个1ms的时间
参 数:n--延时长度
返回值 :无
********************************************/
void DelayNms(int n)
{
int i;
for(i = n;i > 0;i--) Delay1ms();
}