issue128:tutoriel2
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue128:tutoriel2 [2018/01/10 19:45] – d52fr | issue128:tutoriel2 [2018/01/13 14:14] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 9: | Ligne 9: | ||
Now, open it from your start menu. First, we get Geany to handle Great Cow BASIC code as FreeBASIC code. Most of the Great Cow BASIC syntax then gets highlighted. This works good for me. Edit the filetype_extensions.conf under “tools - configuration files”, lookup the entry for FreeBASIC, and add “; | Now, open it from your start menu. First, we get Geany to handle Great Cow BASIC code as FreeBASIC code. Most of the Great Cow BASIC syntax then gets highlighted. This works good for me. Edit the filetype_extensions.conf under “tools - configuration files”, lookup the entry for FreeBASIC, and add “; | ||
- | Dans le premier article, j'ai présenté Great Cow BASIC et comment vous faire clignoter une LED en allumant et en éteignant dans une boucle et en laissant le microprocesseur | + | Dans le premier article, j'ai présenté Great Cow BASIC et comment vous pouvez |
- | Cette fois-ci, je veux montrer une version plus élaborée | + | Cette fois-ci, je veux montrer une version plus élaborée |
- | Mais, avant ceci, je veux obtenir un éditeur de texte avec des fonctions de mise en valeur de la syntaxe | + | Mais, avant cela, je veux obtenir un éditeur de texte avec la coloration syntaxique |
sudo apt install geany | sudo apt install geany | ||
- | Maintenant, ouvrez-le à partir de votre menu initial. D' | + | Maintenant, ouvrez-le à partir de votre menu démarrer. D' |
**Afterwards, | **Afterwards, | ||
Ligne 29: | Ligne 29: | ||
This way, the compiled program gets right into your microcontroller. Beware: After those steps, the build parameters for FreeBASIC are overwritten, | This way, the compiled program gets right into your microcontroller. Beware: After those steps, the build parameters for FreeBASIC are overwritten, | ||
- | Ensuite, ouvrez le menu « Construire - Définir les commandes de construction » et changez la commande de compilation pour | + | Ensuite, ouvrez le menu « Construire - Définir les commandes de construction » et changez la commande de compilation pour : |
gcbasic / | gcbasic / | ||
- | qui alors compile le fichier d' | + | qui compile |
avrdude -p t13 -c avrisp -P / | avrdude -p t13 -c avrisp -P / | ||
- | De cette façon, le programme compilé | + | De cette façon, le programme compilé ira immédiatement dans votre microprocesseur. Attention : après |
**Dimming LED | **Dimming LED | ||
Ligne 51: | Ligne 51: | ||
LED à éclairage variable | LED à éclairage variable | ||
- | Pour commencer, j'ai présenté le code source pour une LED simplement clignotante. Si vous planifiez de créer | + | Pour commencer, j'ai présenté le code source pour une LED qui ne fait que vaciller. Si vous envisagez la création d'un programme pour une lampe flash pour le morse ou pour un indicateur lumineux, ce sera tout ce qu' |
• un PWM logiciel, | • un PWM logiciel, | ||
• un PWM en mode fixe, et | • un PWM en mode fixe, et | ||
- | • un PWM matériel basé sur le Timer0 de l'AVR d' | + | • un PWM matériel basé sur le temporisateur |
+ | Nous pouvons utiliser les modes PWM, soit logiciel, soit matériel. Pour paramétrer le PWM matériel pour vous-même, vous devrez régler les registres du microcontrôleur ; ceci est en dehors de notre sujet actuellement , mais, peut-être à un moment futur, il pourrait être nécessaire de supprimer le dernier octet de la mémoire flash du microcontrôleur. Si vous avez une demande pressante, référez-vous à la feuille de données. | ||
**The software PWM switches the given channel with the required amount of current for the given number of cycles (called the duty cycle). Since the duty cycle is defined as a byte value, the duty cycle would be between 0 and 255, think of it like this: 255 means 100 % current, 127 would be around 50 % current and so on. The number of cycles defines the number of pulses of the software PWM and defines how long the LED is dimmed. Although instantly repeated while in the main loop, the higher the cycles the smoother it will look. With the software PWM you can light up an arbitrary number of LEDs – contrary to the hardware PWM which is limited to the channels it is linked to. But it’s at the cost that all is software calculated, and the microcontroller is busy while the LED is lit. With the following code you can set up a randomly flickering LED with software PWM. | **The software PWM switches the given channel with the required amount of current for the given number of cycles (called the duty cycle). Since the duty cycle is defined as a byte value, the duty cycle would be between 0 and 255, think of it like this: 255 means 100 % current, 127 would be around 50 % current and so on. The number of cycles defines the number of pulses of the software PWM and defines how long the LED is dimmed. Although instantly repeated while in the main loop, the higher the cycles the smoother it will look. With the software PWM you can light up an arbitrary number of LEDs – contrary to the hardware PWM which is limited to the channels it is linked to. But it’s at the cost that all is software calculated, and the microcontroller is busy while the LED is lit. With the following code you can set up a randomly flickering LED with software PWM. | ||
The Setup of the software PWM is easy; you define one or more PINs used as a PWM channel. Then, the LED lights up at random brightness and goes off after the delay. This first result looks like an unsteady flickering candle; with refinements to the code it will look a bit smoother. We will come back to this point later.** | The Setup of the software PWM is easy; you define one or more PINs used as a PWM channel. Then, the LED lights up at random brightness and goes off after the delay. This first result looks like an unsteady flickering candle; with refinements to the code it will look a bit smoother. We will come back to this point later.** | ||
+ | |||
+ | Le PWM logiciel commute le canal donné avec la quantité requise de courant pour un nombre défini de cycles (appelé le rapport cyclique). Comme le rapport cyclique est défini par une valeur en octets, il sera entre 0 et 255 ; pensez à ceci : 255 signifie 100 % du courant, 127 sera environ 50 % du courant et ainsi de suite. Le nombre de cycles définit le nombre d' | ||
+ | |||
+ | Le paramétrage du PWM logiciel est facile : vous définissez une ou plusieurs bornes utilisées comme canaux PWM. Puis, la LED s' | ||
**The hardware PWM, on the other hand, works similarly but uses hardware circuitry within the microcontroller to generate the required on/off pulses. The frequency and the duty cycle of the fixed mode PWM (ref. No. 6) can be set only once in the program out-of-the-box. This mode could be useful for applications where you want to light a LED at a constant level , or for other purposes which need a constant powerage. This can be changed, but you must change it within the source code. | **The hardware PWM, on the other hand, works similarly but uses hardware circuitry within the microcontroller to generate the required on/off pulses. The frequency and the duty cycle of the fixed mode PWM (ref. No. 6) can be set only once in the program out-of-the-box. This mode could be useful for applications where you want to light a LED at a constant level , or for other purposes which need a constant powerage. This can be changed, but you must change it within the source code. | ||
For dimming the LED, we want to change at least the duty cycle. Instead of calculating the timings in software, there is an oscillator in the Atmel microcontroller which gives the timing for the pulses. The attiny13a has one 8-bit Timer with 2 channels for hardware PWM. One channel ‘ OC0A’ is not available for hardware PWM because it is sacrificed for other usages, eg, the pseudo random number generator. But the channel ‘OC0B’ can be used for hardware PWM and thus for dimming a LED. The hardware PWM that comes predefined with Great Cow BASIC uses the fast PWM mode of the attiny13a (see chap. 11.7.3 in ref. No. 5 for details). In short: it means the timer counts from 0 to 255, falls back to 0, and repeats until the timer is stopped. The frequency of the oscillator is calculated from the internal frequency of the attiny13a. The microcontroller frequency is actually set to 1.2 MHz, which means the hardware PWM can be driven at frequencies of 1.2 MHz, 150 kHz, 18 kHz, 4 kHz and 1 kHz (rounded to full kHz). The speed is dependent of the microcontroller’s frequency. When using the hardware PWM, the microcontroller drives the LED independently which means that other tasks can be processed virtually in parallel. If you want a fixed PWM for the LED, then you can set up the PWM this way (top right).** | For dimming the LED, we want to change at least the duty cycle. Instead of calculating the timings in software, there is an oscillator in the Atmel microcontroller which gives the timing for the pulses. The attiny13a has one 8-bit Timer with 2 channels for hardware PWM. One channel ‘ OC0A’ is not available for hardware PWM because it is sacrificed for other usages, eg, the pseudo random number generator. But the channel ‘OC0B’ can be used for hardware PWM and thus for dimming a LED. The hardware PWM that comes predefined with Great Cow BASIC uses the fast PWM mode of the attiny13a (see chap. 11.7.3 in ref. No. 5 for details). In short: it means the timer counts from 0 to 255, falls back to 0, and repeats until the timer is stopped. The frequency of the oscillator is calculated from the internal frequency of the attiny13a. The microcontroller frequency is actually set to 1.2 MHz, which means the hardware PWM can be driven at frequencies of 1.2 MHz, 150 kHz, 18 kHz, 4 kHz and 1 kHz (rounded to full kHz). The speed is dependent of the microcontroller’s frequency. When using the hardware PWM, the microcontroller drives the LED independently which means that other tasks can be processed virtually in parallel. If you want a fixed PWM for the LED, then you can set up the PWM this way (top right).** | ||
+ | |||
+ | En revanche, le PWM matériel fonctionne d'une façon similaire, mais utilise les circuits matériels du microcontrôleur pour générer les impulsions marche/ | ||
+ | |||
+ | Pour régler la luminosité d'une LED, mous voulons au moins changer le rapport cyclique. Plutôt que de calculer les temps dans le logiciel, un oscillateur existe dans le microcontrôleur Atmel qui donne le tempo pour les impulsions. Le attiny13a a un temporisateur 8-bit avec 2 canaux pour du PWM matériel. Un canal « OC0A » n'est pas disponible pour le PWM matériel car il est consacré à d' | ||
**The fixed mode hardware PWM is easy to set up, but somewhat limited; the only manipulation that can be made during the execution of the program code is by switching the ON-OFF states with the WAIT statement. This first version looks like a candle in heavy wind conditions. Next, we will see that the setup of the fast mode PWM needs some more lines to define the hardware PWM parameter (bottom right). | **The fixed mode hardware PWM is easy to set up, but somewhat limited; the only manipulation that can be made during the execution of the program code is by switching the ON-OFF states with the WAIT statement. This first version looks like a candle in heavy wind conditions. Next, we will see that the setup of the fast mode PWM needs some more lines to define the hardware PWM parameter (bottom right). | ||
Ligne 71: | Ligne 79: | ||
To set up the Timer, the corresponding PWM channel must be set. With the attiny13a, you have only the Timer0 with channel B (constant AVRCHAN2 in Great Cow BASIC) for the fast mode PWM, and can use only PIN PB1 for the blinking or fading LED. The hardware PWM of this version looks like a candle where the wind has subsided, and, as such, looks much smoother than the fixed mode PWM version.** | To set up the Timer, the corresponding PWM channel must be set. With the attiny13a, you have only the Timer0 with channel B (constant AVRCHAN2 in Great Cow BASIC) for the fast mode PWM, and can use only PIN PB1 for the blinking or fading LED. The hardware PWM of this version looks like a candle where the wind has subsided, and, as such, looks much smoother than the fixed mode PWM version.** | ||
+ | Le PWM matériel en mode fixe est facile à paramétrer, | ||
+ | |||
+ | Pour paramétrer le temporisateur, | ||
**Comparison | **Comparison | ||
Ligne 78: | Ligne 89: | ||
Type: avr-size name-of.hex | Type: avr-size name-of.hex | ||
- | The software PWM needs some calculations and further program code to work, but the corresponding executable for one channel is rather small: Program Memory: 217/512 words (42.38%), RAM: 11/64 bytes (17.19%). The corresponding .hex file has a size of 440 KByte. The fixed and the fast mode hardware PWMs need a specific PIN of the microcontroller to work, which limits the number of LEDs you can use. But, once started, the PWM can operate independently (the plan is to prove that this is true for the idle mode, we will see this in a later part). The size of the code differs. For the version of the code with fixed mode PWM, it uses Program Memory: 244/512 words (47.66%), RAM: 9/64 bytes (14.06%). Or around 488 KByte. The fast mode PWM uses Program Memory: 478/512 words (93.36%) RAM: 15/64 bytes (23.44%). Or around 936 KByte. | + | The software PWM needs some calculations and further program code to work, but the corresponding executable for one channel is rather small: Program Memory: 217/512 words (42.38%), RAM: 11/64 bytes (17.19%). The corresponding .hex file has a size of 440 KByte. The fixed and the fast mode hardware PWMs need a specific PIN of the microcontroller to work, which limits the number of LEDs you can use. But, once started, the PWM can operate independently (the plan is to prove that this is true for the idle mode, we will see this in a later part). The size of the code differs. For the version of the code with fixed mode PWM, it uses Program Memory: 244/512 words (47.66%), RAM: 9/64 bytes (14.06%). Or around 488 KByte. The fast mode PWM uses Program Memory: 478/512 words (93.36%) RAM: 15/64 bytes (23.44%). Or around 936 KByte.** |
- | Conclusion | + | |
- | All three types of the PWM will work out-of-the-box and are useful on their own.** | + | Comparaison |
- | **The software PWM gives the possibility to drive LEDs (or motors, or other analogue driven things) without much hassle, but at the cost that other code has to be optimized to be run somehow (thus the microcontroller has so called interrupts, we will briefly introduce this topic later). Secondly, the microcontroller must be in an active state, and cannot be set to the idle sleep mode while using the PWM. | + | Astuce : installez binutils-avr via apt pour obtenir l' |
+ | |||
+ | Tapez : | ||
+ | avr-size name-of.hex | ||
+ | |||
+ | Le PWM logiciel nécessite quelques calculs et du code programme supplémentaire pour fonctionner, | ||
+ | |||
+ | **Conclusion | ||
+ | |||
+ | All three types of the PWM will work out-of-the-box and are useful on their own. | ||
+ | |||
+ | The software PWM gives the possibility to drive LEDs (or motors, or other analogue driven things) without much hassle, but at the cost that other code has to be optimized to be run somehow (thus the microcontroller has so called interrupts, we will briefly introduce this topic later). Secondly, the microcontroller must be in an active state, and cannot be set to the idle sleep mode while using the PWM. | ||
The hardware PWM of the attiny13a is limited due to the few Timers (exactly one) on board, and the resulting code with random numbers will soon get too big to add more functionality. PWM on the tiny13a can look a little odd because of the low timer resolution of the attiny13a and the gaps from the random values. So there is some space left for optimisation. | The hardware PWM of the attiny13a is limited due to the few Timers (exactly one) on board, and the resulting code with random numbers will soon get too big to add more functionality. PWM on the tiny13a can look a little odd because of the low timer resolution of the attiny13a and the gaps from the random values. So there is some space left for optimisation. | ||
Ligne 90: | Ligne 111: | ||
For fun (above), here is one example of a smoother fade effect, if you know ‘Dr Who’, it looks familiar - like the light on top of the TARDIS.** | For fun (above), here is one example of a smoother fade effect, if you know ‘Dr Who’, it looks familiar - like the light on top of the TARDIS.** | ||
+ | |||
+ | Conclusion | ||
+ | |||
+ | Ces trois types de PWM fonctionnent dès l' | ||
+ | |||
+ | Le PWM logiciel donne la possibilité de piloter des LED (ou des moteurs, ou d' | ||
+ | |||
+ | Le PWM matériel du attiny13a est limité par le peu de temporisateurs intégrés (en fait, un), et le code résultant avec des nombres aléatoires deviendra bientôt trop gros pour ajouter d' | ||
+ | |||
+ | Faisons la synthèse | ||
+ | |||
+ | Pour le plaisir (ci-dessus), |
issue128/tutoriel2.1515609930.txt.gz · Dernière modification : 2018/01/10 19:45 de d52fr