找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3398|回复: 0
收起左侧

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

[复制链接]
ID:99624 发表于 2015-12-27 02:38 | 显示全部楼层 |阅读模式
//将收到的数据返回,经测试可以,利用查询方式
#include"stm32f10x_it.h"
#include "stm32f10x_conf.h"
  void delayms(unsigned intcount);//延时程序
  void RCC_Configuration(void); //时钟配置
  void USART_Configuration(void);//定义串口初始化函数
  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);
    //等待外部晶振稳定
    HSEStartUpStatus =RCC_WaitForHSEStartUp();
   //如果外部晶振启动成功,则进行下一步操作
   if(HSEStartUpStatus==SUCCESS)
    {
       //设置HCLK(AHB时钟)=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);
       //启动PLL
       RCC_PLLCmd(ENABLE);
       //等待PLL稳定
       while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) ==RESET);
       //系统时钟SYSCLK来自PLL输出
       RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
       //切换时钟后等待系统时钟稳定
       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);//使能各部分时钟
//NVIC_Configuration(); //串口中断配置
//RCC中打开相应串口
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;      //复用推挽输出
  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)//串口初始化函数

{
   USART_ClockInitTypeDefUSART_ClockInitStructure;
   
  //串口参数初始化
  USART_InitTypeDef USART_InitStructure;//串口设置恢复默认参数
//初始化参数设置
  USART_InitStructure.USART_BaudRate = 9600;//波特率9600
  USART_InitStructure.USART_WordLength =USART_WordLength_8b; //字长8位
  USART_InitStructure.USART_StopBits =USART_StopBits_1; //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;//打开Rx接收和Tx发送功能
  USART_Init(USART2,&USART_InitStructure); //初始化
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//使能接受中断,在接受移位 寄存器中有数据是产生
  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); //启动串口

}
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++);
    }
}


回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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