我可以暂时禁用Arduino串行数据接收吗?

时间:2018-08-31 12:21:11

标签: arduino serial-port

我正在一个项目上,遇到了一些问题。

我正在使用 DHT11温度传感器 Arduino Uno TFT LCD显示屏2.2英寸型号ITDB02-2.2

>

我希望我的项目要做的是对传感器使用2种功能模式,可以在程序开始时从键盘上进行选择(一种是正常模式,一种是在特殊场合使用的模式)(所以我需要串行通信。

我注意到如果以任何速率开始串行通信,屏幕将无法运行,因此我将Arduino Serial.begin(9600)Serial.end()用于程序的模式选择部分。

问题:即使我结束了串行通信并且看起来像这样,我的Arduino仍在通过串行端口发送数据:enter image description here

我发现Serial.end()函数不会关闭串行通讯,而只会断开通讯速率。我很好奇,如果您有什么想法可以用来消除多余的数据,在计算机收到数据之前先忽略它。

我被困住了。我以为打扰会是一个解决方案,但它不像我在互联网上研究的那样。

我的ARDUINO代码:

#include <SimpleDHT.h>
#include <UTFT.h>

UTFT myGLCD(ITDB22,A5,A4,A3,A2);
SimpleDHT11 dht11;

// Declare which fonts we will be using
extern uint8_t BigFont[];
//dht sensor data pin
int dataPinSensor1 = 12;
char mode;
int del;

void setup()
{
    Serial.begin(9600);
    Serial.print("Select functioning mode");
    mode=SensorModeSelect(mode);
    Serial.end();
    pinMode(12, INPUT);
}

void loop()
{
    if(mode=='1') {
        FirstFuncMode(dataPinSensor1);
    }
    if(mode=='2') {
        SecondFuncMode(dataPinSensor1,del);
    }
    delay(10);
}

char SensorModeSelect(char in)
{
    char mode='0';
    while(mode=='0') {
        if(Serial.available() > 0) {
            mode=Serial.read();
        }
    }
    if (mode == '1') {
        Serial.print("\nMOD1 SELECTED: press t key to aquire data \n");
    }
    if (mode == '2') {
        Serial.print("\nMOD2 SELECTED: press q if you want to quit auto mode \n");
        Serial.print("Select the data aquisition period(not smaller than 1 second) \n");
    }
    return mode;
}

int DataAqPeriod()
{
    int del=0;
    while(del==0) {
        while(Serial.available() > 0) {
            //Get char and convert to int
            char a = Serial.read();
            int c = a-48;
            del *= 10;
            del += c;
            delay(10);
        }
    }
    del*=1000;
    return del;
}

void FirstFuncMode(int dataPinSensor1)
{
    byte temperature = 0;
    byte humidity = 0;
    int err = SimpleDHTErrSuccess;
    bool DispCond=false;
    Serial.begin(9600);
    delay(1500);
    if (Serial.read() == 't' ) {
        DispCond=true;
        //read temperature and compare it with an error value
        if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
            Serial.print("unreliable measurement or unselected functioning mode");
        }
        byte f = temperature * 1.8 + 32;
        Serial.print((int)temperature);
        Serial.print(" *C, ");
        Serial.print((int)f);
        Serial.print(" *F, ");
        Serial.print((int)humidity);
        Serial.println(" H humidity");
        delay(1500);
    }

    Serial.end();
    if(DispCond==true) {
        //Setup the LCD
        myGLCD.InitLCD();
        myGLCD.setFont(BigFont);
        //print value on LCD
        displayNoInit((int)temperature,(int)humidity);
    }
}

void SecondFuncMode(int dataPinSensor1,int del)
{
    bool q=false;
    byte temperature = 0;
    byte humidity = 0;
    int err = SimpleDHTErrSuccess;
    Serial.begin(9600);
    del=DataAqPeriod();
    Serial.end();
    //Setup the LCD
    myGLCD.InitLCD();
    myGLCD.setFont(BigFont);

    while(q==false) {
        Serial.begin(9600);
        //read temperature and compare it with an error value
        if((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
            Serial.print("unreliable measurement or unselected functioning mode \n");
        }
        float f = temperature * 1.8 + 32;
        Serial.print((int)temperature);
        Serial.print(" *C, ");
        Serial.print((int)f);
        Serial.print(" *F, ");
        Serial.print((int)humidity);
        Serial.println(" H humidity");
        delay(del);
        if(Serial.read() == 'q')
            q=true;
        Serial.end();
        displayNoInit((int)temperature,(int)humidity);
        delay(10);
    }
}

void displayNoInit(int temperature,int humidity)
{
    //effective data display
    myGLCD.clrScr();
    myGLCD.setColor(255, 255, 0);
    myGLCD.setBackColor(10,10,10);
    myGLCD.print(" Temperature ", CENTER, 10);
    myGLCD.setColor(254, 254, 254);
    myGLCD.printNumI(temperature, CENTER, 45);
    myGLCD.setColor(255, 255, 0);
    myGLCD.print("C ", RIGHT, 45);
    myGLCD.print("Relative Hum.", CENTER, 90);
    myGLCD.setColor(204, 245, 250);
    myGLCD.printNumI(humidity, CENTER, 120);
    myGLCD.print("%", RIGHT, 120);
}

1 个答案:

答案 0 :(得分:1)

您的定义是正确的,Serial.end()不会禁用串行监视器,而只会禁用中断。调用Serial.end()后,您可以像这样禁用串行监视器。

#include <avr/io.h>

// Save status register, disable interrupts
uint8_t oldSREG = SREG;
cli();

// Disable TX and RX    
cbi(UCSRB, RXEN);
cbi(UCSRB, TXEN);

// Disable RX ISR
cbi(UCSRB, RXCIE);

// Flush the internal buffer
Serial.flush();

// Restore status register
SREG = oldSREG;