搜档网
当前位置:搜档网 › stm32串口通讯

stm32串口通讯

stm32串口通讯
stm32串口通讯

作业二:STM32串口通讯

班级:100712班

学号:10071049

姓名:王云鹏

目录

一、STM32串口通讯设计题目..................................................................................................- 3 -

二、STM32串口通讯电路原理图..............................................................................................- 3 -

三、STM32串口通讯程序..........................................................................................................- 3 -

1、查询方式.........................................................................................................................- 3 -

2、中断方式.........................................................................................................................- 5 -

一、STM32串口通讯设计题目

1、用STM32单片机为主芯片设计一个与计算机串口进行通信的电路。

2、要求画出单片机与计算机的RS232口通信的硬件电路图,通信能正常工作。注意电平转换电路的芯片选取。

3、要求:芯片在接收到计算机发送来的数据后,进行加1操作,并将数据返回给计算机。可以用中断和查询两种方式。

二、STM32串口通讯电路原理图

三、STM32串口通讯程序

1、查询方式

#include "stm32f10x.h"

#include "usart1.h"

#include "misc.h"

int main(void)

{

u8 c;

/* USART1 config 115200 8-N-1 */

USART1_Config();

for(;;)

{

while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!= SET);

//等待USART1的接受区不为空c=USART1->DR; //将接收到的值保存在c中

printf("%c",c+1); //将接受到的数据直接返回打印

USART_ClearFlag(USART1, USART_FLAG_RXNE);

}

}

void USART1_Config(void) //USART1 GPIO 配置,工作模式配置

GPIO_InitTypeDefGPIO_InitStructure;

USART_InitTypeDefUSART_InitStructure;

/* config USART1 clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,ENABLE); //打开USART1和GPIOA 的时钟

/* USART1 GPIO config */

/* 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);

/* USART1 mode config */

USART_https://www.sodocs.net/doc/1318990472.html,ART_BaudRate = 115200;

//波特率115200

USART_https://www.sodocs.net/doc/1318990472.html,ART_WordLength = USART_WordLength_8b;

//8 位数据位

USART_https://www.sodocs.net/doc/1318990472.html,ART_StopBits = USART_StopBits_1;

//1 位停止位

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

//无奇偶校验位

USART_https://www.sodocs.net/doc/1318990472.html,ART_HardwareFlowControl=USART_HardwareFlowContr ol_None;

//关硬件流

USART_https://www.sodocs.net/doc/1318990472.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;

//收发模式

USART_Init(USART1, &USART_InitStructure);

//USART1初始化

USART_Cmd(USART1, ENABLE);

//打开USART1

}

intfputc(intch, FILE *f) //将printf内容发往串口

{

USART_SendData(USART1, (unsigned char) ch);

while (!(USART1->SR &USART_FLAG_TXE));

return (ch);

}

2、中断方式

#include "stm32f10x.h"

#include "usart1.h"

#include "misc.h"

int main(void)

{

/* USART1 config 115200 8-N-1 */

USART1_Config(); //配置USART1

NVIC_Configuration(); //配置NVIC

for(;;)

{

}

}

void USART1_Config(void) // USART1 GPIO 配置,工作模式配置{

GPIO_InitTypeDefGPIO_InitStructure;

USART_InitTypeDefUSART_InitStructure;

/* config USART1 clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //打开USART1和GPIOA 的时钟

/* USART1 GPIO config */

/* 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);

/* USART1 mode config */

USART_https://www.sodocs.net/doc/1318990472.html,ART_BaudRate = 115200;

//波特率115200

USART_https://www.sodocs.net/doc/1318990472.html,ART_WordLength = USART_WordLength_8b;

//8 位数据位

USART_https://www.sodocs.net/doc/1318990472.html,ART_StopBits = USART_StopBits_1;

//1 位停止位

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

//无奇偶校验位

USART_https://www.sodocs.net/doc/1318990472.html,ART_HardwareFlowControl=USART_HardwareFlowContr ol_None;

//关硬件流

USART_https://www.sodocs.net/doc/1318990472.html,ART_Mode = USART_Mode_Rx | USART_Mode_Tx;

//收发模式

USART_Init(USART1, &USART_InitStructure);

//USART1初始化

USART_Cmd(USART1, ENABLE);

//打开USART1

}

void NVIC_Configuration(void) //NVIC 中断配置

{

NVIC_InitTypeDefNVIC_InitStructure;

/* Configure the NVIC Preemption Priority Bits */

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

//设置中断优先级组0 /* Enable the USARTy Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //USART1中断

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

//设置打断优先级为1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

//设置响应优先级为0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

//打开NVIC

NVIC_Init(&NVIC_InitStructure); //初始化

}

intfputc(intch, FILE *f) //将Printf内容发往串口

{

USART_SendData(USART1, (unsigned char) ch);

while (!(USART1->SR &USART_FLAG_TXE));

return (ch);

}

#include "stm32f10x_it.h"

void USART1_IRQHandler(void)

{

u8 c;

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)

{

c=USART1->DR;

printf("%c",c+1); //将接受到的数据直接返回打印}

}

相关主题