Introduction: $15.00 BASIC Computer?
Imagine a microcontroller that you can connect a serial terminal up to to get an ok prompt? What would happen if you changed one of the control registers can I see the effect straight away? Is it really possible to download programs onto this chip from the Internet and run them? Can I create a program and have it run at reset? Do I only need a serial link and two capacitors to get the ok prompt?
Yes is the answer to all of the above questions. I am the author of machineBASIC and I have been waiting 40 years for a chip manufacturer to produce in a DIL package something powerful enough to run a high level language that is easy to use, thank you Microchip. Some old timers may have heard of a language called Forth. Well this is not Forth but BASIC however it is compiled in the same way as Forth and so has all of the speed and modularisation advantages without the drawback of write only code.
The IC is a PIC32MX1 with machineBASIC (mB) loaded onto it. It is currently only available from www.byvac.com and can be purchased on its own, as a starter kit or as a more comprehensive kit. I will be explaining here how to get the device going with the starter kit, the individual IC is much cheaper than $15, so you will need the following:
*) Starter kit (BV500-s1) or just the IC if you have a USB to serial link
*) 2 x 0.1uF capacitors
*) 1 x 4.7 to 10uF capacitor
*) Solderless Bread board and hook up wire
*) Terminal software from here http://doc.byvac.com/index.php5?title=Downloads (select BV_Com)
All of the above is provided in BV500-k1 but I will assume that you have already got a breadboard of your own.
Step 1: Video Format
Step 2: Starting
The IC like all modern IC's is static sensitive and so have some care when touching the pins. All you need to do is touch some large metal object first to make sure you are discharged before handling the IC.
When the IC's are new the pins are splayed out and so you need to hold them against a flat surface and use a turning action to push them in otherwise they will not mate with the holes in the breadboard.
Place the IC in the breadboard and connect the wires to the serial interface. The serial interface comes with BV500-s1 and because it uses USB it can provide the power needed as well. This device works from 3.3V. 5V will damage the IC and so if using batteries 2 x 1.5V dry cells or 3 x 1.2V rechargeable cells are ideal as the IC will work down to 2.5V.
Connect link wires to the USB serial interface.
This is the type that comes with the starter kit. If you have your own device than it must be able to work with 3.3V. It doesn't matter if it is a 5V type as the pins that are used on the IC for the serial interface are 5V tolerant. This type however will supply 3v3 from the VCC pin. RX and TX are essential but the connection to DTR is optional and is used to conveniently reset the device using the free BV_COM software. Convenient as you don't need to lean over to press any buttons.
The other end of the link wires are pins that will push into the breadboard
Step 4: Hookup
Don't buy expensive hook up wire use telephone, alarm or even CAT5 wire, the latter only if you have some spare off cuts. This works fine and can do a neat job even though the colours are a bit limited.
Here is the complete wiring diagram. Note that there are three ground connections and two connections that go to the 3.3V supply. The Vcap shown here at 10uF is essential, the positive end goes to pin 20. A 4.7uF will also work but 10uF is recommended by the manufacturers. The 0.1uF capacitor connected between 3.3V and ground is there to cut down noise. As a general rule 0.1uF capacitors are sprinkled round electronic circuits much like hundreds and thousands are on a cake.
The 0.1uF capacitor that connects DTR to pin 1 is optional. If you do not have a DTR then use the circuit as shown for resetting the controller.
The complete breadboard should look something like the picture. If you are using the USB to Serial interface form the BV500-s1 kit then you will probably need to install the driver first this can be found here: http://doc.byvac.com/index.php5?title=Product_BV500 When installed and the device plugged you will get another COM port on your PC. This is the port to use for the next step.
Step 5: Software
You can use any terminal software but as it is free and does not need to be installed then use BV_COM, simply copy to a folder and double click on the exe file. It will write an ini file to the folder to remember that last com port etc. used so it does complain a bit when used from a CD-ROM.
Find out which COM port was installed when you plugged the USB device in and use that. The Baud rate must be 115200 and 1 stop and 8 data bits must be set. Press reset and you will something similar to the picture.
Provided you have checked the wiring and made sure that 3.3V has not been exceeded the only complication is the serial connection. Here are some pointers:
1) Check that the TX (pin 21) goes to the RX of the serial device, if not sure swap them round. No harm will be done if they are the wrong way round.
1a) Make sure that the IC is pushed well home into the breadboard
2) It is important that Vcap is in place and is about 10uF. If this is electrolytic then the + end goers to pin 20.
3) Check the Baud rate, stop bits and data bits are correct.
4) Use the alternate reset circuit and replace DTR with a push button to ground – or a bit of wire that can be momentarily connected to ground. Connecting to ground causes a reset at which point data will come out of pin 21. You will be able to see this if you have a scope or logic probe.
5) If no data is coming out then the wiring is incorrect, is the chip the correct way round? Pin 1 is next to the dot and notch on the IC. Are all three ground connections in place, are both power connections in place. Check Vcap again.
6) If data is coming out but nothing appears on the terminal then check the Baud rate is set to 115200 also make sure any hardware handshaking is turned OFF this will prevent any information from getting through.
7) If the voltage has been exceeded or the supply has been connected the wrong way round then the IC will be destroyed. Note that all IC's are programmed and checked before shipping and so a faulty IC will not have been supplied. If you suspect this has happened then return the IC for checking.
8) Leave the set up for today, come back tomorrow and re-check everything very carefully again.
Step 6: Hello World (software)
Type the above in and then type hello() to run the function, you will see "hello World" printed out 5 times
Step 7: Hello World (hardware)
The hello world in hardware or for microcontrollers anyway is to flash an LED, so you will need one to complete this section. Attach an LED to pin 2 as shown the longer pin goes to pin 1 and the shorter pin on the LED goes to ground.
The function here is slightly more complex so I will explain with the aid of the line numbers shown in the picture. This is a file created in notepad or recommended PSPad (also free)
Line 1: This is a comment anything following // is ignored
Lines 2 to 4: These are the actual addresses for the particular microcontroller registers which are described in the data sheet for the PIC32 (MX1 family). The 0x specifies that a hex number is being specified. The actual addresses could be used but it looks better and is easier to remember if they are given a name. There are two ports on this particular device PORTA and PORTB and as can be seen from the wiring diagram pin 2 to where the LED is connected is called RA0. This means the first bit of PORTA which is normally referred to as bit 0. More on this and the registers later.
Line 5: The LED is connected to bit 0 and we want to refer to this bit in several places in the function so we have the constant set to 1 (confusing bit 0 is set cleared and generally manipulated using a value of 1). Why not just have constant LED 1, well we could and that would be okay but supposing we need to refer to bit 15, this would be constant LED 0x8000. If you work it out that is hex for the 15th bit (starting at 0) being high or we could do this LED 1 << 15 and get exactly the same result. The '<<' is the left shift operator and all it does is shift the '1' 15 places to the left - convenient.
Line 7: All programs are made of functions, just like C but unlike C you can call these at any time just by typing ain its name. A function must have () at the end. More advanced functions can have values between the brackets but that is out of scope for this article.
Line 8: The TRIS register controls whether the pin will be an output or an input, and the TRISA register does this for PORTA. If bit 0 of TRISA is set to 1 then RA0 will be an input, if it is set to 0 then it will be an output. The CLR on the end of the word will clear (set to 0)the specified pin and SET on the end of the word will set (set to 1) the specified pin. So this line is setting RA0 to be an output.
Line 8 supplement: The @ in front of the constant name means the contents. Simply setting TRISACLR to LED would change TRISACLR form its current value of 0xBF886014 to 1. By using the @ we are placing a 1 in the address 0xBF886014, we can also use poke(TRISACLR,LED) which would do exactly the same thing.
Line 9: The IC has amongst other things ADC (analogue to digital) hardware and by default all pins are set to use the ADC. The ANSEL register controls this, setting the specified bit to 0 will make the pin on the port digital and 1 will make it analogue. So this line will make RA0 into a digital pin.
Line 10: Here we could simply doe a while loop that went on forever (while 1=1 would do it) and to stop the LED flashing we simply reset the controller. However to be a tad more elegant we use comkey?(2). The controller has two serial ports, UART1 and UART2. The serial interface (connected to pins 21 & 22) is connected to UART2 so the built in function comkey?(2) will return the number of keys in the UART2 key buffer so this will loop until you press a key at the keyboard which will of course place a key in the buffer and so comkey?(0) = 0 will not be true anymore.
Line 11: The LAT register is the actual pins on the port so setting any bits of this register high or low will be reflected on the port. There is also a PORT register that in this case would do the same thing but this is buffered and so it is better to use LAT for output and PORT for input (probably). Just as there is SET and CLR then INV will 'toggle' the specified pin, so it it were a 1 then it would change to 0 and vice versa. This is exactly the behaviour needed to flash the LED
Line 12: The wait command is specified in milliseconds and so this will flash the LED at 1 second intervals.
Step 8: Summery
The above gives a flavour of the language and what it can do. The main advantage is that when a function is written it is immediately available no need to compile or download to see if it works. Microcontrollers are also controlled by their registers and can get a bit complicated. With this we can examine the contents of any register or address just by using the @ or peek.
Creating an application is done by writing functions, if you have a set of stable functions then they can be saved to flash and used as keywords for further development. A function saved to flash with the name 'main()' will run on reset so a stand alone application can be built.
One last thing. As this is a low cost single IC it is very easy and practical to build into projects that require control; something that is not really practical with a development board.
The syntax guide for machineBASIC can be found here http://www.mbasic.byvac.com/index.php?dir=flb/Documentation/