Outils pour utilisateurs

Outils du site


issue127:tutoriel2

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.

Pour quelques défis à domicile, j'ai pensé que je pourrais faire des projets avec un microcontrôleur. Un microcontrôleur est petit et abordable, et il semblait amusant d'apprendre quelque chose de nouveau. J'ai choisi le attiny13a de ATMEL Corp pour les tous premiers projets car ce microcontrôleur n'est pas trop compliqué (avec peu de bornes), mais il a assez de fonctions pour les premiers projets ; en outre, le attiny13a peut fonctionner sous des conditions de puissance réduites. D'abord, j'ai essayé un projet évident pour un débutant et j'ai installé Arduino etles fonctions centrales de « smeezekitty » (NdT : un développeur). Mais, après quelques essais, le code compilé devenait trop gros et je devais me référer souvent à l'aide pour vérifier la syntaxe d'Arduino. Ainsi, j'ai cherché un compilateur BASIC pour « AVR for Linux » - pour réduire la courbe d'apprentissage et éventuellement obtenir des fichiers hexa plus petits. Après une recherche, il apparaît, malheureusement, que la plupart des produits disponibles sont pour MS Windows. Au début, j'ai cru que c'était le cas du compilateur Great Cow Basic - pas de distribution Linux. L'IDE (interface de développement intégré) ou l'outil de programmation graphique ne sont pas (encore) disponibles sous Linux. Toutefois, le compilateur Great Cow BASIC peut tourner nativement sous Linux. Et, pour modifier les sources des programmes de l'utilisateur dans Linux, vous pouvez utiliser n'importe quel éditeur de texte que vous préférez. Le résultat… un compilateur natif Linux, avec l'éditeur de mon choix, et du support pour le microcontrôleur que j'ai choisi.

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.

Présentation de GreatCowBasic

GreatCowBasic arrive avec une syntaxe similaire à celle de QBASIC/FreeBASIC, et supporte les déclarations de contrôle de flux, les opérateurs mathématiques et les types de données, les sous-routines, les fonctions, les tables de données et un assembleur en ligne. Du côté matériel, des choses comme PWM, SPI, ADC et la gestion d'un timer sont gérées via un ensemble étendu de bibliothèques.

Avec GreatCowBasic, vous pouvez produire du code portable et réutilisable pour la plupart des microcontrôleurs PIC et AVR (le projet liste actuellement à peu près 1100 puces supportées) ; du fait de certaines différences dans le matériel, le code devra être modifié légèrement en passant de AVR à PIC et vice-versa. De plus, le code est, pour autant que je puisse le dire, hautement optimisé à la fois en vitesse et en taille. Récemment, une nouvelle version (la v0.98.01) a été publiée ; pour cet article, j'ai utilisé la version précédente (la v0.97). Je recommande d'utiliser la plus récente.

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

Installation

Parce que FreeBASIC et GreatCowBasic (GCB) ne sont pas disponibles par le gestionnaire de paquets, vous devez installer un tarball du binaire de FreeBASIC et, ensuite, compiler GCB à partir des sources.

Pour installer FreeBASIC, les commandes suivantes (modifiez x86 en x86_64 si vous utilisez un ordinateur 64-bit) feront l'affaire. Si vous préférez, téléchargez les fichiers avec votre navigateur Web préféré et sautez les premières lignes. Je suppose que vous êtes dans votre répertoire home :

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

Pour que le compilateur fonctionne sans chemin préfixé, faites un « softlink » (lien symbolique doux) :

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”).

Après ceci, le compilateur FreeBASIC devrait fonctionner tout simplement ; essayez avec :

fbc –version

Si vous obtenez un message d'erreur, ou, plus tard, si vous avez des problèmes en compilant, vous pourriez avoir besoin de logiciels additionnels pour permettre au compilateur FreeBASIC de fonctionner normalement. Avec la ligne suivante, vous installez les bibliothèques nécessaires comme indiqué dans le README de FreeBASIC :

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

Récupérez l'archive sur le site du projet GreatCowBasic et extrayez-le avec unrar (au prompt, entrez le mot de passe « 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

Optionnel :

sudo apt install unrar (unrar-unfree dépend peut-être de la version de *buntu)

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

sudo unrar x GCB.rar

Pour construire GreatCowBASIC, je le compile simplement sans le script d'installation :

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!

La partie installation se passe bien, de sorte que vous pouvez utiliser le script au moins pour copier les fichiers à leur bonne place :

sudo chmod +x install.sh

sudo ./install.sh install

Faites un autre softlink (pour omettre plus loin le chemin préfixé) :

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

Après cela, testez s'il fonctionne :

gcbasic /version

L'installation est faite ; vous êtes prêt à développer vos premiers programmes avec 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.

Premier programme

