初始化提交
This commit is contained in:
115
arduino-cli/libraries/AH_24CXX/AH_24Cxx.cpp
Normal file
115
arduino-cli/libraries/AH_24CXX/AH_24Cxx.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
|
||||
/********************************************************
|
||||
** Download from: **
|
||||
** http://www.arduino-projekte.de **
|
||||
** **
|
||||
** Based on Code from: **
|
||||
** http://arduino.cc/playground/ **
|
||||
** **
|
||||
** Released into the public domain. **
|
||||
********************************************************/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <AH_24Cxx.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#define AT24C01 0
|
||||
#define AT24C02 1
|
||||
#define AT24C04 2
|
||||
#define AT24C08 3
|
||||
#define AT24C16 4
|
||||
#define AT24C32 5
|
||||
#define AT24C64 6
|
||||
#define AT24C128 8
|
||||
#define AT24C256 9
|
||||
|
||||
|
||||
//************************************************************************
|
||||
|
||||
// Constructor
|
||||
AH_24Cxx::AH_24Cxx(int ic_type, int deviceaddress){
|
||||
// Wire.begin(); // initialise the connection
|
||||
_mode = ic_type;
|
||||
_deviceaddress = B01010<<3 | deviceaddress; //Address: B01010(A2)(A1)(A0) A2=0; A1=0; A0=0 => Address: B01010000(BIN) | 0x50(HEX) | 80(DEC)
|
||||
}
|
||||
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
// WARNING: address is a page address, 6-bit end will wrap around
|
||||
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
|
||||
|
||||
void AH_24Cxx::write_page( unsigned int eeaddresspage, byte* data, byte length ) {
|
||||
|
||||
Wire.beginTransmission(_deviceaddress);
|
||||
if (_mode>4){
|
||||
Wire.write((int)(eeaddresspage >> 8)); // MSB
|
||||
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
|
||||
}
|
||||
else {
|
||||
Wire.write((int)eeaddresspage);
|
||||
}
|
||||
|
||||
byte c;
|
||||
for ( c = 0; c < length; c++)
|
||||
Wire.write(data[c]);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
// maybe let's not read more than 30 or 32 bytes at a time!
|
||||
|
||||
void AH_24Cxx::read_buffer(unsigned int eeaddress, byte *buffer, int length ) {
|
||||
Wire.beginTransmission(_deviceaddress);
|
||||
if (_mode>4){
|
||||
Wire.write((int)(eeaddress >> 8)); // MSB
|
||||
Wire.write((int)(eeaddress & 0xFF)); // LSB
|
||||
}
|
||||
else {
|
||||
Wire.write((int)eeaddress);
|
||||
}
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(_deviceaddress,length);
|
||||
int c = 0;
|
||||
for ( c = 0; c < length; c++ )
|
||||
if (Wire.available()) buffer[c] = Wire.read();
|
||||
}
|
||||
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
void AH_24Cxx::write_byte(unsigned int eeaddress, byte data) {
|
||||
|
||||
int rdata = data;
|
||||
Wire.beginTransmission(_deviceaddress);
|
||||
if (_mode>4){
|
||||
Wire.write((int)(eeaddress >> 8)); // MSB
|
||||
Wire.write((int)(eeaddress & 0xFF)); // LSB
|
||||
}
|
||||
else {
|
||||
Wire.write((int)eeaddress);
|
||||
}
|
||||
Wire.write(rdata);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
|
||||
byte AH_24Cxx::read_byte(unsigned int eeaddress) {
|
||||
byte rdata = 0xFF;
|
||||
Wire.beginTransmission(_deviceaddress);
|
||||
if (_mode>4){
|
||||
Wire.write((int)(eeaddress >> 8)); // MSB
|
||||
Wire.write((int)(eeaddress & 0xFF)); // LSB
|
||||
}
|
||||
else {
|
||||
Wire.write((int)eeaddress);
|
||||
}
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(_deviceaddress,1);
|
||||
if (Wire.available()) rdata = Wire.read();
|
||||
return rdata;
|
||||
}
|
||||
|
||||
38
arduino-cli/libraries/AH_24CXX/AH_24Cxx.h
Normal file
38
arduino-cli/libraries/AH_24CXX/AH_24Cxx.h
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
/********************************************************
|
||||
** Download from: **
|
||||
** http://www.arduino-projekte.de **
|
||||
** **
|
||||
** Based on Code from: **
|
||||
** http://arduino.cc/playground/ **
|
||||
** **
|
||||
** Released into the public domain. **
|
||||
********************************************************/
|
||||
|
||||
#ifndef AH_24Cxx_h
|
||||
#define AH_24Cxx_h
|
||||
|
||||
#include <Arduino.h> //Arduino IDE >= V1.0
|
||||
|
||||
|
||||
|
||||
class AH_24Cxx
|
||||
{
|
||||
public:
|
||||
// Constructor
|
||||
AH_24Cxx(int ic_type,int deviceaddress);
|
||||
void write_byte (unsigned int eeaddress, byte data);
|
||||
void write_page (unsigned int eeaddresspage, byte* data, byte length);
|
||||
byte read_byte (unsigned int eeaddress);
|
||||
void read_buffer (unsigned int eeaddress, byte *buffer, int length);
|
||||
|
||||
private:
|
||||
int _mode;
|
||||
int _deviceaddress;
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
|
||||
/********************************************************
|
||||
** Download from: **
|
||||
** http://www.arduino-projekte.de **
|
||||
** **
|
||||
** Based on Code from: **
|
||||
** http://arduino.cc/playground/ **
|
||||
** **
|
||||
** Released into the public domain. **
|
||||
********************************************************/
|
||||
|
||||
#include <AH_24Cxx.h>
|
||||
#include <Wire.h>
|
||||
|
||||
#define AT24C01 0
|
||||
#define AT24C02 1
|
||||
#define AT24C04 2
|
||||
#define AT24C08 3
|
||||
#define AT24C16 4
|
||||
#define AT24C32 5
|
||||
#define AT24C64 6
|
||||
#define AT24C128 8
|
||||
#define AT24C256 9
|
||||
//Initialisation
|
||||
|
||||
int data;
|
||||
|
||||
#define BUSADDRESS 0x00
|
||||
#define EEPROMSIZE 2048 //AT24C16 2048byte
|
||||
|
||||
AH_24Cxx ic_eeprom = AH_24Cxx(AT24C16, BUSADDRESS);
|
||||
|
||||
void setup(){
|
||||
Serial.begin(9600);
|
||||
Wire.begin();
|
||||
Serial.println("Write data");
|
||||
for(int i = 0; i < 10; i++) {
|
||||
data = i;
|
||||
ic_eeprom.write_byte(i,data);
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
void loop(){
|
||||
Serial.println("Read data");
|
||||
for (int i=0;i<EEPROMSIZE;i++){
|
||||
Serial.print("pos. ");
|
||||
Serial.print(i);
|
||||
Serial.print(": ");
|
||||
Serial.println(ic_eeprom.read_byte(i));
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
7
arduino-cli/libraries/AH_24CXX/keywords.txt
Normal file
7
arduino-cli/libraries/AH_24CXX/keywords.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
AH_24Cxx KEYWORD1
|
||||
|
||||
write_byte KEYWORD2
|
||||
write_page KEYWORD2
|
||||
read_byte KEYWORD2
|
||||
read_buffer KEYWORD2
|
||||
|
||||
Reference in New Issue
Block a user