分享自己做的一个单片机串口助手和测试代码 欢迎评论
单片机源程序如下:
- #include "stm32f10x.h"
- void delay(int i)
- {
- int x;
- while(i--)
- for(x = 0; x < 1000; x++);
- }
- int main()
- {
- GPIO_InitTypeDef gpio;
- USART_InitTypeDef usart1;
- NVIC_InitTypeDef nvic;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断分组,只能设置一次
-
- //使能串口时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
-
- //配置IO口为串口功能
- //PA9--TX PA10--RX
- gpio.GPIO_Pin = GPIO_Pin_9;
- gpio.GPIO_Speed = GPIO_Speed_10MHz;
- gpio.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &gpio);
-
- gpio.GPIO_Pin = GPIO_Pin_10;
- gpio.GPIO_Speed = GPIO_Speed_10MHz;
- gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &gpio);
-
- //设置串口属性
- usart1.USART_BaudRate = 115200;
- usart1.USART_WordLength = USART_WordLength_8b;
- usart1.USART_StopBits = USART_StopBits_1;
- usart1.USART_Parity = USART_Parity_No;
- usart1.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- usart1.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_Init(USART1, &usart1);
-
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
-
- USART_Cmd(USART1, ENABLE);
-
- //要使用串口中断,那么就需要添加NVIC组
-
- // NVIC_EnableIRQ(); //开启NVIC中断
- // NVIC_SetPriority(); //设置抢占优先级
- //
- nvic.NVIC_IRQChannel = USART1_IRQn;
- nvic.NVIC_IRQChannelPreemptionPriority = 2;
- nvic.NVIC_IRQChannelSubPriority = 2;
- nvic.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&nvic);
-
- while(1);
- }
- void USART1_IRQHandler()
- {
- unsigned char ch;
- if(USART_GetITStatus(USART1, USART_IT_RXNE)){
- //接收数据
- ch = USART_ReceiveData(USART1);
- USART_SendData(USART1, ch);
- }
- //使能RCC中相应的GPIOA的时钟
- *(int *)0x40021018 |= 1 << 2;
-
- //配置相关的引脚 GPIOA5
- //推挽输出 速度10MHz
- *(int *)0x40010800 &= ~(0xf << 20);
- *(int *)0x40010800 |= 1 << 20;
-
- while(1){
- //开灯
- *(int *)0x40010810 |= 1 << 5;
- *(int *)0x40010810 &= ~(1 << 21);
- delay(1000);
- //关灯
- *(int *)0x40010810 &= ~(1 << 5);
- *(int *)0x40010810 |= 1 << 21;
- delay(1000);
- }
- }
复制代码- #include "widget.h"
- #include "ui_widget.h"
- #include <QDebug>
- #include <QMessageBox>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- ser_info = QSerialPortInfo::availablePorts();
- foreach (const QSerialPortInfo &info, ser_info) {
- ui->comboBox->addItem(info.portName());
- qDebug() << info.portName();
- }
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::on_pb_clicked()
- {
- //打开串口
- QString name = ui->comboBox->currentText();
- serial.setPortName(name);
- serial.setBaudRate(QSerialPort::Baud115200);
- serial.setDataBits(QSerialPort::Data8);
- serial.setFlowControl(QSerialPort::NoFlowControl);
- serial.setStopBits(QSerialPort::OneStop);
- serial.setParity(QSerialPort::NoParity);
- if(serial.open(QIODevice::ReadWrite) != true){
- QMessageBox::about(NULL, "警告", "打开串口失败");
- return;
- }
- qDebug() << "serial open success";
- connect(&serial, SIGNAL(readyRead()), this, SLOT(recv_data()));
- }
- void Widget::recv_data()
- {
- ui->lineEdit_2->setText(serial.readAll());
- }
- void Widget::on_pb_send_clicked()
- {
- serial.write(ui->lineEdit->text().toStdString().c_str());
- }
复制代码
源码下载:
Serial Port.zip
(975.1 KB, 下载次数: 9)
|