DSP28335驱动Lcd12864显示Ds18b20采集到的温度,并通过Sci方式传输至PC,使用Matlab制作上位机软件进行数据保存与显示

本文介绍了如何使用DSP28335驱动Lcd12864显示Ds18b20传感器采集的温度,并通过SCI方式将数据传输到PC。利用Matlab2017b的AppDesigner制作上位机软件,实现了串口通信、数据接收、实时绘图和保存功能。示例中展示了DSP端的代码和上位机的App链接,提供了一个简易的数据采集与分析系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一篇文章是我前一篇文章的续集,主要是为了弥补 CCS无法实时捕捉数据至上位机的缺陷(可能CCS有,但是我却没找到,如果有读者知道具体答案,请留言告诉我。)。当然串口传输来的数据也有不足的地方,就是这些数据只能让我们感性的观看,如果想做数据分析,如FFT等,可能数据的采样精度就不够了。所以这也是一个不足的地方,希望日后能有解决的办法。下面开始正文部分吧!

 

废话不多说,先来张效果图:

图1是DSP_demo板加Lcd12864的显示。显示的比较粗糙,026.56就是26.56℃,一些细节没有做好,主要是功能的实现。


图2是使用Matlab2017b的AppDesigner制作的串口通信上位机软件,具体功能有:发送(TX)、接收(RX)、实时绘图、数据实时保存等。其中COM口和波特率需要设置,其他的如:数据位、校验位、停止位等等,已经在代码中默认设置了,因为只是这个小设计的定制版本,也没有高兴将所有功能都全部放置在界面上。

图像中,出现了34℃,那是我用手拿住了 Ds18b20。目前是6月底,江苏黄梅天,今天室温就是27℃左右。


图3是采集到的数据。只是展示了部分,刚开机的时候是没有打开文本存储按钮的,因此第一个数据与图2中接收数据框中前几个数据不是对应的。


                         图1                                                         图2                                                                    图3


效果展示完了,那么接下来就是代码部分:

 

/*
 * 功能:1.DS18B20进行温度采集,并使用12864进行显示
 * 		2.将采集到的温度 通过SCIA传输,因为SCIB的9口被12864占用,所以不能用SCIB
 * 		3.SCIA使用CpuTimer0中断,1s传输一次温度数据给PC,未使用FIFO中断
 * 波特率:9600  8位数据位,1位停止位,无校验位
 */

#include "DSP2833x_Project.h"
#include "math.h"

#include "lcd12864.h"
#include "ds18b20_para.h"

#define uchar  unsigned char

void init_port(void);
uchar Init_DS18B20();
uchar ReadOneChar(void);
void WriteOneChar(uchar dat);
float ReadTemperature();
void lcd_init(void);
void lcd_write_cmd(uchar cmd);			
void lcd_write_dat(uchar dat);			
void LCD12864SetAddress_f( uchar x, uchar y ); 	 //地址转换
void show(uchar x, uchar y, uchar * data);	

void scia_init(void);
void GPIO_init();
interrupt void Timer0_ISR(void);

uchar table[7];

int main(void)
{

	float tt;
	int tt1;

	InitSysCtrl();

	init_port();   //ds18b20 & 12864 端口初始化

	DINT;
	InitPieCtrl();	//初始化中断控制
	IER = 0x0000;
	IFR = 0x0000;
	InitPieVectTable();//初始化中断矢量表

	GPIO_init();  //配置端口为SCI

	EALLOW;        // This is needed to write to EALLOW protected registers
	PieVectTable.TINT0 = &Timer0_ISR;//将定时器0中断服务函数入口放入中断向量表
	EDIS;   // This is needed to disable write to EALLOW protected registers

	InitCpuTimers();
	ConfigCpuTimer(&CpuTimer0, 150, 1000000);	//定时器0定时时间为 1s

	scia_init();	//SCIA端口初始化

	lcd_init();

	PieCtrlRegs.PIECTRL.bit.ENPIE = 1;   // Enable the PIE block
	PieCtrlRegs.PIEIER1.bit.INTx7=1;     // PIE Group 1, INT7
	IER = 0x001;
	EINT;


	CpuTimer0Regs.TCR.bit.TSS = 0;  // 启动定时器0

	while (1)
	{
		tt=ReadTemperature();
		tt1=tt*100+0.5;
		//留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
		//后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
		//算加上0.5,还是在小数点后面。

		table[0]=tt1/10000+0x30;  //百位
		table[1]=tt1%10000/1000+0x30;//十位
		table[2]=tt1%1000/100+0x30;//个位
		table[3]='.';
		table[4]=tt1%100/10+0x30;//十分位;
		table[5]=tt1%10+0x30;//百分位;
		table[6]='\0';  //用来中止一组显示数据
		show(0,0,table);
	}

}

