标题: stm32串口2通信 USART2(发送数据)程序 [打印本页]

作者: geige    时间: 2015-7-26 02:13
标题: stm32串口2通信 USART2(发送数据)程序
#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



作者: wxc19960311    时间: 2017-11-9 13:33
你好,我可以借鉴一下你的工程码,方便的话QQ1306501801谢谢




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1