stm32的UART串口通信

来源:互联网 发布:注册淘宝用什么名字好 编辑:程序博客网 时间:2024/05/17 22:03

问题:  手上有一款AT91SAM9260的开发板。开发板上跑的是2.6版本内核的Linux系统,现在想通过UART来完成stm M0与开发板的通信。

一、关于AT91开发板上的UART相关知识都在我的上篇博客中说明:http://blog.csdn.net/qq_37474534/article/details/78405446

AT91部分的UART通信已经完成就绪:

二、stm M0的uart通信基础

1、电路图

UART通信用的是PA9 PA10两个引脚:

2、驱动程序配置

#include "ALL_Includes.h"#include "USART1.h"/* USART初始化初始化初始化 */void USART1_Init(void){GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStruct;RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  //使能GPIOA的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能UART的时钟/* USART1的端口配置*/GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);//PA9为TXGPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);//PA10为RXGPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOA, &GPIO_InitStructure);/* USAR1d的基本配置*/USART_InitStructure.USART_BaudRate = 115200;             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_ITConfig(USART1,USART_IT_RXNE,ENABLE);           USART_Cmd(USART1, ENABLE);                            /* USART1中断配置 */NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStruct.NVIC_IRQChannelPriority = 0x02;NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStruct);}/*中断函数*/void USART1_IRQHandler(void){if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){    USART_SendData(USART1,USART_ReceiveData(USART1)); while (USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);}}/**  * @brief  Retargets the C library printf function to the USART.  * @param  None  * @retval None  */PUTCHAR_PROTOTYPE{  /* Place your implementation of fputc here */  /* e.g. write a character to the USART */  USART_SendData(USART1, (uint8_t) ch);  /* Loop until the end of transmission */  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)  {}  return ch;}
stm函数中就需要进行对printf()函数进行实现:把printf的输出流导向UART1 重新定义fputc函数
三、通信实现
当对接UART的两根线之后,交叉编译ARM的应用程序,由于read函数和write函数都为阻塞函数,就会等待发送或者接受UART上的数据,
而M0这边通过printf函数调用UART_sendData函数向AT91发送数据。
到此就完成了两个设备的交互,AT91开发板就可以通过M0发送指令、完成对M0芯片的控制。从而完成一些工作。

原创粉丝点击