Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-------------------------------------------------------------------
- File name: "lcd162.c"
- Driver for "LCD Keypad Shield" alphanumeric display.
- Display controller = HD44780U (LCD-II).
- Max. uC clock frequency = 16 MHz (Tclk = 62,5 ns)
- Connection : PORTx (4 bit mode) :
- [LCD] [Portx]
- RS ------ PH 5
- RW ------ GND
- E ------ PH 6
- DB4 ------ PG 5
- DB5 ------ PE 3
- DB6 ------ PH 3
- DB7 ------ PH 4
- Henning Hargaard, February 5, 2018
- ---------------------------------------------------------------------*/
- #define F_CPU 16000000
- // ADC values for button presses
- #define BUTTON_SELECT 540
- #define BUTTON_LEFT 308
- #define BUTTON_UP 50
- #define BUTTON_DOWN 155
- #define BUTTON_RIGHT 0
- #define NO_KEY_PRESSED 1000
- #include <avr/io.h>
- #include <util/delay.h>
- // Enabling us to use macro _NOP() to insert the NOP instruction
- #include <avr/cpufunc.h>
- #include "lcd162.h"
- // library function itoa() is needed
- #include <stdlib.h>
- //*********************** PRIVATE (static) operations *********************
- static void waitBusy()
- {
- _delay_ms(2);
- }
- static void pulse_E()
- {
- PORTH = PORTH | (1<<6); // Enable E (PH6)
- // min 230ns delay
- _NOP();
- _NOP();
- _NOP();
- _NOP();
- PORTH = PORTH & ~(1<<6); // Disable E (PH6)
- // Fit cycle time
- _NOP();
- _NOP();
- }
- // Sets the display data pins according to the 4 lower bits of data
- static void set4DataPins(unsigned char data)
- {
- PORTH = (PORTH & 0b11100111) | ((data<<1) & 0b00011000);
- PORTE = (PORTE & 0b11110111) | ((data<<2) & 0b00001000);
- PORTG = (PORTG & 0b11011111) | ((data<<5) & 0b00100000);
- }
- static void sendInstruction(unsigned char data)
- {
- unsigned char msb_data = (data>>4);
- waitBusy(); // To make sure that previous tasks are done
- // Send MS 4 bits
- PORTH &= 0b10010000; // RS and E low for instructions
- set4DataPins(msb_data);
- pulse_E();
- set4DataPins(data);
- pulse_E();
- }
- static void sendData(unsigned char data)
- {
- unsigned char msb_data = (data>>4);
- waitBusy(); // To make sure that previous tasks are done
- PORTH = PORTH | (1<<5); // Enable RS for data
- PORTH = PORTH & ~(1<<6); // E=0 to enable E pulse
- set4DataPins(msb_data);
- pulse_E();
- set4DataPins(data);
- pulse_E();
- _delay_us(0.5);
- PORTH = PORTH & ~(1<<5); // Disable RS
- }
- //*********************** PUBLIC functions *****************************
- // Initializes the display, blanks it and sets "current display position"
- // at the upper line, leftmost character (cursor invisible)
- // Reference: Page 46 in the HD44780 data sheet
- void LCDInit()
- {
- // Initializing the used port
- DDRH |= 0b01111000; // Outputs
- DDRE |= 0b00001000;
- DDRG |= 0b00100000;
- DDRF |= 0b00000001; // Keys as input
- // Wait 50 ms (min. 15 ms demanded according to the data sheet)
- _delay_ms(50);
- // Function set (still 8 bit interface)
- PORTG |= 0b00100000;
- PORTE |= 0b00001000;
- pulse_E();
- // Wait 10 ms (min. 4,1 ms demanded according to the data sheet)
- _delay_ms(10);
- // Function set (still 8 bit interface)
- pulse_E();
- // Wait 10 ms (min. 100 us demanded according to the data sheet)
- _delay_ms(10);
- // Function set (still 8 bit interface)
- pulse_E();
- // Wait 10 ms (min. 100 us demanded according to the data sheet)
- _delay_ms(10);
- // Function set (now selecting 4 bit interface !)
- PORTG &= 0b11011111;
- pulse_E();
- // Function Set : 4 bit interface, 2 line display, 5x8 dots
- sendInstruction( 0b00101000 );
- // Display, cursor and blinking OFF
- sendInstruction( 0b00001000 );
- // Clear display and set DDRAM adr = 0
- sendInstruction( 0b00000001 );
- // By display writes : Increment cursor / no shift
- sendInstruction( 0b00000110 );
- // Display ON, cursor and blinking OFF
- sendInstruction( 0b00001100 );
- }
- // Blanks the display and sets "current display position" to
- // the upper line, leftmost character
- void LCDClear()
- {
- sendInstruction(0b00000001);
- }
- // Sets DDRAM address to character position x and line number y
- void LCDGotoXY( unsigned char x, unsigned char y )
- {
- // To be implemented
- unsigned char instruction = 0b10000000;
- if (y == 0)
- instruction |= x ;
- else if (y == 1)
- instruction |= (0b11000000 | x); // Shifted 0x40 as second line starts there
- sendInstruction(instruction);
- }
- // Display "ch" at "current display position"
- void LCDDispChar(char ch)
- {
- sendData(ch);
- }
- // Displays the string "str" starting at "current display position"
- void LCDDispString(char* str)
- {
- for (uint8_t i = 0; i < strlen(str); i++)
- {
- LCDDispChar(str[i]);
- }
- }
- // Displays the value of integer "i" at "current display position"
- void LCDDispInteger(int i)
- {
- char buffer [40];
- itoa(i, buffer, 10);
- LCDDispString(buffer);
- }
- // Loads one of the 8 user definable characters (UDC) with a dot-pattern,
- // pre-defined in an 8 byte array in FLASH memory
- void LCDLoadUDC(unsigned char UDCNo, const unsigned char *UDCTab)
- {
- // To be implemented
- }
- // Selects, if the cursor has to be visible, and if the character at
- // the cursor position has to blink.
- // "cursor" not 0 => visible cursor.
- // "blink" not 0 => the character at the cursor position blinks.
- void LCDOnOffControl(unsigned char cursor, unsigned char blink)
- {
- unsigned char controlBit = 0b00001111;
- if (cursor == 0)
- controlBit &= ~(1<<1);
- if (blink == 0)
- controlBit &= ~(1<<0);
- sendInstruction(controlBit);
- }
- // Moves the cursor to the left
- void LCDCursorLeft()
- {
- unsigned char controlBit = 0b00010000;
- sendInstruction(controlBit);
- }
- // Moves the cursor to the right
- void LCDCursorRight()
- {
- unsigned char controlBit = 0b00010100;
- sendInstruction(controlBit);
- }
- // Moves the display text one position to the left
- void LCDShiftLeft()
- {
- unsigned char controlBit = 0b00011000;
- sendInstruction(controlBit);
- }
- // Moves the display text one position to the right
- void LCDShiftRight()
- {
- unsigned char controlBit = 0b00011100;
- sendInstruction(controlBit);
- }
- // Sets the backlight intensity to "percent" (0-100)
- void setBacklight(unsigned char percent)
- {
- // To be implemented
- }
- // Reads the status for the 5 on board keys
- // Returns 0, if no key pressed
- unsigned char readKeys()
- {
- int keyValue = ADCW;
- if (keyValue >= NO_KEY_PRESSED)
- return 0;
- if (keyValue >= BUTTON_SELECT)
- {
- LCDDispString("Select");
- return 1;
- }
- else if (keyValue >= BUTTON_LEFT)
- {
- LCDCursorLeft();
- return 1;
- }
- else if (keyValue >= BUTTON_DOWN)
- {
- LCDDispString("Down");
- return 1;
- }
- else if (keyValue >= BUTTON_UP)
- {
- LCDDispString("Up");
- return 1;
- }
- else if (keyValue >= BUTTON_RIGHT)
- {
- LCDCursorRight();
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement