11.串口收发实验
更新时间:2024-03-22 08:29:01 阅读量: 综合文库 文档下载
11.串口收发实验:
/* Includes ------------------------------------------------------------------*/ #include \
#include \
/** @addtogroup STM32F10x_StdPeriph_Examples * @{ */
/** @addtogroupUSART_Interrupt * @{ */
GPIO_InitTypeDefGPIO_InitStructure;
/* Private typedef -----------------------------------------------------------*/ typedefenum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/ #define TxBufferSize1 (countof(TxBuffer1) - 1) #define TxBufferSize2 (countof(TxBuffer2) - 1) #define RxBufferSize1 TxBufferSize2 #define RxBufferSize2 TxBufferSize1
/* Private macro -------------------------------------------------------------*/ #define countof(a) (sizeof(a) / sizeof(*(a)))
/* Private variables ---------------------------------------------------------*/ USART_InitTypeDefUSART_InitStructure;
uint8_t TxBuffer1[] = \串口中断收发示例: 串口1 ->串口2 (中断收发)%uint8_t TxBuffer2[] = \串口中断收发示例: 串口2 ->串口1 (中断收发)%uint8_t RxBuffer1[RxBufferSize1]; uint8_t RxBuffer2[RxBufferSize2]; __IO uint8_t TxCounter1 = 0x00; __IO uint8_t TxCounter2 = 0x00; __IO uint8_t RxCounter1 = 0x00; __IO uint8_t RxCounter2 = 0x00;
uint8_t NbrOfDataToTransfer1 = TxBufferSize1; uint8_t NbrOfDataToTransfer2 = TxBufferSize2; uint8_t NbrOfDataToRead1 = RxBufferSize1; uint8_t NbrOfDataToRead2 = RxBufferSize2; __IO TestStatus TransferStatus1 = FAILED; __IO TestStatus TransferStatus2 = FAILED;
/* Private function prototypes -----------------------------------------------*/ voidRCC_Configuration(void); voidGPIO_Configuration(void); voidNVIC_Configuration(void);
TestStatusBuffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
/* Private functions ---------------------------------------------------------*/ /**
* @brief Main program * @param None * @retvalNone */
int main(void) {
/*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */
/* System Clocks Configuration */ RCC_Configuration();
/* NVIC configuration */ NVIC_Configuration();
/* Configure the GPIO ports */ GPIO_Configuration();
GPIO_ResetBits(GPIO_LED,LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN);/*关闭所有的LED指示灯*/ /* USART1 and USART2 configuration ------------------------------------------------------*/
/* USART1 and USART2 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity
- Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */
USART_InitStructure.USART_BaudRate = 115200; /*设置波特率为115200*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*设置数据位为8*/ USART_InitStructure.USART_StopBits = USART_StopBits_1; /*设置停止位为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; /*发送和接收*/
/*配置串口1 */
USART_Init(USART1, &USART_InitStructure); /*配置串口2*/
USART_Init(USART2, &USART_InitStructure);
/*使能串口1的发送和接收中断*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/*使能串口2的发送和接收中断*/
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* 使能串口1 */
USART_Cmd(USART1, ENABLE);
/* 使能串口2 */
USART_Cmd(USART2, ENABLE);
/* Wait until end of transmission from USART1 to USART2 */ while(RxCounter2 < RxBufferSize2) { }
/* Wait until end of transmission from USART2 to USART1 */ while(RxCounter1 < RxBufferSize1) { }
/* Check the received data with the send ones */
TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);
/* TransferStatus1 = PASSED, if the data transmitted from USART2 and received by USART1 are the same */
/* TransferStatus1 = FAILED, if the data transmitted from USART2 and received by USART1 are different */
TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);
/* TransferStatus2 = PASSED, if the data transmitted from USART1 and received by USART2 are the same */
/* TransferStatus2 = FAILED, if the data transmitted from USART1 and received by USART2 are different */
while (1) {
if(TransferStatus1 == PASSED) { GPIO_SetBits(GPIO_LED,LD1_PIN);/*点亮LD1,串口1接收的数据与串口2发送的数据相同*/ }
else if(TransferStatus1 == FAILED) {
GPIO_SetBits(GPIO_LED,LD2_PIN);/*点亮LD2,串口1接收的数据与串口2发送的数据不相同*/ }
if(TransferStatus2 == PASSED) { GPIO_SetBits(GPIO_LED,LD3_PIN);/*点亮LD3,串口2接收的数据与串口1发送的数据相同*/ }
else if(TransferStatus2 == FAILED) { GPIO_SetBits(GPIO_LED,LD4_PIN);/*点亮LD4,串口2接收的数据与串口1发送的数据不相同*/ } } } /**
* @brief Configures the different system clocks. * @param None * @retvalNone */
voidRCC_Configuration(void) {
/*使能串口1和串口2使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(USART1_GPIO_CLK |USART2_GPIO_CLK, ENABLE); /* Enable USART1 Clock */ /*使能串口1时钟*/
RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE); /*使能串口2时钟*/
RCC_APB1PeriphClockCmd(USART2_CLK, ENABLE);
/*使能LED灯使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE); } /**
* @brief Configures the different GPIO ports. * @param None * @retvalNone */
voidGPIO_Configuration(void) {
GPIO_InitTypeDefGPIO_InitStructure;
/*串口1 RX管脚配置*/
/* Configure USART1 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = USART1_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/*串口2 RX管脚配置*/
/* Configure USART2 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = USART2_RxPin; GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
/*串口1 TX管脚配置*/
/* Configure USART1 Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = USART1_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/*串口2 TX管脚配置*/
/* Configure USART2 Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = USART2_TxPin; GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
/* 配置LED灯使用的GPIO管脚模式*/
GPIO_InitStructure.GPIO_Pin = LD1_PIN|LD2_PIN|LD3_PIN|LD4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIO_LED, &GPIO_InitStructure); } /**
* @brief Configures the nested vectored interrupt controller. * @param None * @retvalNone */
voidNVIC_Configuration(void) {
NVIC_InitTypeDefNVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /**
* @brief Compares two buffers.
* @param pBuffer1, pBuffer2: buffers to be compared. * @paramBufferLength: buffer's length
* @retval PASSED: pBuffer1 identical to pBuffer2 * FAILED: pBuffer1 differs from pBuffer2 */
TestStatusBuffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) {
while(BufferLength--) {
if(*pBuffer1 != *pBuffer2) {
return FAILED; }
pBuffer1++; pBuffer2++; }
return PASSED; }
#ifdef USE_FULL_ASSERT /**
* @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name
* @param line: assert_param error line source number * @retvalNone */
voidassert_failed(uint8_t* file, uint32_t line) {
/* User can add his own implementation to report the file name and line number, ex: printf(\
/* Infinite loop */ while (1) { } }
#endif /** * @} */ /** * @} */
/*****END OF FILE****/
正在阅读:
11.串口收发实验03-22
我学会了做寿司作文400字06-21
西沟参观学习心得体会12-05
村民一事一议筹资筹劳管理办法07-09
关于发布JGJT250-2011《建筑与市政工程施工现场专业人员职业标准05-29
加强国防教育107-03
施工现场临时用电组织设计05-06
广东造价员考试案例分析试题与答案05-07
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 串口
- 收发
- 实验
- 11
- 公司法案例分析与讨论
- 12.16课堂作业
- 约读书房的故事:我的书香2018
- 宁波大学海洋生物资源与环境专业培养方案及教学计划 - 图文
- 电器产品规格书格式
- 机务员鉴定题库-交换维护专业理论
- 备考2014高考数学--(五年高考)(三年联考)精品题库:第一章集
- 入党申请之入党志愿书谈话意见
- 浅谈农村中小学生辍学流失的成因及对策
- 2018年社区基层人员入党思想汇报范文
- 毕业设计--基于PLC的桥式起重机的设计
- Michael演讲稿
- 科学老师发言稿
- 2010年高考试题-英语-湖南卷 - 图文
- 无机材料物理性能考试试题及答案
- 安全维稳承诺书
- 审计费用影响因素浅析
- 小学古诗80首
- 2014年高考化学一轮复习测试卷及解析(1):实验常用仪器和试剂
- 初中英语将来时态专题练习及答案