Using the BV4221 with Just Basic
Resources: Zip file containing programs
Introduction
The BV4221 is a USB to I2C converter and can be used as an interactive
terminal for manipulating I2C devices or it can be controlled by a program. Used
in this way it is possible to build applications that can be controlled by a PC.
In addition to all of this it can also be used to display the contents of the
I2C bus but this aspect is out of scope for this article.
The programming language of choice for this is Just Basic. The reason for using this is that it is very easy to use and so the principles can be translated to other languages if required. It is also FREE and thus available to all. Just Basic can be obtained form here: http://www.justbasic.com
Getting Started
It is assumed that you have read the data sheet and established communications with the BV4221 via the USB interface. For this article the communication port happened to be COM39 and the chosen Baud rate was 38,400.

The picture shows the experimental set up and the BV4221 connected to a USB cable that goes to the PC.

The block diagram is very simple, all of the power is supplied by the BV4221, Analogue channel 0 is connected to +5V and channel 1 is connected to a variable voltage source that can be varied between 0V and 5V.
Software
All of the software is written in JB and provided as .bas files. These are text files that can be read and compiled by JB. All of the files are in two sections, the firs section deals with the BV devices directly and the second section are general routines and functions for dealing with hex numbers, com ports etc. and are common to all files.
For the purpose of this text and set of files, the LCD (BV4208) address was set to 0x20 and the ADC (BV4205) was set to 0x42, this was done prior to installing the IC's in the above circuit.
The file descriptions are as follows:
BVUtil.bas: A couple of utilities that makes it easier to change the device address.
lcd.bas: This is a simple demonstration of clearing the LCD and sending text
BV-lcd+adc.bas & BV-lcd+adc-auto.bas
Both of these files capture the values on CH0 and CH1 and display them on the LCD display, the first one sets the ADC channel, and reads it. The second uses the feature of the BV4205 whereby it can scan its own channels all that needs to be done is read the appropriate channel.
End of Line
It is important that the end of line (EOL) is handled correctly. All operating systems do this differently and use single or a combination of CR,LF. CR is ASCII 13 and LF is ASCII 10. At the normal line end JB sends CRLF so
print #comm, "fred"
would send "fred"+chr$(13)+chr$(10)
While this would work with the BV4221 it can cause problems and would certainly give rise to two prompts "0x42>0x42>", this is because as far ad the BV4221 is concerned two EOL have been received. To prevent this a routine "pp" is used:
sub pp pr$
print "Sending to device ["+pr$+"]"
print #comm, pr$;
print #comm, chr$(13);
end sub
This outputs only one EOL character and also informs the user what is actually being sent to the BV4221, this feature can be removed after debugging by deleting the first line.
Baud Rate Select
The BV4221 waits for the first CR to select a Baud rate, this is done here:
' send initial CR
print #comm, chr$(13)
call doPause "300"
a$ = input$(#comm, lof(#comm)) ' clear buffer
The last line of this 3 line code is used fairly extensively to clear out the input buffer but it can also be used to check if communication with the BV4221 has been established okay:
if instr(a$,">") > 0 then
Print "Communication with BV4221 okay ";
The above simply searches for the ">" in the prompt. The routines can be written for decimal or hexadecimal mode and so a further check (see file BV-lcd+adc-auto.bas) is done to establish this. The BV I2C devices now all have a command 55 that enables the communication to them to be verified, this is also used.
Because hex is used all of the addresses and numbers are passed in string form and converted if required, this is because JB does not directly handle hex. In hindsight it may have been better to use decimal mode and then there would be less need for the conversions to take place.
lcd.bas was written first and BV-lcd+adc-auto.bas was written last over a period of time and so the first is more primitive. I have purposely not changed the files as the more primitive (less checking) software is easier to follow. The techniques used here are simple to follow and could be applied to any other language, VB Express is also free but it is an order of magnitude more complex to get into, contributions are most welcome.
