Тема: ATtiny13 + LCD1602_I2C
#define ADDR 0x27
#define SDA 3 // PB3
#define SCL 4 // PB4
#define RS 0
#define E 2
#define LED 3
byte led_b,h[8];
void setup() {
i2c_write(led_b |= (1<<LED));// включаем подсветку
lcdInit();
}
void loop() {
lcdCurs(0,3);
lcdString("ATtiny__13");
lcdCurs(1,3);
lcdLong(1234567890);
}
void lcdSend(bool rs, byte data) {
if(rs==0){led_b |= (1<<RS);} else {led_b &= ~(1<<RS);}//RS
del();
if(((data >> 7) & 1) ==1){i2c_write(led_b |= (1<<7));} else {i2c_write(led_b &= ~(1<<7));}
if(((data >> 6) & 1) ==1){i2c_write(led_b |= (1<<6));} else {i2c_write(led_b &= ~(1<<6));}
if(((data >> 5) & 1) ==1){i2c_write(led_b |= (1<<5));} else {i2c_write(led_b &= ~(1<<5));}
if(((data >> 4) & 1) ==1){i2c_write(led_b |= (1<<4));} else {i2c_write(led_b &= ~(1<<4));}
e_pin();
if(((data >> 3) & 1) ==1){i2c_write(led_b |= (1<<7));} else {i2c_write(led_b &= ~(1<<7));}
if(((data >> 2) & 1) ==1){i2c_write(led_b |= (1<<6));} else {i2c_write(led_b &= ~(1<<6));}
if(((data >> 1) & 1) ==1){i2c_write(led_b |= (1<<5));} else {i2c_write(led_b &= ~(1<<5));}
if(((data >> 0) & 1) ==1){i2c_write(led_b |= (1<<4));} else {i2c_write(led_b &= ~(1<<4));}
e_pin();
}
void lcdInit(){
del();
lcd(0x03);delayMicroseconds(4500);
lcd(0x03);delayMicroseconds(4500);
lcd(0x03);delayMicroseconds(200);
lcd(0b00000010);del();
lcd(0b00001100);del();
lcdClear();
}
void lcdClear(){lcd(0b00000001);}
void lcd(uint8_t cmd) {lcdSend(true, cmd);}
void lcdChar(const char chr) {lcdSend(false, (uint8_t)chr);}
void lcdString(const char* str) {while(*str != '\0') {del();lcdChar(*str);str++;}}
void del(){delayMicroseconds(1000);}
void e_pin(){i2c_write(led_b |= (1<<E));del();i2c_write(led_b &= ~(1<<E));}
void lcdCurs(byte str, byte mesto){
if(str==0){lcd(0b10000000+mesto);}
if(str==1){lcd(0b11000000+mesto);}
}
void lcdLong(long int_x){
byte h[8];
long int_y=int_x;
int i,i_kol;
if(int_x<0){int_x=abs(int_x);lcdChar('-');} // если минус
for(i_kol=0;int_x>0;i_kol++){int_x=int_x/10;} // определяем кол-во цифр в long
for(i=0;i<i_kol;i++){h[i]=int_y%10; int_y=int_y/10;}// разбиваем число на отдельные цифры
for(i=i_kol-1;i>=0;i--){lcdChar(h[i] +'0');} // преобразуем числа в char
if(i_kol==0){lcdChar('0');} // если long = 0, то выводить ноль
}
bool i2c_read_bit() {
bool i2c_bit = 1;
DDRB &= ~(1 << SDA);
delayMicroseconds(10);
DDRB &= ~(1 << SCL);
if((PINB >> SDA) & 1) i2c_bit=0;
delayMicroseconds(10);
DDRB |= (1 << SCL);
return i2c_bit;
}
byte i2c_write_byte(byte data){
for (byte i=0; i<8; i++){i2c_write_bit((data&0x80)==0);data<<=1;}
return i2c_read_bit();
}
void i2c_write_bit(byte b){
delayMicroseconds(5);
if(b){DDRB |= (1 << SDA);}else{DDRB &= ~(1 << SDA);}
delayMicroseconds(5);
DDRB &= ~(1 << SCL);
delayMicroseconds(10);
DDRB |= (1 << SCL);
}
void i2c_start(){
delayMicroseconds(10);
DDRB &= ~(1 << SDA); DDRB &= ~(1 << SCL);
delayMicroseconds(10);
DDRB |= (1 << SDA); PORTB &= ~(1 << SDA);
delayMicroseconds(10);
DDRB |= (1 << SCL); PORTB &= ~(1 << SCL);
delayMicroseconds(10);
}
void i2c_stop() {
DDRB |= (1 << SDA);
delayMicroseconds(10);
DDRB &= ~(1 << SCL);
delayMicroseconds(10);
DDRB &= ~(1 << SDA);
}
void i2c_write(byte lcd){
i2c_start();
i2c_write_byte(ADDR<<1);
i2c_write_byte(lcd);
i2c_stop();
}
Скетч использует 840 байт (82%) памяти устройства. Всего доступно 1024 байт.
Глобальные переменные используют 13 байт (20%) динамической памяти, оставляя 51 байт для локальных переменных. Максимум: 64 байт.