Outils pour utilisateurs

Outils du site


issue127:tutoriel2

Ceci est une ancienne révision du document !


For a few challenges at home, I thought I could make some projects with a microcontroller. A microcontroller is small, affordable, and it seemed fun to learn something new. I chose the attiny13a from ATMEL Corp for my first few projects because this microcontroller is not too complicated (with a low pinout), but has enough functions for the first projects, and the attiny13a can operate under low power conditions. First, I tried the obvious for a beginner, and installed Arduino and the smeezekitty-core. But, after some experiments, the compiled code got too big and I had to often refer to the Help to look up the Arduino syntax. Thus, I was looking for a BASIC compiler for AVR for Linux – to flatten the learning curve and eventually get smaller hex files. After research, it shows that, unfortunately, most available products are for MS Windows. In the first place, I thought this was the case for Great Cow Basic - no Linux distribution. The Integrated Development Environment (IDE) or the Graphical Programming Tool are not (yet) available for Linux. However, the Great Cow BASIC compiler can run natively on Linux. And, to edit the source user programs in Linux, you could use any text editor you like. The result… a native Linux compiler, with an editor of my choice, and support for the microcontroller I had chosen.

GreatCowBasic Overview

GreatCowBasic comes with a syntax similar to QBASIC/FreeBASIC, and supports flow control statements, math operators and data type, subroutines, functions, data tables, and inline assembler. On the hardware side, things like PWM, SPI, ADC and timer handling are handled via an extensive set of libraries.

With GreatCowBasic, you can produce portable, reusable code for most 8-bit PIC and AVR microcontrollers (actually the project lists around 1100 supported chips); due to some hardware differences, one would have to change the code slightly if one changes from AVR to PIC and vice versa. In addition, the code is, as far as I can say, highly optimized both in speed and size aspects. Recently, a new version (v0.98.01) was released, for this article I used the former version (v0.97). I recommend using the newest release.

Installation

Because FreeBASIC and GreatCowBasic are not available through the package management, you have to install a binary tarball for FreeBASIC, and then compile GCB from source.

To install FreeBASIC, the following commands (change x86 to x86_64 if using a 64 bit computer) should do. If it is more convenient for you, download the files with your browser of choice and omit the first lines. I assume you are in your home directory:

wget ‘https://sourceforge.net/projects/fbc/files/Binaries%20-%20Linux/_ FreeBASIC-1.05.0-linux-x86.tar.gz' -O FreeBASIC.tar.gz

tar xvf FreeBASIC.tar.gz

cd FreeBASIC*

sudo ./install.sh –i

To get the compiler to work without the path prefix, make a softlink:

sudo ln –s /opt/bin/fbc /bin/fbc

After that, the FreeBASIC compiler should just work; try with:

fbc –version

If you get an error message, or, later on, have issues with compiling, you may need some additional software in order to get the FreeBASIC compiler to run. With the following, you install the necessary libraries as stated in the FreeBASIC README:

sudo apt install gcc libncurses5-dev libffi-dev libgl1-mesa-dev libx11-dev libxext-dev libxrender-dev libxrandr-dev libxpm-dev

Get the archive from the GreatCowBasic project site, extract it with unrar (when prompted, enter the password “GCB”).

Optional:

sudo apt install unrar (maybe unrar-unfree depending on *buntu-Version)

wget ‘https://sourceforge.net/projects/gcbasic/files/ _ GCBasic%20-%20Linux%20Distribution/GCB%40Syn_9801.rar/download’ –O GCB.rar

sudo unrar x GCB.rar

For building GreatCowBASIC, I just compile it without the installation script:

cd Great*/Sources/

fbc –exx –v –arch native gcbasic.bas

The installation part works fine, so that you can use the script at least to copy the files to the right place:

sudo chmod +x install.sh

sudo ./install.sh install

Make another softlink (to omit the path prefix later on):

sudo ln –s /opt/GCBASIC/gcbasic /bin/gcbasic

After that, test if it works:

gcbasic /version

The installation is done; you are ready to develop your first programs with Great Cow BASIC!

First program

