STM32 serial optoth experiment

In experimental phase 1, the timer is timed for 1s to trigger an interrupt, and data is sent out during the interrupt.

The timer is set to 36000 frequency division, the period is set to 2000, and the interrupt is turned on, configuration The code is as follows

 tim_timebase_struct.TIM_CounterMode = TIM_CounterMode_Up;
tim_timebase_struct.TIM_ClockDivision
= TIM_CKD_DIV1;
tim_timebase_struct.TIM_Prescaler
= DEBUG_UART_TX_TIM_PSC;
tim_timebase_struct.TIM_Period
= DEBUG_UART_TX_TIM_PERIOD;
TIM_TimeBaseInit(DEBUG_UART_TX_TIM,
&tim_timebase_struct);

TIM_Cmd(DEBUG_UART_TX_TIM, ENABLE) ;

TIM_ITConfig(DEBUG_UART_TX_TIM, TIM_IT_Update, ENABLE);

Serial port configuration can be the same as the PC-side debugging assistant setting, generally configured as None Hardware flow control, no parity, 1 stop bit, 8 data bits

 usart_struct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usart_struct.USART_HardwareFlowControl
= USART_HardwareFlowControl_None;
usart_struct.USART_Parity
= USART_Parity_No;
usart_struct.USART_StopBits
= USART_StopBits_1;
usart_struct.USART_WordLength
= USART_WordLength_8b;
usart_struct.USART_BaudRate
= DEBUG_UART_BAUDRATE;
USART_Init(DEBUG_UART,
&usart_struct);

USART_Cmd(DEBUG_UART, ENABLE);

In the interrupt service function, it is still a routine to check the interrupt flag bit and then clear the interrupt flag bit

