Browser Based I2C

Introduction

This section has been designed to illustrate the possibilities of using BV products with a PC via a web browser. This is made possible by the use of VBScript, other languages could of course be used but VBScript (VBS) is very simple, can be used with many Microsoft applications and is powerful enough to be useful.

There are lots of choices when accessing the outside world from a PC, these include the printer port, the COM port, USB, Network and dedicated PC card. Unfortunately none of these were intended as a general purpose input output port. The printer port comes close but that is obsolete on some machines. The best answer is to use a protected USB device, this has lots of advantages such as it can power other devices, it is reasonably standard and best of all it can be connected to the PC without any modifications. A protected device should be used if it is intended to use the USB power supply, although the internals of a PC will protect against a short circuit on the USB bus, it is not guaranteed and so the device needs its own protection.

The BV4221

the bv4221 usb to i2c convertorThe BV4221 is a USB to I2C translator or it could be called a terminal. Commands sent to the BV4221 are translated into I2C. The BV4221 acts as an I2C master. For example "s 3 p" means send a start condition using the default I2C device address, send a byte with the value of 3 and then send a stop condition. Another example is "s-20 r g-3 p" This means send a start condition to a device with the address of 20, send a restart condition using address 21, get 3 bytes from the slave and then send a stop condition. The key to understanding how this works is by knowing that the BV4221 behaves as a COM device, in other words when the driver is installed it will become a COM port and any applications that use a COM port can use this device, HyperTerminal for example. In fact to get to grips with the device and any I2C hardware it is connected to BV Terminal is recommended.

The scripts and code presented here assume that you have already got the device working and connected to some I2C hardware, the BV4221 comes with a datasheet. NOTE: VBS does not seem to like COM ports with high numbers, as Windows simply increases the COM port number for every new device defined it can get out of hand, this file explains how to change the COM port number of a particular device.

The htm applications will, most of the time,  work directly off of a web site, they must be downloaded to your PC and run from there if not. There will be one or more warnings about security, this is because VBScript can be dangerous if run from an untrusted source. You will get security warnings that you will have to accept in order for the scripts to run properly. Also the scripts will only run in a browser capable of running VBScript, this is Microsoft's IE.

As an example of how to use I2C with a browser see PCF8591 in the menu list, that page contains code that will, using a BV4221, run the I2C, PCF8591 chip from a browser. There are other examples on the ByVac website and these are listed below.

  • PCF8591.htm This file is a simple demonstration for using the Philips A to D, D to A converter
  • BV4218-lcd+key This is a demonstration for using the BV4218 which is an LCD and keypad interface, it also has a routine for setting up the keys
  • BV4205-ADC.htm The BV4205 is a 20 pin DIL IC, 10 bit A to D converter
  • BV4205-LM35 This uses the A to D chip connected to an analogue LM35 temperature gauge, it will read out the temperature in degrees Celsius.
  • BV4206-gpio.htm This demonstrates the i/o and timing capability of the BV4206. The PWM capability was not added but could be very easily.

The above files contain within them an explanation of what is going on but basically at the top of the file is the VBScript that communicates with the BV4221. Only a handful of routines are needed for this and can be re-used in any application. The next section after the VB functions and subroutines is the events section. This is the part that mostly changes from file to file as it responds to the different inputs from the HTML forms.