基于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

  • 20
    点赞
  • 115
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值