基於CC2530的溫溼度傳感器和基於C#的應用界面設計

1.問題提出

1.1、如何編寫溫溼度傳感器實驗程序

1.2、如何編寫串口通信代碼

1.3、如何利用c#的Serialport去獲取串口傳輸的數據並完成上層應用界面設計

2.功能要求

2.1、設置溫度報警值爲26度,達到報警值時,紅色閃亮,當低於報警值時,停止閃亮,同時,按下按鈕,也可以解除報警;

2.2、將溫溼度採集通過串口發送到電腦,上層用串口小助手查看,或者用VC等編程語言讀取串口數據。

3.實驗準備

3.1、IAR 安裝

3.2、USB 轉串口驅動安裝

3.3、ZigBee 仿真器驅動安裝

3.4、USBmini數據線、物聯網基礎實驗箱、下載器

4.實驗結果
4.1總體設計

4.2實驗結果

描述:當溫度高於26攝氏度時,Led閃爍警示,按下按鍵之後,報警解除,Led熄滅
4.3功能模塊代碼實現
void inttail(void)
{
	P1DIR |=0x03;
	PLED=0;
}
static void delay( uint32 d )
{
	uint32 i,j;
	for( i = 0; i < d; i ++ )
	{
		j = 1000;
		while( j-- );
	}
}
void Init_IO_AND_LED(void)
{    
    P0INP &= ~0X0c; //配置P0口上拉下拉情況
    P1INP &= ~0X40; //配置P1口上拉下拉情況
    P0IEN |= 0X40;  //P06爲中斷模式
    PICTL |= 0X02;  //下降沿
    EA = 1;
    IEN1 |= 0X20;   // P0IE = 1;開啓P0口中斷

    P0IFG |= 0x00;
};
void initUARTtest(void)
{
    CLKCONCMD &= ~0x40;              //晶振
    while(!(SLEEPSTA & 0x40));      //等待晶振穩定
    CLKCONCMD &= ~0x47;             //TICHSPD128分頻,CLKSPD不分頻
    SLEEPCMD |= 0x04; 		    //關閉不用的RC振盪器

    PERCFG = 0x00;				//位置1 P0口
    P0SEL = 0x3c;				//P0用作串口
    P2DIR &= ~0XC0;                             //P0優先作爲串口0

    U0CSR |= 0x80;				//UART方式
    U0GCR |= 10;				//baud_e
    U0BAUD |= 216;				//波特率設爲57600
    UTX0IF = 0;
}
</pre><pre name="code" class="cpp">/****************************************************************
*函數功能 :串口發送字符串函數								
****************************************************************/
void UartTX_Send_String(char *Data,int len)
{
	int j;
	for(j=0;j<len;j++)
	{
		U0DBUF = *Data++;
		while(UTX0IF == 0);
		UTX0IF = 0;
	}
}
void main(void)
{
	uint16 sht;
    float sht11;
	intClock(); 
	P2DIR |= 0x01;        //打開電源
	P2 |= 0x01;  
    initUARTtest();
    SHT1X_INT();
	ugOled9616int();      //初始化OLed配置   
    inttail();
    Init_IO_AND_LED();
    delay(10); 
	while(1)
	{       
		char tempBuf[5];
        sht = Read_SHT1X(3);
		sht&=0x3fff;
        sht11 = (float)(sht*0.01) - 39.60;
        sprintf( (void *)tempBuf,"%f",sht11);
        LcdPutString16_8(0,0,(uint8*)tempBuf,5,1);
        delay(10);
        if(m==0)
		{
            if(sht11>=29)
            {
              PLED=!PLED;
            }
            else PLED=1;
        }
        if(sht11<29) 
			m=0;
        char humBuf[6];
        sht = Read_SHT1X(5);
        sht&=0x0fff;
        sht11 = (float)(sht*0.0405) - 4 - (sht * sht * 0.000028); 
        sprintf( (void *)humBuf,"%f",sht11);
        humBuf[5] = 37;//%
        LcdPutString16_8(48,0,(uint8*)humBuf,6,1);
        delay(10);
        UartTX_Send_String(tempBuf,5);
        delay(10);       //延時
        delay(10);
        UartTX_Send_String(humBuf,6); //串口發送數據 //串口發送數據
        delay(10);       //延時
        delay(10);
        delay(10);
    }
}
#pragma vector = P1INT_VECTOR
 __interrupt void P1_ISR(void)
{
	if(P0IFG>0)         //按鍵中斷
	{
		P0IFG = 0;
		PLED = 1;
		m=1;
		delay(10);
	}
	P0IF = 0;          //清中斷標誌
 }

