thienminh_npn
Thành Viên PIF
Mình đang vọc con này, cụ thể là phần UART mà gặp khó khăn. Đã receive đc nhưng mà không hiểu sao transmit thì k. Có ace nào đã chạy đc uart cho mình xin code tham khảo vs.
Tham khảo file UART_echo nàyMình đang vọc con này, cụ thể là phần UART mà gặp khó khăn. Đã receive đc nhưng mà không hiểu sao transmit thì k. Có ace nào đã chạy đc uart cho mình xin code tham khảo vs.
/****************************************************************
*
* Module : main_UART.c
* Tool : KeilC ARM 4.22a
* Chip : STM32F407VG
* History : 3/12/2011
* Description : Day 5
+ USART1 communicate PC
+ echo the received character
*
* Author : Dang Anh Tung
* Description : www.payitforward.edu.vn
*
*
****************************************************************/
/****************************************************************
* IMPORT
****************************************************************/
#include "stm32f4_discovery.h"
/*****************************************************************
* DEFINE
******************************************************************/
#define TRUE 1
#define FALSE 0
/*****************************************************************
* GLOBAL VARIABLE
******************************************************************/
/*****************************************************************
* SUBROUTINES
******************************************************************/
void myGPIOInit(void);
void myUARTInit(void);
void mySystemInit(void);
void Delay(void);
/****************************************************************
* INTERRUPT *
*****************************************************************/
/****************************************************************
* MAIN PROGRAM
****************************************************************/
int main(void)
{
uint16_t getChar;
mySystemInit();
USART_SendData(USART1, 'A');
while(1)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
//CLEAR FLAG : clear by write bit 0 (call USART_ClearFlag()) or read Data register ( call function USART_ReceiveData()
getChar = USART_ReceiveData(USART1);
//Echo the character
USART_SendData(USART1, getChar);
}
}
/****************************************************************
** Function name: SystemInit
**
** Descriptions: Initiate system
**
** parameters: None
** Returned value: None
**
*****************************************************************/
void mySystemInit(void)
{
/*
Clock setting is already configured in SystemInit() function
which is called from startup file (startup_stm32f4xx.s)
before to branch to application main.
*/
myGPIOInit();
myUARTInit();
}
/****************************************************************
** Function name: myGPIOInit
**
** Descriptions: Initiate system
**
** parameters: None
** Returned value: None
**
*****************************************************************/
void myGPIOInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
// GPIOA Periph clock enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// configure PA0 in input mode
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* GPIOD Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/****************************************************************
** Function name: myUARTInit
**
** Descriptions: Initiate system
**
** parameters: None
** Returned value: None
**
*****************************************************************/
void myUARTInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Init USART1:
* PB6 --> USART1_TX
* PB7 --> USART1_RX
*/
//Enable clock for UART1 peripheral
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE) ;
//enable clock for TX,RX pins PB6,PB7
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//enable alternate function at TX,RX pins
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); //TX USART1
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); //RX USART1
// Init GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//TX PIN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//RX PIN
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//INIT Uart properties
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
//enable usart1
USART_Cmd(USART1, ENABLE);
}
/****************************************************************
** Function name: Delay
**
** Descriptions: loop in certain time before continue next step
**
** parameters: None
** Returned value: None
**
*****************************************************************/
void Delay()
{
uint32_t count = 16800000;
while(count--);
}
/****************************************************************
* END OF standard form.c
****************************************************************/