Build rough functions for 4bit parrallel use.

This commit is contained in:
Jake Goodwin 2023-01-06 16:32:08 -08:00
parent 1492aea0d1
commit 441f13c9a2
2 changed files with 160 additions and 48 deletions

View File

@ -15,73 +15,197 @@
#define RS_DDR DDD0 #define RS_DDR DDD0
#define RW_PORT PORTD //#define RW_PORT PORTD
#define RW_PIN PIND1 //#define RW_PIN PIND1
#define RW_DDR DDD1 //#define RW_DDR DDD1
#define ENA_PORT PORTD #define ENA_PORT PORTD
#define ENA_PIN PIND2 #define ENA_PIN PIND1
#define ENA_DDR DDD2 #define ENA_DDR DDD1
#define BIT4_PORT PORTD #define BIT4_PORT PORTD
#define BIT4_PIN PIND3 #define BIT4_PIN PIND4
#define BIT4_DDR DDD3 #define BIT4_DDR DDD4
#define BIT5_PORT PORTD #define BIT5_PORT PORTD
#define BIT5_PIN PIND4 #define BIT5_PIN PIND5
#define BIT5_DDR DDD4 #define BIT5_DDR DDD5
#define BIT6_PORT PORTD #define BIT6_PORT PORTD
#define BIT6_PIN PIND5 #define BIT6_PIN PIND6
#define BIT6_DDR DDD5 #define BIT6_DDR DDD6
#define BIT7_PORT PORTD #define BIT7_PORT PORTD
#define BIT7_PIN PIND6 #define BIT7_PIN PIND7
#define BIT7_DDR DDD6 #define BIT7_DDR DDD7
#define LED_PIN PINC0 #define LED_PIN PINC0
#define LED_PORT PORTC #define LED_PORT PORTC
#define LED_DDR DDC0 #define LED_DDR DDC0
//LCD CONNECTIONS //LCD CONNECTIONS:
//PIND0 RS, H: data input L: Instruction input /*
//PIND1 RW, H: read L: write * VSS --> GND
//PIND2 ENABLE, Enable signal * VDD --> VCC
//PIND3 BIT4 * V0 --> POT --> GND
//PIND4 BIT5 * RS --> PD0(AVR)
//PIND5 BIT6 * RW --> GND
//PIND6 BIT7 * E --> PD1(AVR)
* D4 --> PD4(AVR)
* D5 --> PD5(AVR)
* D6 --> PD6(AVR)
* D7 --> PD7(AVR)
*
* PURPOSE:
* RS: register select,
* RW: Read/Write select signal,
* E: Operation(data read/write) enable signal.
* DB0~DB3: not used for 4bit operations.
* DB4~DB7: high order three-state data lines.
*/
//#############################
//MACROS
//#############################
//already defined
//#define _BV(bit) (1 << bit)
#define setbit(port, bit) (port) |= (1 << (bit))
#define clearbit(port, bit) (port) &= ~(1 << (bit))
//#############################
//FUNCTION PROTOTYPES
//#############################
void init_lcd(void);
//void cmd_lcd(uint8_t cmd);
void cmd_nibble_lcd(uint8_t cmd);
void clear_lcd(void);
static void led_blink(void);
void set_lcd_one_line_(void);
void set_lcd_two_line(void);
void return_home_lcd(void);
void turn_off_lcd(void);
void turn_on_lcd(void);
void set_cgram_lcd_address(uint8_t addr);
void set_ddram_lcd_address(uint8_t addr);
void write_lcd_address(uint8_t addr);
int main() {
//SETUP HERE.
_delay_ms(500);
while(1) {
led_blink();
_delay_ms(1000);
set_lcd_one_line();
_delay_ms(1000);
set_lcd_two_line();
}
return 0;
}
//############################# //#############################
//FUNCTIONS //FUNCTIONS
//############################# //#############################
void init_lcd(void) { void init_lcd(void) {
DDRD |= (1<<RS_DDR)|(1<<RW_DDR)|(1<<ENA_DDR)| //allow us to read pin7
(1<<BIT4_PIN)|(1<<BIT5_PIN)|(1<<BIT6_PIN)|(1<<BIT7_PIN); DDRD = 0x00;
setbit(DDRD, BIT7_DDR);
while(PORTD & (1<<BIT7_PIN)){
led_blink();
}
_delay_ms(40); DDRD = 0xff; //All output
_delay_ms(20);
PORTD = (0<<RS_PIN)|(0<<RW_PIN)|(0<<ENA_PIN)|
(1<<BIT4_PIN)|(1<<BIT5_PIN)|(0<<BIT6_PIN)|(1<<BIT7_PIN);
_delay_ms_(5);
PORTD = (0<<RS_PIN)|(0<<RW_PIN)|(0<<ENA_PIN)|
(1<<BIT4_PIN)|(1<<BIT5_PIN)|(0<<BIT6_PIN)|(1<<BIT7_PIN);
_delay_ms(1);
set_lcd_two_line();
return_home_lcd();
clear_lcd();
} }
void cmd_lcd() {
/*
* Input: None
* Outupt: None
* Description: Sets the LCD into 4bit two line display mode.
*/
void set_lcd_two_line(void) {
cmd_nibble_lcd(0x20);
cmd_nibble_lcd(0x80);
}
void set_lcd_one_line(void) {
cmd_nibble_lcd(0x20);
cmd_nibble_lcd(0x00);
}
void clear_lcd(void) {
cmd_nibble_lcd(0x00);
cmd_nibble_lcd(0x10);
}
void return_home_lcd(void) {
cmd_nibble_lcd(0x00);
cmd_nibble_lcd(0x20);
}
void turn_off_lcd(void) {
cmd_nibble_lcd(0x00);
cmd_nibble_lcd(0x80);
}
void turn_on_lcd(void) {
cmd_nibble_lcd(0x00);
cmd_nibble_lcd(0x80);
}
void set_cgram_lcd_address(uint8_t addr) {
; ;
} }
void set_ddram_lcd_address(uint8_t addr) {
;
}
void write_lcd_address(uint8_t addr) {
;
}
//A version that doesn't require multiple calls.
void cmd_lcd(uint8_t cmd) {
;
}
void cmd_nibble_lcd(uint8_t cmd) {
DDRD = 0xff;
PORTD &= 0x0f;
setbit(PORTD, ENA_PIN);
PORTD |= cmd;
_delay_ms(1);
clearbit(PORTD, ENA_PIN);
_delay_ms(1);
}
/* /*
* Input: None * Input: None
* Output: None * Output: None
@ -98,16 +222,4 @@ static void led_blink(void) {
} }
int main() {
//SETUP HERE.
init_lcd();
while(1) {
led_blink();
test_lcd();
_delay_ms(1000);
off_lcd();
}
return 0;
}

Binary file not shown.