void LCDWrite(uint8_t data,uint8_t rs) { /** rs = 0 instruction else data */ if (rs) LCD_RS_PORT = 1; /** DATA */ else LCD_RS_PORT = 0; /** INSTRUCTION */ /** 4 MSB bits */ if(data & 0x80) LCD_DATA3_PORT = 1; if(data & 0x40) LCD_DATA2_PORT = 1; if(data & 0x20) LCD_DATA1_PORT = 1; if(data & 0x10) LCD_DATA0_PORT = 1 ; toggleE(); Cpu_Delay100US(10); LCD_DATA0_PORT = 0; LCD_DATA1_PORT = 0; LCD_DATA2_PORT = 0; LCD_DATA3_PORT = 0; /** 4 LSB bits */ if(data & 0x08) LCD_DATA3_PORT = 1; if(data & 0x04) LCD_DATA2_PORT = 1; if(data & 0x02) LCD_DATA1_PORT = 1; if(data & 0x01) LCD_DATA0_PORT = 1; toggleE(); Cpu_Delay100US(10); LCD_DATA0_PORT = 0; LCD_DATA1_PORT = 0; LCD_DATA2_PORT = 0; LCD_DATA3_PORT = 0; }
// Clears all text from display // DB0-6 are stored on PORTB, while E, RS, R/W are stored on bits at position // 5 - 7 respectively. void clear_display() { int finalValue = GPIO_PORTB_DATA_R | 1; finalValue &= 0xffffff81; GPIO_PORTB_DATA_R = finalValue; GPIO_PORTD_DATA_R &= 0xffffff3f; toggleE(); }
// Turns off LCD display // DB0-6 are stored on PORTB, while E, RS, R/W are stored on bits at position // 5 - 7 respectively. void display_off() { int finalValue = GPIO_PORTB_DATA_R | (1 << 3); //set bit in 3rd index to 0 finalValue &= 0xffffff88; //clear everything but bit 4 GPIO_PORTB_DATA_R |= finalValue; //set the value GPIO_PORTD_DATA_R &= 0xffffff3f; toggleE(); }
// Sets the address counter to the DD RAM address given by argument a, // where a is a 7-bit addresss // // Parameters for a: // 1 line display module : 00 - 4f // 2 line display module : 00 - 27 (first line), 40 - 67 (second line) void set_ddram(unsigned int a) { GPIO_PORTB_DATA_R &= 0xffffff80; GPIO_PORTB_DATA_R |= a; //set bits 0-6 GPIO_PORTD_DATA_R &= 0xffffff3f; //0 rs toggleE(); }
// Display a character on the LCD display. Super exciting! // RS is bit 7, set it high to write the char // // toDisp: character that will be displayed void display_write(char toDisp) { write_ddram(toDisp); // //GPIO_PORTB_DATA_R &= 0xffffff80; //GPIO_PORTB_DATA_R |= toDisp; //GPIO_PORTD_DATA_R |= 0x80; // Set bit seven high toggleE(); // GPIO_PORTD_DATA_R |= 0xffffff7f; //unset rs }
// Sets mode of entry to the display, with values i and s // i is increment mode // s is no display shift op // DB0-6 are stored on PORTB, while E, RS, R/W are stored on bits at position // 5 - 7 respectively. void entry_mode_set(int i, int s) { i = i << 1; // Bitshift iassemble final port value int finalBit = 0; finalBit |= i; finalBit |= s; finalBit |= 0x4; // Set bits manually GPIO_PORTB_DATA_R |= finalBit; GPIO_PORTD_DATA_R &= 0xffffff3f; toggleE(); }
// Turns LCD display on with value c setting cursor options and b setting blink // options. // DB0-6 are stored on PORTB, while E, RS, R/W are stored on bits at position // 5 - 7 respectively. void display_on(int c, int b) { c = c << 1; // Bitshift c to assemble final port value int finalBit = 0; finalBit |= c; finalBit |= b; finalBit |= 0xD; // Set bits manually GPIO_PORTB_DATA_R |= finalBit; GPIO_PORTD_DATA_R &= 0xffffff3f; toggleE(); }
// Set 8 bit interface, set number of lines n, and display font, f. // DB0-6 are stored on PORTB, while E, RS, R/W are stored on bits at position // 5 - 7 respectively. void function_set(int n, int f) { n = n << 3; // Bitshift n, f to assemble final port value f = f << 2; //build the final bit int finalBit = 0; finalBit |= n; finalBit |= f; finalBit |= 0x30; //zero and set the register GPIO_PORTB_DATA_R &= 0xffffff80; GPIO_PORTB_DATA_R |= finalBit; GPIO_PORTD_DATA_R &= 0xffffff3f; toggleE(); //toggle e to set the display }
/*The writeFourBits() function takes the lower four bits of the word and writes to the LCD*/ void writeFourBits(char word, unsigned int commandType, unsigned int delayAfter, unsigned int lower){ //TODO: if(commandType == DATA){ LATCbits.LAT_RS = DATA; } else { LATCbits.LAT_RS = COMMAND; } // set the commandType (RS value) LATEbits.LAT_D7 = (word&(0b1000)) >> 3; LATEbits.LAT_D6 = (word&(0b0100)) >> 2; LATEbits.LAT_D5 = (word&(0b0010)) >> 1; LATEbits.LAT_D4 = word&(0b0001); //enable toggleE(); if(lower == 1) { delayUs(delayAfter); } //delay //disable return; }