找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5491|回復(fù): 1
收起左側(cè)

stm32串口2通信 USART2(發(fā)送數(shù)據(jù))程序

[復(fù)制鏈接]
ID:86860 發(fā)表于 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時(shí)鐘在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--);
}



切記。。≡趏ptions設(shè)置中C/C++ Compiler的Preprocessor選項(xiàng)卡的defined symbols中填入STM32F10X_HD需要和你的芯片類型對應(yīng)!否則發(fā)送錯誤!


startup_stm32f10x_cl.s互聯(lián)型的器件,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字節(jié)的STM32F101xx,STM32F102xx,STM32F103xx


回復(fù)

使用道具 舉報(bào)

ID:186936 發(fā)表于 2017-11-9 13:33 | 顯示全部樓層
你好,我可以借鑒一下你的工程碼,方便的話QQ1306501801謝謝
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表