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.

No comments:

Post a Comment