Thursday, September 30, 2010

GCBASIC Tutorial #1: Play with LED

The first tutorial I will focus on how to light up LED. If you’re reading any programming books you will found their first tutorial is about how to print ‘Hello World’. Same as any programming language you needed to know the basic programming of microcontroller.

As I mention in last post I only focus on PIC16F84a microcontroller. Below is the basic circuit of PIC16F84a with 8 LED. It used clock of 4 MHz.



The basic program of microcontroller is to light up the LED. It will light up for the first LED and then to second LED until last LED. At one time only one LED is ON for 250 milliseconds. If all LED is ON it will repeat to the first LED.

To program a microcontroller, firstly you need to declare a type of microcontroller. As for this tutorial I use PIC16F84a. The #chip is used to determine the program to refer microcontroller and clock is used.

Syntax: #chip model, speed

'A program to flash LED in sequence

'Chip model
#chip 16F84a, 4   ‘used PIC16F84a with clock of 4 Mhz

After that you need to set the direction of the port. The direction is the input/output of the port. Each port usually consists of 4 to 8 pins. Each pin you can set either as the input or output. The #dir is used to set direction of the port.

Syntax:
    Dir port.bit {In | Out} (Individual Form)
    Dir port {In | Out | DirectionByte} (Entire Port Form)

'Set the pin directions
dir PORTB out                        ‘All pin in Port B is set output direction

Once define is complete you can start program the microcontroller. Below you can find that I use two styles to light up the LED.

First I used simple PORTB. It can set which pin to ON. As an example I set only pin 0 is ON. It is set used decimal number. You also can set in binary or hexadecimal as below.

PORTB = b’00000001’ or PORTB = h’01’

Second method I used #rotate command. It will rotate variable in specified direction. In this case I use to rotate PORTB variable to the left. Example below will describe better.

Syntax:    
    Rotate variable {Left | Right} [Simple]

00000001 -> 00000010 -> 00000100 -> 00001000 -> 00010000 -> 00100000 -> 01000000 -> 10000000

However to have the variable to move from right to left you need a loop. #for…next command was used. Below is the main program for this tutorial. Not to forget #wait command is used to delay for specified time.

'Main routine
Start:

 'Turn one LED on, the other off
 PORTB = 1
 wait 250 ms

 for loop = 1 to 7       
            rotate PORTB left simple
            wait 250 ms    
 next

goto Start

You can copy all text programming from declaration to main program as above in one file. I suggested you copy all that in notepad and save the file as LED.txt.

After that you can compile it using GCBASIC. The hex file will produce by the compiler. Take the hex file to download into your microcontroller. Good luck.

Click the images below to enlarge and watch the LED run in animation.

Monday, September 27, 2010

3 Step to setup microcontroller project

Electronic projects will not complete without microcontroller. It is the most easier and low cost project you could ever build. You only need to program and it will run accordingly. However if you want to start microcontroller projects you need only three step to setup a project as describe below.

1. Need microcontroller
Microcontroller is small device consists of CPU, memory (RAM and ROM), I/O port, serial and parallel port, timers, and some type of microcontroller have built in Analog to Digital (A/D) and Digital to Analog (D/A). The cost of single microcontroller relatively is cheap compare to microprocessor.

For the mean time I only focus microcontroller of PIC. After this I will write the tutorial only for PIC16F84A and PIC16F877A. This PIC is the easiest to setup and to program for the beginner.

If you search through internet you will get more type of microcontroller. After all is up to you to decide the best. I suggest for the beginner you can start with PIC to gain an experience.

2. Compiler
Before I go too far it is better I introduce the compiler. The compiler will able a user to program in high-level language. It is using English style language and easier than assembly language.

As student or beginner user, I prefer to use open source compiler. There are varieties of open source compiler available in internet. The popular open source compiler is Small Device C compiler (SDCC) or Great Cow Basic (GCBASIC). SDCC is using C language and GCBASIC using Basic language.

The compiler also will execute your program and produce hex file. The hex file is binary code. It will upload to your microcontroller using programmer. It also produce assembly file. The file could use to simulate your code using simulation program.

3. Programmer
Programmer consists of software and hardware to upload hex file into microcontroller. The software is a program to communicate with programmer hardware via serial or parallel communication. It will take hex file produce by compiler to upload into microcontroller.

The programmer hardware however is to burn binary code from hex file into ROM of microcontroller. After it successfully burned the microcontroller is ready to use.

Software and hardware programmer have varieties type in market. You can buy or build it yourself. There are many circuits available in internet to setup hardware yourself and download software freely. The most popular software and hardware of programmer is at http://www.winpicprog.co.uk/

Finally I hope that you now have an idea how to setup microcontroller project. I also will try to put some tutorial to program microcontroller. The compiler that I used is GCBASIC compiler.

Saturday, September 25, 2010

Great Cow Basic (GCBASIC) installation guide

After struggling to install GCBasic for few days finally I make it. I had installed two version of GCBasic. The versions are Version 24/2/2007 and 6/2/2010. You can download it at http://gcbasic.sourceforge.net/download.html.

These two versions give different resulted. I prefer to use version 6/2/2010 because it give satisfying resulted. However I make an adjustment to gcbasic.ini. For the version 24/2/2007 I think the gputils was outdated. If you want to use it make sure you download GCBASIC minimal 0903.exe and download the latest gputils.

In this post I only focus to version 6/2/2010. The adjustment gcbasic.ini is the assembler path. I used MPWINASM to compile the program. Luckily I already have MPASM which is I did installed the Proteus software before. It will produce asm file, hex file and lst file when you compile your program. However you can use either MPASM or GPASM to compile your program.


The gcbasic.ini adjustment
Prior to use GCBasic you must make sure that gcbacic.ini correctly modifies. At your GCBasic folder you can edit sample gcbasic.ini file in notepad. Prior to that makes sure rename the file to gcbasic.ini. Then open it using notepad to modify.








Observe the red underline as picture above (Please click picture to enlarge). I rewrite the assembler path to my Proteus path so that I can access my MPASM in Proteus without to install twice.

First program
After the gcbasic.ini correctly setup, it is time to test the software. The test can be performing by programming GCBASIC first. In this example I write a program to ON a led in pin 0 of port B. I use PIC16F84A with clock of 4 MHz.

'A program to ON a  LEDs on PORTB,

'Chip model
#chip 16F84a, 4

'Set the pin directions
dir PORTB.0 out

'Main routine
Start:

 'Turn one LED on, the other off
 SET PORTB.0 ON

goto Start

 Copy the program above into your notepad and save it to test.txt. I suggest that the file is place on your desktop (only for this tutorial). Open your GCBasic folder, usually c:\program files\Great Cow BASIC.

In this folder you will see gcbasic.exe file. Drag your test.txt file at your desktop onto gcbasic.exe. After that your test.txt will compile and it will automatically created new files. It will produce compiled.asm, compiled.hex and compiled.lst.

Use compiled.hex to programmer into your PIC16F84a. I consider that you already know how to programmer the PIC. If don’t know… please google…

However for this program I not try in real PIC. I just use Proteus to simulate the program. Below is the circuit diagram for this program. Good luck.