void LCDdogmSPI::init() { char clr; char linesreg; pinMode(SPI_MOSI_PIN, OUTPUT); pinMode(SPI_CLK_PIN,OUTPUT); pinMode(cs_pin,OUTPUT); pinMode(rs_pin,OUTPUT); SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1); clr=SPSR; clr=SPDR; delay(50); linesreg = (lines == 2) ? 8 : 0; commandWrite(LCDCMD_FUN_REG_2 | linesreg); //function set delay(1); commandWrite(LCDCMD_FUN_REG_2 | linesreg); //function set delay(1); commandWrite(LCDCMD_BIAS_SET); commandWrite(LCDCMD_POWER_CTL); commandWrite(LCDCMD_FOLLOWER_CTL); commandWrite(LCDCMD_CONTRAST_SET); commandWrite(LCDCMD_ON); commandWrite(LCDCMD_CLR); commandWrite(LCDCMD_ENTRY_MODE_SET); commandWrite(LCDCMD_HOME); }
//send the clear screen command to the LCD static void clear(){ commandWrite(CMD_CLR); _delay_ms(2); if(EMPTY_FIRST_COL) commandWrite(0x14); cursor_pos=EMPTY_FIRST_COL; }
// Write char into CGRAM memory void LCD4Bit_mod::buildChar(uint8_t loc, const uint8_t charmap[]) { // we only have 8 locations: 0-7 commandWrite(0x40 + ((loc&0x7) << 3)); for(uint8_t i=0; i<8; ++i) print(charmap[i]); commandWrite(CMD_SETDDRAM); }
// initiatize lcd after a short pause //while there are hard-coded details here of lines, cursor and blink settings, you can override these original settings after calling .init() void LCD4Bit_mod::begin () { pinMode(Enable,OUTPUT); pinMode(RS,OUTPUT); if (USING_RW) { pinMode(RW,OUTPUT); } pinMode(DB[0],OUTPUT); pinMode(DB[1],OUTPUT); pinMode(DB[2],OUTPUT); pinMode(DB[3],OUTPUT); delay(50); //The first 4 nibbles and timings are not in my DEM16217 SYH datasheet, but apparently are HD44780 standard... commandWriteNibble(0x03); delay(5); commandWriteNibble(0x03); delayMicroseconds(100); commandWriteNibble(0x03); delay(5); // needed by the LCDs controller //this being 2 sets up 4-bit mode. commandWriteNibble(0x02); commandWriteNibble(0x02); //todo: make configurable by the user of this library. //NFXX where //N = num lines (0=1 line or 1=2 lines). //F= format (number of dots (0=5x7 or 1=5x10)). //X=don't care byte num_lines_ptn = (num_lines - 1) << 3; byte dot_format_ptn = 0x00; //5x7 dots. 0x04 is 5x10 commandWriteNibble(num_lines_ptn | dot_format_ptn); delayMicroseconds(60); //The rest of the init is not specific to 4-bit mode. //NOTE: we're writing full bytes now, not nibbles. // display control: // turn display on, cursor off, no blinking commandWrite(0x0C); delayMicroseconds(60); //clear display commandWrite(0x01); delay(3); // entry mode set: 06 // increment automatically, display shift, entire shift off commandWrite(0x06); delay(1);//TODO: remove unnecessary delays }
//non-core stuff -------------------------------------- //move the cursor to the given absolute position. line numbers start at 1. //if this is not a 2-line LCD4Bit_mod instance, will always position on first line. void LCD4Bit_mod::setCursor(byte x, byte line) { //first, put cursor home commandWrite(CMD_HOME); //offset 40 chars in if second line requested x += line * 40; //advance the cursor to the right according to position. (second line starts at position 40). for (byte i=0; i<x; i++) { commandWrite(0x14); } }
// initialize LCD after a short pause //while there are hard-coded details here of lines, cursor and blink settings, you can override these original settings after calling .init() void LCD4Bit_mod::init() { // Configure pins as outputs setbits(CONTROL_DDR, CONTROL_EN | CONTROL_RS); #ifdef USING_RW setbits(CONTROL_DDR, CONTROL_RW); #endif setbits(DATA_DDR, DATA_PINS); delay(50); //The first 4 nibbles and timings are not in my DEM16217 SYH datasheet, but apparently are HD44780 standard... commandWriteNibble(0x03); delay(5); commandWriteNibble(0x03); delayMicroseconds(100); commandWriteNibble(0x03); delay(5); // needed by the LCDs controller //this being 2 sets up 4-bit mode. commandWriteNibble(0x02); commandWriteNibble(0x02); //todo: make configurable by the user of this library. //NFXX where //N = num lines (0=1 line or 1=2 lines). //F= format (number of dots (0=5x7 or 1=5x10)). //X=don't care uint8_t num_lines_ptn = g_num_lines - 1 << 3; uint8_t dot_format_ptn = 0x00; //5x7 dots. 0x04 is 5x10 commandWriteNibble(num_lines_ptn | dot_format_ptn); delayMicroseconds(60); //The rest of the init is not specific to 4-bit mode. //NOTE: we're writing full bytes now, not nibbles. // display control: // turn display on, cursor off, no blinking // commandWrite(CMD_DISPLAY | CMD_DISPLAY_DISPLAY_ON/*0x0C*/); delayMicroseconds(60); //clear display commandWrite(CMD_CLEAR); delay(3); // entry mode set: 06 // increment automatically, display shift, entire shift off commandWrite(0x06); delay(1);//TODO: remove unnecessary delays }
//scroll whole display to left void LCD4Bit_mod::leftScroll(byte num_chars, unsigned int delay_time) { for (byte i=0; i<num_chars; i++) { commandWrite(CMD_LEFT); delay(delay_time); } }
//non-core stuff -------------------------------------- //move the cursor to the given absolute position. line numbers start at 1. //if this is not a 2-line LCD4Bit_mod instance, will always position on first line. void cursorTo(int line_num, int x){ //if we are on a 1-line display, set line_num to 1st line, regardless of given //offset 40 chars in if second line requested x += 0x40*(((g_num_lines>line_num)?line_num:g_num_lines)-1); commandWrite(CMD_DDADDR(x)); cursor_pos=x; }
//non-core stuff -------------------------------------- //move the cursor to the given absolute position. line numbers start at 1. //if this is not a 2-line LCD4Bit_mod instance, will always position on first line. void LCD4Bit_mod::cursorTo(int line_num, int x){ //first, put cursor home commandWrite(CMD_HOME); //if we are on a 1-line display, set line_num to 1st line, regardless of given if (g_num_lines==1){ line_num = 1; } //offset 40 chars in if second line requested if (line_num == 2){ x += 40; } //advance the cursor to the right according to position. (second line starts at position 40). for (int i=0; i<x; i++) { commandWrite(0x14); } }
//scroll whole display to left void leftScroll(int num_chars, int delay_time){ while (num_chars-- > 0) { commandWrite(CMD_LEFT); int i=delay_time; while (i-- > 0) _delay_ms(1); } }
//non-core stuff -------------------------------------- //move the cursor to the given absolute position. line numbers start at 1. //if this is not a 2-line LCD4Bit_mod instance, will always position on first line. void cursorTo(int line_num, int x){ //first, put cursor home commandWrite(lcd_Home); //if we are on a 1-line display, set line_num to 1st line, regardless of given if (g_num_lines==1){ line_num = 1; } //offset 40 chars in if second line requested if (line_num == 2){ x += 40; } //advance the cursor to the right according to position. (second line starts at position 40). while (x-- > 0) { commandWrite(0x14); } }
// Enables LCD, cursor and cursor blinking void LCD4Bit_mod::setDisplay(bool display, bool cursor, bool blink) { byte cmd = CMD_DISPLAY; if(display) cmd |= CMD_DISPLAY_DISPLAY_ON; if(cursor) cmd |= CMD_DISPLAY_CURSOR_ON; if(blink) cmd |= CMD_DISPLAY_BLINK_ON; commandWrite(cmd); }
//non-core stuff -------------------------------------- //move the cursor to the given absolute position. line numbers start at 1. //if this is not a 2-line LCD4Bit_mod instance, will always position on first line. void LCD4Bit_mod::cursorTo(uint8_t line_num, uint8_t x){ if (g_num_lines==1){ line_num = 1; } //offset 40 chars in if second line requested if (line_num == 2){ x += 0x40; } commandWrite(CMD_SETDDRAM | x); }
void set_char_at(char c, int line_num, int x) { uint8_t orig_cursor=cursor_pos; //move cursor cursorTo(line_num, x); //write dataWrite(c); //restore cursor commandWrite(CMD_DDADDR(orig_cursor)); cursor_pos=orig_cursor; }
void LCDdogmSPI::cursorTo(int line_num, int x) { char address = 0; if (line_num > lines) line_num = lines; if (lines == 1) { x = x % 8; } else { x = x % 16; } address = (lines == 3) ? (line_num * 0x10) : (line_num * 0x40); address += x; address = address & 0x7F; commandWrite(LCDCMD_SET_DDRAM_ADDRESS | address); }
void LCDI2C4Bit::setCustomCharacter(uint8_t slot, uint8_t bitmask[8]) { //64 = write CGRAM address //slot<<3 = 3 bit (8 possible) cust. char. address commandWrite(64 | (slot << 3)); //write the character to CGRAM dataPlusMask |= 0x10; commandWrite(bitmask[0]); commandWrite(bitmask[1]); commandWrite(bitmask[2]); commandWrite(bitmask[3]); commandWrite(bitmask[4]); commandWrite(bitmask[5]); commandWrite(bitmask[6]); commandWrite(bitmask[7]); dataPlusMask ^= 0x10; //set DDRAM address to make sure next write command is to DDRAM instead of CGRAM //this will trash your cursor position, but prevent you from accidentally overwriting custom characters commandWrite(128); }
// initiatize lcd - cursor and blink settings could be overriden after initializing void init_lcd() { pinMode(EN_PORT, EN_BIT, OUTPUT); pinMode(RS_PORT, RS_BIT, OUTPUT); set_db_pin_mode(OUTPUT); digitalWrite(EN_PORT, EN_BIT, 0); _delay_ms(20); //init (interface still 8-bit) commandWriteNibble(0x03); _delay_ms(5); commandWriteNibble(0x03); _delay_us(100); commandWriteNibble(0x03); _delay_us(100); // needed by the LCDs controller //this being 2 sets up 4-bit mode. commandWriteNibble(0x02); commandWriteNibble(0x02); //todo: make configurable by the user of this library. //NFXX where //N = num lines (0=1 line or 1=2 lines). //F= format (number of dots (0=5x7 or 1=5x10)). //X=don't care int num_lines_ptn = (g_num_lines - 1) << 3; int dot_format_ptn = 0x00; //5x7 dots. 0x04 is 5x10 commandWriteNibble(num_lines_ptn | dot_format_ptn); _delay_us(60); //The rest of the init is not specific to 4-bit mode. //NOTE: we're writing full bytes now, not nibbles. // display control: // turn display on, cursor off, no blinking set_display_cursor_blink(0b100); //clear display clear(); // entry mode set: 06 // increment automatically, display shift, entire shift off commandWrite(0x06); _delay_ms(1);//TODO: remove unnecessary delays }
void Communications(void * pvParameters){ msg.Payload = msgBuff; vSemaphoreCreateBinary(DataSmphr); if (DataSmphr==NULL){ while(1); } qUART_Register_RBR_Callback(UART_GROUNDCOMM, UART_Rx_Handler); qUART_EnableRx(UART_GROUNDCOMM); comms_trcLabel = xTraceOpenLabel("Comms task"); for (;;){ if (pdTRUE == xSemaphoreTake(DataSmphr,500/portTICK_RATE_MS)){ vTracePrintF(comms_trcLabel,"Got joystick package"); switch (msg.Type){ case MSG_TYPE_CONTROL: memcpy(&quadrotor.joystick,msg.Payload,10); case MSG_TYPE_DEBUG: break; case MSG_TYPE_SYSTEM: switch (msg.Payload[0]) { case COMMAND_READ: commandRead(&msg.Payload[1]); break; case COMMAND_WRITE: commandWrite(&msg.Payload[1]); break; default: break; } break; default: break; } }else{ // Timeout to get a new joystick commands, values to 0 vTracePrintF(comms_trcLabel,"Joystick package timeout"); memset(&quadrotor.joystick,0,sizeof(quadrotor.joystick)); } } }
/* * function to redirect stdout to lcd by FDEV_SETUP_STREAM() macro * Sends a character to the LCD display. * '\n' clears the display after the *next* character * '\r' sets the cursor address to begin of 2nd line */ int lcd_putchar(char c, FILE *unused) { static char nl_seen; if (nl_seen && c != '\n') { // first character after newline, clear display and home cursor. clear(); nl_seen = 0; } if (c == '\n') { nl_seen = 1; } else if (c != '\r') { dataWrite(c); } else { commandWrite(CMD_DDADDR(64)); } return 0; }
//scroll whole display to left void LCD4Bit_mod::leftScroll(int num_chars, int delay_time){ for (int i=0; i<num_chars; i++) { commandWrite(CMD_LEFT); delay(delay_time); } }
//scroll whole display to left void LCD4Bit_mod::leftScroll(uint8_t num_chars, uint8_t delay_time){ for (uint8_t i=0; i<num_chars; ++i) { commandWrite(CMD_SHIFT | CMD_SHIFT_CURSOR_LEFT); delay(delay_time); } }
void serialEvent() { while (Serial.available()) { char inChar = Serial.read(); switch (comState) { case COMSTATE_COM: command = inChar; comState = COMSTATE_REG; break; case COMSTATE_REG: reg = inChar; count = 0; comState = COMSTATE_COM; if (command == 'r') { commandRead(reg, buffer); Serial.write(buffer, BURST); comState = COMSTATE_WAITACK; } else if (command == 'w') { comState = COMSTATE_WRITE; } else { // wat comState = COMSTATE_COM; } break; case COMSTATE_WAITACK: if (inChar == ACK) { // acked } else if (inChar == NACK) { // nacked } else { // this is really bad } comState = COMSTATE_COM; break; case COMSTATE_WRITE: buffer[count] = inChar; if (++count == BURST) { if (commandWrite(reg, buffer)) Serial.write(ACK); else Serial.write(NACK); comState = COMSTATE_COM; } break; default: comState = COMSTATE_COM; break; } } }
// initiatize lcd - cursor and blink settings could be overriden after initializing void init_lcd() { //Power-up delay _delay_ms(100); pinMode(EN_PORT, EN_BIT, OUTPUT); pinMode(RS_PORT, RS_BIT, OUTPUT); #if USING_RW != 0 if (USING_RW) { pinMode(RW_PORT, RW_BIT, OUTPUT); } #endif pinMode(DB0_PORT, DB0_BIT,OUTPUT); pinMode(DB1_PORT, DB1_BIT,OUTPUT); pinMode(DB2_PORT, DB2_BIT,OUTPUT); pinMode(DB3_PORT, DB3_BIT,OUTPUT); digitalWrite(EN_PORT, EN_BIT, 1); commandWriteNibble(lcd_FunctionReset); // first part of reset sequence _delay_ms(10); commandWriteNibble(lcd_FunctionReset); // second part of reset sequence _delay_us(200); commandWriteNibble(lcd_FunctionReset); // third part of reset sequence _delay_us(200); // this delay is omitted in the data sheet commandWriteNibble(lcd_Home); _delay_ms(1); commandWriteNibble(lcd_Home); _delay_ms(1); commandWriteNibble(lcd_DisplayOn); _delay_us(80); commandWriteNibble(lcd_DisplayOff); _delay_us(80); commandWriteNibble(0x00); _delay_ms(1); commandWriteNibble(lcd_Clear); _delay_ms(4); commandWriteNibble(0x00); _delay_ms(1); commandWriteNibble(lcd_EntryMode); _delay_us(80); // needed by the LCDs controller //this being 2 sets up 4-bit mode. commandWriteNibble(lcd_Home); commandWriteNibble(lcd_Home); //todo: make configurable by the user of this library. //NFXX where //N = num lines (0=1 line or 1=2 lines). //F= format (number of dots (0=5x7 or 1=5x10)). //X=don't care int num_lines_ptn = (g_num_lines - 1) << 3; int dot_format_ptn = 0x00; //5x7 dots. 0x04 is 5x10 commandWrite(num_lines_ptn | dot_format_ptn | 0x20); _delay_us(60); //The rest of the init is not specific to 4-bit mode. //NOTE: we're writing full bytes now, not nibbles. // display control: // turn display on, cursor off, no blinking commandWrite(lcd_DisplayOn); _delay_us(60); //clear display commandWrite(lcd_Clear); _delay_ms(3); // entry mode set: 06 // increment automatically, display shift, entire shift off commandWrite(lcd_EntryMode); _delay_ms(1);//TODO: remove unnecessary delays }
void ofxInFocusSerial::setLampEco(bool eco) { commandWrite("IPM", eco ? 1 : 0); }
void ofxInFocusSerial::setPowerOn(bool on) { commandWrite("PWR", on ? 1 : 0); }
void LCD4Bit_mod::clear(){ commandWrite(CMD_CLR); delay(1); }
void set_display_cursor_blink(char s) { commandWrite(0x08 | (s&0x07)); _delay_us(60); }
//send the clear screen command to the LCD static void clear(){ commandWrite(lcd_Clear); _delay_ms(10); }
void LCD4Bit::lcdCommandWriteAndPrintIn_P(uint8_t cmd, const char *chars) { commandWrite (cmd); printIn_P(chars); }
//send the clear screen command to the LCD void LCD4Bit::clear(){ commandWrite(CMD_CLR); delay(3); }