打印

串口收发程序,只能发送数据,不能接收数据,求指导

[复制链接]
202|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
小蘭|  楼主 | 2018-7-28 21:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <stm32f10x.h>


void RCC_Config(void);
void GPIO_Config(void);
void USART_Config(void);
void NVIC_Config(void);

int main(void)
{   

    RCC_Init();
    GPIO_Config();
    NVIC_Config();
    USART_Config();

     while(1);
}
void RCC_Init()
{   

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Config(void)
{
    //configure USARTx_Tx as alternate function push-pull//
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);



    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
   void NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
void USART_Config (void)
{
  USART_InitTypeDef USART_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_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口的接收中断
  USART_Cmd(USART1,ENABLE);
  USART_Init(USART1, &USART_InitStructure);

}
    串口中断服务程序
void USART1_IRQHandler(void)
{
   if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断串口是否产生接收中断
   {


     USART_SendData(USART1,USART_ReceiveData(USART1));

     while (USART_GetFlagStatus (USART1,USART_FLAG_TXE)!=RESET);//判断是否发送成
     USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除中断标志位
   }

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

451

主题

463

帖子

1

粉丝