Bascom and AVR, what do you need to get started.


The following list is the minimum you need to get started with the first application: Your first led flasher program.
- a PC with a parallel port running Windows (95 and up).
- Bascom installed on your PC
- the Atmel AVR datasheet of the AT90S2313
- a 'programmer', a device that loads the code that is compiled by Bascom into the program memory
of the AT90S2313
- a test setup with an AVR AT90S2313 microcontroller

The steps you have to take will now be described in more detail. I am assuming you will use the AT90S2313 controller.

Your PC

Your PC should run W95 or higher, check the Bascom AVR page for details. If you want to build your own simple programmer, the PC should have a parallel or serial port. If your PC runs NT, 2000 or XP, access to the parallel port requires a separate small program. Details follow later.


Install Bascom.

Start with downloading the free Bascom version. It will limit the code-size you can compile (translate from Basic to AVR machine code) to 2k, but it will get you started with almost all important aspects of Bascom and AVR controllers.
Go to the MCS Electronics, web-site then select Downloads, then Bascom, then Bascom-AVR, and download the Bascom-AVR Demo Zip-file. Unzip into a temporary directory. Then, start the setup exe program which should now be in this temporary directory. Install Bascom, you may choose your installation directory.
Create a Bascom working directory, for example: C:\Yourname\Bascom\.

Build (or buy) a 'programmer'

Here, a programmer is a device to get to compiled Bascom code into the flash memory of the AVR microcontroller.
The easiest way would be to buy a programmer. If you choose the Atmel STK500, you get a programmerboard with an AVR controller and some peripherals such as led's. This enables you to get going right away. However, building your own programmer is easy and cheap. I have built several, all based on the SP12 and the work of Roland Walter. His programmer hardware is controlled through the PC parallel port with a small program called TWinAVR. You should download that too. Unzip TWinAVR to a convenient location on your PC. There is no setup, you can run TWinAVR.exe directly from the download directory.

Roland's version is a programmer where you have to place the AVR microcontroller in a socket, program, remove the microntroller and place it in the set setup. I did that a few times and considered that it would be possible to modify his programmer hardware to enable In System Programming (ISP). To do ISP, you could simply reserve the microcontroller pins concerned with programming for this purpose. Wire these pins to a suitable connector, connect the programmer and you are ready. However, while the programmer is connected, these pins cannot be used for any other purpose, because there may occur a conflict between the programmer, the microcontroller and anything else attached to these pins. This would not be so much of a problem with large microcontrollers with lots of I/O pins, but on the smaller AVR's, sacrificing these pins to external programming is often not possible. The solution is is modify the programmer in such a way that the programming pins are only connected to the microcontroller while programming is taking place. At other times they should be disconnected or 'isolated'. This is easlily done by adding a buffer to Roland's programmer. The schematic is then modified to use the Vcc generated by some parallel port pins to switch the buffer. For a buffer, I have tried a 74HCT244 and 74HCT541. The outputs of these buffers can be tri-stated, meaning that their outputs can be left isolated or 'floating', thereby preventing problems with other hardware connected to the programming pins.
See the schematic with a 74HCT244 buffer IC. We have also built a version with a 74HCT541, but have experienced occasional problems, where the 74HCT244 version proved very reliable.
These simple programmers can be built on a piece of veroboard. I have tried several layouts, some

large, one built into a

DB25 connector, and one on a

PCB a good friend provided.

You will need a standard DB25-female to DB25-female cable to connect the programmer to the parallel port of your PC. You will have to make a six-wire cable between your programmer and the AVR controller. The wires are:
1. Ground
2. Vcc (+5V, to to be supplied by the breadboard)
3. Reset
4. Clock
5. MISO
6. MOSI
You can use all sorts of cables and connectors. I have used band-cable and straight 8-pin 1/10" headers (8-pins to provide a locking mechanism) as well as standard UTP cable with RJ-45 connectors.

The programmer and programmer software works in general as follows:
1. Pull the Reset low. This will get the AVR into programming mode.
2. Set MOSI (Master-out, Slave-in) according to the state of the bit to be sent.
3. Cycle the clocksignal from low-to-high-to-low, the AVR reads the bit on the MOSI line.
4. Repeat 2 and 3 until a full program word is written into the AVR flash-memory.
5. Read back this word through MISO (Master-in, Slave-out)
6. Verify with programming word sent.
7. Repeat until all programming words have been sent and verified.
The programmer must be powered with an externally supplied +5V, as we are using the parallel port pins that generate Vcc for switching the buffer. Be aware of a potential problem: most modern PC's and laptops will not pull-up the lines you need to a full +5V but sometimes will not get higher then +3V.

The programmer software


