msp430f5529捕获加串口,可以供大家学习
单片机源程序如下:
#include "msp430f5529.h"
#define uint unsigned int
#define uchar unsigned char
uint flag,TA_cent,PerVal;
unsigned long int val,cha;
int m[5];
void Delay10ms(unsigned int c) //误差 0us
{
unsigned char a, b;
//--c已经在传递过来的时候已经赋值了,所以在for语句第一句就不用赋值了--//
for (;c>0;c--)
{
for (b=38;b>0;b--)
{
for (a=130;a>0;a--);
}
}
}
void USCI_A0_Init() //USCI_A0的初始化
{
P3SEL |= BIT3+BIT4; // P3.3和P3.4选择UART通信功能
UCA0CTL1 |= UCSWRST; // 复位寄存器设置
UCA0CTL0 = 0x00;
UCA0CTL1 |= UCSSEL_1; // 波特率发生器参考时钟设置为ACLK,ACLK=32768Hz
UCA0BR0 = 0x03; // 波特率设置为9600bps
UCA0BR1 = 0x00;
UCA0MCTL |= UCBRS_3 + UCBRF_0; // 调制器设置
UCA0CTL1 &= ~UCSWRST; // 完成USCI_A0初始化设置
}
void fasongzifu (char Cha) //单个字符发送函数
{
UCA0TXBUF = Cha;
while (!(UCA0IFG&UCTXIFG)); //等待上一个字节发送完毕
}
//void yunsuan()
//{
//int m[5];
//m[0]=val%10000/1000;
//m[1]=val%1000/100;
// m[2]=val%100/10;
//m[3]=val%10;
//m[4]='.';
// USCI_A0_Putchar(m[0]+48);
//USCI_A0_Putchar(m[4]);
//USCI_A0_Putchar(m[1]+48);
//USCI_A0_Putchar(m[2]+48);
//USCI_A0_Putchar(m[3]+48);
//}
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1DIR&=~BIT2; //捕捉P1.2口
P1SEL |= BIT2;
TA0CCTL1 |= CAP + CM_1 + CCIS_0 + SCS + CCIE;//捕获模式,上升和下降都捕获,选择CCI2A,同步,捕获中断开
TA0CTL |= TASSEL_2 + MC_2 + ID_0 + TAIE + TACLR; //SMCLK=1M,连续计数模式
USCI_A0_Init(); //USCI_A0初始化
_EINT();//中断起始位
while(1);
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
switch(TA0IV)
{
case 2:
val = TA0CCR1 - PerVal;
PerVal = TA0CCR1;
flag++;
if(flag >2)
{
flag =0;
val = 1048576 / val; //1048576 :系统稳定后,DCOCLK默认为2.097152MHZ,FLL默认2分频,则MCLK和SMCLK的频率都为1.048576MHZ。(实验三会提到如何计算)
m[0]=val%10000/1000;
m[1]=val%1000/100;
m[2]=val%100/10;
m[3]=val%10;
m[4]=' ';
fasongzifu(m[0]+48);
fasongzifu(m[1]+48);
fasongzifu(m[2]+48);
fasongzifu(m[3]+48);
fasongzifu(m[4]);
Delay10ms(10);
}
TA1CCTL0 &= ~CCIFG;
break;
case 4:
break;
case 10:
TA1CCTL0 &= ~CCIFG;
break;
}
}