//----------------------
//--- 12864 及  DS18B20 的初始化
//----------------------
void init_port(void)
{
	EALLOW;
		GpioCtrlRegs.GPBPUD.bit.GPIO40 = 0;    // 使能GPIO10 引脚内部上拉

		GpioCtrlRegs.GPBMUX1.bit.GPIO40 =0;   // 配置GPIO10为通用I/O口

		GpioCtrlRegs.GPBQSEL1.bit.GPIO40 = 0;    // GPIO40与系统时钟SYSCLKOUT 同步

		//lcd12864 use
		GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;    // 使能GPIO0 引脚内部上拉

		GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1;   // 禁止GPIO1 引脚内部上拉
		GpioCtrlRegs.GPAQSEL1.all = 0x0000;    // GPIO0-GPIO15与系统时钟SYSCLKOUT 同步

		GpioCtrlRegs.GPADIR.all = 0x003FF;// 配置GPIO0-GPIO9为输出引脚

		//输出数据LCD_RW和LCD_EN清零
		GpioDataRegs.GPADAT.bit.GPIO0 = 1;
		GpioDataRegs.GPADAT.bit.GPIO1 = 0;
    EDIS;
}

//----------------------
//---SCI的初始化
//----------------------
void GPIO_init()
{
	EALLOW;
	GpioCtrlRegs.GPBPUD.bit.GPIO35 = 0;     // Enable pull-up for GPIO35  (SCITXDA)
	GpioCtrlRegs.GPBMUX1.bit.GPIO35 = 1;    // GPIO35 for SCITXDA operation

	GpioCtrlRegs.GPBPUD.bit.GPIO36 = 0;    // Enable pull-up for GPIO36 (SCIRXDA)
	GpioCtrlRegs.GPBQSEL1.bit.GPIO36 = 3;  // Asynch input GPIO36 (SCIRXDA)
	GpioCtrlRegs.GPBMUX1.bit.GPIO36 = 1;   // GPIO36 for SCIRXDA operation

	EDIS;
}



void scia_init(void)
{
	SciaRegs.SCICCR.all =0x0007;    // 1 stop bit,
							   	   // No parity,8 char bits,
							   	   // async mode, idle-line protocol
	SciaRegs.SCICTL1.all =0x0003;   // enable TX, RX, internal SCICLK,
							   	   // Disable RX ERR, SLEEP, TXWAKE

	SciaRegs.SCIHBAUD    =0x0001;
	SciaRegs.SCILBAUD    =0x00E7;   //构成 0x01e7=487 波特率计算为   37.5M/[(487+1)*8]=9605 近似等于9600

	SciaRegs.SCICTL1.bit.SWRESET=1;//Relinquish SCI from Reset

}

interrupt void Timer0_ISR(void)
{
	int i=0;

    while(table[i] != '\0')			//不断发送,直到1组温度数据发送完毕
    {
    	SciaRegs.SCITXBUF=table[i];  //发送数据
    	DELAY_US(1000);				//若不加,会出现数据丢失
    	i++;
    }

    SciaRegs.SCITXBUF='\n';			//每接收一组数据后,换行

   	PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;//PIE组1应答
}

 


以上是DSP上的代码。接下来是上位机代码,供大家学习。这其中的一部分代码,我也是从网上寻找来的,因此在此要感谢那位哥们开放的串口软件。Appdesigner比较新,界面也不错,如果大家喜欢,可以自己做出修改,但是我希望大家在我这个版本基础上修改后,能在留言处开源自己的内容,让大家一起进步。首先将app的链接给出:


链接:https://pan.baidu.com/s/1NEqKQJX87mv86V9ENZ2kkA 
提取码:x3y0 

或  https://download.csdn.net/download/wx_simba/11259531


以下是代码,有Matlab的朋友可以直接用 Appdesigner打开查看。注意,至少需要 2016a版本才具有Appdesigner功能。

