void glcd_init(void) { #if defined(GLCD_CONTROLLER_PCD8544) /* Set pin directions */ /* * Set up SPI for AVR8 * Note: AVR's SS pin must be set to output, regardless of whether we * actually use it. This is a requirement of SPI mster mode. */ sbi(DDR(AVR_SS_PORT),AVR_SS_PIN); /* * Set MOSI, Master SS, SCK to output (otherwise SPI won't work) * Must be done even if native SS pin not used */ sbi(DDR(CONTROLLER_MOSI_PORT),CONTROLLER_MOSI_PIN); sbi(DDR(CONTROLLER_SS_PORT),CONTROLLER_SS_PIN); sbi(DDR(CONTROLLER_SCK_PORT),CONTROLLER_SCK_PIN); /* Set SS, DC and RST pins to output */ sbi( DDR(CONTROLLER_SS_PORT), CONTROLLER_SS_PIN ); sbi( DDR(CONTROLLER_DC_PORT), CONTROLLER_DC_PIN ); sbi( DDR(CONTROLLER_RST_PORT), CONTROLLER_RST_PIN ); /* Deselect LCD */ GLCD_DESELECT(); /* * Max allowed SPI clock is 4 MHz from datasheet. * Enable SPI, set master mode and clock rate to /4 (4MHz with F_CPU=8MHz) */ SPCR = (1<<SPE)|(1<<MSTR); SPSR = 0; glcd_PCD8544_init(); /* Select screen buffer */ glcd_select_screen(glcd_buffer,&glcd_bbox); /* Clear screen, we are now ready to go */ glcd_clear(); #elif defined(GLCD_CONTROLLER_ST7565R) /* Set up GPIO directions */ /* * Set up SPI for AVR8 * Note: AVR's SS pin must be set to output, regardless of whether we * actually use it. This is a requirement of SPI mster mode. */ sbi(DDR(AVR_SS_PORT),AVR_SS_PIN); /* Set SCK and MOSI as output */ sbi(DDR(CONTROLLER_SCK_PORT),CONTROLLER_SCK_PIN); sbi(DDR(CONTROLLER_MOSI_PORT),CONTROLLER_MOSI_PIN); /* * Set MISO as input with pullup. This needs to be set for * SPI to work, even though we never use or read it. */ cbi(DDR(CONTROLLER_MISO_PORT),CONTROLLER_MISO_PIN); // B3 MISO as input sbi(CONTROLLER_MISO_PORT,CONTROLLER_MISO_PIN); /* Set pin to controller SS as output */ sbi(DDR(CONTROLLER_SS_PORT),CONTROLLER_SS_PIN); // A5 /* Set LCD A0 pin as output */ sbi(DDR(CONTROLLER_A0_PORT),CONTROLLER_A0_PIN); // A6 /* Init SS pin high (i.e LCD deselected) */ sbi(CONTROLLER_SS_PORT,CONTROLLER_SS_PIN); /* Deselect LCD */ GLCD_DESELECT(); /* MSB first, double speed, SPI mode 0 */ SPCR = (1<<SPE) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA); sbi(SPSR,SPI2X); /* Enable interrupts */ sei(); delay_ms(30); /* Example in datasheet does this (20ms) */ glcd_ST7565R_init(); glcd_all_on(); delay_ms(500); glcd_normal(); glcd_set_start_line(0); glcd_clear_now(); glcd_select_screen(glcd_buffer,&glcd_bbox); glcd_clear(); #else #error "Controller not supported" #endif /* GLCD_CONTROLLER_* */ }
void glcd_init(void) { #if defined(GLCD_CONTROLLER_PCD8544) /* Set pin directions */ /* * Set up SPI for AVR8 * Note: AVR's SS pin must be set to output, regardless of whether we * actually use it. This is a requirement of SPI mster mode. */ sbi(DDR(AVR_SS_PORT),AVR_SS_PIN); /* * Set MOSI, Master SS, SCK to output (otherwise SPI won't work) * Must be done even if native SS pin not used */ sbi(DDR(CONTROLLER_MOSI_PORT),CONTROLLER_MOSI_PIN); sbi(DDR(CONTROLLER_SS_PORT),CONTROLLER_SS_PIN); sbi(DDR(CONTROLLER_SCK_PORT),CONTROLLER_SCK_PIN); /* Set SS, DC and RST pins to output */ sbi( DDR(CONTROLLER_SS_PORT), CONTROLLER_SS_PIN ); sbi( DDR(CONTROLLER_DC_PORT), CONTROLLER_DC_PIN ); sbi( DDR(CONTROLLER_RST_PORT), CONTROLLER_RST_PIN ); /* Deselect LCD */ GLCD_DESELECT(); /* * Max allowed SPI clock is 4 MHz from datasheet. * Enable SPI, set master mode and clock rate to /4 (4MHz with F_CPU=8MHz) */ SPCR = (1<<SPE)|(1<<MSTR); SPSR = 0; glcd_reset(); /* Get into the EXTENDED mode! */ glcd_command(PCD8544_FUNCTION_SET | PCD8544_EXTENDED_INSTRUCTION); /* LCD bias select (4 is optimal?) */ glcd_command(PCD8544_SET_BIAS | 0x2); /* Set VOP */ glcd_command(PCD8544_SET_VOP | 50); // Experimentally determined /* Back to standard instructions */ glcd_command(PCD8544_FUNCTION_SET); /* Normal mode */ glcd_command(PCD8544_DISPLAY_CONTROL | PCD8544_DISPLAY_NORMAL); /* Select screen buffer */ glcd_select_screen(glcd_buffer,&glcd_bbox); /* Clear screen, we are now ready to go */ glcd_clear(); #elif defined(GLCD_CONTROLLER_ST7565R) /* Set up GPIO directions */ /* * Set up SPI for AVR8 * Note: AVR's SS pin must be set to output, regardless of whether we * actually use it. This is a requirement of SPI mster mode. */ sbi(DDR(AVR_SS_PORT),AVR_SS_PIN); /* Set SCK and MOSI as output */ sbi(DDR(CONTROLLER_SCK_PORT),CONTROLLER_SCK_PIN); sbi(DDR(CONTROLLER_MOSI_PORT),CONTROLLER_MOSI_PIN); /* * Set MISO as input with pullup. This needs to be set for * SPI to work, even though we never use or read it. */ cbi(DDR(CONTROLLER_MISO_PORT),CONTROLLER_MISO_PIN); // B3 MISO as input sbi(CONTROLLER_MISO_PORT,CONTROLLER_MISO_PIN); /* Set pin to controller SS as output */ sbi(DDR(CONTROLLER_SS_PORT),CONTROLLER_SS_PIN); // A5 /* Set LCD A0 pin as output */ sbi(DDR(CONTROLLER_A0_PORT),CONTROLLER_A0_PIN); // A6 /* Init SS pin high (i.e LCD deselected) */ sbi(CONTROLLER_SS_PORT,CONTROLLER_SS_PIN); /* Deselect LCD */ GLCD_DESELECT(); /* MSB first, double speed, SPI mode 0 */ SPCR = (1<<SPE) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA); sbi(SPSR,SPI2X); /* Enable interrupts */ sei(); delay_ms(30); // example in datasheet does this (20ms) glcd_command(ST7565R_RESET); // internal reset glcd_command(0xa2); // 1/9 bias glcd_command(0xa0); // ADC select, normal glcd_command(0xc8); // com output reverse glcd_command(0xa4); // display all points normal glcd_command(0x40); // display start line set glcd_command(0x25); // internal resistor ratio glcd_command(0x81); // electronic volume mode set //glcd_command(0x10); // electronic volume - datasheet's contrast example doesn't work glcd_command(45); // this works better glcd_command(0x2f); // power controller set glcd_command(0xaf); // display on glcd_all_on();