void DEBUG_UART_TX_TIM_IRQ_HANDLER(void)
{
if(TIM_GetITStatus(DEBUG_UART_TX_TIM, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(DEBUG_UART_TX_TIM, T IM_IT_Update);

printf(
"hello < /span>");
}
}

The serial port sending function uses output stream redirection, and you need to check Use MicroLIB in the magic wand configuration.

int fputc(int ch, FILE* stream)
{
while(USART_GetFlagStatus(DEBUG_UART, USART_FLAG_TXE) == RESET);

USART_SendData(DEBUG_UART, ch);

return ch;
}

Experimental stage 2, a data transmission is generated by pressing the button to trigger an external interrupt, and the button GPIO is configured as a general output. Just pull it down, and you need to call a function to connect GPIO to EXTI

 gpio_struct.GPIO_Mode = GPIO_Mode_IN;
gpio_struct.GPIO_PuPd
= GPIO_PuPd_DOWN;
gpio_struct.GPIO_Speed ​​
= GPIO_Speed_Level_1;
gpio_struct.GPIO_Pin
= PUSHBUTTON_PIN;
GPIO_Init(PUSHBUTTON_GPIO,
&gpio_struct);

SYSCFG_EXTILineConfig(PUSHBUTTON_EXTI_PORTSOURCE, PUSHBUTTON_EXTI_PINSOURCE);

This experiment uses ST’s official NUCLEO development board, the MCU model is STM32F303RE, and the standard library is used for development. The serial GPIO configuration is as follows< /p>

 gpio_struct.GPIO_Mode = GPIO_Mode_AF;
gpio_struct.GPIO_OType
= GPIO_OType_PP;
gpio_struct.GPIO_PuPd
= GPIO_PuPd_NOPULL;
gpio_struct.GPIO_Speed ​​
= GPIO_Speed_Level_1;
gpio_struct.GPIO_Pin
= DEBUG_UART_TX | DEBUG_UART_RX;
GPIO_Init(DEBUG_UART_GPIO,
&gpio_struct);

GPIO_PinAFConfig(DEBUG_UART_GPIO, DEBUG_UART_TX_PINSOURCE, GPIO_AF_7);
GPIO_PinAFConfig(DEBUG_UART_GPIO, DEBUG_UART_RX_PINSOURCE), GPIO_pan_pre>

The interrupt service function is as follows

void PUSHBUTTON_IRQ_HANDLER(void)
{
if(EXTI_GetITStatus(PUSHBUTTON_EXTI_LINE) != RESET)
{
EXTI_ClearITPendingBit(PUSHBUTTON_EXTI_LINE);

printf(
< span style="color: #800000">"Push button clicked!! ");
}
}

The experimental feature has an obvious BUG, that is, the external interrupt may be triggered continuously, which may cause repeated sending of data after a key press.

Experimental phase 3, make a function of accepting the return, turn on the serial port interrupt, and perform the received data in the interrupt Post back

void DEBUG_UART_IRQ_HANDLER(void)
{
if(USART_GetITStatus(DEBUG_UART, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(DEBUG_UART, USART_IT_RXNE);

uint16_t data
= USART_ReceiveData(DEBUG_UART);
USART_SendData(DEBUG_UART, data);
}
}

 tim_timebase_struct.TIM_CounterMode = TIM_CounterMode_Up;< br /> tim_timebase_struct.TIM_ClockDivision = TIM_CKD_DIV1;
tim_timebase_struct.TIM_Prescaler
= DEBUG_UART_TX_TIM_PSC;< br /> tim_timebase_struct.TIM_Period = DEBUG_UART_TX_TIM_PERIOD;
TIM_TimeBaseInit(DEBUG_UART_TX_TIM,
&tim_timebase_struct);

TIM_Cmd(DEBUG_UART_TX_TIM, ENABLE);

TIM_ITConfig(DEBUG_UART_TX_TIM, TIM_IT_Update, ENABLE);< /pre>

 usart_struct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
usart_struct.USART_HardwareFlowControl
= USART_HardwareFlowControl_None;
usart_struct.USART_Parity
= USART_Parity_No;
usart_struct.USART_StopBits
= USART_StopBits_1;
usart_struct.USART_WordLength
= USART_WordLength_8b;
usart_struct.USART_BaudRate
= DEBUG_UART_BAUDRATE;
USART_Init(DEBUG_UART,
&usart_struct);

USART_Cmd(DEBUG_UART, ENABLE);< /span>

void DEBUG_UART_TX_ TIM_IRQ_HANDLER(void)
{
if(TIM_GetITStatus(DEBUG_UART_TX_TIM, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(DEBUG_UART_TX_TIM, TIM_IT_Update);

printf(
"hello ");
}
}

int fputc(int ch, FILE* stream)
{
while(USART_GetFlagStatus(DEBUG_UART, USART_FLAG_TXE) == RESET);

USART_SendData(DEBUG_UART, ch);

return ch;
}

 gpio_struct.GPIO_Mode = GPIO_Mode_IN;
gpio_struct.GPIO_PuPd
= GPIO_PuPd_DOWN;
gpio_struct.GPIO_Speed ​​
= GPIO_Speed_Level_1;
gpio_struct.GPIO_Pin
= PUSHBUTTON_PIN;
GPIO_Init(PUSHBUTTON_GPIO,
&gpio_struct);

SYSCFG_EXTILineConfig(PUSHBUTTON_EXTI_PORTSOURCE, PUSHBUTTON_EXTI_PIN)

 gpio_struct.GPIO_Mode = GPIO_Mode_AF;
gpio_struct.GPIO_OType
= GPIO_OType_PP;
gpio_struct.GPIO_PuPd
= GPIO_PuPd_NOPULL;
gpio_struct.GPIO_Speed ​​
= GPIO_Speed_Level_1;
gpio_struct.GPIO_Pin
= DEBUG_UART_TX | DEBUG_UART_RX;
GPIO_Init(DEBUG_UART_GPIO,
&gpio_struct);

GPIO_PinAFConfig(DEBUG_UART_GPIO, DEBUG_UART_TX_PINSOURCE, GPIO_AF_7);
GPIO_Pin_UART_GPIO, DEBUG_UART, DEBUGS GPIO_AF_7);

void PUSHBUTTON_IRQ_HANDLER( void)
{
if(EXTI_GetITStatus(PUSHBUTTON_EXTI_LINE ) != RESET)
{
EXTI_ClearITPendingBit(PUSHBUTTON_EXTI_LINE);

printf(
"Push button clicked!! " );
}
}

void DEBUG_UART_IRQ_HANDLER(void)
{
if(USART_GetITStatus(DEBUG_UART, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(DEBUG_UART, USART_IT_RXNE);

uint16_t data
= USART_ReceiveData(DEBUG_UART) ;
USART_SendData(DEBUG_UART, data);
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1244 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.