RS232通信实验
1 通信的基本感念
2 F28335的SCI介绍
3 SCI配置步骤
4 硬件设计
5 软件设计
查询方式实现
#include "uart.h"
void UARTa_Init(Uint32 baud)
{
unsigned char scihbaud=0;
unsigned char scilbaud=0;
Uint16 scibaud=0;
scibaud=37500000/(8*baud)-1;
scihbaud=scibaud>>8;
scilbaud=scibaud&0xff;
EALLOW;
SysCtrlRegs.PCLKCR0.bit.SCIAENCLK = 1; // SCI-A
EDIS;
InitSciaGpio();
//Initalize the SCI FIFO
SciaRegs.SCIFFTX.all=0xE040;
SciaRegs.SCIFFRX.all=0x204f;
SciaRegs.SCIFFCT.all=0x0;
// Note: Clocks were turned on to the SCIA peripheral
// in the InitSysCtrl() function
SciaRegs.SCICCR.all =0x0007; // 1 stop bit, No loopback
// No parity,8 char bits,
// async mode, idle-line protocol
SciaRegs.SCICTL1.all =0x0003; // enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
SciaRegs.SCICTL2.all =0x0003;
SciaRegs.SCICTL2.bit.TXINTENA =1;
SciaRegs.SCICTL2.bit.RXBKINTENA =1;
SciaRegs.SCIHBAUD =scihbaud; // 9600 baud @LSPCLK = 37.5MHz.
SciaRegs.SCILBAUD =scilbaud;
// SciaRegs.SCICCR.bit.LOOPBKENA =1; // Enable loop back
SciaRegs.SCICTL1.all =0x0023; // Relinquish SCI from Reset
}
// Transmit a character from the SCI'
void UARTa_SendByte(int a)
{
while (SciaRegs.SCIFFTX.bit.TXFFST != 0);
SciaRegs.SCITXBUF=a;
}
void UARTa_SendString(char * msg)
{
int i=0;
while(msg[i] != '\0')
{
UARTa_SendByte(msg[i]);
i++;
}
}
#ifndef UART_H_
#define UART_H_
#include "DSP2833x_Device.h" // DSP2833x 头文件
#include "DSP2833x_Examples.h" // DSP2833x 例子相关头文件
void UARTa_Init(Uint32 baud);
void UARTa_SendByte(int a);
void UARTa_SendString(char * msg);
#endif /* UART_H_ */
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#include "leds.h"
#include "time.h"
#include "uart.h"
/*******************************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void main()
{
int i=0;
char *msg;
Uint16 ReceivedChar=0;
InitSysCtrl();
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
LED_Init();
TIM0_Init(150,200000);//200ms
UARTa_Init(4800);
msg = "Hello World!\r\n";
UARTa_SendString(msg);
msg = "You will enter a character, and the DSP will echo it back!\r\n";
UARTa_SendString(msg);
while(1)
{
msg = "\r\nEnter a character: ";
UARTa_SendString(msg);
// Wait for inc character
while(SciaRegs.SCIFFRX.bit.RXFFST !=1);// wait for XRDY =1 for empty state
// Get character
ReceivedChar = SciaRegs.SCIRXBUF.all;
// Echo character back
msg = " You sent: ";
UARTa_SendString(msg);
UARTa_SendByte(ReceivedChar);
DELAY_US(1000);
}
}
中断方式实现
void main()
{
int i;
InitSysCtrl();
InitGpio();
InitSciGpio();
InitPieCtrl();
DINT;
IER = 0x0000;// 禁用 CPU 中断和清除所有 CPU 中断标志位:
IFR = 0x0000;
InitPieVectTable();
SCI_Init(9600);
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.SCIRXINTB = &ScibRxFifoIsr;
PieVectTable.SCITXINTB = &ScibTxFifoIsr;
EDIS; // This is needed to disable write to EALLOW protected registers
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER9.bit.INTx3=1; // PIE Group 9, INT3
PieCtrlRegs.PIEIER9.bit.INTx4=1;
IER |= M_INT9; // Enable CPU INT9--- SCIA/B INT8--- SCIC/D
IER |= M_INT1; //定时器0//或运算不干扰其他中断
EINT;//使能全局中断
ERTM;//使能全局实时中断
for(i = 0; i<12; i++) //缓存初始化
{
buffer[i] = '0';
}
while(1)
{
}
}
void SCI_Init(Uint32 baud)
{
unsigned char sci_h_baud = 0;
unsigned char sci_l_baud = 0;
Uint16 sci_baud = 0;
sci_baud = 37500000 / (8 * baud) - 1;//baud是输入的波特率
sci_h_baud = sci_baud >> 8;
sci_l_baud = sci_baud & 0xff;//按位与运算,均为1才能为1,0000 0000 1111 1111
EALLOW;
SysCtrlRegs.PCLKCR0.bit.SCIBENCLK = 1;//SCIB使能
EDIS;
ScibRegs.SCICCR.all = 0x0007;
ScibRegs.SCIHBAUD = sci_h_baud;
ScibRegs.SCILBAUD = sci_l_baud;
//功能初始化
ScibRegs.SCICTL1.all = 0x0003; // enable TX, RX, internal SCICLK,
// Disable RX ERR, SLEEP, TXWAKE
ScibRegs.SCICTL2.all = 0x0003;
//FIFO初始化
ScibRegs.SCIFFTX.all = 0xC028;
ScibRegs.SCIFFRX.all = 0x0028;
ScibRegs.SCIFFCT.all = 0x0000;
ScibRegs.SCICTL1.all = 0x0023;
ScibRegs.SCIFFTX.bit.TXFIFOXRESET=1; // 重新使能发送FIFO的操作
ScibRegs.SCIFFRX.bit.RXFIFORESET=1; // 重新使能接收FIFO的操作
}
//----------------------------------------------------------
interrupt void ScibTxFifoIsr(void) //fifo发送中断服务子程序
{
Uint16 i;
for(i=0;i<8; i++)
{
ScibRegs.SCITXBUF=buffer[i]; // Send data 将缓存中的数据发出
}
PieCtrlRegs.PIEACK.all|=PIEACK_GROUP9; // Issue PIE ACK
}
interrupt void ScibRxFifoIsr(void)//fifo接收中断服务子程序
{
Uint16 i;
for(i=0; i<8; i++)
{
buffer[i]=ScibRegs.SCIRXBUF.all; // 将fifo中的数据读到缓存
}
ScibRegs.SCIFFTX.bit.TXFFINTCLR=1; // 很重要 若不清fifo发送中断标志则,不进入发送中断
ScibRegs.SCIFFRX.bit.RXFFINTCLR=1; // 清接收中断标志
PieCtrlRegs.PIEACK.all|=PIEACK_GROUP9; // Issue PIE ack
}