欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
分享自己做的一個單片機串口助手和測試代碼
[打印本頁]
作者:
qq1459123867
時間:
2022-1-7 13:26
標題:
分享自己做的一個單片機串口助手和測試代碼
分享自己做的一個單片機串口助手和測試代碼 歡迎評論
單片機源程序如下:
#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); //設(shè)置中斷分組,只能設(shè)置一次
//使能串口時鐘
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);
//設(shè)置串口屬性
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(); //設(shè)置搶占優(yōu)先級
//
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)){
//接收數(shù)據(jù)
ch = USART_ReceiveData(USART1);
USART_SendData(USART1, ch);
}
//使能RCC中相應(yīng)的GPIOA的時鐘
*(int *)0x40021018 |= 1 << 2;
//配置相關(guān)的引腳 GPIOA5
//推挽輸出 速度10MHz
*(int *)0x40010800 &= ~(0xf << 20);
*(int *)0x40010800 |= 1 << 20;
while(1){
//開燈
*(int *)0x40010810 |= 1 << 5;
*(int *)0x40010810 &= ~(1 << 21);
delay(1000);
//關(guān)燈
*(int *)0x40010810 &= ~(1 << 5);
*(int *)0x40010810 |= 1 << 21;
delay(1000);
}
}
復(fù)制代碼
#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());
}
復(fù)制代碼
51hei.png
(10.43 KB, 下載次數(shù): 66)
下載附件
2022-1-8 17:39 上傳
源碼下載:
Serial Port.zip
(975.1 KB, 下載次數(shù): 9)
2022-1-7 13:25 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1