第一范文网 - 专业文章范例文档资料分享平台

STM32串口通讯程序

来源:用户分享 时间:2025/10/11 6:55:23 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

将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_InitStructure.USART_BaudRate = baudrate;

USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //

USART_Init(USART1, &USART_InitStructure);

USART_ClockInitqlt.USART_Clock= USART_Clock_Disable;

USART_ClockInitqlt.USART_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;

搜索更多关于: STM32串口通讯程序 的文档
STM32串口通讯程序.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4ygu61784a35m4y30uvy_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top