int main() { bool data = 1; // 8-bit mode. bool lines = 1; // 2 display lines. bool font = 1; // 5x8 font. bool display = 1; // Display on. bool cursor = 0; // Cursor off. bool blink = 0; // Blink (block cursor) off. bool counter = 1; // Increment DDRAM counter after data write bool shift = 0; // Do not shift display after data write. bool mode = 0; // Shift cursor. bool direction = 0; // Right. int8_t err; // Initialise MCP23017. err = mcp23017Init( 0x20 ); if ( err < 0 ) { printf( "Couldn't init. Try loading i2c-dev module.\n" ); return -1; } // Set direction of GPIOs. mcp23017WriteByte( mcp23017[0], IODIRA, 0x00 ); // Output. mcp23017WriteByte( mcp23017[0], IODIRB, 0x00 ); // Output. // Writes to latches are the same as writes to GPIOs. mcp23017WriteByte( mcp23017[0], OLATA, 0x00 ); // Clear pins. mcp23017WriteByte( mcp23017[0], OLATB, 0x00 ); // Clear pins. // Set BANK bit for 8-bit mode. mcp23017WriteByte( mcp23017[0], IOCONA, 0x80 ); struct hd44780 *hd44780this; hd44780this = malloc( sizeof( struct hd44780 )); /* +---------------------------------------------------------------+ | GPIOB | GPIOA | |-------------------------------+-------------------------------| | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---| |DB7|DB6|DB5|DB4|DB3|DB2|DB1|DB0|RS |R/W| E |---|---|---|---|---| +---------------------------------------------------------------+ */ // Set up hd44780 data. hd44780this->rs = 0x80; // HD44780 RS pin. hd44780this->rw = 0x40; // HD44780 R/W pin. hd44780this->en = 0x20; // HD44780 E pin. hd44780[0] = hd44780this; // Initialise display. hd44780Init( mcp23017[0], hd44780[0], data, lines, font, display, cursor, blink, counter, shift, mode, direction ); hd44780WriteString( mcp23017[0], hd44780[0], "Initialised" ); // Set up structure to display current time. struct calendar time = { .mcp23017 = mcp23017[0], // Initialised MCP23017. .hd44780 = hd44780[0], // Initialised HD44780. .delay.tv_sec = 0, // 0 full seconds. .delay.tv_usec = 500000, // 0.5 seconds. .row = 1, .col = 4, .length = 16, .frames = FRAMES_MAX, .format[0] = "%H:%M:%S", .format[1] = "%H %M %S" }; // Set up structure to display current date. struct calendar date = { .mcp23017 = mcp23017[0], // Initialised MCP23017. .hd44780 = hd44780[0], // Initialised HD44780. .delay.tv_sec = 60, // 1 minute. .delay.tv_usec = 0, // 0 microseconds. .row = 0, .col = 0, .length = 16, .frames = 1, .format[0] = "%a %d %b %Y" }; // Set ticker tape properties. struct ticker ticker = { .mcp23017 = mcp23017[0], // Initialised MCP23017. .hd44780 = hd44780[0], // Initialised HD44780. .delay.tv_sec = 0, // Seconds. } .delay.tv_usec = 100000, // Microseconds. } Adjust for flicker. .text = "This text is really long and used to demonstrate the ticker!", .length = strlen( ticker.text ), .padding = 6, .row = 1, .increment = 1 }; // Create threads and mutex for animated display functions. pthread_mutex_init( &displayBusy, NULL ); pthread_t threads[2]; /* The HD44780 has a slow response so animation times should be as large as possible and not mixed with other routines that have high animation duty, e.g. ticker and time display with animation. */ pthread_create( &threads[0], NULL, displayCalendar, (void *) &date ); pthread_create( &threads[1], NULL, displayCalendar, (void *) &time ); // pthread_create( &threads[1], NULL, displayPacMan, (void *) pacManRow ); // pthread_create( &threads[1], NULL, displayTicker, (void *) &ticker ); while (1) { }; hd44780Clear( mcp23017[0], hd44780[0] ); hd44780Home( mcp23017[0], hd44780[0] ); // Clean up threads. pthread_mutex_destroy( &displayBusy ); pthread_exit( NULL ); }
// --------------------------------------------------------------------------- // Main (functional test). // --------------------------------------------------------------------------- int main( void ) { struct timeval start, end; uint32_t elapsed; // Elapsed time in milliseconds. uint8_t i; struct display_mode_t { bool data; // Data mode (4-bit or 8-bit). bool lines; // Display lines. bool font; // Font. bool display; // Display on/off. bool cursor; // Cursor on/off. bool blink; // Blink (block cursor) on/off. bool counter; // Counter incrementation. bool shift; // Display shift. bool mode; // Cursor mode. bool direction; // Cursor direction. } display_mode = { .data = 1, // 8-bit mode. .lines = 1, // 2 display lines. .font = 1, // 5x8 font. .display = 1, // Display on. .cursor = 0, // Cursor off. .blink = 0, // Blink (block cursor) off. .counter = 1, // Increment DDRAM counter after data write .shift = 0, // Do not shift display after data write. .mode = 0, // Shift cursor. .direction = 0, // Right. }; struct hd44780 *hd44780this; int8_t err; // Initialise MCP23017. err = mcp23017Init( 0x20 ); if ( err < 0 ) { printf( "Couldn't init. Try loading i2c-dev module.\n" ); return -1; } // Set direction of GPIOs. mcp23017WriteByte( mcp23017[0], IODIRA, 0x00 ); // Output. mcp23017WriteByte( mcp23017[0], IODIRB, 0x00 ); // Output. // Writes to latches are the same as writes to GPIOs. mcp23017WriteByte( mcp23017[0], OLATA, 0x00 ); // Clear pins. mcp23017WriteByte( mcp23017[0], OLATB, 0x00 ); // Clear pins. // Set BANK bit for 8-bit mode. mcp23017WriteByte( mcp23017[0], IOCONA, 0x80 ); hd44780this = malloc( sizeof( struct hd44780 )); // Set up hd44780 data. /* +---------------------------------------------------------------+ | GPIOB | GPIOA | |-------------------------------+-------------------------------| | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---| |DB7|DB6|DB5|DB4|DB3|DB2|DB1|DB0|RS |R/W| E |---|---|---|---|---| +---------------------------------------------------------------+ */ hd44780this->rs = 0x80; // HD44780 RS pin. hd44780this->rw = 0x40; // HD44780 R/W pin. hd44780this->en = 0x20; // HD44780 E pin. hd44780[0] = hd44780this; // Initialise display. hd44780Init( mcp23017[0], hd44780[0], display_mode.data, display_mode.lines, display_mode.font, display_mode.display, display_mode.cursor, display_mode.blink, display_mode.counter, display_mode.shift, display_mode.mode, display_mode.direction ); // hd44780WriteString( mcp23017[0], hd44780[0], "Initialised" ); // Custom characters. const uint8_t meter_chars[CUSTOM_MAX][CUSTOM_SIZE] = {{ 0x1f, 0x17, 0x17, 0x17, 0x17, 0x17, 0x11, 0x1f }, { 0x1f, 0x11, 0x15, 0x11, 0x13, 0x15, 0x15, 0x1f }, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x1f }, { 0x1f, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x1f }, { 0x1f, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x1f, 0x1d, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f }, { 0x1f, 0x1d, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f }}; hd44780LoadCustom( mcp23017[0], hd44780[0], meter_chars ); vis_check(); pthread_mutex_init( &displayBusy, NULL ); pthread_t threads[1]; // Calculate number of samples for integration time. peak_meter.samples = vis_get_rate() * peak_meter.int_time / 1000; if ( peak_meter.samples < 1 ) peak_meter.samples = 1; peak_meter.samples = 2; // Minimum samples for fastest response but may miss peaks. printf( "Samples for %dms = %d.\n", peak_meter.int_time, peak_meter.samples ); // Do some loops to test response time. gettimeofday( &start, NULL ); for ( i = 0; i < TEST_LOOPS; i++ ) { get_dBfs( &peak_meter ); get_dB_indices( &peak_meter ); get_peak_strings( peak_meter, lcd_meter ); // Write to LCD. hd44780Goto( mcp23017[0], hd44780[0], 0, 0 ); hd44780WriteString( mcp23017[0], hd44780[0], lcd_meter[0], 16 ); hd44780Goto( mcp23017[0], hd44780[0], 1, 0 ); hd44780WriteString( mcp23017[0], hd44780[0], lcd_meter[1], 16 ); usleep( METER_DELAY ); } gettimeofday( &end, NULL ); elapsed = (( end.tv_sec - start.tv_sec ) * 1000 + ( end.tv_usec - start.tv_usec ) / 1000 ) / 10; if ( elapsed < peak_meter.hold_time ) peak_meter.hold_count = peak_meter.hold_time / elapsed; pthread_create( &threads[0], NULL, update_meter, NULL ); while ( 1 ) { }; pthread_mutex_destroy( &displayBusy ); pthread_exit( NULL ); return 0; }
int main(void) #endif { HD44780OBJ hd44780ObjOne; HD44780OBJ hd44780ObjTwo; HD44780NUM hd44780One; HD44780NUM hd44780Two; HHD44780 hHd44780One; HHD44780 hHd44780Two; LCDIFFP lcdIfFuncPointers; LCDIFNUM lcdIfOne; LCDIFNUM lcdIfTwo; HLCDIF hLcdIfOne; HLCDIF hLcdIfTwo; // PBIFC18OBJ pbIf; PBIFOBJ pbIf; LCDIFOBJ lcdIfOneObj; LCDIFOBJ lcdIfTwoObj; unsigned char readData=0; unsigned char readAddress=0; unsigned char returnValue; unsigned char * pString; // unsigned int timerValue; /* Init Timer */ T0CON = 0b10001000; /* Init modules */ lcdifInit(); TMR0H = 0x00; TMR0L = 0x00; hd44780Init(); // timerValue = TMR0L; _asm movf TMR0L, 0, ACCESS movlb 0 movwf timerValue, BANKED _endasm timerValue += (TMR0H << 8); /* Make RW, RS and E lines outputs */ #if defined(__18CXX) /* Clear LATD register (functions as */ /* data lines on both PICDEM 2 Plus */ /* boards ) */ LATD = 0; #if defined (PICDEM2PLUS_GREEN) /* On GREEN PICDEM 2 Plus these are: */ /* RD5 = RW */ /* RD4 = RS */ /* RD6 = E */ TRISDbits.TRISD5 = 0; TRISDbits.TRISD4 = 0; TRISDbits.TRISD6 = 0; /* Turn on the LCD display (uses RD7) */ TRISDbits.TRISD7 = 0; LATDbits.LATD7 = 1; #elif defined(PICDEM2PLUS_RED) /* On RED PICDEM 2 Plus these are: */ /* RA2 = RW */ /* RA3 = RS */ /* RA1 = E */ /* Make PORTA bits digital too and */ /* clear LATA register */ ADCON1 = 0x0F; LATA = 0; TRISAbits.TRISA2 = 0; TRISAbits.TRISA3 = 0; TRISAbits.TRISA1 = 0; #endif #endif /* Fill parallel bus interface struct */ #if defined(PICDEM2PLUS_GREEN) pbIf.RW_LAT = &LATD; pbIf.RW_BIT = (1 << 5); pbIf.RS_LAT = &LATD; pbIf.RS_BIT = (1 << 4); #elif defined(PICDEM2PLUS_RED) pbIf.RW_LAT = &LATA; pbIf.RW_BIT = (1 << 2); pbIf.RS_LAT = &LATA; pbIf.RS_BIT = (1 << 3); #endif pbIf.DATA_LAT = &LATD; pbIf.DATA_PORT = &PORTD; pbIf.DATA_TRIS = &TRISD; pbIf.DATA_MASK = 0x0F; /* Fill lcd interface struct */ //#if defined(__18CXX) #if defined(PICDEM2PLUS_GREEN) lcdIfOneObj.E_LAT = &LATD; lcdIfOneObj.E_BIT = (1 << 6); #elif defined(PICDEM2PLUS_RED) lcdIfOneObj.E_LAT = &LATA; lcdIfOneObj.E_BIT = (1 << 1); #endif //#endif lcdIfOneObj.pbIfObject = &pbIf; /* Create an LCD interface */ lcdIfOne = lcdifCreate(&lcdIfOneObj); /* Open the interface */ hLcdIfOne = lcdifOpen(lcdIfOne); #if defined(PICDEM2PLUS_GREEN_OLD) /* Fix nibble swap on address reads */ lcdifFixNibbleSwap(hLcdIfOne); #endif /* Fill an LCD function pointers */ /* struct */ lcdIfFuncPointers.pGetBus = lcdifGetPb; lcdIfFuncPointers.pReturnBus = lcdifReturnPb; lcdIfFuncPointers.pWriteData = lcdifWriteData; lcdIfFuncPointers.pReadData = lcdifReadData; lcdIfFuncPointers.pWriteInstr = lcdifWriteInstruction; lcdIfFuncPointers.pReadAddr = lcdifReadAddress; lcdIfFuncPointers.p4BitFunctionSet = lcdif4BitFunctionSet; TMR0H = 0x00; TMR0L = 0x00; /* Create an HD44780 display object */ hd44780One = hd44780Create(hLcdIfOne, &lcdIfFuncPointers, &hd44780ObjOne); _asm movf TMR0L, 0, ACCESS movlb 0 movwf timerValue, BANKED _endasm timerValue += (TMR0H << 8); TMR0H = 0x00; TMR0L = 0x00; /* Open an HD44780 object */ hHd44780One = hd44780Open(hd44780One); _asm movf TMR0L, 0, ACCESS movlb 0 movwf timerValue, BANKED _endasm timerValue += (TMR0H << 8); do { returnValue = hd44780InstructionInit(hHd44780One, HD44780U, FS_5X8DOTS & FS_2LINE, DOFC_BLINKINGOFF & DOFC_CURSOROFF & DOFC_DISPLAYOFF, EMS_CURSORMOVE & EMS_INCREMENT); if (returnValue > 1) { wait(returnValue); } } while (returnValue != 0); TMR0H = 0x00; TMR0L = 0x00; hd44780DisplayControl(hHd44780One, DOFC_BLINKINGON & DOFC_CURSOROFF & DOFC_DISPLAYON); _asm movf TMR0L, 0, ACCESS movlb 0 movwf timerValue, BANKED _endasm timerValue += (TMR0H << 8); TMR0H = 0x00; TMR0L = 0x00; hd44780WriteChar(hHd44780One, '1'); _asm movf TMR0L, 0, ACCESS movlb 0 movwf timerValue, BANKED _endasm timerValue += (TMR0H << 8); hd44780WriteChar(hHd44780One, '4'); hd44780WriteChar(hHd44780One, '2'); hd44780WriteChar(hHd44780One, '2'); hd44780WriteChar(hHd44780One, 'G'); hd44780WriteChar(hHd44780One, 'S'); hd44780WriteChar(hHd44780One, 'M'); hd44780WriteChar(hHd44780One, ' '); hd44780WriteChar(hHd44780One, '@'); do { returnValue = hd44780SetCursorAddr(hHd44780One, 0x40); } while (returnValue == 0); pString = test; do { pString = hd44780WriteRAMString(hHd44780One, pString); } while (pString != (unsigned char *) 0); #if 0 hd44780EntryModeSet(hHd44780One, EMS_DISPLAYSHIFT & EMS_INCREMENT); hd44780WriteChar(hHd44780One, 'B'); hd44780WriteChar(hHd44780One, 'e'); hd44780WriteChar(hHd44780One, 'l'); hd44780WriteChar(hHd44780One, 'l'); hd44780WriteChar(hHd44780One, 'o'); hd44780WriteChar(hHd44780One, '!'); do { returnValue = hd44780EntryModeSet(hHd44780One, EMS_CURSORMOVE & EMS_DECREMENT); } while (returnValue != 1); do { returnValue = hd44780WriteChar(hHd44780One, 'e'); } while (returnValue != 1); hd44780WriteChar(hHd44780One, 'e'); hd44780WriteChar(hHd44780One, 'c'); hd44780WriteChar(hHd44780One, 'i'); hd44780WriteChar(hHd44780One, 'p'); hd44780WriteChar(hHd44780One, 'S'); hd44780WriteChar(hHd44780One, ':'); hd44780EntryModeSet(hHd44780One, EMS_CURSORMOVE & EMS_INCREMENT); #endif #if 0 //hd44780WriteString(hHd44780One, "This is a test ;o)"); pString = test; do { pString = hd44780WriteRAMString(hHd44780One, pString); } while (pString != (unsigned char *) 0); hd44780ReturnHome(hHd44780One); hd44780ClearDisplay(hHd44780One); hd44780WriteChar(hHd44780One, 0); hd44780WriteChar(hHd44780One, 1); hd44780WriteChar(hHd44780One, 2); hd44780WriteChar(hHd44780One, 3); hd44780WriteChar(hHd44780One, 4); hd44780WriteChar(hHd44780One, 5); hd44780SetCGRAMAddr(hHd44780One,0x00); hd44780WriteCGRAM(hHd44780One, character1, CGRAMFONT_5X8); hd44780SetCursorAddr(hHd44780One, 0x40); hd44780WriteChar(hHd44780One, 0); hd44780WriteChar(hHd44780One, 0); hd44780WriteChar(hHd44780One, 0); hd44780WriteChar(hHd44780One, 0); #endif TRISBbits.TRISB0 = 0; /* Congratulations! If we stop here all tests passed */ while(1) { volatile unsigned int x; for(x=0; x<0x0FFF; x++) { } LATBbits.LATB0 = !LATBbits.LATB0; } }