1个回答
展开全部
我以前做过的串口试验
MSP430F149程序
----------------------------------------------------------------------------------------------------------------------
#include <msp430x14x.h>
#include "uart0.h"
#include "uart0.c"
#define uchar unsigned char
#define uint unsigned int
uchar data=8;
uchar mm;
//大约一秒延时
void delay()
{
uint m=40000,n=200; //5ms
while(n--)
{
for(;m>0;m--);
}
}
void main(void)
{
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0XFF;
P5DIR = 0XFF;P5OUT = 0XFF;
P6DIR = 0XFF;P6OUT = 0XFF;
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
P6DIR |= BIT2;P6OUT |= BIT2; //关闭电平转换
InitUART(); //串口初始化
IE1 |= URXIE0; // 使能USART0的接收中断
while(1)
{
_EINT(); //打开全局中断
LPM1; //进入LPM1模式
while (!(IFG1 & UTXIFG0)); //等待以前的字符发送完毕
TXBUF0 = RXBUF0; //将收到的字符发送出去
mm = RXBUF0;
P2OUT = mm;
delay();
delay();
TXBUF0 = RXBUF0; //将收到的字符发送出去
}
}
/*******************************************
函数名称:UART0_RXISR
功 能:UART0的接收中断服务函数,在这里唤醒
CPU,使它退出低功耗模式
参 数:无
返回值 :无
********************************************/
#pragma vector = UART0RX_VECTOR
__interrupt void UART0_RXISR(void)
{
LPM1_EXIT; //退出低功耗模式
}
----------------------------------------------------------------------------------------------------------------------
//串口源文件(.c)
#include <msp430.h>
typedef unsigned char uchar;
/*******************************************
函数名称:InitUART
功 能:初始化UART端口
参 数:无
返回值 :无
********************************************/
void InitUART(void)
{
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
ME1 |= URXE0 + UTXE0; // Enable USART0 T/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00 = 0x03; // 32k/9600 - 3.41
UBR10 = 0x00; //
UMCTL0 = 0x4A; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
}
/*******************************************
函数名称:Send1Char
功 能:向PC机发送一个字符
参 数:sendchar--要发送的字符
返回值 :无
********************************************/
void Send1Char(uchar sendchar)
{
while (!(IFG1 & UTXIFG0)); //等待发送寄存器为空
TXBUF0 = sendchar;
}
/*******************************************
函数名称:PutSting
功 能:向PC机发送字符串
参 数:ptr--指向发送字符串的指针
返回值 :无
********************************************/
void PutString(uchar *ptr)
{
while(*ptr != '\0')
{
Send1Char(*ptr++); // 发送数据
}
while (!(IFG1 & UTXIFG0));
TXBUF0 = '\n'; //发送换行指令
}
/*******************************************
函数名称:Get1Char
功 能:接收一个来自PC机的字符
参 数:无
返回值 :接收到的字符
********************************************/
unsigned char Get1Char(void)
{
while (!(IFG1 & URXIFG0)); //等待接收到字符 中断后IFG1=0X82
return RXBUF0;
}
----------------------------------------------------------------------------------------------------------------------
//串口头文件(.h)
void InitUART(void);
void Send1Char(unsigned char sendchar);
void PutString(unsigned char *ptr);
void PutString0(unsigned char *ptr);
unsigned char Get1Char(void);
-=-=-===-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
51串口程序
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 18432000L //System frequency
#define BAUD 9600 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
sbit bit9 = P2^2; //P2.2 show UART data bit9
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
SendString("STC89-90xx\r\nUart Test !\r\n");
while(1);
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
P0 = SBUF; //P0 show UART data
bit9 = RB8; //P2.2 show parity bit
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
MSP430F149程序
----------------------------------------------------------------------------------------------------------------------
#include <msp430x14x.h>
#include "uart0.h"
#include "uart0.c"
#define uchar unsigned char
#define uint unsigned int
uchar data=8;
uchar mm;
//大约一秒延时
void delay()
{
uint m=40000,n=200; //5ms
while(n--)
{
for(;m>0;m--);
}
}
void main(void)
{
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0XFF;
P5DIR = 0XFF;P5OUT = 0XFF;
P6DIR = 0XFF;P6OUT = 0XFF;
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
P6DIR |= BIT2;P6OUT |= BIT2; //关闭电平转换
InitUART(); //串口初始化
IE1 |= URXIE0; // 使能USART0的接收中断
while(1)
{
_EINT(); //打开全局中断
LPM1; //进入LPM1模式
while (!(IFG1 & UTXIFG0)); //等待以前的字符发送完毕
TXBUF0 = RXBUF0; //将收到的字符发送出去
mm = RXBUF0;
P2OUT = mm;
delay();
delay();
TXBUF0 = RXBUF0; //将收到的字符发送出去
}
}
/*******************************************
函数名称:UART0_RXISR
功 能:UART0的接收中断服务函数,在这里唤醒
CPU,使它退出低功耗模式
参 数:无
返回值 :无
********************************************/
#pragma vector = UART0RX_VECTOR
__interrupt void UART0_RXISR(void)
{
LPM1_EXIT; //退出低功耗模式
}
----------------------------------------------------------------------------------------------------------------------
//串口源文件(.c)
#include <msp430.h>
typedef unsigned char uchar;
/*******************************************
函数名称:InitUART
功 能:初始化UART端口
参 数:无
返回值 :无
********************************************/
void InitUART(void)
{
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
ME1 |= URXE0 + UTXE0; // Enable USART0 T/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL0; // UCLK = ACLK
UBR00 = 0x03; // 32k/9600 - 3.41
UBR10 = 0x00; //
UMCTL0 = 0x4A; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
}
/*******************************************
函数名称:Send1Char
功 能:向PC机发送一个字符
参 数:sendchar--要发送的字符
返回值 :无
********************************************/
void Send1Char(uchar sendchar)
{
while (!(IFG1 & UTXIFG0)); //等待发送寄存器为空
TXBUF0 = sendchar;
}
/*******************************************
函数名称:PutSting
功 能:向PC机发送字符串
参 数:ptr--指向发送字符串的指针
返回值 :无
********************************************/
void PutString(uchar *ptr)
{
while(*ptr != '\0')
{
Send1Char(*ptr++); // 发送数据
}
while (!(IFG1 & UTXIFG0));
TXBUF0 = '\n'; //发送换行指令
}
/*******************************************
函数名称:Get1Char
功 能:接收一个来自PC机的字符
参 数:无
返回值 :接收到的字符
********************************************/
unsigned char Get1Char(void)
{
while (!(IFG1 & URXIFG0)); //等待接收到字符 中断后IFG1=0X82
return RXBUF0;
}
----------------------------------------------------------------------------------------------------------------------
//串口头文件(.h)
void InitUART(void);
void Send1Char(unsigned char sendchar);
void PutString(unsigned char *ptr);
void PutString0(unsigned char *ptr);
unsigned char Get1Char(void);
-=-=-===-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
51串口程序
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 18432000L //System frequency
#define BAUD 9600 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
sbit bit9 = P2^2; //P2.2 show UART data bit9
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
SendString("STC89-90xx\r\nUart Test !\r\n");
while(1);
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
P0 = SBUF; //P0 show UART data
bit9 = RB8; //P2.2 show parity bit
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询