输出字符R,了解中断。
#include "stm32f10x.h" // Device header
#include "Delay.h"
#define RX_BUFFER_SIZE 16
#define TIMER_CLOCK_FREQ 1000000
#define TIMER_OVERFLOW_TIME 12.0
volatile uint32_t timerValue = 0;
volatile uint8_t rxBuffer[RX_BUFFER_SIZE];
volatile uint8_t rxIndex = 0;
volatile uint8_t rxComplete = 0;
void TIM2_Init(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = TIMER_CLOCK_FREQ - 1;
TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock / TIMER_CLOCK_FREQ - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE);
}
void USART2_Init(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
void USART2_SendChar(uint8_t ch) {
while (!(USART2->SR & USART_SR_TXE));
USART_SendData(USART2, ch);
}
void USART2_SendString(char* str) {
while (*str) {
USART2_SendChar(*str++);
}
}
uint32_t GetTimerValue(void) {
uint32_t val = TIM2->CNT;
if (TIM2->SR & TIM_SR_UIF) {
val += TIMER_CLOCK_FREQ;
}
return val;
}
void TIM2_IRQHandler(void) {
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
timerValue++;
if (timerValue % 12 == 0) {
USART2_SendChar('R');
USART2_SendChar('\n');
}
}
}
void USART2_IRQHandler(void) {
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
uint8_t data = USART_ReceiveData(USART2);
if (data != '\n') {
rxBuffer[rxIndex++] = data;
if (rxIndex == RX_BUFFER_SIZE || data == '\r') {
rxBuffer[rxIndex] = '\0';
rxComplete = 1;
rxIndex = 0;
}
}
}
}
int main(void) {
TIM2_Init();
USART2_Init();
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while (1) {
if (rxComplete) {
float interval = (float)timerValue / TIMER_CLOCK_FREQ;
char intervalStr[6];
snprintf(intervalStr, sizeof(intervalStr), "%.1f", interval);
USART2_SendString("P->Q:");
USART2_SendString(intervalStr);
USART2_SendString("s\n");
rxComplete = 0;
timerValue = 0;
} else {
if (timerValue % 12 == 0) {
USART2_SendChar('R');
USART2_SendChar('\n');
}
}
}
}
记录一下今天是2024.3.24 /hfgydx/znkxyjs/2023217~~~/wyt