Added cmd_lcd() function that uses bitshift operator.

This commit is contained in:
Jake Goodwin 2023-01-07 17:24:18 -08:00
parent 8756d36410
commit a3ead335c0
1 changed files with 42 additions and 17 deletions

View File

@ -66,6 +66,7 @@
* DB4~DB7: high order three-state data lines. * DB4~DB7: high order three-state data lines.
*/ */
//############################# //#############################
//MACROS //MACROS
//############################# //#############################
@ -83,7 +84,7 @@
void init_lcd(void); void init_lcd(void);
//void cmd_lcd(uint8_t cmd); void cmd_lcd(uint8_t cmd);
void cmd_nibble_lcd(uint8_t cmd); void cmd_nibble_lcd(uint8_t cmd);
void clear_lcd(void); void clear_lcd(void);
static void led_blink(void); static void led_blink(void);
@ -101,12 +102,20 @@ void write_lcd_address(uint8_t addr);
int main() { int main() {
//SETUP HERE. //SETUP HERE.
_delay_ms(500); _delay_ms(500);
//setup for single line mode.
//cmd_lcd(0x20);
//_delay_ms(1000);
//cmd_lcd(0x28);
while(1) { while(1) {
led_blink(); led_blink();
_delay_ms(1000); _delay_ms(1000);
set_lcd_one_line(); cmd_lcd(0x20);
_delay_ms(1000); _delay_ms(1000);
set_lcd_two_line(); cmd_lcd(0x28);
_delay_ms(2);
} }
return 0; return 0;
@ -142,34 +151,28 @@ void init_lcd(void) {
* Description: Sets the LCD into 4bit two line display mode. * Description: Sets the LCD into 4bit two line display mode.
*/ */
void set_lcd_two_line(void) { void set_lcd_two_line(void) {
cmd_nibble_lcd(0x20); cmd_lcd(0x28);
cmd_nibble_lcd(0x80);
} }
void set_lcd_one_line(void) { void set_lcd_one_line(void) {
cmd_nibble_lcd(0x20); cmd_lcd(0x20);
cmd_nibble_lcd(0x00);
} }
void clear_lcd(void) { void clear_lcd(void) {
cmd_nibble_lcd(0x00); cmd_lcd(0x01);
cmd_nibble_lcd(0x10);
} }
void return_home_lcd(void) { void return_home_lcd(void) {
cmd_nibble_lcd(0x00); cmd_lcd(0x02);
cmd_nibble_lcd(0x20);
} }
void turn_off_lcd(void) { void turn_off_lcd(void) {
cmd_nibble_lcd(0x00); cmd_lcd(0x08);
cmd_nibble_lcd(0x80);
} }
void turn_on_lcd(void) { void turn_on_lcd(void) {
cmd_nibble_lcd(0x00); cmd_lcd(0x08);
cmd_nibble_lcd(0x80);
} }
@ -188,9 +191,31 @@ void write_lcd_address(uint8_t addr) {
} }
//A version that doesn't require multiple calls. /*
* Input: uint8_t, a 8bit command code.
* Output: none
* Description: Takes a 8bit command code and sends it as two 4bit nibbles
* to the 1602LCD. The timing of delays is very important to match the
* datasheets timing diagrams.
*/
void cmd_lcd(uint8_t cmd) { void cmd_lcd(uint8_t cmd) {
; DDRD = 0xff;
PORTD &=0x0f;
//Send first nibble
setbit(PORTD, ENA_PIN);
PORTD |= (cmd & 0xf0);
_delay_ms(1);
clearbit(PORTD, ENA_PIN);
_delay_ms(2);
//Send the second nibble and shift cmd by four.
PORTD &= 0x0f;
setbit(PORTD, ENA_PIN);
PORTD |= (cmd<<4);
_delay_ms(1);
clearbit(PORTD, ENA_PIN);
_delay_ms(2);
} }
void cmd_nibble_lcd(uint8_t cmd) { void cmd_nibble_lcd(uint8_t cmd) {