Regardez dans le dossier /opt/Demos récemment installé. Il y a plein d'exemples de projets déjà faits avec GreatCowBasic. « First-start-sample.gcb » est un bon point de départ pour un premier « hello world » réel, par ex., une LED clignotante. Le attiny13a a une horloge interne jusqu'à 9,6 MHz qui est réglée à 1,2 MHz en sortie d'usine. Pour la changer pour avoir un séquencement plus rapide, vous devez changer les fusibles, ce qui ne relève pas de cet article. Et, sauf si vous savez comment faire, ne changez pas les fusibles, car vous pourriez endommager la puce et la rendre inopérante.

#chip tiny13a, 1.2 Do Forever

PulseOut PortB.4, 100 ms
Wait 900 ms

Loop

Ce code allume sans fin la LED pendant 100 millisecondes, après quoi la LED reste éteinte pendant 900 autres millisecondes. Dans un éditeur de texte, sauvegardez le code sous blink.gcb. Puis, compilez-le en utilisant GreatCowBasic avec l'assembleur intégré, dans un fichier en hexa que le attiny13a comprend. D'autres puces AVR supportées nécessitent d'autres options ; regardez la feuille de données pour des modifications individuelles.

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.

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

GreatCowBasic donne quelques informations sur la compilation : dans blink.lst, il y a des infos sur les registres utilisés et l'usage de la RAM et, dans blink.html, vous trouvez une présentation complète sur l'exécution de la compilation. Pour cet exemple, il montre : Modèle de puce : TINY13A, Mémoire du programme : 32/512 mots (6.25%), RAM: 0/64 octets (0.%).

Pour, ensuite, programmer le microcontrôleur, vous avez besoin de : • 1 platine d'essai • 6 fils de liaison, peut-être de courts cavaliers, • 1 LED ; une 5 mm rouge, jaune ou verte devrait suffire • 1 résistance, disons de 220 ohms • 1 Arduino avec ISP-Sketch ou tout autre programmeur AVR • (1 condensateur de 10 µF si vous utilisez Arduino comme ISP, voir les références)

Si vous utilisez un Arduino UNO comme ISP : mettez le condensateur entre RESET et GND (masse) (faites attention à la polarité), puis regardez la table pour voir comment connecter l'Arduino UNO au 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.

Après avoir préparé la platine d'essai, l'Arduino et la puce, utilisez avrdude pour passer le fichier hexa au attiny13a.

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

Cette commande dit à avrdude de flasher le fichier hexa de ArduinoISP dans /dev/ttyACM0 avec une vitesse de transfert de 19200 bauds vers le attiny13a (si avrdude se plaint que l'identifiant de la puce ne correspond pas, essayez avec le paramètre -F pour forcer l'opération de flashage ; normalement, ceci ne détruit pas la puce). Une fois que le fichier hexa a été correctement écrit, connectez la résistance et l'anode (le côté avec la patte la plus longue) de la LED à PB4 (PIN3). Connectez la cathode (la plus courte patte ou une petite partie plate sur le côté) de la LED à la masse (GND).

Note : vous pouvez écrire un petit script shell qui donne les noms de la puce et du fichier hexa de sorte que GCB a un traitement plus fluide en compilant et en flashant le microcontrôleur. Par exemple, regardez ‘\GreatCowBasic\flash.sh’ dans le répertoire des programmes et vérifiez la section du paramètre cli du fichier d'aide. J'ai omis cette étape et donc le flashage du microcontrôleur est une étape supplémentaire pour moi.

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.

Conclusion

Pour moi, le compilateur de GreatCowBasic offre un moyen pratique et efficace de programmer le microcontrôleur de son choix. En plus, il donne l'option de changer rapidement de microcontrôleur entre PIC et AVR. Le projet est actif et les développeurs (et aussi la communauté) semblent être très aimables et serviables. Il y a plein de pilotes de composants et d'exemples de code pour tout un ensemble de projets qui peuvent être construits avec lui (il y a des pilotes pour EEPROM, LCD, RTC - trop pour tous les lister). Si cet article a attiré votre attention, essayez GreatCowBasic. Dans les prochains articles, je montrerai ce qu'offrent attiny13a et GreatCowBasic.

Si vous le souhaitez, laissez des commentaires sur : https://www.evil-publishing.de/fcm.

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

Références

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

Notes de publication de GreatCowBasic https://sourceforge.net/p/gcbasic/discussion/579125/thread/d86422f0/

Fichier d'aide de GreatCowBasic http://gcbasic.sourceforge.net/help/

Paramètres de ligne de commande de GreatCowBasic http://gcbasic.sourceforge.net/help/_command_line_parameters.html

FreeBASIC https://www.freebasic.net/

Feuille d'information de attiny13a 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.

BIO

Boris est diplômé en gestion des affaires et travaille pour une compagnie d'assurance. Quand il ne travaille pas, il reste en famille, prenant plaisir à jouer avec ses enfants et à bricoler avec ses projets personnels.

issue127/tutoriel2.txt · Dernière modification : 2017/12/07 13:52 de auntiee