STM32F4之串口(三)

版权声明:本文为博主原创文章,转载请注明原创链接! https://blog.csdn.net/qq_34706280/article/details/83823248

实现STM32开发板向计算机传送数据就需要准备好STM32开发板和上位接收程序。
上位机部分使用QT开发,版本为5.8.0
STM32部分使用STM32F429芯片,开发环境为uVision V5.24.2.0

上位机效果为:
在这里插入图片描述

代码如下:
mainwindow.h文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

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 <QApplication>

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("\r\n You send information is: \r\n");
			for(t = 0; t < len; t++)
			{
				USART1 -> DR = USART_RX_BUF[t];
				while(0 == (USART1 -> SR & 0x40));
			}
			printf("\r\n\r\n");
			USART_RX_STA = 0;
		}
		else
		{
			times++;
			if(0 == times % 5000)
			{
				printf("\r\nALIENTEK MXX STM32F4/F7 develop board Serial test\r\n");
				printf("mengxiangxing@MXX\r\n\r\n\r\n");
			}
			if(0 == times % 200)
			{
				printf("Please input data and end with ENTER\r\n");
			}
			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  	   
	//²¨ÌØÂÊÉèÖÃ
 	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机发送数据程序。

猜你喜欢

转载自blog.csdn.net/qq_34706280/article/details/83823248