Programming Arduino with GCBasic

Experiment #1 – A simple LED blink program:

Now that your GCBasic IDE is installed, you can try your first program – blinking the LED on the Arduino Uno board.

Open the Crimson Editor and click on the menu item Document”, then select GCBasic from the list. Type the lines of code at the right into the Crimson Editor window. Notice that key words and parameters that are recognized by GCBasic are highlighted automatically by Crimson. When you are sure that the program is entered correctly, Select the menu item “File”, “Save As” and then type in the name you want to give to this program file. Let’s call it “BlinkLED” for now.

Notice a few important things:

  • The’#chip’ statement tells GCBasic that the Arduino Uno uses an Atmega 328p chip running at a crystal clock frequency of 16 Mhz. GCBasic needs to know the clock frequency in order to time things like ‘wait’ statements correctly.
  • The ‘dir’  statement tells GCBasic that I/O port B, bit 5 is to be treated as an output. This is the programmable LED on the Arduino Uno board. We’ll refer to it in the program as “LED”.
  • ‘LED = 1’ sets portb.5 high. It could also be written as ‘LED = on’. I put it in to initialize the pin to a known state, but it serves no other useful purpose in this application.
  • The main part of the program is just a continuous loop, starting at the label ‘start:’ and ending at ‘goto start’
  • In the Arduino Uno, setting portb.5 to “1” (0r ‘on’) turns the LED on. “0” or ‘off’ turns it off.

Compile the program and upload it into the Arduino board. The LED should flash on and off, pausing 1/2 second in each state.

Experiment #2 – Using timer interrupts to keep time in the background

In this experiment, we’ll use one of the AVR chip’s internal hardware timers to generate precisely timed interrupts instead of having to code software ‘wait’ statements that use 100% of the chip’s throughput while the wait is timing out. This allows us to do the timekeeping in the background while we do other things in our main program.

As in the previous example, lines 1 through 4 define the hardware configuration and assigns a name to the LED I/O port.

Line 7 initializes Timer1 (a 16 bit counter with a counting range of 65,536). We specify that the 16 Mhz clock should be preceded by a fixed divide by 256 prescaler and feed the output into Timer1.

Line 8 defines an interrupt that will occur every time Timer1 overflows (about once per second in this example) and prescribes that the program will execute the subroutine TMR1_Interrupt every time this happens.

Line 9 starts the timer. Interrupts will now occur once per second unless the instruction “StopTimer 1” is encountered.

Lines 11 through 14 are the main program loop. This is where your main program would go. Nothing happens here in this example.

Line 17 is the interrupt processing routine. Every time Timer1 overflows, the chip will immediately stop whatever it was doing (just looping in “Main” in this example) and execute the code in this routine. In this case, we coded a ‘brute force’ method to toggle the state of the LED pin every time the interrupt occurs. A much simpler and more elegant way to do this would be to exclusive-or portb with a bit pattern that has a “1” in bit 5.

Compile the code and upload it to your Arduino Uno. If all goes well, The LED should continuously blink on and off at one second intervals.

Experiment #3 – Even I don’t know yet


Stay tuned…….

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: