- 灵动微MM32F003硬件最小系统 --- 串口中断服务程序验证
- 灵动微MM32F003硬件最小系统 --- 串口中断服务程序发送
- 灵动微MM32F003硬件最小系统 --- 串口循环发送,中断接
- 灵动微MM32F003硬件最小系统 --- GPIO外部中断例程
- 灵动微MM32F003硬件最小系统 --- 串口DMA循环发送接
- 灵动微MM32F003硬件最小系统 --- 串口循环发送接收,变
- 灵动微MM32F003硬件最小系统 --- GPIO OUTPU
- 灵动微MM32F003硬件最小系统 --- GPIO INPUT
- 灵动微MM32F003硬件最小系统 --- keil mdk编译
- 灵动微MM32F003硬件最小系统 --- PWM例程解析【17
本例程示范了利用串口中断服务程序进行串口数据的接受与发送。从电脑接收数据,当接收到\n
字符后,将接收的所有数据,通过TX发送给电脑。
1 串口GPIO的初始化
void Uart_ConfigInit(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
UART_InitTypeDef UART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_3); //RX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_4); //TX
UART_InitStructure.UART_BaudRate = bound;
UART_InitStructure.UART_WordLength = UART_WordLength_8b;
UART_InitStructure.UART_StopBits = UART_StopBits_1;
UART_InitStructure.UART_Parity = UART_Parity_No;
UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
UART_Init(UART, &UART_InitStructure);
UART_ITConfig( UART, UART_IT_RXIEN, ENABLE);
UART_Cmd(UART, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
2 串口中断服务程序
void NVIC_ConfigInit(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = UART_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0;
NVIC_Init(& NVIC_InitStruct);
}
void UART2_IRQHandler(void)
{
if(UART_GetITStatus(UART, UART_IT_RXIEN) == SET) { //check receive status
UART_ClearITPendingBit(UART, UART_IT_RXIEN);
printBuf[rx_cnt % 100] = UART_ReceiveData(UART);
if( printBuf[rx_cnt % 100] == '\n') { //check '\n',and set send flag;
tx_flag = 1;
pTx = printBuf;
}
rx_cnt++;
}
if(UART->IER & UART_IT_TXIEN) { //judge send empty interrupt enable
if((UART_GetITStatus(UART, UART_IT_TXIEN) == SET)) {
UartSendByte(*pTx++);
UART_ClearITPendingBit(UART, UART_IT_TXIEN);
rx_cnt--;
if(rx_cnt == 0) {
UART_ITConfig( UART, UART_IT_TXIEN, DISABLE);
tx_flag = 0;
}
}
}
}
刚开始例程不能正常工作,检查后发现官方例程中断服务子程序名称为UART_IRQHandler
,改为UART2_IRQHandler
,就可以正常实现收发功能了。
3 main主程序流程
int main(void)
{
InitSystick();
RCC_ConfigInit();
GPIO_ConfigInit();
NVIC_ConfigInit();
Uart_ConfigInit(9600);
UartSendGroup((u8*)printBuf, sprintf(printBuf, "sprintf ok\r\n")); //pintf stdio data
UartSendAscii("UartSendAscii\r\n"); //printf string
UartSendAscii("Please input data ,end with '\\n' \r\n");
while(1) {
if(tx_flag != 0) {
UART_ITConfig( UART, UART_IT_TXIEN, ENABLE); //clear send flag;
}
}
}
官方例程的修正已基本完成,需要程序的同学可以到百度网盘下载。
提取码:0o5i
网友评论