On the basis of successful experiments based on the serial port for debugging method to practice. After successful completion of the hardware codes, to redefine the future printf debugging need to use for debugging, fixed in their own library functions.
b) initialization function is defined:
void USART_Configuration (void); // initialization function defines the serial port
c) initialization function call:
void UART_Configuration (void); // serial port initialization function calls
Initialization code:
void USART_Configuration (void) // serial port initialization function
{
// initialize the serial port parameters
USART_InitTypeDef USART_InitStructure; // serial port settings to default parameters
// initialization parameter settings
USART_InitStructure.USART_BaudRate = 9600; // 9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 1 stop bit byte
USART_InitStructure.USART_Parity = USART_Parity_No; // no parity
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无 流 控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // open receiver Rx and Tx sending
USART_Init (USART1, & USART_InitStructure); // initialize
USART_Cmd (USART1, ENABLE); // start serial
}
RCC open the appropriate port
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
GPIO pin serial mode which is set corresponding
// initialize serial port pin 1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // pins 9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // push-pull output multiplexing
GPIO_Init (GPIOA, & GPIO_InitStructure); // TX initialization
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // pin 10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // floating input
GPIO_Init (GPIOA, & GPIO_InitStructure); // RX initialization
d) simple application:
Send a character
USART_SendData (USART1, data); // sending a bit
while (USART_GetFlagStatus (USART1, USART_FLAG_TXE) == RESET) {} // wait been sent
Receiving a character
while (USART_GetFlagStatus (USART1, USART_FLAG_RXNE) == RESET) {} // wait been received
Variable = (USART_ReceiveData (USART1)); // accepts a byte
Send a string
Define string: char rx_data [250];
Then add the following code to be transmitted to the local
int i; // loop variable defined
while (rx_data! = '\ 0') // loop output verbatim, to the end of the word '\ 0'
{USART_SendData (USART1, rx_data); // send characters
while (USART_GetFlagStatus (USART1, USART_FLAG_TXE) == RESET) {} // waits for the character has been sent
i++;}
e) USART Note:
And accept the need to launch with flag wait.
Only one byte operations, the operation of large amounts of data need to write function like strings
Use your serial settings: RCC initialization inside open RCC_APB2PeriphClockCmd
(RCC_APB2Periph_USARTx); GPIO pin is set inside: serial RX (50Hz, IN_FLOATING); serial TX (50Hz, AF_PP);
f) printf redefine the function (without having to understand, for later use by the debugger)
(1) c required standard functions:
#include "stdio.h"
(2) Paste function definition codes
#define PUTCHAR_PROTOTYPE int __io_putchar (int ch) // defined as putchar application
(3) RCC open the appropriate port
(4) GPIO pin serial mode which is set corresponding
(6) increased to putchar function.
int putchar (int c) // putchar function
{
if (c == '\ n') {putchar ( '\ r');} // printf of the \ n becomes \ r
USART_SendData (USART1, c); // send characters
while (USART_GetFlagStatus (USART1, USART_FLAG_TXE) == RESET) {} // end wait transmission
return c; // Return value
}
(8) through the test successfully. printf output variables used:% c character,% d integers,% f float,% s string, / n or / r to a newline. Note: it can only be used in main.c.
3, NVIC serial port interrupt applications
Practice using hardware interrupt input transferred through the front of the base, and the code of several functions, the serial port: a) Objective. Because in practical applications, do not use interrupt input efficiency is very low, this usage is rare, most serial inputs can not do without interruption.
b) initialization function definitions and function calls: Do not call the initialization function and add in the debug address specified time has been called, add the appropriate open break the code on the line in the NVIC_Configuration inside.
c) process:
. I In the serial port initialization before joining USART_Cmd interrupt settings:
USART_ITConfig (USART1, USART_IT_TXE, ENABLE); // TXE transmit interrupt, TC transfer complete interrupt, the interrupt receiving RxNE, PE Parity Error Interrupt, may be plural.
ii. RCC, GPIO port which opens a respective basic clock, set pins
. Iii NVIC which joined the serial port interrupt to open the code:
NVIC_InitTypeDef NVIC_InitStructure; // interrupt default parameters
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; // channel is set to the serial port interrupt 1
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // interrupt preemption level 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // interrupt priority 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // open the interruption
NVIC_Init (& NVIC_InitStructure); // initialize
iv. stm32f10x_it.c find void USART1_IRQHandler function in the file, in which the insertion of code execution. Usually at least three steps: first if statement to determine the interrupt occurs, it will be cleared of the last string assigned to, or do other things.
void USART1_IRQHandler (void) // interrupt serial port 1
{
char RX_dat; // define the character variable
if (USART_GetITStatus (USART1, USART_IT_RXNE)! = RESET) // receive interrupt occurs is determined
{USART_ClearITPendingBit (USART1, USART_IT_RXNE); // Clear interrupt flag
GPIO_WriteBit (GPIOB, GPIO_Pin_10, (BitAction) 0x01); // start transmission
RX_dat = USART_ReceiveData (USART1) & 0x7F; // receiving data, sorting the first two removed
USART_SendData (USART1, RX_dat); // send data
while (USART_GetFlagStatus (USART1, USART_FLAG_TXE) == RESET) {} // end wait transmission
}
}
d) interrupt Note:
You can always use USART_ITConfig (USART1, USART_IT_TXE, DISABLE) in the program; turn off interrupts.
NVIC_InitTypeDef NVIC_InitStructure the definition must be added in the first sentence NVIC initialization module.
Global variables defined function: variable or function defined in any .c file, use extern in other .c file + custom code definitions can be directly invoked again.
STM32 nine notes: break it to work for me, EXIT (external I / O interrupts) applications
a) Purpose: with similar serial input, interrupts are not used IO input efficiency is very low, and can be inserted through the button event EXTI, this section contact EXTI interrupted.
b) initialization function is defined:
void EXTI_Configuration (void); // define the IO interrupt initialization function
c) initialization function call:
EXTI_Configuration (); // IO interrupt initialization function call a simple application:
d) initialization function:
void EXTI_Configuration(void)
{EXTI_InitTypeDef EXTI_InitStructure; EXTI structure defines // Initialization
EXTI_ClearITPendingBit (EXTI_LINE_KEY_BUTTON); // Clear interrupt flag
GPIO_EXTILineConfig (GPIO_PortSourceGPIOA, GPIO_PinSource3); // Select Pin
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource6);
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; // Event Selection
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // trigger mode
EXTI_InitStructure.EXTI_Line = EXTI_Line3 | EXTI_Line4; // line selection
EXTI_InitStructure.EXTI_LineCmd = ENABLE; // start interrupt
EXTI_Init (& EXTI_InitStructure); // initialize
}
e) RCC open initialization function I / O clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
GPIO initialization function defined input I / O pins.
// IO input, GPIOA 4 feet input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // pull input
GPIO_Init (GPIOA, & GPIO_InitStructure); // initialize
f) Add the following code NVIC initialization function which opens the associated interrupt:
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; // channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // preemption level
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // response class
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //启动
NVIC_Init (& NVIC_InitStructure); // initialize
g) found in the void USART1_IRQHandler function stm32f10x_it.c file, where the insertion of the code execution. Usually at least three steps: first if statement to determine the interrupt occurs, it will be cleared of the last string assigned to, or do other things.
if (EXTI_GetITStatus (EXTI_Line3)! = RESET) // determine the source of an interrupt occurs
{EXTI_ClearITPendingBit (EXTI_Line3); // Clear interrupt flag
USART_SendData (USART1, 0x41); // send character "a"
GPIO_WriteBit (GPIOB, GPIO_Pin_2, (BitAction) (1-GPIO_ReadOutputDataBit (GPIOB, GPIO_Pin_2))); // LED alternating light and dark occur
}
h) interrupt Note:
After the interrupt occurs must clear the interrupt bit, there would be an infinite loop continue to occur this interruption. Then we need to be determined for the type of interrupt and then execute the code.
EXTI use of I / O interrupts, after the completion of RCC and GPIO hardware settings need to do three things: initialize EXTI, NVIC open break, write interrupt code execution.
Video learning materials:
(Stm32 USART serial application)
http://www.makeru.com.cn/live/1392_1164.html?s=45051
PWM technology
http://www.makeru.com.cn/live/4034_2146.html?s=45051
Based on STM32 explain serial operation
http://www.makeru.com.cn/live/1758_490.html?s=45051
SERIAL transparently transmitted by Z-stack Stack
http://www.makeru.com.cn/live/1758_330.html?s=45051
(STM32 DC motor drives)
http://www.makeru.com.cn/live/1392_1218.html?s=45051
(ADC reading light sensor)
http://www.makeru.com.cn/live/1392_1004.html?s=45051
Communication protocols - UART serial port protocol
http://www.makeru.com.cn/live/3576_1437.html?s=45051
The SPI communication stm32
http://www.makeru.com.cn/live/3523_1795.html?s=45051
SPI protocol driver norFlash
http://www.makeru.com.cn/live/4034_2151.html?s=45051