ATTiny85 硬件I2C的使用
本帖最后由 希岩 于 2019-10-12 21:37 编辑看ATtiny85的数据手册上说有I2C和SPI硬件,纳尼?在哪呢?我咋找到,后来知道居然由一个叫USI的硬件来做的,一看USI看的我头大,真是难受。
在外国人心肠好,把USI配置成I2C的给做了,也就是Digispark那个开发板(核心为ATtiny85),有一个官方示例叫TinyWireM的例子里有好几个硬件I2C的例子,这里把DS1621温度传感器的示例贴出来,用的是硬件I2C,核心库是
#include <TinyWireM.h>// I2C Master lib for ATTinys which use USI
废话不多说,贴出官方代码的ATTinys的I2C主库,
/* ATtiny85 as an I2C MasterEx1 BroHogan 1/21/11
* I2C master reading DS1621 temperature sensor. (display with leds)
* SETUP:
* ATtiny Pin 1 = (RESET) N/U ATtiny Pin 2 = (D3) LED3
* ATtiny Pin 3 = (D4) to LED1 ATtiny Pin 4 = GND
* ATtiny Pin 5 = SDA on DS1621 ATtiny Pin 6 = (D1) to LED2
* ATtiny Pin 7 = SCK on DS1621 ATtiny Pin 8 = VCC (2.7-5.5V)
* NOTE! - It's very important to use pullups on the SDA & SCL lines!
* DS1621 wired per data sheet. This ex assumes A0-A2 are set LOW for an addeess of 0x48
* TinyWireM USAGE & CREDITS: - see TinyWireM.h
* NOTES:
* The ATtiny85 + DS1621 draws 1.7mA @5V when leds are not on and not reading temp.
* Using sleep mode, they draw .2 @5V @ idle - see http://brownsofa.org/blog/archives/261
*/
#include <TinyWireM.h> // I2C Master lib for ATTinys which use USI
#define DS1621_ADDR 0x48 // 7 bit I2C address for DS1621 temperature sensor
#define LED1_PIN 4 // ATtiny Pin 3
#define LED2_PIN 1 // ATtiny Pin 6
#define LED3_PIN 3 // ATtiny Pin 2
int tempC = 0; // holds temp in C
int tempF = 0; // holds temp in F
void setup(){
pinMode(LED1_PIN,OUTPUT);
pinMode(LED2_PIN,OUTPUT);
pinMode(LED3_PIN,OUTPUT);
Blink(LED1_PIN,2); // show it's alive
TinyWireM.begin(); // initialize I2C lib
Init_Temp(); // Setup DS1621
delay (3000);
}
void loop(){
Get_Temp();
Blink(LED1_PIN,tempC/10); // blink 10's of temperature on LED 1
delay (1000);
Blink(LED2_PIN,tempC%10); // blink 1's of temperature on LED 2
delay (4000); // wait a few sec before next reading
}
void Init_Temp(){ // Setup the DS1621 for one-shot mode
TinyWireM.beginTransmission(DS1621_ADDR);
TinyWireM.send(0xAC); // Access Command Register
TinyWireM.send(B00000001); // Using one-shot mode for battery savings
//TinyWireM.send(B00000000); // if setting continious mode for fast reads
TinyWireM.endTransmission(); // Send to the slave
}
void Get_Temp(){// Get the temperature from a DS1621
TinyWireM.beginTransmission(DS1621_ADDR);
TinyWireM.send(0xEE); // if one-shot, start conversions now
TinyWireM.endTransmission(); // Send 1 byte to the slave
delay(750); // if one-shot, must wait ~750 ms for conversion
TinyWireM.beginTransmission(DS1621_ADDR);
TinyWireM.send(0xAA); // read temperature (for either mode)
TinyWireM.endTransmission(); // Send 1 byte to the slave
TinyWireM.requestFrom(DS1621_ADDR,1); // Request 1 byte from slave
tempC = TinyWireM.receive(); // get the temperature
tempF = tempC * 9 / 5 + 32; // convert to Fahrenheit
}
void Blink(byte led, byte times){ // poor man's GUI
for (byte i=0; i< times; i++){
digitalWrite(led,HIGH);
delay (400);
digitalWrite(led,LOW);
delay (175);
}
}
感谢分享,我的项目用自带wire.h函数工作在nano上工作正常。但是用nano烧录attiny85(单芯片)却失败了。显示exit status 1错误。不知道怎么回事。一定要用sprak attiny85的板子吗。
nano烧录blink程序到attiny85芯片是可以正常运行的。 把函数库wire.h替换为TinyWireM.h可以编译通过,没有报错。但功能没起来。
语法上是不是直接替换就可以了。
Wire.beginTranmission=TniyWireM.beginTransminssion;
Wire.endTransmission=TinyWireM.endTransmission;
Wire.Write()=TinyWireM.send()
Wire.Read()=TinyWireM.recieve()
有啥要注意的吗 飞向北冰洋 发表于 2020-11-25 22:19
感谢分享,我的项目用自带wire.h函数工作在nano上工作正常。但是用nano烧录attiny85(单芯片)却失败了。显 ...
只要attiny85的就行,我是直接用ISP烧录的 飞向北冰洋 发表于 2020-11-26 07:50
把函数库wire.h替换为TinyWireM.h可以编译通过,没有报错。但功能没起来。
语法上是不是直接替换就可以了。 ...
不知道,没啥注意的
页:
[1]