//-----------------------------------------------------------------
// 串口初始化
//-----------------------------------------------------------------
void USCI_A0_graceInit(void)
{
/* USER CODE START (section: USCI_A0_graceInit_prologue) */
/* User initialization code */
/* USER CODE END (section: USCI_A0_graceInit_prologue) */
/* Disable USCI */
UCA0CTL1 |= UCSWRST;
/*
* Control Register 1
*
* UCSSEL_2 -- SMCLK
* ~UCRXEIE -- Erroneous characters rejected and UCAxRXIFG is not set
* ~UCBRKIE -- Received break characters do not set UCAxRXIFG
* ~UCDORM -- Not dormant. All received characters will set UCAxRXIFG
* ~UCTXADDR -- Next frame transmitted is data
* ~UCTXBRK -- Next frame transmitted is not a break
* UCSWRST -- Enabled. USCI logic held in reset state
*
* Note: ~ indicates that has value zero
*/
UCA0CTL1 = UCSSEL_2 | UCSWRST;
/*
* Modulation Control Register
*
* UCBRF_0 -- First stage 0
* UCBRS_1 -- Second stage 1
* ~UCOS16 -- Disabled
*
* Note: ~UCOS16 indicates that UCOS16 has value zero
*/
UCA0MCTL = UCBRF_0 | UCBRS_1;
/* Baud rate control register 0 */
UCA0BR0 = 104;
/* Enable USCI */
UCA0CTL1 &= ~UCSWRST;
/* USER CODE START (section: USCI_A0_graceInit_epilogue) */
/* User code */
/* USER CODE END (section: USCI_A0_graceInit_epilogue) */
}
//-----------------------------------------------------------------
// 串口发送一个字节
//-----------------------------------------------------------------
void com_send_command(char Onebyte)
{
UCA0TXBUF = Onebyte;
__delay_cycles(2000);//发送汉字参数2000,发送字符参数为1000
}
//------------------------------------------------------------------
// 串口接收中断程序
//------------------------------------------------------------------
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR_HOOK(void)
{
uint8_t temp;
uint8_t temp1;
// USER CODE START (section: USCI0RX_ISR_HOOK)
// replace this comment with your code
// USER CODE END (section: USCI0RX_ISR_HOOK)
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
//判断串口的发送是否正在工作,
//因为要使用发送功能,所以得判断发送是否忙碌,
//只有在不忙碌的时候才可以启用发送。
//这一句就是判断发送是否在工作,如果是在工作,那么一直等待到不工作为止。
temp1 = UCA0RXBUF; //读取数据 // TX -> RXed character
temp = pint_write + 1; /* 判断是否可以写入 */
if (temp == MAX_RINTL)
{
temp=0;
}
if (temp != pint_read)
{
pint_buf[pint_write] = temp1; /* 读取数据 */
pint_write = temp;
}
}
void buff_init(void)
{
uint8_t loop;
loop = UCA0RXBUF; /* 清串口缓冲区 */
for (loop=0; loop
{
pint_buf[loop] = 0;
}
}
Comint.h
#ifndef __COMINT_H__
#define __COMINT_H__
#include
#include
#define INT8U unsigned char
#define INT16U unsigned int
#define uint8_t unsigned char
#define u_short unsigned int
#define uint16_t unsigned int
#define ZU 0x01 /*组地址*///通讯地址修改这两项
#define ZHAN 0x02 /*站地址*///通讯地址修改这两项