HWA
Bare metal programming with style
hwa/atmel/avr/examples/99-03-HD44780-PCF8574/main.c
Drive a LCD through a HD44780 controller connected to a PCF8574 TWI

This example shows how HWA deals with external hardware. Here, a HD44780-based LCD controller is driven through a PCF8574 I²C bus expander connected to a two-wire interface (TWI). The TWI can be embedded into the MCU or software-emulated.

config.h
/* This file is part of the HWA project.
* Copyright (c) 2018 Christophe Duparquet.
* All rights reserved. Read LICENSE.TXT for details.
*/
/* Include the target board (and device) definitions
*/
#include BOARD_H
#if HW_ADDRESS(twi0) != -1
# define TWI twi0
#else
# define TWI HW_SWTWIMASTER( scl, (portc,5), \
sda, (portc,4), \
bps, TWI_BPS )
#endif
#define TWI_BPS 100000
#define LCD HW_HD44780( lines, 2, \
cols, 16, \
e, (PCF, 1, 2), \
rs, (PCF, 1, 0), \
rw, (PCF, 1, 1), \
data, (PCF, 4, 4) )
/* #define LCD HW_HD44780( lines, 2, \ */
/* cols, 16, \ */
/* e, (portb,2), \ */
/* rs, (portb,0), \ */
/* rw, (portb,1), \ */
/* data, (port1,4,4) ) */
#define PCF HW_PCF8574( interface, TWI, address, 0x27 )
#define LCD_LED (PCF, 1, 3)
HW_DECLARE( TWI );
HW_DECLARE( PCF );
HW_DECLARE( LCD );
main.c
/* This file is part of the HWA project.
* Copyright (c) 2018 Christophe Duparquet.
* All rights reserved. Read LICENSE.TXT for details.
*/
#include "config.h"
#include "xprintf.h"
int main(void)
{
hw( configure, TWI, bps, TWI_BPS );
hw( configure, PCF );
hw( clear, LCD_LED );
hw( configure, LCD,
init, yes,
display, on ,
cursor, off,
blink, off,
shift, cursor,
direction, right );
hw( write, LCD_LED, 1 );
while(1) {
int i = 0;
int line = 0;
for(i=0; i<1000 ; i++) {
if ( line == 0 ) {
hw( gotoxy, LCD, 0, line );
xprintf( HW(LCD,putchar), " g=%3d", i);
}
else {
hw( gotoxy, LCD, 8, line );
xprintf( HW(LCD,putchar), " g=%03d", i);
}
line++;
line %= 2;
hw_waste_cycles( 0.25 * HW_SYSHZ );
}
}
}
HW_DECLARE
#define HW_DECLARE(...)
Declares the functions that implement an object.
Definition: hwa_1.h:521
hw
#define hw(...)
hw( action, object [,...] ) executes an action immediately on an object.
Definition: hwa_macros.h:523
swtwimaster.h
Software-emulated TWI master.
pcf8574.h
PCF8574 8-bit I/O expander for I²C bus.
HW
#define HW(...)
Get the definition of an object or a function name from a path.
Definition: hwa_path.h:268
hw_waste_cycles
#define hw_waste_cycles(n)
Definition: hwa_2.h:98
hd44780.h
HD44780 LCD controller.
HW_IMPLEMENT
#define HW_IMPLEMENT(...)
Defines the functions that implement an object.
Definition: hwa_1.h:543