void delay_ms(int n){ int i; if (n != 0) { delay_us(94) delay_100us(9); for (i = 1; i<n; i++) { delay_us(96) delay_100us(9); } } }
BYTE SPI_RW(BYTE byte) { BYTE res = SPI_WriteRead(byte); delay_100us();//must be after the transaction is started TODO debug the timing to check with which values commands are correctly handled return(res); }
int main() { int k; DDRB = (1 << PB7) | (1 << PB5) | (1 << PB4) | (1 << PB3) | (1 << PB2) ; // SCK, MOSI, CE. D/C and RST outputs SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1); //Enable SPI, Master, set clock rate fck/16 PORTB &= ~RST; delay_100us(1); PORTB = RST; spiwrite(0x21); // extended instruction mode spiwrite(0x90); // Vop //spiwrite(0x14); // bias to 100b //spiwrite(0x20); // normal instruction mode spiwrite(0x0D); // normal mode PORTB |= DOC; // enter data mode for(;;) //k = 0; k < 5; ++k) { spiwrite(0x1f); spiwrite(0x5); spiwrite(0x7); spiwrite(0x0); } return 0; }
void spiwrite(uint8_t data) { PORTB &= ~CEN; SPDR = data; delay_100us(1); PORTB |= CEN; //while( !(SPSR & (1<<SPIF) ) ) ; }
void LCDinit(void) { PORTE = 00; // Clear Control port LATE = 00; // and its corresponding Latch TRISE = 00; // Make PORTE as output port TRISD &= 0x0F; // Make output; delay(); delay(); delay(); //~15 ms delay lcd_cmd1(0x30); // #1 control byte delay(); lcd_cmd1(0x30); // #2 control byte delay_100us(); lcd_cmd1(0x30); // #3 control byte delay_100us(); lcd_cmd1(0x20); // #4 control byte, sets 4 bit mode delay_100us(); lcd_cmd(0x28); // #5 control byte Function set lcd_cmd(0x0D); // Turn on Display lcd_cmd(0x01); // Clear Display (clears junk if any) lcd_cmd(0x06); // Entry mode selection }