實現STM32開發板向計算機傳送數據就需要準備好STM32開發板和上位接收程序。
上位機部分使用QT開發,版本為5.8.0
STM32部分使用STM32F429芯片,開發環境為uVision V5.24.2.0
上位機效果為:
代碼如下:
mainwindow.h文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include #include #include #include namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_clearButton_clicked(); void on_sendButton_clicked(); void on_openButton_clicked(); void Read_Data(); private: Ui::MainWindow *ui; QSerialPort *serial; }; #endif // MAINWINDOW_H mainwindow.cpp文件: #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //查找可用的串口 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { QSerialPort serial; serial.setPort(info); if(serial.open(QIODevice::ReadWrite)) { ui -> PortBox ->addItem(serial.portName()); serial.close(); } } //設置波特率下拉菜單默認顯示第三項 ui -> BaudBox -> setCurrentIndex(3); //關閉發送按鈕的使能 ui -> sendButton -> setEnabled(false); qDebug() << tr("界面設定成功!"); } MainWindow::~MainWindow() { delete ui; } //清空接收窗口 void MainWindow::on_clearButton_clicked() { ui -> textEdit -> clear(); } void MainWindow::on_sendButton_clicked() { serial -> write(ui -> textEdit_2 -> toPlainText().toLatin1()); } void MainWindow::Read_Data() { QByteArray buf; buf = serial -> readAll(); if(!buf.isEmpty()) { QString str = ui -> textEdit -> toPlainText(); str += tr(buf); ui -> textEdit -> clear(); ui -> textEdit -> append(str); } buf.clear(); } void MainWindow::on_openButton_clicked() { if(ui -> openButton -> text() == tr("打開串口")) { serial = new QSerialPort; //設置串口名 serial -> setPortName(ui -> PortBox -> currentText()); //打開串口 serial -> open(QIODevice::ReadWrite); //設置波特率 serial -> setBaudRate(ui -> BaudBox -> currentText().toInt()); //設置數據位 switch(ui -> BitNumBox -> currentIndex()) { case 8: serial -> setDataBits(QSerialPort::Data8); break; default: break; } //設置奇偶校驗位 switch(ui -> ParityBox -> currentIndex()) { case 0: serial -> setParity(QSerialPort::NoParity); break; default: break; } //設置停止位 switch(ui -> StopBox -> currentIndex()) { case 1: serial -> setStopBits(QSerialPort::OneStop); break; case 2: serial -> setStopBits(QSerialPort::TwoStop); break; default: break; } //設置控制流 serial -> setFlowControl(QSerialPort::NoFlowControl); //關閉設置菜單使能 ui -> PortBox -> setEnabled(false); ui -> BaudBox -> setEnabled(false); ui -> BitNumBox -> setEnabled(false); ui -> ParityBox -> setEnabled(false); ui -> StopBox -> setEnabled(false); ui -> openButton -> setText(tr("關閉串口")); ui -> sendButton -> setEnabled(false); } else { //關閉串口 serial -> clear(); serial -> close(); serial -> deleteLater(); //恢復設置使能 ui -> PortBox -> setEnabled(true); ui -> BaudBox -> setEnabled(true); ui -> BitNumBox -> setEnabled(true); ui -> ParityBox -> setEnabled(true); ui -> StopBox -> setEnabled(true); ui -> openButton -> setText(tr("打開串口")); ui -> sendButton -> setEnabled(true); } //連接信號槽 QObject::connect(serial, &QSerialPort::readyRead, this, &MainWindow::Read_Data); } main.c文件為: #include "mainwindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } mainwidow.ui為: STM32硬件驅動程序代碼如下: main.c文件: #include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" int main(void) { u8 t; u8 len; u16 times = 0; Stm32_Clock_Init(360, 25, 2, 8); delay_init(180); uart_init(90, 115200); LED_Init(); while(1) { if(USART_RX_STA & 0x8000) { len = USART_RX_STA & 0x3fff; printf("rn You send information is: rn"); for(t = 0; t < len; t++) { USART1 -> DR = USART_RX_BUF[t]; while(0 == (USART1 -> SR & 0x40)); } printf("rnrn"); USART_RX_STA = 0; } else { times++; if(0 == times % 5000) { printf("rnALIENTEK MXX STM32F4/F7 develop board Serial testrn"); printf("mengxiangxing@MXXrnrnrn"); } if(0 == times % 200) { printf("Please input data and end with ENTERrn"); } if(0 == times % 30) { LED0 = !LED0; } delay_ms(10); } } } 串口初始化代碼: void uart_init(u32 pclk2,u32 bound) { float temp; u16 mantissa; u16 fraction; temp=(float)(pclk2*1000000)/(bound*16); mantissa=temp; fraction=(temp-mantissa)*16; mantissa<<=4; mantissa+=fraction; RCC->AHB1ENR|=1<<0; RCC->APB2ENR|=1<<4; GPIO_Set(GPIOA,PIN9|PIN10,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU); GPIO_AF_Set(GPIOA,9,7); //PA9,AF7 GPIO_AF_Set(GPIOA,10,7);//PA10,AF7 //2¨ì??êéè?? USART1->BRR=mantissa; USART1->CR1&=~(1<<15); USART1->CR1|=1<<3; #if EN_USART1_RX USART1->CR1|=1<<2; USART1->CR1|=1<<5; MY_NVIC_Init(3,3,USART1_IRQn,2); #endif USART1->CR1|=1<<13; } 由此變簡單的實現了STM32向PC機發送數據程序。
上一篇:STM32F4之按鍵(二)
下一篇:ARM - STM32 使用11.0592MHz晶振
推薦閱讀
史海拾趣
BOOKLY公司非常重視人才培養和團隊建設。公司不僅為員工提供優厚的福利待遇和廣闊的職業發展空間,還積極開展各種培訓和學習活動,提升員工的專業技能和綜合素質。同時,BOOKLY還注重團隊文化建設,倡導團結協作、創新進取的精神。這些舉措使得BOOKLY的團隊凝聚力不斷增強,為公司的發展提供了有力的人才保障。
在電子行業快速發展的背景下,E. Dold & S?hne KG公司始終緊跟科技潮流,致力于技術創新。公司投入大量資源研發新型電子元件和系統,以滿足市場對于高性能、高可靠性產品的需求。通過不斷的技術創新,E. Dold & S?hne KG公司在行業內樹立了良好的口碑,成為眾多知名企業的合作伙伴。
在E. Dold & S?hne KG公司的發展歷程中,品質始終是公司最重視的方面之一。公司嚴格把控原材料采購、生產過程和產品檢驗等環節,確保每一件產品都符合高標準的質量要求。這種對品質的執著追求使得E. Dold & S?hne KG公司的產品在市場上贏得了廣泛的認可和信任。
隨著產品線的逐漸豐富和技術的不斷進步,微芯生物開始積極拓展市場。他們與多家國內外醫藥企業建立了合作關系,將自主研發的藥物推向市場。同時,公司也積極參與各類學術會議和展覽,提升品牌知名度和市場影響力。通過不懈的努力,微芯生物逐漸獲得了市場的認可和信賴。
在21世紀初,由一群資深的留學歸國科學家組成的團隊在深圳創立了微芯生物。他們專注于創新藥物研發,特別是在小分子藥物領域。初創期,公司面臨技術難題和資金壓力,但他們憑借對技術的執著和深厚的專業背景,成功研發出多款具有突破性的新藥候選物,為公司的發展奠定了堅實的基礎。
面對國內市場的飽和,BOOKHAM公司決定實施國際化戰略,拓展海外市場。公司先后在多個國家和地區設立了分支機構,積極參加國際電子展會和交流活動,與國際同行建立起了廣泛的合作關系。通過不斷的努力,BOOKHAM公司的產品逐漸打入國際市場,公司的知名度和影響力也得到了顯著提升。
我的連線方式是:板子+TTL轉RS232+RS232轉USB+PC。 “+”代表連接,這樣轉換會有什么問題么,為什么收不到數據呢。 直接將TTL轉232接到PC機上就能收到,并且RS232轉USB的線肯定是好的,希望懂得朋友指點迷津,謝謝了… 查看全部問答∨ |
|
為何總提示要在.NET Frame 1.1下安裝,可我的系統(winxp)已經裝了 microsoft visual studio 2005 和 microsoft .net Framework SDK V2.0 ???… 查看全部問答∨ |
請問下 用到EDB數據庫編程時 在“VS2005|項目|屬性|屬性配置|C/C++|預處理器”是怎樣設置的。 我的做法是在后面加了“EDB”,但出現好多錯誤: error C2065: \'CEPROPSPEC\' : undeclared identifier ……… 查看全部問答∨ |
我用的是2440+ce5.0,是優龍的BSP,后來發現原來用的是UART1作為調試串口,而UART0用做功能串口。現在我把bootloader里面和BSP下面的相應的UART0和UART1的地址定義交換了,發現UART0是可以做調試串口了,但是UART1卻無法收發數據。在platform.reg下 ...… 查看全部問答∨ |
我想從頭自學嵌入式,就在網上買了套MINI2440開發板,那個店主說調試要用到并口,我的電腦沒有并口,一定要買那個JLINK嗎,要180元,好貴。。。… 查看全部問答∨ |
常見ecc 源碼中: static const u_char nand_ecc_precalc_table[] = { 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00, 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x ...… 查看全部問答∨ |