Chinaunix首页 | 论坛 | 博客
  • 博客访问: 313678
  • 博文数量: 169
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-10 12:00
文章分类

全部博文(169)

文章存档

2014年(38)

2013年(131)

我的朋友

分类: C/C++

2014-02-24 22:08:02

原文地址:stm32f407 usart1的使用 作者:M甲壳虫M

#include "USART.h"


void USART1_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure; 
USART_ClockInitTypeDef USART_ClockInitStructure;


  RCC_AHB1PeriphClockCmd(Open_USART_TX_GPIO_CLK,ENABLE);
  RCC_AHB1PeriphClockCmd(Open_USART_RX_GPIO_CLK,ENABLE);//RCC_APB2PeriphClockCmd


  RCC_APB2PeriphClockCmd(Open_USART_CLK,ENABLE);  //??usart?±????????RCC_APB2PeriphClockCmd




  GPIO_PinAFConfig(Open_USART_TX_GPIO_PORT, Open_USART_TX_SOURCE, Open_USART_TX_AF);
  GPIO_PinAFConfig(Open_USART_RX_GPIO_PORT, Open_USART_RX_SOURCE, Open_USART_RX_AF);


  /*
  *  Open_USART_TX -> PA9 , Open_USART_RX -PA10
  */
  GPIO_InitStructure.GPIO_Pin = Open_USART_TX_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;


  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(Open_USART_TX_GPIO_PORT, &GPIO_InitStructure);


  GPIO_InitStructure.GPIO_Pin = Open_USART_RX_PIN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//GPIO_Mode_IN_FLOATING
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(Open_USART_RX_GPIO_PORT, &GPIO_InitStructure);


/*
    USARTx configured as follow:
         - BaudRate = 115200 baud  
- Word Length = 8 Bits
         - One Stop Bit
         - No parity
         - Hardware flow control disabled (RTS and CTS signals)
         - Receive and transmit    
 */


  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_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(USART1, &USART_ClockInitStructure);
 // USART_Init(USART1, &USART_InitStructure); 
  USART_Init(USART1, &USART_InitStructure);
  /* Enable the Open_USART Transmit interrupt: this interrupt is generated when the 
     Open_USART transmit data register is empty */
  USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);  //usart1??????????
  USART_Cmd(USART1, ENABLE);
}


void USART1_NVIC_Config(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;


  /* Enable the USARTx Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = Open_USART_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}




void USART1_INIT(void)
{
USART1_Configuration();
  USART1_NVIC_Config();
  BSP_IntVectSet(BSP_INT_ID_USART1, usart1_test);  //??×????¨????????  BSP_IntHandlerUSART1
  BSP_IntPrioSet(BSP_INT_ID_USART1,1);    
  BSP_IntEn(BSP_INT_ID_USART1);                             //????????


}
uint16_t Uart1_Get_Data;
uint8_t aa[2]={0x34,0x35};
void usart1_test(void)
{
//uint16_t pdata = 0x13;
//uint16_t bb=0;
BSP_LED_Toggle(3);
 if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
 {
Uart1_Get_Data=USART_ReceiveData(USART1);
    USART_ClearITPendingBit(USART1,USART_IT_RXNE);


  USART_SendData(USART1,0x48);


 // Uart1_Get_Flag=1;
 }
 
 if(USART_GetITStatus(USART1,USART_IT_TXE)!= RESET)
{  
  USART_ClearITPendingBit(USART1,USART_IT_TXE);
 
  USART_SendData(USART1,aa[0]); //????
  while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);  {}
 

  } 




 
 
//// 
 //????-????·??ú?????è??????SR,????DR?????÷ ?ò??????????????????????
 if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)
{
  USART_ClearFlag(USART1,USART_FLAG_ORE); //??SR
  USART_ReceiveData(USART1);    //??DR
 }


}

哎网上找了些资料不是很全,自己总结了下usart的使用希望对大家有用

阅读(1392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~