Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LCD1602_H
- #define LCD1602_H
- #include <delay.h>
- //* Define port masks like (1 << 3) for GPIO_PIN_3.
- #define RS GPIO_PIN_5
- #define RnW GPIO_PIN_6
- #define E GPIO_PIN_7
- #define DB7 GPIO_PIN_7
- #define DB6 GPIO_PIN_6
- #define DB5 GPIO_PIN_5
- #define DB4 GPIO_PIN_0
- //* Define port names like (GPIOA) for port A.
- #define RS_PORT GPIOC
- #define RnW_PORT GPIOC
- #define E_PORT GPIOC
- #define DATA_PINS_PORT GPIOD
- #define DB7_PORT GPIOD
- #define DB6_PORT GPIOD
- #define DB5_PORT GPIOD
- #define DB4_PORT GPIOD
- #define DATA_PINS ((DB7)|(DB6)|(DB5)|(DB4))
- // #define DATA_PINS ((DB7)|(DB6)|(DB5)|(DB4))
- // #define DATA_PINS_OFFSET 4
- #define PIN_LOW(X) X##_PORT->ODR &= ~X
- #define PIN_HIGH(X) X##_PORT->ODR |= X
- #define PIN_OUTPUT(X) X##_PORT->DDR |= X
- #define PIN_INPUT(X) X##_PORT->DDR &= ~X
- #define PIN_READ(PIN_NAME) (PIN_NAME##_PORT->IDR & (PIN_NAME))
- // #define LCD_CMD00_CLEAR
- // #define LCD_CMD01_RETURN
- // #define LCD_CMD02_ENTRY_MODESET
- // #define LCD_CMD02_INC_nDEC
- // #define LCD_CMD02_SHIFT_DISP_nCURSOR
- // #define LCD_CMD03_DISP_ON_OFF_CTL
- // #define LCD_CMD03_DISPLAY
- // #define LCD_CMD03_CURSOR
- // #define LCD_CMD03_BLINK
- // #define LCD_CMD04_DISP_CURSOR_SHIFT
- // #define LCD_CMD04_SCREEN_CURSOR
- // #define LCD_CMD04_RIGHT_LEFT
- // #define LCD_CMD05_FUNCT_SET
- // #define LCD_CMD05_DATA_LENGTH
- // #define LCD_CMD05_LINES_NUM
- // #define LCD_CMD05_FONT
- // #define LCD_CMD0
- // #define LCD_CMD0
- // #define LCD_CMD0
- // #define LCD_CMD0
- // #define LCD_CMD0
- // #define LCD_CMD0
- // #define LCD_CMD0
- #define LCD_CMD 0
- #define LCD_DATA 1
- // Using project specific pin assigment
- uint8_t read_data_pins()
- {
- // return (((DATA_PINS_PORT->IDR & DATA_PINS) >> DATA_PINS_OFFSET) & 0x0F);
- uint8_t tmp;
- tmp = DATA_PINS_PORT->IDR & DATA_PINS;
- tmp = (tmp >> 4)|(tmp & 0x01);
- return tmp;
- }
- // Using project specific pin assigment
- void write_data_pins(uint8_t data)
- {
- // DATA_PINS_PORT->ODR = (DATA_PINS_PORT->ODR & ~DATA_PINS)|(data << DATA_PINS_OFFSET);
- uint8_t tmp;
- tmp = ((data << 4) & 0xE0)|(data & 0x01);
- __critical
- {
- DATA_PINS_PORT->ODR = (DATA_PINS_PORT->ODR & ~DATA_PINS)|(tmp);
- }
- }
- uint8_t read_nibble()
- {
- uint8_t nibble;
- PIN_HIGH(E);
- __asm__("nop;\nnop;\n");
- nibble = read_data_pins();
- PIN_LOW(E);
- __asm__("nop;\nnop;\n");
- return nibble;
- }
- void write_nibble(uint8_t nibble)
- {
- PIN_HIGH(E);
- write_data_pins(nibble & 0x0F);
- __asm__("nop;\nnop;\n");
- PIN_LOW(E);
- __asm__("nop;\nnop;\n");
- }
- uint8_t read_byte(uint8_t is_data)
- {
- uint8_t byte;
- PIN_LOW(E);
- PIN_HIGH(RnW);
- if (is_data)
- {PIN_HIGH(RS);}
- else
- {PIN_LOW(RS);}
- PIN_INPUT(DATA_PINS);
- byte = read_nibble() << 4;
- byte |= read_nibble();
- return byte;
- }
- void write_byte(uint8_t byte, uint8_t is_data)
- {
- PIN_LOW(E);
- PIN_LOW(RnW);
- (is_data) ? (PIN_HIGH(RS)) : (PIN_LOW(RS));
- PIN_OUTPUT(DATA_PINS);
- write_nibble(byte >> 4);
- write_nibble(byte & 0x0F);
- }
- uint8_t lcd_is_busy()
- {
- // _delay_ms(1); // Needed by ST7066 LCD controller
- return (read_byte(LCD_CMD) & 0x80) ? (0xFF) : (0x00);
- }
- void LCD1602_Init()
- {
- PIN_LOW(RnW);
- PIN_OUTPUT(DATA_PINS);
- _delay_ms(40);
- write_nibble(0x03);
- _delay_ms(5);
- write_nibble(0x03);
- _delay_ms(5);
- write_nibble(0x03);
- _delay_ms(1);
- write_nibble(0x02);// LCD 4bit.
- _delay_ms(1);
- write_nibble(0x02);
- _delay_ms(1);
- write_nibble(0x08);
- _delay_ms(1);
- write_nibble(0x00);
- _delay_ms(1);
- write_nibble(0x08|0x04);
- _delay_ms(1);
- write_nibble(0x00);
- _delay_ms(1);
- write_nibble(0x01);
- _delay_ms(1);
- write_nibble(0x00);
- _delay_ms(1);
- write_nibble(0x06);
- _delay_ms(1);
- // while (lcd_is_busy());
- // write_byte(0x28, LCD_CMD); // LCD 4bit, 2 lines, 5x8 font
- // _delay_ms(1);
- // while (lcd_is_busy());
- // write_byte(0x08, LCD_CMD); // Display ON
- // _delay_ms(1);
- // while (lcd_is_busy());
- // write_byte(0x01, LCD_CMD); // Display Clear
- // _delay_ms(1);
- // while (lcd_is_busy());
- // write_byte(0b0110, LCD_CMD); // Entry Mode Set: increment addr, don't shift display
- // _delay_ms(1);
- // while (lcd_is_busy());
- }
- void LCD_SetXY(uint8_t x, uint8_t y)
- {
- while (lcd_is_busy());
- write_byte(0b10000000|((0x40*y) + x), LCD_CMD);
- }
- void LCD_PrintString(char *str)
- {
- while(*str != 0)
- {
- while(lcd_is_busy());
- write_byte(*str, LCD_DATA);
- str++;
- }
- }
- void LCD_Clear()
- {
- LCD_SetXY(0,0);
- LCD_PrintString(" ");
- LCD_SetXY(0,1);
- LCD_PrintString(" ");
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement