搜档网
当前位置:搜档网 › STM32串口通讯程序

STM32串口通讯程序

STM32串口通讯程序
STM32串口通讯程序

通过学习USART1深入STM32F107VCT6的串口通信

(2012-05-05 13:27:28)

转载▼

分类:STM32

标签:

波特率

串口

引脚

函数

杂谈

S TM32有数个串口,每个串口都有一个自己独立的波特率寄存器USART_BRR,通过设置该寄存器就可以达到配置不同波特率的目的,由于STM32采用分数波特率,所以STM32的串口波特率设置范围很宽,而且误差很小。

在STM32的参考手册中,串口被描述成通用同步异步收发器(USART),它提供了一种灵活的方法与使用工业标准NRZ异步串行数据格式的外部设备之间进行全双工数据交换。USART利用分数波特率发生器提供宽范围的波特率选择。它支持同步单向通信和半双工单线通信,也支持LIN(局部互联网),智能卡协议和IrDA(红外数据组织)SIR ENDEC 规范,以及调制解调器(CTS/RTS)操作。它还允许多处理器通信。还可以使用DMA方式,实现高速数据通信。

USART通过3个引脚与其他设备连接在一起,任何USART双向通信至少需要2个引脚:接受数据输入(RX)和发送数据输出(TX)。

RX: 接受数据串行输入。通过过采样技术来区别数据和噪音,从而恢复数据。

TX: 发送数据输出。当发送器被禁止时,输出引脚恢复到它的I/O端口配置。当发送器被激活,并且不发送数据时,TX引脚处处于高电平。在单线和智能卡模式里,此I/O口被同时用于数据的发送和接收。

一般有两种工作方式:查询和中断。

(1)查询:串口程序不断地循环查询,看看当前有没有数据要它传送。如果有,就帮助传送(可以从PC到STM32板子,也可以从STM32板子到PC)。

(2)中断:平时串口只要打开中断即可。如果发现有一个中断来,则意味着要它帮助传输数据——它就马上进行数据的传送。同样,可以从PC到STM3板子,也可以从STM32板子到PC

注意:

发动和接受都需要配合标志等待。

只能对一个字节操作,对字符串等大量数据操作需要写函数

使用串口所需设置:RCC初始化里面打开RCC_APB2PeriphClockCmd (RCC_APB2Periph_USARTx);GPIO里面管脚设定:串口RX(50Hz,

IN_FLOATING);串口TX(50Hz,AF_PP);

printf函数重定义(不必理解,调试通过以备后用)

需要c标准函数:#include "stdio.h"

增加为putchar函数。

int putchar(int c) //putchar函数

{if (c == '\n'){putchar('\r');} //将printf的\n变成\r

USART_SendData(USART1, c); //发送字符

while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){} //等待发送结束

return c; //返回值

}

printf使用变量输出:%c字符,%d整数,%f浮点数,%s字符串,/n或/r为换行。注意:只能用于main.c中。

另外:

STM32支持三个串口,在usart1默认引脚被占用的情况下可以将usart1映射到PB0.6和PB0.7上。实现的方法如下:

1、打开GPIO的AFIO时钟,使用stm32功能模块之前,必须开时钟;

2、使能USART1的映射,

3、配置USART1映射后的GPIO(PB0.6,PB0.7)

具体实现:

1、在set_systm函数中添加如下模块,打开AFIO时钟,使能USART1映射

#ifdef USB_TO_KLINE_USART1_REMAP

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);

#endif

2、在set_systm函数中添加如下模块,配置USART1映射后的GPIO #ifdef USB_TO_KLINE_USART1_REMAP

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

#else

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

#endif

例:

#include "stm32f10x.h"

#include "stm32_eval.h"

#include

USART_InitTypeDef USART_InitStructure; //定义结构体类型变量void GPIO_Configuration(void); //声明GPIO配置函数

#ifdef __GNUC__

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch) //此处定义为putchar应用

#else

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif

int main(void)

{

SystemInit(); //配置系统时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|

RCC_APB2Periph_AFIO, ENABLE); //打开APB2功能时钟(UART1为连接在APB2上的高速外设)开启了串口时钟和复用功能时钟

GPIO_Configuration(); //调用GPIO配置函数

USART_https://www.sodocs.net/doc/a65294347.html,ART_BaudRate =

115200; //设置USART传输波特率

USART_https://www.sodocs.net/doc/a65294347.html,ART_WordLength =

USART_WordLength_8b; //设置USART传输数据位一帧为8位

USART_https://www.sodocs.net/doc/a65294347.html,ART_StopBits =

USART_StopBits_1; //设置USART传输每帧一个停止位

USART_https://www.sodocs.net/doc/a65294347.html,ART_Parity =

USART_Parity_No; //设置USART无奇偶校验

USART_https://www.sodocs.net/doc/a65294347.html,ART_HardwareFlowControl =

USART_HardwareFlowControl_None; //设置USART无硬件流控制

USART_https://www.sodocs.net/doc/a65294347.html,ART_Mode = USART_Mode_Rx |

USART_Mode_Tx;//开启USART发送和接受功能

USART_Init(USART1,

&USART_InitStructure); //初始化USART1设置

USART_Cmd(USART1, ENABLE); //开启USART1

printf("\n\r 李继超是个好人吗? \n\r");

printf("\n\r 回答:李继超是个大好人! \n\r");

printf("\n\r 菏泽是个是个美丽的地方! \n\r");

printf("\n\r 发生了什么?你是猴子请来的救兵

吗? \n\r");

printf("\n\r 嗯!李继超的确是个大好人!!!你才魔道呢!哼!!! \n\r"); //配置输出数据

while (1)

{

}

}

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure; //定义结构体变量类型 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开GPIOA的功能时钟

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //选择GPIO 引脚GPIO_Pin_9

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置GPIO速率

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO_pin_9为复用功能推挽输出

GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO_Pin_9设置

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //选择GPIO引脚GPIO_Pin_10

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置GPIO_Pin_10浮空输入

GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化

GPIO_Pin_10设置

}

PUTCHAR_PROTOTYPE //重定义printf函数

{

USART_SendData(USART1, (uint8_t)

ch); //发送字符串

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)// 检测是否发送完成

{}

return ch;

}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

while (1)

{}

}

#endif

将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_https://www.sodocs.net/doc/a65294347.html,ART_BaudRate = baudrate;

USART_https://www.sodocs.net/doc/a65294347.html,ART_WordLength = USART_WordLength_8b; USART_https://www.sodocs.net/doc/a65294347.html,ART_StopBits = USART_StopBits_1;

USART_https://www.sodocs.net/doc/a65294347.html,ART_Parity = USART_Parity_No;

USART_https://www.sodocs.net/doc/a65294347.html,ART_HardwareFlowControl = USART_HardwareF

lowControl_None;

USART_https://www.sodocs.net/doc/a65294347.html,ART_Mode = USART_Mode_Rx | USART_Mode _Tx; //

USART_Init(USART1, &USART_InitStructure);

USART_https://www.sodocs.net/doc/a65294347.html,ART_Clock= USART_Clock_Disable;

USART_https://www.sodocs.net/doc/a65294347.html,ART_CPOL = USART_CPOL_Low; // USART_Cl https://www.sodocs.net/doc/a65294347.html,ART_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_APB2Peri ph_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_MAS K; // 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_MAS

K; 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();

}

}

相关主题