AnsweredAssumed Answered

usart1 configuration

Question asked by louati.amir on Jun 24, 2013
Latest reply on Jun 24, 2013 by louati.amir
I wrote this program whitch send what it receive and each time it toggel the 4 leds in the board !
when i'm debugging the program don't catch the receive interruption !
01.#include "stm32f4xx.h"
02.#include "stm32f4xx_gpio.h"
03.#include "stm32f4xx_rcc.h"
04.#include "stm32f4xx_tim.h"
05.#include "misc.h"
06.#include "stm32f4xx_exti.h"
07.#include "stm32f4xx_syscfg.h"
08.#include "stm32f4xx_dma.h"
09.#include "stm32f4xx_i2c.h"
10.#include "stm32f4xx_usart.h"
11. 
12. 
13.void UART_Config(){
14.  GPIO_InitTypeDef GPIO_InitStruct;
15.  USART_InitTypeDef USART_InitStruct;
16.  NVIC_InitTypeDef NVIC_InitStructure;
17.   
18.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
19.  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
20.  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
21.  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
22.  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
23.  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
24.  GPIO_Init(GPIOB, &GPIO_InitStruct);
25.   
26.  GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1);//TX
27.  GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1);//RX
28. 
29.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 
30.  USART_InitStruct.USART_BaudRate = 9600;
31.  USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
32.  USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
33.  USART_InitStruct.USART_Parity = USART_Parity_No;
34.  USART_InitStruct.USART_StopBits = USART_StopBits_1;
35.  USART_InitStruct.USART_WordLength = USART_WordLength_8b;
36.  USART_Init(USART1,&USART_InitStruct);
37.   
38.  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
39.  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
40.  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
41.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
42.  NVIC_Init(&NVIC_InitStructure);
43.   
44.  USART_Cmd(USART1,ENABLE);
45.}
46. 
47.void GPIO_Configuration(void)
48.{
49.  /* GPIOD clock enable */
50.  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
51.   
52.  GPIO_InitTypeDef GPIO_InitStructure;
53.   
54.  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
55.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
56.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
57.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
58.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
59.  GPIO_Init(GPIOD, &GPIO_InitStructure);
60.}
61. 
62.uint16_t a;
63. 
64.void USART1_IRQHandler(void){
65.  USART_ClearITPendingBit(USART1,USART_IT_RXNE);
66.  GPIO_ToggleBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
67.  a=USART_ReceiveData(USART1);
68.  USART_SendData(USART1,a);
69.}
70. 
71.void main(){
72.    I2C_Config();
73.    UART_Config();
74.    GPIO_Configuration();
75.    while(1){
76.    }
77.}


Outcomes