***这是回头打的字,插入代码居然没有Matlab的m语言格式。。。还有下面代码看着多,其实估计80%都是系统生成的,我们是无法修改的,实现功能的代码其实不多。
 

classdef app2 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure             matlab.ui.Figure
        pbOpenSerial         matlab.ui.control.StateButton
        RXLabel              matlab.ui.control.Label
        TXLabel              matlab.ui.control.Label
        Label_RX             matlab.ui.control.Label
        Label_TX             matlab.ui.control.Label
        ReceiveView          matlab.ui.control.TextArea
        Button_Send          matlab.ui.control.Button
        transmitView         matlab.ui.control.TextArea
        Button_Clear         matlab.ui.control.Button
        DropDownLabel        matlab.ui.control.Label
        ppCOM                matlab.ui.control.DropDown
        Label                matlab.ui.control.Label
        Label_2              matlab.ui.control.Label
        Lamp                 matlab.ui.control.Lamp
        Label_3              matlab.ui.control.Label
        ppCOM_2              matlab.ui.control.DropDown
        Panel                matlab.ui.container.Panel
        Label_5              matlab.ui.control.Label
        txtStoreSwich        matlab.ui.control.Switch
        Label_6              matlab.ui.control.Label
        txtStoreFlag         matlab.ui.control.Lamp
        UIAxes               matlab.ui.control.UIAxes
        UartsEditFieldLabel  matlab.ui.control.Label
        UartsEditField       matlab.ui.control.NumericEditField
    end

    
    properties (Access = public)
        COM;    % 端口号
        Baud_Rate; %波特率
        s  ;    %端口设置句柄
        RX_num; %接收统计
        TX_num; %发送统计
        RX_once;%一次接收的数据
        RX_Date;%接收的所有数据
        is_open;%是否打开串口标志位
        Flag_i %绘图横坐标的点
        
    end
    
    
    
    methods (Access = public)
        function  EveBytesAvailableFcn(app, src, event)
            n = src.BytesAvailable;%获取接收到的字符个数
            if n>0%n>0才继续执行,因为0也会触发中断
                app.RX_num=app.RX_num+n;
                app.Label_RX.Text=num2str(app.RX_num);%将数字转化为字符串输出
                app.RX_once=fread(src,n,'uchar');%读取函数,读取后矩阵为一列
                app.RX_Date =strcat(app.RX_Date, app.RX_once');%字符串拼接,需要转置化为一行
                app.ReceiveView.Value= app.RX_Date;%textarea的设置格式为cell,或单行字符串

                app.Flag_i=app.Flag_i+1;
                t=app.Flag_i*app.UartsEditField.Value;
                y=str2double(char(app.RX_once'));    
                plot(app.UIAxes,t,y,'marker','+','linewidth',5,'linestyle','--');
                hold(app.UIAxes,'on');
                
                if strcmp(app.txtStoreSwich.Value,'On')
                    app.txtStoreFlag.Color=[0 1 0];
                    name_ref=['wx',datestr(now,29),'.txt']; % name_ref=['wx',datestr(now,29),'.xls'];
                    fid = fopen(name_ref,'a'); %fid = fopen('c.xls','a');
                    fprintf(fid,'%s\r\n',app.RX_once');  %需要转置,进行换行处理,txt换行 要\r\n..xls只\n就可以换行
                    fclose(fid);
                else
                    app.txtStoreFlag.Color=[0 0 0];
                end
            end
        end

        
    end
    
    

    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.RX_num=0;
            app.TX_num=0;
            app.is_open=0;
            app.Flag_i=0;
            scoms = instrfind;%获取占用的串口号
            if scoms ~= 0%如果存在则关闭,否则不能打开
                stopasync(scoms);
                fclose(scoms);
                delete(scoms);
            end
            
            
        end

        % Button pushed function: Button_Send
        function Button_SendPushed(app, event)
            val=app.transmitView.Value;
            if length(val{1})>0%textarea控件是cell格式,获取需要用{1}
                app.TX_num=app.TX_num+length(val{1});
                app.Label_TX.Text=num2str(app.TX_num);
                fwrite(app.s, char(val), 'uint8', 'async');%需要将val转化为char
            end
        end

        % Value changed function: pbOpenSerial
        function pbOpenSerialValueChanged2(app, event)
            
            app.COM=get(app.ppCOM,'Value');
            app.Baud_Rate=get(app.ppCOM_2,'Value');
            if strcmp(get(app.pbOpenSerial,'Text'),'打开串口')
                try
                    app.s=serial(app.COM);
                    app.s.BaudRate=str2double(app.Baud_Rate);%设置波特率,str2double 很重要
                    app.s.DataBits=8;%设置数据长度
                    app.s.StopBits=1;%设置停止位长度
                    app.s.InputBufferSize=1024000;%设置输入缓冲区大小为1M

                    app.s.BytesAvailableFcnMode='byte';  %串口事件回调设置
                    %  app.s.Terminator='CR/LF';
                    app.s.BytesAvailableFcnCount=1; %输入缓冲区存在10个字节触发回调函数
                    app.s.BytesAvailableFcn={@app.EveBytesAvailableFcn};%回调函数的指定
                    
                    fopen(app.s);%打开串口
                    app.is_open=1;
                    app.pbOpenSerial.Text='关闭串口';
                    app.Lamp.Color=[0 1 0];
                catch err
                    msgbox('打开失败');
                end
            else
                try
                    fclose(app.s);
                    app.pbOpenSerial.Text='打开串口';
                    app.Lamp.Color=[0.15 0.15 0.15];
                catch err
                    msgbox('关闭失败');
                end
                delete(app.s);
                app.is_open=0;
            end
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            delete(app)
            
        end

        % Button pushed function: Button_Clear
        function Button_ClearPushed(app, event)
            app.RX_Date ='';
            app.ReceiveView.Value= app.RX_Date;
            app.TX_num=0;
            app.RX_num=0;
            app.Label_TX.Text=num2str(app.TX_num);
            app.Label_RX.Text=num2str(app.RX_num);
            cla(app.UIAxes,'reset');
        end

        % Value changed function: txtStoreSwich
        function txtStoreSwichValueChanged(app, event)
            value = app.txtStoreSwich.Value;
            if strcmp(value,'On')
                app.txtStoreFlag.Color=[0 1 0];
            else
                app.txtStoreFlag.Color=[0 0 0];
            end
        end
    end

    % App initialization and construction
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure
            app.UIFigure = uifigure;
            app.UIFigure.Position = [500 300 895 521];
            app.UIFigure.Name = 'UI Figure';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create pbOpenSerial
            app.pbOpenSerial = uibutton(app.UIFigure, 'state');
            app.pbOpenSerial.ValueChangedFcn = createCallbackFcn(app, @pbOpenSerialValueChanged2, true);
            app.pbOpenSerial.Text = '打开串口';
            app.pbOpenSerial.FontSize = 16;
            app.pbOpenSerial.FontWeight = 'bold';
            app.pbOpenSerial.Position = [740 311 81 27];

            % Create RXLabel
            app.RXLabel = uilabel(app.UIFigure);
            app.RXLabel.Position = [42 60 25 15];
            app.RXLabel.Text = 'RX:';

            % Create TXLabel
            app.TXLabel = uilabel(app.UIFigure);
            app.TXLabel.Position = [102 60 25 15];
            app.TXLabel.Text = 'TX:';

            % Create Label_RX
            app.Label_RX = uilabel(app.UIFigure);
            app.Label_RX.Position = [66 60 37 15];
            app.Label_RX.Text = '0';

            % Create Label_TX
            app.Label_TX = uilabel(app.UIFigure);
            app.Label_TX.Position = [126 60 82 15];
            app.Label_TX.Text = '0';

            % Create ReceiveView
            app.ReceiveView = uitextarea(app.UIFigure);
            app.ReceiveView.Position = [25 192 161 270];

            % Create Button_Send
            app.Button_Send = uibutton(app.UIFigure, 'push');
            app.Button_Send.ButtonPushedFcn = createCallbackFcn(app, @Button_SendPushed, true);
            app.Button_Send.FontSize = 16;
            app.Button_Send.FontWeight = 'bold';
            app.Button_Send.Position = [716 96 100 27];
            app.Button_Send.Text = '发送';

            % Create transmitView
            app.transmitView = uitextarea(app.UIFigure);
            app.transmitView.Position = [27 88 159 60];

            % Create Button_Clear
            app.Button_Clear = uibutton(app.UIFigure, 'push');
            app.Button_Clear.ButtonPushedFcn = createCallbackFcn(app, @Button_ClearPushed, true);
            app.Button_Clear.FontSize = 16;
            app.Button_Clear.FontWeight = 'bold';
            app.Button_Clear.Position = [716 49 100 27];
            app.Button_Clear.Text = '清空';

            % Create DropDownLabel
            app.DropDownLabel = uilabel(app.UIFigure);
            app.DropDownLabel.HorizontalAlignment = 'right';
            app.DropDownLabel.FontName = 'Calibri';
            app.DropDownLabel.FontSize = 18;
            app.DropDownLabel.Position = [684 422 41 24];
            app.DropDownLabel.Text = '串口';

            % Create ppCOM
            app.ppCOM = uidropdown(app.UIFigure);
            app.ppCOM.Items = {'COM1', 'COM2', 'COM3', 'COM4'};
            app.ppCOM.FontName = 'Calibri';
            app.ppCOM.FontSize = 18;
            app.ppCOM.Position = [740 424 76 25];
            app.ppCOM.Value = 'COM1';

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.FontSize = 18;
            app.Label.FontWeight = 'bold';
            app.Label.Position = [39 475 42 23];
            app.Label.Text = '接收';

            % Create Label_2
            app.Label_2 = uilabel(app.UIFigure);
            app.Label_2.FontSize = 18;
            app.Label_2.FontWeight = 'bold';
            app.Label_2.Position = [39 151 42 23];
            app.Label_2.Text = '发送';

            % Create Lamp
            app.Lamp = uilamp(app.UIFigure);
            app.Lamp.Position = [695 317 20 20];
            app.Lamp.Color = [0.149 0.149 0.149];

            % Create Label_3
            app.Label_3 = uilabel(app.UIFigure);
            app.Label_3.HorizontalAlignment = 'right';
            app.Label_3.FontName = 'Calibri';
            app.Label_3.FontSize = 18;
            app.Label_3.Position = [682 373 59 24];
            app.Label_3.Text = '波特率';

            % Create ppCOM_2
            app.ppCOM_2 = uidropdown(app.UIFigure);
            app.ppCOM_2.Items = {'300', '600', '1200', '2400', '4800', '9600', '19200', '38400', '115200'};
            app.ppCOM_2.FontName = 'Calibri';
            app.ppCOM_2.FontSize = 18;
            app.ppCOM_2.Position = [756 375 60 25];
            app.ppCOM_2.Value = '9600';

            % Create Panel
            app.Panel = uipanel(app.UIFigure);
            app.Panel.Position = [655 173 231 87];

            % Create Label_5
            app.Label_5 = uilabel(app.Panel);
            app.Label_5.HorizontalAlignment = 'center';
            app.Label_5.Position = [136 24 77 15];
            app.Label_5.Text = '文本存储按钮';

            % Create txtStoreSwich
            app.txtStoreSwich = uiswitch(app.Panel, 'slider');
            app.txtStoreSwich.ValueChangedFcn = createCallbackFcn(app, @txtStoreSwichValueChanged, true);
            app.txtStoreSwich.Position = [151 54 45 20];

            % Create Label_6
            app.Label_6 = uilabel(app.Panel);
            app.Label_6.HorizontalAlignment = 'right';
            app.Label_6.Position = [26 24 77 15];
            app.Label_6.Text = '文本存储指示';

            % Create txtStoreFlag
            app.txtStoreFlag = uilamp(app.Panel);
            app.txtStoreFlag.Position = [52 45 29 29];
            app.txtStoreFlag.Color = [0 0 0];

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            title(app.UIAxes, 'Figure')
            xlabel(app.UIAxes, 'time')
            ylabel(app.UIAxes, 'Y')
            app.UIAxes.LineStyleOrder = {'- '};
            app.UIAxes.NextPlot = 'add';
            app.UIAxes.Position = [197 46 445 438];

            % Create UartsEditFieldLabel
            app.UartsEditFieldLabel = uilabel(app.UIFigure);
            app.UartsEditFieldLabel.HorizontalAlignment = 'right';
            app.UartsEditFieldLabel.Position = [262 18 121 15];
            app.UartsEditFieldLabel.Text = 'Uart发送数据的周期/s';

            % Create UartsEditField
            app.UartsEditField = uieditfield(app.UIFigure, 'numeric');
            app.UartsEditField.Limits = [0 Inf];
            app.UartsEditField.Position = [398 14 49 22];
            app.UartsEditField.Value = 1;
        end
    end

    methods (Access = public)

        % Construct app
        function app = app2

            % Create and configure components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值