/***********************************************************************************************
* file uart.c *
* author Ha Vu *
* version V1.0.0 *
* date 29-03-2013 *
* brief USART *
************************************************************************************************/
#include "uart.h"
#include "stm32f4xx.h"
#include "timer.h"
/*********************************************************************************************/
/****************************************DEFINE***********************************************/
/*********************************************************************************************/
/*********************************************************************************************/
unsigned char buff[8];
unsigned char flag, index2;
/*********************************************************************************************/
/****************************************END**************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/****************************************CONFIG USART 2***************************************/
/*********************************************************************************************/
void Uart2_Init(void)
{
USART_InitTypeDef USART_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//Enable peripheral clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//Peripheral's alternate function
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//Configure PA2(Uart2_tx), PA3(Uart2_rx)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ;
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(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_USART2);
//INF USART2
USART_InitStruct.USART_BaudRate=19200;
USART_InitStruct.USART_WordLength=USART_WordLength_8b;
USART_InitStruct.USART_StopBits=USART_StopBits_1;
USART_InitStruct.USART_Parity=USART_Parity_No;
USART_InitStruct.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init( USART2,&USART_InitStruct);
//Uart interrupt
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*********************************************************************************************/
/**************************************END CONFIG USART 2*************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/**************************************ENABLE USART 2*****************************************/
/*********************************************************************************************/
void Uart2_Enable(void)
{
//Enable uart2
USART_Cmd(USART2, ENABLE);
}
/*********************************************************************************************/
/****************************************END**************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/**************************************DISABLE USART 2****************************************/
/*********************************************************************************************/
void Uart2_Disable(void)
{
//Disable uart2
USART_Cmd(USART2, DISABLE);
}
/*********************************************************************************************/
/****************************************END**************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/**************************************SEND DATA**********************************************/
/*********************************************************************************************/
void Uart2_Send_Byte(uint16_t data)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET);
USART_SendData(USART2,data);
}
void Uart2_Send_Bytes(char *data, uint16_t len)
{
uint16_t temp_1;
for(temp_1=0;temp_1<len;temp_1++)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET);
USART_SendData(USART2,data[temp_1]);
}
}
/*********************************************************************************************/
/****************************************END**************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/**************************************RECEIVE DATA*******************************************/
/*********************************************************************************************/
void USART2_IRQHandler(void)
{
char data;
//unsigned char buff[8];
//unsigned char flag, index2;
if(USART_GetITStatus(USART2,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
data=USART_ReceiveData(USART2);
//if (data=='C')
//{
//Timer2_Counter_ENABLE();
//Timer2_IT_ENABLE();
//}
//else
//{
//Timer2_Counter_DISABLE();
//Timer2_IT_DISABLE();
//}
}
}
/*********************************************************************************************/
/****************************************END**************************************************/
/*********************************************************************************************/
/*********************************************************************************************/
/****************************************CONFIG USART 3***************************************/
/*********************************************************************************************/
void Uart3_Init(void)
{
USART_InitTypeDef USART_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//Enable peripheral clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//Peripheral's alternate function
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//Configure PC10(Uart3_tx), PC11(Uart3_rx)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 ;
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(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSo