标题:
stm32f103 USART异步通信串口例程
[打印本页]
作者:
robben334
时间:
2018-4-14 17:38
标题:
stm32f103 USART异步通信串口例程
需要的童鞋自行下载吧 usart通讯。。。
单片机源程序如下:
#include "stm32f10x.h"
#include "GLCD.h"
#include "USART.h"
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
const char menu[] =
"\n\r"
"+******************** 火牛开发板 **********************+\n\r"
"| 火牛STM32开发板试验程序 |\n\r"
"| USART异步通信试验 |\n\r"
"| 技术支持群:121939788 |\n\r"
"+-------------------------------------------------------+\n\r";
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure IO connected to LD1, LD2, LD3 and LD4 leds *********************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &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);
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//系统中断管理
void NVIC_Configuration(void)
{
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
//配置系统时钟,使能各外设时钟
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
|RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
|RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO
|RCC_APB2Periph_SPI1, ENABLE );
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 | RCC_APB1Periph_USART2
|RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2
, ENABLE );
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}
void InitDis(void)
{
/* LCD Module init */
GLCD_init();
GLCD_clear(White);
GLCD_setTextColor(Blue);
GLCD_displayStringLn(Line1, " FireBull");
GLCD_displayStringLn(Line2, " USART example");
GLCD_setTextColor(Red);
}
//配置所有外设
void Init_All_Periph(void)
{
RCC_Configuration();
InitDis();
GPIO_Configuration();
NVIC_Configuration();
USART1_Configuration();
USART2_Configuration();
USART1Write((u8*)" FireBull USART_example ",sizeof(" FireBull USART_example "));
USART2Write((u8*)" FireBull USART_example ",sizeof(" FireBull USART_example "));
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
//发送开机信息
void SendMessage(void)
{
printf(menu);
USART1Write((u8*)"Hello World! USART1_Test\n\r",sizeof("Hello World! USART1_Test\n\r")) ;
USART2Write((u8*)"Hello World! USART2_Test\n\r",sizeof("Hello World! USART2_Test\n\r")) ;
}
int main(void)
{
Init_All_Periph();
SendMessage();
while(1)
{
/* Turn on LD1 */
GPIO_SetBits(GPIOD, GPIO_Pin_8);
/* Insert delay */
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
/* Turn on LD2 */
GPIO_SetBits(GPIOD, GPIO_Pin_9);
/* Turn off LD1 */
GPIO_ResetBits(GPIOD, GPIO_Pin_8);
/* Insert delay */
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
/* turn on LD3*/
GPIO_SetBits(GPIOD, GPIO_Pin_10);
/* Turn off LD2 */
GPIO_ResetBits(GPIOD, GPIO_Pin_9);
/* Insert delay */
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
/* Turn on LD4 */
GPIO_SetBits(GPIOD, GPIO_Pin_11);
/* Turn off LD2 and LD3 */
GPIO_ResetBits(GPIOD, GPIO_Pin_10);
/* Insert delay */
Delay(0xAFFFF);
Delay(0xAFFFF);
Delay(0xAFFFF);
/* Turn off LD4 */
GPIO_ResetBits(GPIOD, GPIO_Pin_11);
}
}
/*******************************************************************************
* Function Name : PUTCHAR_PROTOTYPE
* Description : Retargets the C library printf function to the USART.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (u8) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
Usart1-3.rar
(285.27 KB, 下载次数: 62)
2018-4-17 04:02 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
田联
时间:
2018-8-28 16:14
非常感谢,正需要资料!!
作者:
笑浅
时间:
2018-10-17 20:51
好的,感谢
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1