#include "iom16.h" void lcd_wait(void) { char x=1; DDRB &= ~0xF; //3..0 data lines for input PORTB &= ~0x10; // RS = 0 (command register) PORTB |= 0x20; // RW = 1 (read) do{ PORTB |= 0x40; // E = 1 (enable) PORTB |= 0x40; // E = 1 (enable) delay for correct read if(~PINB & 0x8) x = 0;//if no busy PORTB &= ~0x40; // E = 0 PORTB |= 0x40; // E = 1 (enable) PORTB &= ~0x40; // E = 0 }while(x); PORTB &= ~0x20; // RW = 0 (write) DDRB |= 0xF; //3..0 data lines for output } void lcd_command(unsigned char command) { lcd_wait(); PORTB = (PORTB & 0xF0) | (command>>4 & 0xf); //Hi nibble PORTB |= 0x40; // E = 1 (enable) PORTB &= ~0x40; // E = 0 PORTB = (PORTB & 0xF0) | (command & 0xf); //Lo nibble PORTB |= 0x40; // E = 1 (enable) PORTB &= ~0x40; // E = 0 } void lcd_data(unsigned char data) { lcd_wait(); PORTB = (PORTB & 0xF0) | (data>>4 & 0xf); //Hi nibble PORTB |= 0x10; // RS = 1 (data register) PORTB |= 0x40; // E = 1 (enable) PORTB &= ~0x40; // E = 0 PORTB = (PORTB & 0xF0) | (data & 0xf); //Lo nibble PORTB |= 0x40; // E = 1 (enable) PORTB &= ~0x40; // E = 0 } void main(void) { //int c=0x3456; char str1[]="Hello, World!"; unsigned char i; DDRB |= 0x70; //RS, RW, E DDRA |= 1; //A0 output lcd_command(0x28); //4bit/2lines/5x7 // lcd_command(0xf); //display On/cursor On/cursor BlinkOn lcd_command(0xc); //display On/cursor Off/cursor BlinkOff lcd_command(0x01); //clears display // lcd_data('a'); for(i=0; str1[i] !=0; i++) lcd_data(str1[i]); for(;;) { PORTA |= 1; //A0 ->1 PORTA &= 0xFE; //A0 ->0 } }
Как ни странно, фурычит! Остаётся удивляться, какие прочные бывают дисплеи : )