赞
踩
今天研究了半天终于把stm32的串口能搞出来了,然后顺便写了一个qt的上位机来控制小灯
效果就是如下:
qt上位机:
说难吧也不难,就是探索的过程费时间很费力气,但同时锻炼了解决问题的能力,很多人的博客都是基于他们会的基础上写的,所以很多东西你可能入门的话看不懂,学习就是这样,入门很难,我拿到板子当时都不知道咋样串口通信,咋样把代码写进去。
这其中的快乐酸楚只有体会过才懂
usart.h:
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
#include <stdio.h>
void usart_init(void);
void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data);
void USARTSendStr(USART_TypeDef* USARTx, char* str);
#endif
usart.c:
#include "usart.h" #include "stm32f10x.h" void usart_init() { GPIO_InitTypeDef gpioInitStructure; USART_InitTypeDef usartInitStructure; NVIC_InitTypeDef nvicInitStructure; //1.配置时钟 //1.1配置GPIOA、串口时钟、复用时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //2.配置GPIOA结构体 //2.1 A9 TX gpioInitStructure.GPIO_Mode = GPIO_Mode_AF_PP; gpioInitStructure.GPIO_Pin = GPIO_Pin_9; gpioInitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &gpioInitStructure); //2.2 A10 RX gpioInitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; gpioInitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &gpioInitStructure); //3.配置串口结构体 usartInitStructure.USART_BaudRate = 115200; usartInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usartInitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; usartInitStructure.USART_Parity = USART_Parity_No; usartInitStructure.USART_StopBits = USART_StopBits_1; usartInitStructure.USART_WordLength = USART_WordLength_8b; //3.1 初始化串口 USART_Init(USART1, &usartInitStructure); //3.2 配置串口中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //3.3 使能串口 USART_Cmd(USART1, ENABLE); //4.配置NVIC中断优先级 //4.1 配置优先级组 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //4.2 配置NVIC优先级结构体 nvicInitStructure.NVIC_IRQChannel = USART1_IRQn; nvicInitStructure.NVIC_IRQChannelPreemptionPriority = 1; nvicInitStructure.NVIC_IRQChannelSubPriority = 1; nvicInitStructure.NVIC_IRQChannelCmd = ENABLE; //4.3 初始化NVIC优先级 NVIC_Init(&nvicInitStructure); } void USARTSendByte(USART_TypeDef* USARTx, uint16_t Data) { USART_SendData( USARTx, Data); while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET ); } void USARTSendStr(USART_TypeDef* USARTx, char* str) { uint16_t i=0; while( *(str+i) != '\0' ) { /* USART_SendData( USARTx, *(str+i)); while( USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET ); */ USARTSendByte( USART1, *(str+i) ); i++; } while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET ); }
led.h:
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h" // Device header
void Led_Init();
#endif
led.c:
#include "led.h" // Device header
void Led_Init(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
main.c:
#include "stm32f10x.h" #include "led.h" #include "usart.h" void delay(uint16_t time) { uint16_t i=0; while(time--) { i=10000; while(i--); } } int main() { usart_init(); Led_Init(); //GPIO_SetBits(GPIOA, GPIO_Pin_0); while(1) { } } //5. 搭建串口中断服务函数 void USART1_IRQHandler(void) { char temp; if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET ) { temp=USART_ReceiveData(USART1); if(temp=='1') { GPIO_SetBits(GPIOA, GPIO_Pin_0); USARTSendStr(USART1, "LED is open"); } else if(temp=='2') { GPIO_ResetBits(GPIOA, GPIO_Pin_0); USARTSendStr(USART1, "LED is close"); } } }
qt上位机关键代码:
void SerialLedCtr::on_pushButton_clicked() { QString strButFlag = ((QPushButton*)sender()) -> text(); if(strButFlag == "开") { ui ->pushButton -> setText("关"); ui->led1p->setPixmap(QPixmap(":/new/img/redled.png")); //判断串口数据 if(qSerPort.write("1") == -1){ ui->pT_MesageDis->setStyleSheet("color:red"); ui->pT_MesageDis->appendPlainText("发送失败,串口未发现!!!"); } else { ui->pT_MesageDis->setStyleSheet("color:green"); qSerPort.write("1"); } } else { ui->pushButton -> setText("开"); ui->led1p->setPixmap(QPixmap(":/new/img/blueled.png")); //判断串口数据 if(qSerPort.write("2") == -1){ ui->pT_MesageDis->setStyleSheet("color:red"); ui->pT_MesageDis->appendPlainText("发送失败,串口未发现!!!"); } else { ui->pT_MesageDis->setStyleSheet("color:green"); qSerPort.write("2"); } } }
需要的可以联系卫星,zrccode
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。