Arduino BV4618

  • BV4618 Library Download Library in zip format. Updated June 2012 Arduino version 1.01

Also contains other examples.
The BV4618 has two main interface options, Serial (Via a PC COMM port and Via a microcontroller) and I2C. There are therefore two libraries for this, one for serial and the other for I2C. This text will cover both libraries beginging with the serial first.

The data sheet and a pdf version of this can be found at BV4618 Product page

Ardiono Serial Library Class BV4618_S

ByVac Serial Library - Library file for buffered serial communication
VT100 - Library that uses Bserial but will also input (receive) easily

Methods Inherited from Bserial

baud(rate)
handshake(uint8_t rtsPin, uint8_t ctsPin)
flush()
putch(char c)
unsigned char puts(char *s)
unsigned char buffer()
char getch()

A description of what they do and how they work are in the “Bserial Library” which is given in the link above.

Writing to the display, including setting up is simply sending text and so a single method is used for all of this and advantage is taken of the built in compiler escape codes. To write to the display use puts(<string>) (from BSerial). Some Examples (assuming bv is the instance of the class): bv.puts(“\e[2j”); // clear the screen
bv.puts("\e[4L\e[20c"); // set display to 4 lines by 20
bv.puts(“\e[?26I”); // turn off the back light
vb.puts(“\e[3;5HFred”); // print Fred on the third line, 5 column

Basically look at the command table in the datasheet and substitute esc for \e and that it.

Input or Read

This is always more difficult and to help some methods have been created to get information from the device. The best way to see this is to have a look at the example.

Setup

There are a few options for setting up how the device will work. The class itself has two or three options:

BV4618_S bv(rxPin, txPin, int_pin)

The first two parameters are obvious. The last one is a pin to specify for the interrupt pin on the device. This can then be used by the keyint() method and return the value on the pin. Specifying the pin value is optional. When the class has been instantiated, it must be followed by begin:

begin(<Baud rate>,<delay>,<ack>);

Example begin(9600,0,'*');
When receiving data from a serial port, how do you know that you have received the last character? For example retrieving the number of keys in the buffer could return 5 or 12, how do you know to wait for the 2 of 12 and return 12 and not 1?

There are two methods used in this device:

1) is to simply wait until you are sure no more characters are forth coming (about 100mS).

2) to have a special character at the end of every output from the device, this is called and ACK. In the example given and using an ACK character of ‘*’ 12 would be 12*. So it is a simple matter to wait for the ACK and anything before it is the correct output. ACK can be any value from 1 to 255 (not 0) it does not have to be a printable character.

The advantage of this is speed as there is no unnecessary waiting, however in a noisy environment the ACK may be missed and this will spoil the output so just a delay may be advisable in that situation.

Using this with the begin(..) method, set delay to a value in mS, delay can also be used with ACK although I cant see there will be a reason to do so. If ACK is not required then set it to 0 and it will be ignored.

setkeycodes(const char *codes)

The keybuffer can be interrogated with keyscan() or keys(). There is not need to set this if using keyscan as that will always return the scan code. It may be more convenient to map the keys to the values on the keytops. In this case supply this with a constant array of the scan key codes that represent the keytops. For example if the scan code is 0xdd when 0 is pressed then put 0xdd as the first byte in the constant array.

char keyint()

Returns 0 if there is a key in the key buffer otherwise 1. The interrupt pin used must be set in the constructor.

char keyscan()

This returns the scan code from the key buffer.

char keys()

Gets the number of keys in the key buffer.

char key()

Gets 1 byte from the key buffer and looks it up in the constant character array provided to return a key value. If the value can’t be found then it will return 0xff.

clskeybuf()

Clears the key buffer. This is included but can be also set using puts(..)

keydebounce(char db)

This is an arbitrary value (default 50) that will be applied as a delay to the keypad input. A ‘noisy’ keypad will require a greater value but it will cause a delay when entering values. This is included but can be also set using puts(..)

int cmd(string)

This is the workhorse for getting information out of the display. It uses the delay and ACK as set in the begin(..) method. See the example of how to obtain the device ID using this method.

Arduino I2C Library Class BV4618_I

BV4618(char i2adr, char int_pin)

i2adr is the I2C address of the device
int_pin an input pin that is used for detecting when there are keys in the buffer.<//blockquote>

BV4618(char i2adr)

Alternative is interrupt pin is not used. It is possible to pole the keyboard buffer with keys() to see if there is any keys in the buffer and so the interrupt will not be required. This does load the I2C bus and processor more though.

setdisplay(char cols, char rows)

Sets type of display or rather the number of lines and characters it has.

putch(char c)

Sends a single character to the display.

puts(char *s)

Sends a string to the i2c bus. The string must be a string, i.e. null terminated.

crup()

Moves cursor up one line.

crdown()

Moves cursor down one line.

crright()

Moves cursor right one space.

crleft()

Moves cursor left one space.

rowcol(char line, char col)

Moves the cursor to a specified row and column position.

lineposition(char line, char pos)

To move to the start of a particular line on a display an address is sent as a command. This address is normally 0x80,0xc0,0x94 and 0xd0 for lines 1 to 4 respectively. However on a 2 line x 40 display or some other combination this may not be the case. This command will set the starting line address for the line specified.

backlight(char blon)

Turns the back light on and off, 1 is on.

crhome()

Returns the cursor to the home position – without clearing the screen.

int deviceid()

Obtains the device ID which will be 4618, this is useful if multiple devices are on the same bus

version(char *ver)

Gets the firmware version as a string.

setaddress(char newaddress)

Sets a new I2C address for the device. The new address will not take effect until the device has been reset.

reset()

Resets the device.

resetEEPROM()

Resets EEPROM back to the factory settings, this will also set the I2C address back to its default setting.

delayms(char del)

Causes a delay of ‘del’ milliseconds, the display will stop responding for that time. As there is no ACK mechanism for I2C there is no way to tell if the delay has finished so it limits the usefulness of this command when using I2C

delays(char del)

Causes a delay of ‘del’ seconds, the display will stop responding for that time. As there is no ACK mechanism for I2C there is no way to tell if the delay has finished so it limits the usefulness of this command when using I2C.

cls()

Clears the screen and sets the cursor in the home position

clright()

Clears a line from the current cursor position to the end of the line.

cleft()

Clears a line from the current cursor position to the beginning of the line.

clall()

Clears the whole line that the cursor is presently on.

setkeycodes(const char *codes)

The keybuffer can be interrogated with keyscan() or keys(). There is not need to set this if using keyscan as that will always return the scan code. It may be more convenient to map the keys to the values on the keytops. In this case supply this with a constant array of the scan key codes that represent the keytops. For example if the scan code is 0xdd when 0 is pressed then put 0xdd as the first byte in the constant array.

char keyint()

Returns 0 if there is a key in the key buffer otherwise 1. The interrupt pin used must be set in the constructor.

char keyscan()

This returns the scan code from the key buffer.

char keys()

Gets the number of keys in the key buffer.

char key()

Gets 1 byte from the key buffer and looks it up in the constant character array provided to return a key value. If the value can’t be found then it will return 0xff.

clskeybuf()

Clears the key buffer.

keydebounce(char db)

This is an arbitrary value (default 50) that will be applied as a delay to the keypad input. A ‘noisy’ keypad will require a greater value but it will cause a delay when entering values.

I2C Example

The software and example have been tested on the Arduino Nano with an ATmega328 fitted.

Wiring the display is straightforward. Connect the BV4618 to the Arduino using the +5V and GND lines. SDA goes to A4 and SCL goes to A5 on the Arduino. Pin 9 (D9) was used as the interrupt pin and this goes to the Int pin on the BV4618. A 4x4 keybaord was also connected to the BV4618.

Bv4618 i2c library example.jpg

The above is an extract from the example. Line 1 initialises the class and passes the I2c address (7 bit) and the pin that is used for the interrupt. Line 5 is the constant character array that is used to translate scan codes to key numbers (there is a few values missing from this picture). When pressing the 0 key the scan code 0x7d was returned, when pressing 1 0xee was returned etc. The codes are set using di.setkeycodes();

Line 8 sets the display to a 4 line by 20 character. If this is omitted the display controller will default to 2x16.

Line 17 gets the state of the interrupt pin. Low means there are keys in the keypad buffer that can be read out. A simple translation to text is performed and printed out to the LCD display.