Have a look at the recently installed folder /opt/Demos. There you can find plenty of examples of projects already done with GreatCowBasic. ‘First-start-sample.gcb’ is a good starting point for a first physical “hello world”, eg, blinking a LED. An attiny13a has an internal clock up to 9.6 MHz which is set at 1.2 MHz fresh from factory. To change this in order to get faster timing, you would have to change the fuses, which is not within the scope of this article. And unless you know what you are doing, do not change the fuses, because you could brick the chip and render it useless.

#chip tiny13a, 1.2 Do Forever

PulseOut PortB.4, 100 ms
Wait 900 ms

Loop

This code infinitely lights up the LED for 100 milliseconds, after this period of time the LED stays off for another 900 milliseconds. Save the code in a text editor under blink.gcb. Then compile it, using GreatCowBasic with the integrated assembler, to a hex file that the attiny13a understands. Other supported AVR chips need other options, have a look at the datasheet for individual changes.

gcbasic /O:blink.hex /A:GCASM blink.gcb

GreatCowBasic gives some information about the compilation: in blink.lst, there is info about used registers and ram usage, and, in blink.html, you get a comprehensive overview about the compile run. For this example, it shows Chip Model: TINY13A, Program Memory: 32/512 words (6.25%), RAM: 0/64 bytes (0.%).

To program the microcontroller afterwards, you need: • 1 breadboard • 6 jumper wires, maybe some short wire bridges • 1 LED, a 5 mm red, yellow, green would suffice • 1 resistor, say 220 ohm • 1 Arduino with ISP-Sketch or any other AVR programmer • (1 10 µF capacitor if using Arduino as ISP, see references)

If using an Arduino UNO as ISP; Put the capacitor between RESET and GND (please note the right polarity), then look at the table for how to connect from Arduino UNO to attiny13a.

After preparing the breadboard, the arduino, and the chip, use avrdude to get the hex file to the attiny13a.

avrdude –p t13 –P /dev/ttyACM0 –c avrisp –b 19200 –U flash:w:blink.hex

This command tells avrdude to flash the hex file with the ArduinoISP on /dev/ttyACM0 with a baudrate of 19200 to the attiny13a (if avrdude complains that the chip identifier does not match, try the –F parameter to force the flash operation, this normally does not brick the chip). After the hex file is written successfully, connect the resistor and the anode (the side with the longer lead) of the LED to PB4 (PIN3). Connect the cathode (with the shorter lead or the flat spot on one side) of the LED with GND.

Note: you could write a small shell script which gets the name of the chip and of the hex file so that GCB has a more streamlined workflow of compiling and flashing the microcontroller. For example, see ‘\GreatCowBasic\flash.sh’ in the programs directory, and check the cli parameter section of the help file. I omitted this step and therefore flashing the microcontroller is an extra step for me.

Conclusion For me, the GreatCowBasic compiler offers a convenient and efficient way to program the microcontroller of choice. Additionally, it gives the option to quickly change the microcontroller between PIC and AVR. The project is active, and the developers (and also the community) seem to be very kind and helpful. There are plenty of device drivers and example code for an array of projects which can be built with it (there are drivers for EEPROM, LCD, RTC - too many to list them all). If this article has attracted your attention, try GreatCowBASIC yourself. In further articles, I will show what the attiny13a and GreatCowBASIC have to offer.

Drop some comments at https://www.evil-publishing.de/fcm if you wish.

References

GreatCowBasic Project http://gcbasic.sourceforge.net/

GreatCowBasic Release notes https://sourceforge.net/p/gcbasic/discussion/579125/thread/d86422f0/

GreatCowBasic help file http://gcbasic.sourceforge.net/help/

GreatCowBasic cli parameter http://gcbasic.sourceforge.net/help/_command_line_parameters.html

FreeBASIC https://www.freebasic.net/

attiny13a datasheet http://www.atmel.com/images/doc8126.pdf

ArduinoISP https://www.arduino.cc/en/Tutorial/ArduinoISP

ArduinoISP > attiny13a https://gist.github.com/dwaq/8239080

BIO

Boris holds a bachelor degree in business administration, and works for an insurance company. While not working, he is a family person and enjoys playing with his kids or tinkering with his personal projects.

issue127/tutoriel2.1511866600.txt.gz · Dernière modification : 2017/11/28 11:56 de auntiee