Get TWinAVR.
This is the software that communicates through the programmer hardware you built with the AVR controller. I use TWinAVR made by Ronald Walter. Go to his website , in the left-hand column select Microcontroller, select Programmer, read the text (you'll learn some German!) and download TWinAVR. Roland has made a series of well-written articles on Bascom and AVR in the German Funkamateur magazine. This was the series that got me interested in using AVR controllers. Make sure you read Roland's web-pages thoroughly, because he has other nice tools such as a Terminal program and Baud calculator.

Setup TWinAVR.
TWinAVR has to be setup to use the Vcc generated by some parallel port pins to switch the buffer. Start

TWinAVR, click on the TWinAVR logo in the upper lefthand corner, click on

Settings. The

Settings window appears. In this window,

uncheck the Vcc box. This means that Vcc is only generated while TWinAVR is active. If we then use this Vcc to switch the 74HCT244 buffer, the bufferports will be isolated when TWinAVR is not active, resulting in 'In System Programming'. Use this opportunity to check the parallel setting: if your port is not on the standard "&378" address, (check this in the PC's BIOS if necessary) change the LPT setting accordingly.

Testing the programmer hard- and software.
Before you start writing programs in Bascom and try to program an AVR controller, it is wise to test the programmer hard- and software first.
Build a simple

blinking LED application,

preferrably on a breadboard.
You will need:
- one AT90S2313 AVR
- one 4MHz crystal
- two 33pF (or 27pF, not very critical) capacitors
- one LED
- one 560 Ohm (anything from 470 Ohm up to 1 kOhm will do) resistor
- one 10kOhm pull-up resistor on the Reset pin
You also need an external +5V power-supply, able to supply 100 millamperes. Connect the programmer to the breadboard with the 6-wire cable.
We will not need to flash the LED now, that will come in the next chapter. Now, we will test if the PC can communicate with the programmer and if the programmer can communicate with the AVR controller.

Get Userport if you need to

If you are using Windows NT, 2000, or XP, TWinAVR will not have direct access to the parallel port. A solution is to use a special driver program that provides direct access. One such a program is Userport. It must be installed and setup prior to using TWinAVR. First, download Userport. Unzip to a directory, and copy Userport.sys from this directory to C:\Windows\System32\drivers. Read Userport.pdf. Start Userport.exe,

the main window shows default settings. Assuming your parallel port is on "&378", you only need the first entry in the left window to remain. In the left window, select 3BC-3BF and 3E8-3FF and click on Remove. Repeat this for all entries in the right window. What remains

is 200-37F in the left window. Click on Start. Userport now activates the selected adress range for you and will do so every time you start your PC. If you need to use the parallel port for other purposes, say a printer, you must start the Userport program and click on Stop. The driver will then be terminated.

Your first test

Do the Userport stuff if your PC runs Windows NT, 2000 or XP. Use a standard DB25 cable to connect your PC parallel port to your programmer. Connect your programmer using a 6-wire cable to your AVR breadboard setup with the LED flasher schematic.
Power up the AVR breadboard and start TWinAVR. Make sure that the clock speed specified in the 'AVR clock:' window is 4000 kHz (4MHz):

This value does not seem to be very critical, but it should of course not be much larger than the actual crystal frequency you use in the ledflasher example.
Then, click on Config. TWinAVR will now communicate with the AVR on the breadboard and try to read the controller configuration. If all goes well, the

Chip configuration will appear, showing that TWinAVR has indeed detected an AT90S2313 in the breadboard.

Now, if NOT all goes well, TWinAVR will show an Error

window. (For this error to occur, I disconnected the 6-wire cable from programmer to the breadboard)
All the Error windows says is that the 'programming sequence' was not successfull. What to do? You will have to check and recheck several items:
- Is the AT90S2313 breadboard powered up?
- Is the AT90S2313 clock running? Check with an oscilloscope on the Xtal1 pin. It should show a

healthy 4MHz sine wave of app. 4V peak-peak.
- Is the programmer powered up?
- Is your programmer connected to the PC?
- Is your programmer connected to the breadboard?
- Did you connect all six wires correctly? A common mistake (with me...) is to switch MISO and MOSI. Check wiring against programmer schematic and AT90S2313 datasheet
- If your PC runs Windows NT, 2000 or XP, did you run and setup Userport?
- Is your parallel port on "&378"?
- Did you change the TWinAVR setup?
- Is the 'AVR clock:' setting in TWinAVR correct? If this fails, you'll have to check the programmer using an oscilloscope. Run TWinAVR, click on Setup and observe the Reset, Clock, MISO and MOSI lines. Do they behave as expected? You may try a very low 'AVR clock' setting to enable debugging signals with an oscilloscope. If necessary, read the Atmel DOC0943.pdf application note on In System Programming. If that also fails, can you locate someone nearby that has a similar programmer? If yes, perhaps you can compare notes and measurements or swap programmers.

TOC