共2条
1/1 1 跳转至页
MSP430F425 MSP430F425串口程序
问
从项目中提取出来的,板子烧不进去,未通过测试。改天有好板子再测试一下。
答 1:
好用的东西没人下啊!!!
答 2:
有没有谁用过?
答 3:
好东西,顶了!!
答 4:
因为重写了putchar()函数,可以直接使用printf()函数从串口输出数据。
参考了IAR的一片应用笔记写的。
#include <IO430x42x.h>
#include <string.h>
#include <stdio.h>
#include "putchar.h"
__interrupt void USART0_TX_sir(void);
__interrupt void USART0_RX_isr(void);
int putchar(int c)
{
if (c == 0x0D) // Convert EOL to CR/LF
{
RS232TXChar(0x0A);
}
if (c == '\n') // Convert EOL to CR/LF
{
RS232TXChar(0x0D);
}
RS232TXChar(c);
return c;
}
#define TX_INT_DISABLE IE1 &= ~UTXIE0
#define TX_INT_ENABLE IE1 |= UTXIE0
#define RX_INT_DISABLE IE1 &= ~URXIE0
#define RX_INT_ENABLE IE1 |= URXIE0
unsigned char ucRXBuffer[RXBUFSIZE]; // receive buffer
// receive buffer indexes :
unsigned char ucRXReadIndex =0, ucRXWriteIndex =0;
// count of received bytes:
unsigned int ucRXCharCount =0;
unsigned char ucTXBuffer[TXBUFSIZE]; // transmit buffer
// transmit buffer indexes:
unsigned char ucTXReadIndex =0, ucTXWriteIndex =0;
// not yet transmitted bytes:
unsigned char ucTXCharCount =0;
#define BUFFER_EMPTY 1 // 1: nothing to send
unsigned char bTXBufferEmpty; // flag for synchronization
void RS232Init (void)
{
UCTL0 = CHAR; // 8-bit character
UTCTL0 = SSEL0; // UCLK = ACLK
// 32768Hz/2400
//UBR00 = 0x0D;
//UBR10 = 0x00; //
//UMCTL0 = 0x6D;
// 32768Hz/9600
UBR00 = 0x03;
UBR10 = 0x00; //
UMCTL0 = 0x4A;
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
IE1 |= URXIE0 + UTXIE0; // Enable USART0 RX interrupt
P2SEL |= BIT4 + BIT5; // P2.4,5 = USART0 TXD/RXD
P2DIR |= BIT4; // P2.4 output direction
reset_ucRXWriteIndex();
}
//////////////////////////////////////////////////////////////
// Name: RS232TXChar
// Description: stores one char in TX buffer. If it's the first one,
// send it immediately. Rest is sent by TXInterrupt automatically
// Parameter: char cByte (to store in buffer)
// Returns: -
//////////////////////////////////////////////////////////////
void RS232TXChar (char cByte)
{
ucTXBuffer[ucTXWriteIndex++] = cByte; // load byte to buffer and inc index
ucTXWriteIndex &= TXBUFSIZE-1; // adjust index to borders of buffer
TX_INT_DISABLE; // disable transmit interrupt (in IE2)
ucTXCharCount++; // new char, inc count
TX_INT_ENABLE; // enable interrupt (in IE2)
if (bTXBufferEmpty && ucTXCharCount) // buffer had been empty
{
//P2DIR |= BIT3;
//P2OUT |= BIT3;
//delay(1000);
bTXBufferEmpty = !BUFFER_EMPTY; // reset empty flag
TXBUF0 = ucTXBuffer[ucTXReadIndex++]; // load tx register, inc index
ucTXReadIndex &= TXBUFSIZE-1; // adjust index
ucTXCharCount--; // char sent, dec count
}
}
//////////////////////////////////////////////////////////////
// Name: TXInterrupt
// Description: Transmit interrupt service routine called by transmit register
// empty
// Parameter: -
// Returns: -
//////////////////////////////////////////////////////////////
#pragma vector=USART0TX_VECTOR
__interrupt void USART0_TX_sir(void)
{
//_EINT();
if (ucTXCharCount)
{ // send if chars are in buffer
TXBUF0 = ucTXBuffer[ucTXReadIndex++]; // load tx register, inc index
ucTXReadIndex &= TXBUFSIZE-1; // adjust index
ucTXCharCount--; // char sent, dec count
}
else // buffer empty, nothing to do
{
bTXBufferEmpty = BUFFER_EMPTY; // set empty flag
//delay(1000);
P2DIR |= BIT3;
//P2OUT &= ~BIT3;
}
}
//////////////////////////////////////////////////////////////
// Name: RXInterrupt
// Description: Receive interrupt service routine called by receive register
// full
// Parameter: -
// Returns: -
//////////////////////////////////////////////////////////////
#pragma vector=USART0RX_VECTOR
__interrupt void USART0_RX_isr(void)
{
ucRXBuffer[ucRXWriteIndex++] = RXBUF0; // store received byte and
// inc receive index
ucRXWriteIndex &= RXBUFSIZE-1; // reset index
ucRXCharCount++; // received, inc count
//LPM3_EXIT;
}
reset_ucRXWriteIndex(void)
{
ucRXWriteIndex = 0;
}
//////////////////////////////////////////////////////////////
// Name: ucRS232RXBufferCount
// Description: How many chars are stored in RX buffer ?
// if main routine wants to read chars, it has
// to check first if ucRS232RXBufferCount() returns !=0
// Parameter: -
// Returns: number of chars in receive buffer
//////////////////////////////////////////////////////////////
unsigned char RS232RXBufferCount (void)
{
return (ucRXCharCount);
}
//////////////////////////////////////////////////////////////
// Name: cRS232GetChar
// Description: Get one char from RX buffer. Multiple calls will
// return all chars.
// Parameter: -
// Returns: next valid char in receive buffer
//////////////////////////////////////////////////////////////
char RS232GetChar (void)
{
char Byte;
if (ucRXCharCount)
{ // char still available
Byte = ucRXBuffer[ucRXReadIndex++]; // get byte from buffer
ucRXReadIndex &= RXBUFSIZE-1; // adjust index
RX_INT_DISABLE; // disable rx interrupt (IE2)
ucRXCharCount--; // one char read, dec count
RX_INT_ENABLE; // done, enable int (IE2)
return (Byte);
}
else
return (0); // if there is no new char
}
//////////////////////////////////////////////////////////////
// Name: send_bytes
// Description: 发送特定长度的字符到发送缓冲区
// return 实际发送的字节数.
// Parameter: char *bytes,发送字符串的首地址。int len,发送字符长度
//////////////////////////////////////////////////////////////
int rs232_send_bytes(char *bytes,int len)
{
unsigned int i;
for(i = 0;i < len;i++)
{
putchar(bytes[i]);
}
return(i);
}
int rs232_send_string(char *string)
{
unsigned int i,str_len;
str_len = strlen(string);
for(i = 0;i < str_len;i++)
{
putchar(string[i]);
}
putchar(0x0D);
return(i);
}
unsigned char *getRxBuffer(void)
{
return ucRXBuffer;
}
参考了IAR的一片应用笔记写的。
#include <IO430x42x.h>
#include <string.h>
#include <stdio.h>
#include "putchar.h"
__interrupt void USART0_TX_sir(void);
__interrupt void USART0_RX_isr(void);
int putchar(int c)
{
if (c == 0x0D) // Convert EOL to CR/LF
{
RS232TXChar(0x0A);
}
if (c == '\n') // Convert EOL to CR/LF
{
RS232TXChar(0x0D);
}
RS232TXChar(c);
return c;
}
#define TX_INT_DISABLE IE1 &= ~UTXIE0
#define TX_INT_ENABLE IE1 |= UTXIE0
#define RX_INT_DISABLE IE1 &= ~URXIE0
#define RX_INT_ENABLE IE1 |= URXIE0
unsigned char ucRXBuffer[RXBUFSIZE]; // receive buffer
// receive buffer indexes :
unsigned char ucRXReadIndex =0, ucRXWriteIndex =0;
// count of received bytes:
unsigned int ucRXCharCount =0;
unsigned char ucTXBuffer[TXBUFSIZE]; // transmit buffer
// transmit buffer indexes:
unsigned char ucTXReadIndex =0, ucTXWriteIndex =0;
// not yet transmitted bytes:
unsigned char ucTXCharCount =0;
#define BUFFER_EMPTY 1 // 1: nothing to send
unsigned char bTXBufferEmpty; // flag for synchronization
void RS232Init (void)
{
UCTL0 = CHAR; // 8-bit character
UTCTL0 = SSEL0; // UCLK = ACLK
// 32768Hz/2400
//UBR00 = 0x0D;
//UBR10 = 0x00; //
//UMCTL0 = 0x6D;
// 32768Hz/9600
UBR00 = 0x03;
UBR10 = 0x00; //
UMCTL0 = 0x4A;
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
IE1 |= URXIE0 + UTXIE0; // Enable USART0 RX interrupt
P2SEL |= BIT4 + BIT5; // P2.4,5 = USART0 TXD/RXD
P2DIR |= BIT4; // P2.4 output direction
reset_ucRXWriteIndex();
}
//////////////////////////////////////////////////////////////
// Name: RS232TXChar
// Description: stores one char in TX buffer. If it's the first one,
// send it immediately. Rest is sent by TXInterrupt automatically
// Parameter: char cByte (to store in buffer)
// Returns: -
//////////////////////////////////////////////////////////////
void RS232TXChar (char cByte)
{
ucTXBuffer[ucTXWriteIndex++] = cByte; // load byte to buffer and inc index
ucTXWriteIndex &= TXBUFSIZE-1; // adjust index to borders of buffer
TX_INT_DISABLE; // disable transmit interrupt (in IE2)
ucTXCharCount++; // new char, inc count
TX_INT_ENABLE; // enable interrupt (in IE2)
if (bTXBufferEmpty && ucTXCharCount) // buffer had been empty
{
//P2DIR |= BIT3;
//P2OUT |= BIT3;
//delay(1000);
bTXBufferEmpty = !BUFFER_EMPTY; // reset empty flag
TXBUF0 = ucTXBuffer[ucTXReadIndex++]; // load tx register, inc index
ucTXReadIndex &= TXBUFSIZE-1; // adjust index
ucTXCharCount--; // char sent, dec count
}
}
//////////////////////////////////////////////////////////////
// Name: TXInterrupt
// Description: Transmit interrupt service routine called by transmit register
// empty
// Parameter: -
// Returns: -
//////////////////////////////////////////////////////////////
#pragma vector=USART0TX_VECTOR
__interrupt void USART0_TX_sir(void)
{
//_EINT();
if (ucTXCharCount)
{ // send if chars are in buffer
TXBUF0 = ucTXBuffer[ucTXReadIndex++]; // load tx register, inc index
ucTXReadIndex &= TXBUFSIZE-1; // adjust index
ucTXCharCount--; // char sent, dec count
}
else // buffer empty, nothing to do
{
bTXBufferEmpty = BUFFER_EMPTY; // set empty flag
//delay(1000);
P2DIR |= BIT3;
//P2OUT &= ~BIT3;
}
}
//////////////////////////////////////////////////////////////
// Name: RXInterrupt
// Description: Receive interrupt service routine called by receive register
// full
// Parameter: -
// Returns: -
//////////////////////////////////////////////////////////////
#pragma vector=USART0RX_VECTOR
__interrupt void USART0_RX_isr(void)
{
ucRXBuffer[ucRXWriteIndex++] = RXBUF0; // store received byte and
// inc receive index
ucRXWriteIndex &= RXBUFSIZE-1; // reset index
ucRXCharCount++; // received, inc count
//LPM3_EXIT;
}
reset_ucRXWriteIndex(void)
{
ucRXWriteIndex = 0;
}
//////////////////////////////////////////////////////////////
// Name: ucRS232RXBufferCount
// Description: How many chars are stored in RX buffer ?
// if main routine wants to read chars, it has
// to check first if ucRS232RXBufferCount() returns !=0
// Parameter: -
// Returns: number of chars in receive buffer
//////////////////////////////////////////////////////////////
unsigned char RS232RXBufferCount (void)
{
return (ucRXCharCount);
}
//////////////////////////////////////////////////////////////
// Name: cRS232GetChar
// Description: Get one char from RX buffer. Multiple calls will
// return all chars.
// Parameter: -
// Returns: next valid char in receive buffer
//////////////////////////////////////////////////////////////
char RS232GetChar (void)
{
char Byte;
if (ucRXCharCount)
{ // char still available
Byte = ucRXBuffer[ucRXReadIndex++]; // get byte from buffer
ucRXReadIndex &= RXBUFSIZE-1; // adjust index
RX_INT_DISABLE; // disable rx interrupt (IE2)
ucRXCharCount--; // one char read, dec count
RX_INT_ENABLE; // done, enable int (IE2)
return (Byte);
}
else
return (0); // if there is no new char
}
//////////////////////////////////////////////////////////////
// Name: send_bytes
// Description: 发送特定长度的字符到发送缓冲区
// return 实际发送的字节数.
// Parameter: char *bytes,发送字符串的首地址。int len,发送字符长度
//////////////////////////////////////////////////////////////
int rs232_send_bytes(char *bytes,int len)
{
unsigned int i;
for(i = 0;i < len;i++)
{
putchar(bytes[i]);
}
return(i);
}
int rs232_send_string(char *string)
{
unsigned int i,str_len;
str_len = strlen(string);
for(i = 0;i < str_len;i++)
{
putchar(string[i]);
}
putchar(0x0D);
return(i);
}
unsigned char *getRxBuffer(void)
{
return ucRXBuffer;
}
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |