1 | //*********************************
|
2 | // MyUsart_1
|
3 | //*********************************
|
4 | MyUsart_1::MyUsart_1( void )
|
5 | {
|
6 |
|
7 | USART_InitTypeDef USART_InitStructure;
|
8 | GPIO_InitTypeDef GPIO_InitStructure;
|
9 | NVIC_InitTypeDef NVIC_InitStructure;
|
10 |
|
11 |
|
12 | /* Enable USART1 clock */
|
13 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
|
14 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
15 |
|
16 | /* Configure USART1 Rx as input floating */
|
17 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
18 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
19 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
20 |
|
21 | /* Configure USART1 Tx as alternate function push-pull */
|
22 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
23 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
24 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
25 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
26 |
|
27 | USART_InitStructure.USART_BaudRate = 9600;
|
28 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
29 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
30 | USART_InitStructure.USART_Parity = USART_Parity_No ;
|
31 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
32 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
33 |
|
34 | /* Configure the USART */
|
35 | USART_Init(USART1, &USART_InitStructure);
|
36 |
|
37 |
|
38 | USART_Cmd(USART1, ENABLE); /* Enable the USART */
|
39 | //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // enable intterupt
|
40 |
|
41 | /* Enable the USARTy Interrupt */
|
42 | NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
|
43 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
44 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
45 | NVIC_Init(&NVIC_InitStructure);
|
46 | }
|