找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8866|回复: 0
打印 上一主题 下一主题
收起左侧

USART接收发送数据(可以中断可以查询发送接收数据)

[复制链接]
跳转到指定楼层
楼主
ID:73735 发表于 2015-2-19 00:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

#include "stm32f10x_lib.h"
//#define TxBufferSize   (countof(TxBuffer) - 1)
//#define RxBufferSize   0x65
//#define countof(a)   (sizeof(a) / sizeof(*(a)))
//u8 TxBuffer[] = "\n\r Stm32.USART->Computer \n\n\r";
//u8 RxBuffer[RxBufferSize];
//u8 NbrOfDataToTransfer = TxBufferSize;
//u8 NbrOfDataToRead = RxBufferSize;
//u8 TxCounter = 0;
//u16 RxCounter = 0;
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
u32 USART_Scanf(u32 value);
int main(void)
{
#ifdef DEBUG
  debug();
#endif
  RCC_Configuration();
  NVIC_Configuration();
  GPIO_Configuration();
  
  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_InitStructure.USART_Clock = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
  
  USART_Init(USART1, &USART_InitStructure);       /* Configure the USART1 */
  USART_ITConfig(USART1, USART_IT_TXE, ENABLE);   /* Enable the USART Transmoit interrupt: this interrupt is generated when the USART1 transmit data register is empty */
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  /* Enable the USART Receive interrupt: this interrupt is generated when the USART1 receive data register is not empty */
  USART_Cmd(USART1, ENABLE);                      /* Enable USART1 */
  
  while (1)
  {
    /*
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
   
    RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0xFF);   
    USART_SendData(USART1, RxBuffer[RxCounter-1]);
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    if(RxCounter == NbrOfDataToRead)
      {
      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
      }
    }
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
   {
    USART_SendData(USART1, TxBuffer[TxCounter++]);   
    USART_ClearITPendingBit(USART1, USART_IT_TXE);
    if(TxCounter == NbrOfDataToTransfer)
    {
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    }   
   }*/
   
  }
  
}
void RCC_Configuration(void)
{               
  RCC_DeInit();
  RCC_HSEConfig(RCC_HSE_ON);
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    FLASH_SetLatency(FLASH_Latency_2);
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
    RCC_PCLK2Config(RCC_HCLK_Div1);
    RCC_PCLK1Config(RCC_HCLK_Div2);
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    RCC_PLLCmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
   
  /* Enable GPIOA and USART1 clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
  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_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
#ifdef  VECT_TAB_RAM  
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
#ifdef  DEBUG
void assert_failed(u8* file, u32 line)
{
  while (1)
  {
  }
}
#endif

中断程序:
#include "stm32f10x_it.h"
#define TxBufferSize   (countof(TxBuffer) - 1)
#define RxBufferSize   0x51
#define countof(a)   (sizeof(a) / sizeof(*(a)))

u8 TxBuffer[] = "\n\r Stm32.USART->Computer \n\n\r";
u8 RxBuffer[RxBufferSize];
u8 NbrOfDataToTransfer = TxBufferSize;
u8 NbrOfDataToRead = RxBufferSize;
u8 TxCounter = 0;
u16 RxCounter = 0;

void USART1_IRQHandler(void)
{
  /*
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
   
    RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0xFF);         
   
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    if(RxCounter == NbrOfDataToRead)
    {
      
      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
    }
  }
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {   
   
    USART_SendData(USART1, TxBuffer[TxCounter++]);                    
   
    USART_ClearITPendingBit(USART1, USART_IT_TXE);
    if(TxCounter == NbrOfDataToTransfer)
    {
      
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    }   
  }
*/
  
  
   if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
   
    RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0xFF);   
    USART_SendData(USART1, RxBuffer[RxCounter-1]);
    USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    if(RxCounter == NbrOfDataToRead)
      {
      USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
      }
    }
  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
   {
    USART_SendData(USART1, TxBuffer[TxCounter++]);   
    USART_ClearITPendingBit(USART1, USART_IT_TXE);
    if(TxCounter == NbrOfDataToTransfer)
    {
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
    }   
   }
}



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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