找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 4149|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32串口通信程序-查詢方式

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:99624 發(fā)表于 2015-12-27 02:38 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
//將收到的數(shù)據(jù)返回,經(jīng)測(cè)試可以,利用查詢方式
#include"stm32f10x_it.h"
#include "stm32f10x_conf.h"
  void delayms(unsigned intcount);//延時(shí)程序
  void RCC_Configuration(void); //時(shí)鐘配置
  void USART_Configuration(void);//定義串口初始化函數(shù)
  void NVIC_Configuration( void);
  void Uart1_PutChar(u8 ch);
  void Uart1_PutString(u8* buf , u8len);
   voidGPIO1_Configuration(void);
   voidGPIO_Configuration(void);
void RCC_Configuration(void)
{
    ErrorStatusHSEStartUpStatus;

    //使能外部晶振
   RCC_HSEConfig(RCC_HSE_ON);
    //等待外部晶振穩(wěn)定
    HSEStartUpStatus =RCC_WaitForHSEStartUp();
   //如果外部晶振啟動(dòng)成功,則進(jìn)行下一步操作
   if(HSEStartUpStatus==SUCCESS)
    {
       //設(shè)置HCLK(AHB時(shí)鐘)=SYSCLK
       RCC_HCLKConfig(RCC_SYSCLK_Div1);

       //PCLK1(APB1) = HCLK/2
       RCC_PCLK1Config(RCC_HCLK_Div2);

       //PCLK2(APB2) = HCLK
       RCC_PCLK2Config(RCC_HCLK_Div1);
      

       RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_4);
       //啟動(dòng)PLL
       RCC_PLLCmd(ENABLE);
       //等待PLL穩(wěn)定
       while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) ==RESET);
       //系統(tǒng)時(shí)鐘SYSCLK來(lái)自PLL輸出
       RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
       //切換時(shí)鐘后等待系統(tǒng)時(shí)鐘穩(wěn)定
       while(RCC_GetSYSCLKSource()!=0x08);
    }

//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE);
}
int main(void)
{
  int i;
    unsigned char str[]="A";
u8 uart2_get_data;
RCC_Configuration();
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA |
                       RCC_APB2Periph_AFIO, ENABLE);//使能各部分時(shí)鐘
//NVIC_Configuration(); //串口中斷配置
//RCC中打開(kāi)相應(yīng)串口
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE);
GPIO_Configuration();
//串口1的管腳初始化
       GPIO1_Configuration();
USART_Configuration();
while (1)
  {
   
  GPIO_SetBits(GPIOA,GPIO_Pin_8) ;
  GPIO_SetBits(GPIOA,GPIO_Pin_11) ;
  GPIO_SetBits(GPIOA,GPIO_Pin_15) ;
     
if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE)==SET)
{
uart2_get_data = USART_ReceiveData(USART2);
for(i=0;i<1000000;i++);
USART_SendData(USART2,uart2_get_data);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) ==RESET);
}
  GPIO_ResetBits(GPIOA,GPIO_Pin_11);
  GPIO_ResetBits(GPIOA,GPIO_Pin_15);
  for(i=0;i<1000000;i++);

  }

}  
void GPIO_Configuration(void)
{

  GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);

}
   
   voidGPIO1_Configuration(void)
{

  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;          //管腳2
  GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;      //復(fù)用推挽輸出
  GPIO_Init(GPIOA, &GPIO_InitStructure);             //TX初始化


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;          //管腳3
  GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //浮空輸入
  GPIO_Init(GPIOA, &GPIO_InitStructure);             //RX初始化

}

void delayms(unsigned int count)
{
unsigned int i,j;
for(i=0;i
for(j=0;j<5000;j++);
}

void USART_Configuration(void)//串口初始化函數(shù)

{
   USART_ClockInitTypeDefUSART_ClockInitStructure;
   
  //串口參數(shù)初始化
  USART_InitTypeDef USART_InitStructure;//串口設(shè)置恢復(fù)默認(rèn)參數(shù)
//初始化參數(shù)設(shè)置
  USART_InitStructure.USART_BaudRate = 9600;//波特率9600
  USART_InitStructure.USART_WordLength =USART_WordLength_8b; //字長(zhǎng)8位
  USART_InitStructure.USART_StopBits =USART_StopBits_1; //1位停止字節(jié)
  USART_InitStructure.USART_Parity =USART_Parity_No; //無(wú)奇偶校驗(yàn)
USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;//無(wú)流控制
  USART_InitStructure.USART_Mode =USART_Mode_Rx | USART_Mode_Tx;//打開(kāi)Rx接收和Tx發(fā)送功能
  USART_Init(USART2,&USART_InitStructure); //初始化
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//使能接受中斷,在接受移位 寄存器中有數(shù)據(jù)是產(chǎn)生
  USART_ClockInitStructure.USART_Clock =USART_Clock_Disable;
  USART_ClockInitStructure.USART_CPOL =USART_CPOL_Low;
  USART_ClockInitStructure.USART_CPHA =USART_CPHA_2Edge;
  USART_ClockInitStructure.USART_LastBit =USART_LastBit_Disable;
   USART_ClockInit(USART2,&USART_ClockInitStructure);

USART_Cmd(USART2, ENABLE); //啟動(dòng)串口

}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//選擇分組方式0
  

  NVIC_InitStructure.NVIC_IRQChannel =USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
void Uart1_PutChar(u8 ch)
{
   
    USART_SendData(USART2,(u8) ch);
   while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
   
  
}

void Uart1_PutString(u8* buf , u8 len)
{
   u8 i;
    for( i=0;i
    {
       Uart1_PutChar(*buf++);
    }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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