基於c#的應用界面簡單設計

代碼實現

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CommAssist
{
    public partial class Form1 : Form
    {
        SerialPort Comm = new SerialPort("COM3", 57600, Parity.None, 8, StopBits.One);      //初始化串口設置
        public delegate void Displaydelegate(byte[] InputBuf);
        Byte[] OutputBuf = new Byte[128];
        public Displaydelegate disp_delegate;
        public Form1()
        {
            
            disp_delegate = new Displaydelegate(DispUI);
            InitializeComponent();
            comboBox1.SelectedItem = "COM3";
            Comm.DataReceived += new SerialDataReceivedEventHandler(Comm_DataReceived);
            try
            {
                Comm.Open();
            }
            catch 
            {
                MessageBox.Show("你選擇的串口被佔用或不存在,請重新選擇");
                OpenSerialPort.Text = "打開串口";
                textBox1.Text = " ";
                textBox2.Text = " ";
            }
     }


        /**
         *顯示溫度,溼度的值 
         **/
        public void DispUI(byte[] InputBuf)
        {
            //textBox1.Text = Convert.ToString(InputBuf);

            ASCIIEncoding encoding = new ASCIIEncoding();
            if (encoding.GetString(InputBuf).Contains("%"))
            {
                textBox1.Text = encoding.GetString(InputBuf);
            }
            else
            {
                textBox2.Text = encoding.GetString(InputBuf);
            }
        }


        /**
         *獲取緩衝區數據
         **/
        void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            
            Byte[] InputBuf = new Byte[128];

            try
            {               
                Comm.Read(InputBuf,0,Comm.BytesToRead);                                //讀取緩衝區的數據直到“}”即0x7D爲結束符
                //InputBuf = UnicodeEncoding.Default.GetBytes(strRD);             //將得到的數據轉換成byte的格式
                System.Threading.Thread.Sleep(50);
                this.Invoke(disp_delegate, InputBuf);
               
            }
            catch (TimeoutException ex)         //超時處理
            {
                MessageBox.Show(ex.ToString());
            }      

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)     //選擇串口的處理過程
        {
            bool b_OpenFlag = Comm.IsOpen;
            if (b_OpenFlag == true)
            {
                Comm.Close();
                Comm.PortName = comboBox1.SelectedItem.ToString();
                if (!Comm.IsOpen)
                {
                    Comm.Close();
                    try
                    {
                        Comm.Open();
                    }
                    catch
                    {
                        MessageBox.Show("你選擇的串口被佔用或不存在,請重新選擇");
                        OpenSerialPort.Text = "打開串口";
                        textBox1.Text = " ";
                        textBox2.Text = " ";
                    }
                }
            }
            else
            {
                Comm.PortName = comboBox1.SelectedItem.ToString();
            }
         }

        private void OpenSerialPort_Click(object sender, EventArgs e)               //點擊“打開串口”按鈕的操作
        {
            if (OpenSerialPort.Text == "關閉串口")
            {
                Comm.Close();
                OpenSerialPort.Text = "打開串口";
            }
            else
            {
                OpenSerialPort.Text = "關閉串口";
                try
                {
                    Comm.Open();
                }
                catch 
                {
                    MessageBox.Show("你選擇的串口被佔用或不存在,請重新選擇");
                    OpenSerialPort.Text = "打開串口";
                    textBox1.Text = " ";
                    textBox2.Text = " ";
                }
            }
        }
    }
}


以上代碼下載地址:

基於c#的串口通信獲取溫溼度傳感器數據  http://download.csdn.net/detail/kevin_iot/9325183

基於CC2530的溫溼度傳感器及串口通信設計  http://download.csdn.net/detail/kevin_iot/9325203

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章