STM32串口通讯程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

STM32串口通讯程序
编辑整理:
尊敬的读者朋友们:
这里是精品文档编辑中心,本文档内容是由我和我的同事精心编辑整理后发布的,发布之前我们对文中内容进行仔细校对,但是难免会有疏漏的地方,但是任然希望(STM32串口通讯程序)的内容能够给您的工作和学习带来便利。

同时也真诚的希望收到您的建议和反馈,这将是我们进步的源泉,前进的动力。

本文可编辑可修改,如果觉得对您有帮助请收藏以便随时查阅,最后祝您生活愉快业绩进步,以下为STM32串口通讯程序的全部内容。

将AVR上的队列串口驱动程序修改后,运行在STM32开发板,采用中断方式接收和中断发送,并加入了缓冲收发队列操作。

由于该驱动是用来操作西门子的TC35或MC55等通信模块,所以加入了“等待串口接收完成”函数,该函数需要一个
10ms的定时进行计数累加。

#define SMS_UART0_c
/*
**********************************************
***************************************************************

*
STM32 UART1 driver
*
* File : UART0.c
* By : hjjft
*************************************************************************************************************
*/
/////////////////////////////////////////////////
// 这里将串口1写作0,主要原因是AVR是串口0,为了方便移植,这里仍然称为串口0 // /////////////////////////////////////////////////
static char UART0_RxBuf[UART0_RX_BUFFER_SIZE];
static volatile unsigned char UART0_RxHead;//
static volatile unsigned char UART0_RxTail;
static char UART0_TxBuf[UART0_TX_BUFFER_SIZE];//
static volatile unsigned char UART0_TxHead;
static volatile unsigned char UART0_TxTail;
//---———-—-———-——-—————--————————————-———--—-————----—--———-—-
static volatile unsigned char Frame_counting;
/**********************************************
*********************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location。

* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_USART_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure。

NVIC_IRQChannelSubPriority = 10;
// NVIC_InitStructure。

NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void GPIO_USART_Configuration(void)

GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure。

GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure。

GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA。

10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure。

GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configures the USART1.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART_Configuration(unsigned long baudrate)

USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitqlt;
/* USART1 configuration -—---—---——————————-——————---
—————-——--——-—---—-———-——*/
/* USART1 configured as follow:
— BaudRate = 115200 baud
— Word Length = 8 Bits
— One Stop Bit
— No parity
- Hardware flow control disabled (RTS and CTS signals)
— Receive and transmit enabled
— USART Clock disabled
- USART CPOL: Clock is active low
— USART CPHA: Data is captured on the middle
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin */
USART_ART_BaudRate = baudrate;
USART_InitStructure。

USART_WordLength = USART_WordLength_8b;
USART_InitStructure。

USART_StopBits = USART_StopBits_1;
USART_ART_Parity = USART_Parity_No;
USART_InitStructure。

USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_ART_Mode = USART_Mode_Rx | USART_Mode_Tx; // USART_Init(USART1, &USART_InitStructure);
USART_ART_Clock= USART_Clock_Disable;
USART_ART_CPOL = USART_CPOL_Low;
// USART_ClockInitqlt。

USART_CPHA =
USART_CPHA_2Edge; // USART_ClockInitqlt。

USART_LastBit =
USART_LastBit_Disable;//
USART_ClockInit(USART1,&USART_ClockInitqlt);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE);//

//串口初始化
void UART0_InitUART( unsigned long baudrate )

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,ENABLE);
NVIC_USART_Configuration();
GPIO_USART_Configuration();
USART_Configuration(baudrate);
UART0_RxTail = 0;
UART0_RxHead = 0;
UART0_TxTail = 0;
UART0_TxHead = 0;

//——--—--——--————--——-------—-———--———-——--—-—-——--—
void SIGNAL_Usart0_RX(void) //接收中断
{
u8 data;
u8 tmphead;
data = USART_ReceiveData(USART1); //
Frame_counting = 0;
tmphead = ( UART0_RxHead + 1 ) &
UART0_RX_BUFFER_MASK; // UART0_RxHead =
tmphead; //
if ( tmphead == UART0_RxTail )

//这里加入队列溢出保护

UART0_RxBuf[tmphead] = data;
}
//——----—--—---—-----———--———-—-----———--———--—---——
void SIGNAL_Usart0_TX(void) //发送中断

u8 tmptail;
if ( UART0_TxHead != UART0_TxTail )
{
tmptail = ( UART0_TxTail + 1 )&
UART0_TX_BUFFER_MASK; UART0_TxTail = tmptail;
USART_SendData(USART1, UART0_TxBuf[tmptail]);

else

USART_ITConfig(USART1,USART_IT_TXE,DISABLE);//
}

//从接收队列读取一个字符
unsigned char UART0_ReceiveByte( void )
{
unsigned char tmptail;
while ( UART0_RxHead == UART0_RxTail ); //
tmptail = ( UART0_RxTail + 1 ) & UART0_RX_BUFFER_MASK;
UART0_RxTail = tmptail;
return UART0_RxBuf[tmptail];

//将一个字节放入发送队列
void UART0_TransmitByte( char data )
{
unsigned char tmphead;
tmphead = ( UART0_TxHead + 1 ) & UART0_TX_BUFFER_MASK;
while ( tmphead ==
UART0_TxTail );
UART0_TxBuf[tmphead] = data;
UART0_TxHead = tmphead;
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//

//发送一个字符串
void UART0_Transmitstr( char *str) //

// unsigned int i=”0";
while(*str)

UART0_TransmitByte(*str++);
}

//清空接收缓冲区
void UART0_ClearspatialReceiveBuffer(void)
{
unsigned char x;
x = 0;
UART0_RxTail = x;
UART0_RxHead = x;

//等待接收完成
void UART0_RXEND(void)

//UART0_Transmitstr("wite_start-”);
unsigned int x=”0”;
while((!UART0_DataInReceiveBuffer())&(x<100))//
{
x++;
DelayMs(10);//10ms
}
x=0;
while((Frame_counting<2)&&(x<1000))//?D??êy?Y?óê?íê3é,×??à1sí?3?
{
x++;
DelayMs(1);//1ms
}

/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request。

* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)

USART_ClearITPendingBit(USART1,USART_IT_RXNE);//
SIGNAL_Usart0_RX();

if(USART_GetITStatus(USART1,USART_IT_TXE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_TXE); //
SIGNAL_Usart0_TX();}
}。

相关文档
最新文档