Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
Created January 24, 2020 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ti-nspire/20a1a38f46ae2af11ccbe3798f27253b to your computer and use it in GitHub Desktop.
Save ti-nspire/20a1a38f46ae2af11ccbe3798f27253b to your computer and use it in GitHub Desktop.
LM75_Example_for_ATmega328P
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
#include "USART.h"
#include "myI2C.h"
#include "LM75.h"
int main(){
initUSART();
// センサーが1個の場合:
// LM75 tempSensor(0); // ユーザー定義アドレス0 (0~7)のスレーブを実体化する。
// LM75 tempSensor = LM75(0); // と書いてもよい。
// センサーを複数接続する場合:
const int n = 4;
LM75 tempSensor[n];
for(int i=0; i<n; i++){
tempSensor[i] = LM75(i); // n個のスレーブを実体化する。
}
myI2C.enable(80); // システムクロックの80分周をI2CクロックとしてAVRのI2Cモジュールを有効化する。
// 分周比は16から2056まで8刻みで指定する。8刻みでなくてもよい。
// 8刻みにしなかった場合は、分周比が指定値よりも少し低くなる(I2Cクロックが少し高くなる)。
float temp[n];
uint8_t address[n];
while(1){
for(int i=0; i<n; i++){
temp[i] = tempSensor[i].getTemp();
address[i] = tempSensor[i].getAddressA210();
printString("Address ");
printNibble(address[i]);
printString(": ");
printFloat(temp[i]);
printString(" deg C");
printString("\n");
}
printString("\n");
_delay_ms(1000);
}
//myI2C.disable(); // I2Cを無効化する。
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment