【问题标题】:Reducing C code size for ATmega8减少 ATmega8 的 C 代码大小
【发布时间】:2015-02-24 11:43:25
【问题描述】:

我正在使用 ATmega8 进行一个小项目。我想从 DS18B20 传感器读取温度,然后使用 rf433 发射器发送结果。一切都很好,但是我的源大小...... ATmega 只有 7 168 字节的内存,但我的二进制文件是 8 310 字节。我删除了所有不必要的东西,但还是太多了。你能帮我缩小尺寸吗?我是用Arduino IDE来写代码的,也许纯C会更轻一些?

任何帮助表示赞赏:)

代码:

// RF433
#include <VirtualWire.h>
// Dallas
#include <DallasTemperature.h>
#include <OneWire.h> 

//const char *message; // Our message to send
const int ONE_WIRE_BUS = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire oneWire(ONE_WIRE_BUS); // on digital pin 2

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature Sensors(&oneWire);

void setup() {
  // RF433
  vw_set_ptt_inverted(true);
  vw_setup(2000);

  Sensors.begin();
}

void loop() {
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Sensors.requestTemperatures(); // Send the command to get temperatures

  float dTmp = Sensors.getTempCByIndex(0);
  // Why "byIndex"? 
  // You can have more than one IC on the same bus. 
  // 0 refers to the first IC on the wire

  char buf[6];
  dtostrf(dTmp, 6, 3, buf);

  char Msg[9];
  strcpy(Msg, "TP:");

  strncat(Msg, buf, 6);

  // Send temp.
  for (int i = 0; i < 10; ++i) // avoid loosing packets
  {
    vw_send((uint8_t *)Msg, strlen(Msg));
    vw_wait_tx();
    delay(300);
  }

  // Sleep 1 min.
  delay(60000);
}

【问题讨论】:

  • 您正在使用某些库提供的系统函数。你怎么知道这些的大小加起来还没有超过你的?
  • 该测试无效。包括头文件不足以链接到库中;这些函数也必须实际调用。
  • 终于有人要求优化了!
  • 如果您可以忍受不发送纯文本而是打包数据的二进制表示(即更改协议),那么您可以放弃对 dtostrf() 的调用。即使没有,使用char msg[9] = "TP:"; dtostrf(dTmp, 6, 3, msg + 3); 也可以节省一些字节和无用的复制。此外,该字符串的长度应该是固定的,或者?所以不需要在之后的每次循环迭代中都使用 strlen() 。也就是说,这真的是 C 代码还是 C++ 或它们的派生代码?我问是因为Sensors.getFoo() 看起来不像 C,如果是,它包含一个可替换的函数指针。
  • 正如@UlrichEckhardt 所说,您应该尽量不在嵌入式软件中使用任何字符串。字符串处理可能非常繁重。例如,您可以只传输二进制数据并将它们格式化为基于 PC 的软件上的字符串。

标签: c atmega temperature arduino-ide transmission


【解决方案1】:

谢谢大家,伙计们。 昨天我找到了一种方法来完成这项任务。也许将来有人会寻找答案,所以这里是: 我放弃了达拉斯图书馆,终于发现它不是第一次需要。

对 ATmega8 进行编程以与 DS18B20 温度传感器和 RF433Mhz 发射器配合使用:

#include <OneWire.h> 
// RF433
#include <VirtualWire.h>

int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
int led = 13;
void setup(void) {
  // RF433
  vw_set_ptt_inverted(true);
  vw_setup(2000);

  pinMode(led, OUTPUT);
}

void loop(void) {
  float dTmp = getTemp();
  char buf[6];
  dtostrf(dTmp, 6, 3, buf);

  char Msg[10];
  strcpy(Msg, "TMP:");

  strncat(Msg, buf, 6);
  digitalWrite(led, HIGH);
  for (int i = 0; i < 10; ++i) // avoid loosing packets in the air
  {
    vw_send((uint8_t *)Msg, strlen(Msg));
    vw_wait_tx();
    delay(300);
  }
  digitalWrite(led, LOW);

  // Sleep 15 s.
  delay(15000);
}


float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    //no more sensors on chain, reset search
    ds.reset_search();
    return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
    //   Serial.println("CRC is not valid!");
    return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
    //   Serial.print("Device is not recognized");
    return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);  
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;

  return TemperatureSum;

}

以上代码作为二进制文件大约需要 6300 字节,因此还有一点空间可以改进。

希望你会发现它有用:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2012-10-04
    • 2021-05-19
    相关资源
    最近更新 更多