Arduino 代码表现得很奇怪(计时器继续工作,打印的 ADC 值始终为零)

问题描述 投票:0回答:1

我正在使用 Arduino Mega Pro,在代码中我使用 ADC 读取 2 个 LM35 传感器并每 1 秒打印一次值,并且我使用计时器计算 1 秒。

但是,两个传感器在显示器上给我的值为零,尝试调试以查看问题出在哪里,我发现当我尝试打印

analogRead
值时,
sensor1Value
根本没有获得任何值.

我认为计时器可能会干扰我还不知道的东西,因为在我声明实现计时器之前ADC工作正常,所以我评论了初始化计时器的行

init();
但我发现计时器即使在注释了这一行之后,它仍然可以继续工作,并且
ISR
不断出现并打印 DEBUGING 行。

我在另一个草图中使用了这段代码来测试传感器,因为我认为问题可能出在我的连接上,但它工作得很好。

#define INPUT_5_VOLT 5.0
#define TEMP_SENSOR_1_PIN A0  //Define ADC pin for Temp Sensor 1
#define TEMP_SENSOR_2_PIN A1  //Define ADC pin for Temp Sensor 2
#define ADC_RESOLUTION 1024.0

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensor1Value = analogRead(TEMP_SENSOR_1_PIN);
  int sensor2Value = analogRead(TEMP_SENSOR_2_PIN);

  float voltage1 = sensor1Value * (INPUT_5_VOLT / ADC_RESOLUTION);

  // Convert the voltage into the temperature in Celsius
  float temp_sensore_1_read = voltage1 * 100;

  float voltage2 = sensor2Value * (INPUT_5_VOLT / ADC_RESOLUTION);

  // Convert the voltage into the temperature in Celsius
  float temp_sensore_2_read = voltage2 * 100;

  Serial.print("Sensor 1 Value: ");
  Serial.println(temp_sensore_1_read);

  Serial.print("Sensor 2 Value: ");
  Serial.println(temp_sensore_2_read);

  delay(1000);
}

这是我正在尝试调试的代码。

//Hardware Configuratio
//Use input 5V for Temp Sensors
#define INPUT_5_VOLT 5.0
#define TEMP_SENSOR_1_PIN A0  //Define ADC pin for Temp Sensor 1
#define TEMP_SENSOR_2_PIN A1  //Define ADC pin for Temp Sensor 2
#define ADC_RESOLUTION 1024.0


//Enable Debugging
#define DEBUGGING 0

//MACROS
#define ENABLE_GLOBAL_INTERRUPT()  (SREG = SREG | (1<<7))
#define DISABLE_GLOBAL_INTERRUPT() (SREG = SREG & (~(1<<7)))

//Global Variable
char start_app_first_time_flag = false;
volatile bool print_data_flag = false;
volatile float temp_sensore_1_read = 0;
volatile float temp_sensore_2_read = 0;

//Function Declaration
void temp_sensors(void);  //Print the Sensors Data if the flag is set by the timer
void read_temp_sensor_1(void);
void read_temp_sensor_2(void);
void print_temp_sensors_readings(void);
void init(void);
void bit16_timer_1_init(void);


//ISR
ISR(TIMER1_COMPA_vect)
{
  #if DEBUGGING == 1
  Serial.print("Enter Tiemr TIMER1_COMPA ISR\n");
  #endif

  print_data_flag = true;
}

//Init Fucntion
void setup() {
  Serial.begin(9600);
  init();
}

//App Function
void loop() {
  //Print the Sensors Data if the flag is set by the timer.
  temp_sensors();
  //delay(1000);
}

//Fucntion Definition
void temp_sensors(void) {
  #if DEBUGGING == 1
  Serial.print("Enter temp_sensors Function\n");
  #endif

  read_temp_sensor_1();
  read_temp_sensor_2();

  if (true == print_data_flag) {
    print_temp_sensors_readings();

    print_data_flag = false;
  }
}

void read_temp_sensor_1(void) {
  #if DEBUGGING == 1
  Serial.print("Enter read_temp_sensor_1 Function\n");
  #endif

  // Get the voltage reading from the LM35
  int sensor1Value = analogRead(TEMP_SENSOR_1_PIN);
  //Serial.println(sensor1Value);

  // Convert that reading into voltage
  float voltage1 = sensor1Value * (INPUT_5_VOLT / ADC_RESOLUTION);
  //Serial.println(voltage1);

  // Convert the voltage into the temperature in Celsius
  temp_sensore_1_read = voltage1 * 100;
  //Serial.println(temp_sensore_1_read);


}

void read_temp_sensor_2(void) {
  #if DEBUGGING == 1
  Serial.print("Enter read_temp_sensor_2 Function\n");
  #endif

  // Get the voltage reading from the LM35
  int sensor2Value = analogRead(TEMP_SENSOR_2_PIN);

  // Convert that reading into voltage
  float voltage2 = sensor2Value * (INPUT_5_VOLT / ADC_RESOLUTION);

  // Convert the voltage into the temperature in Celsius
  temp_sensore_2_read = voltage2 * 100;
}

void print_temp_sensors_readings(void) {
  #if DEBUGGING == 1
  Serial.print("Enter print_temp_sensors_readings Function\n");
  #endif

  // Print the temperature 1 in Celsius
  Serial.print("Temperature1: ");
  Serial.print(temp_sensore_1_read);
  Serial.print("\xC2\xB0");  // shows degree symbol
  Serial.print("C\n");

  // Print the temperature 2 in Celsius
  Serial.print("Temperature2: ");
  Serial.print(temp_sensore_2_read);
  Serial.print("\xC2\xB0");  // shows degree symbol
  Serial.print("C\n\n");
}

void init(void) {
  #if DEBUGGING == 1
  Serial.print("Enter init Function\n");
  #endif

  bit16_timer_1_init();
}

void bit16_timer_1_init(void)
{
  #if DEBUGGING == 1
  Serial.print("Enter bit16_timer_1_init Function\n");
  #endif
  
  /*
    To make 1 sec timer, we will use this equation 
    Time = (1/(Frequency/Prescaler))*ticks = (1/(16,000,000/1024))*15625 = 1 sec
  */
 DISABLE_GLOBAL_INTERRUPT();
  TCCR1A = 0;
  TCCR1B = 0;  //No Prescaler - Stop Timer
  TCNT1 = 0;
  OCR1A = 15625;
  TIMSK1 = 0b00000010; //Enable interrupt for timer 1 A
  ENABLE_GLOBAL_INTERRUPT();
  TCCR1B |= (1 << WGM12) | (1 << CS12) | (1 << CS10); //1024 Prescaler - Start Timer - Enable CTC

}

我做的最后一步是评论这一行

bit16_timer_1_init();
,我设法停止了计时器。 但是,在设置此标志
print_data_flag 
并始终使用
true
并注释该行并将其清除后,传感器的值将继续打印零。

如果有人知道可能是什么问题,请帮我解决它。 预先感谢您。

timer arduino adc
1个回答
0
投票

您正在使用定时器模式 4,即 CTC,其中 OCR1A 定义 TOP 值。 由于 OCR1A 用于 TOP,因此它不能也用作比较匹配。 数据表甚至提到,如果 OCR1A 设置为 TOP,则匹配将被忽略。

不使用比较匹配,而是使用溢出中断。 将中断处理程序更改为

TIMER1_OVF_vect
。 当计时器达到 OCR1A 中定义的 TOP 值时,它将触发。

© www.soinside.com 2019 - 2024. All rights reserved.