找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4749|回复: 1
打印 上一主题 下一主题
收起左侧

stm32串口2通信 USART2(发送数据)程序

[复制链接]
跳转到指定楼层
楼主
ID:86860 发表于 2015-7-26 02:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x.h"
void Delay(unsigned int x);
void GPIO_Configuration(void);
void UART_Init(void);
void UART2_PutChar(u8 ch);

int main(void)
{
   SystemInit();
   unsigned int temp;
   GPIO_Configuration();
   UART_Init();
   while(1)
   {
     for(temp=0;temp<50;temp++)
    {Delay(65500);}
     UART2_PutChar(0x55);
   }

}


void GPIO_Configuration(void)
{
   GPIO_InitTypeDefGPIO_InitStructure;
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //USART2时钟在APB1上
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
   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);
   
   GPIO_InitStructure.GPIO_Pin =GPIO_Pin_3;
   GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
   GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
   GPIO_Init(GPIOA,&GPIO_InitStructure);
}

void UART_Init(void)
{
   
   
    USART_InitTypeDefUSART_InitStructure;
   USART_InitStructure.USART_BaudRate = 9600;
   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(USART2,&USART_InitStructure);
    USART_Cmd(USART2,ENABLE);
}

void UART2_PutChar(u8 ch)
{
  USART_SendData(USART2,ch);
  while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)== RESET);
}


void Delay(unsigned int x)
{
unsigned int t;
t=x;
while(t--);
}



切记!!!在options设置中C/C++ Compiler的Preprocessor选项卡的defined symbols中填入STM32F10X_HD需要和你的芯片类型对应!!否则发送错误!!


startup_stm32f10x_cl.s互联型的器件,STM32F105xx,STM32F107xx
startup_stm32f10x_hd.s大容量的STM32F101xx,STM32F102xx,STM32F103xx
startup_stm32f10x_hd_vl.s 大容量的STM32F100xx
startup_stm32f10x_ld.s小容量的STM32F101xx,STM32F102xx,STM32F103xx
startup_stm32f10x_ld_vl.s 小容量的STM32F100xx
startup_stm32f10x_md.s中容量的STM32F101xx,STM32F102xx,STM32F103xx
startup_stm32f10x_md_vl.s 中容量的STM32F100xx
startup_stm32f10x_xl.sFLASH在512K到1024K字节的STM32F101xx,STM32F102xx,STM32F103xx


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:186936 发表于 2017-11-9 13:33 | 只看该作者
你好,我可以借鉴一下你的工程码,方便的话QQ1306501801谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表