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édente | |||
issue128:tutoriel2 [2018/01/12 14:33] – auntiee | issue128:tutoriel2 [2018/01/13 14:14] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 11: | Ligne 11: | ||
Dans le premier article, j'ai présenté Great Cow BASIC et comment vous pouvez faire clignoter une LED en l' | Dans le premier article, j'ai présenté Great Cow BASIC et comment vous pouvez faire clignoter une LED en l' | ||
- | Cette fois-ci, je veux montrer une version plus élaborée | + | Cette fois-ci, je veux montrer une version plus élaborée |
Mais, avant cela, je veux obtenir un éditeur de texte avec la coloration syntaxique pour la facilité d' | Mais, avant cela, je veux obtenir un éditeur de texte avec la coloration syntaxique pour la facilité d' | ||
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 / | ||
Ligne 51: | Ligne 51: | ||
LED à éclairage variable | LED à éclairage variable | ||
- | 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'il y aura à faire. Mais faire varier la luminosité d'un LED nécessite un peu plus de travail. Comme le microcontrôleur ne délivre que des signaux digitaux, c' | + | 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'il y aura à faire. Mais faire varier la luminosité d'une LED nécessite un peu plus de travail. Comme le microcontrôleur ne délivre que des signaux digitaux, c' |
• un PWM logiciel, | • un PWM logiciel, | ||
Ligne 57: | Ligne 57: | ||
• un PWM matériel basé sur le temporisateur Timer0 de l'AVR d' | • un PWM matériel basé sur le temporisateur Timer0 de l'AVR d' | ||
- | 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 un demande pressante, référez-vous à la feuille de données. | + | 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. | ||
Ligne 63: | Ligne 63: | ||
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 un valeur en octet, 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 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' | + | 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. | ||
Ligne 71: | Ligne 71: | ||
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/ | + | 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' | 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' | ||
Ligne 81: | Ligne 81: | ||
Le PWM matériel en mode fixe est facile à paramétrer, | Le PWM matériel en mode fixe est facile à paramétrer, | ||
- | Pour paramétrer le temporisateur, | + | Pour paramétrer le temporisateur, |
**Comparison | **Comparison | ||
Ligne 95: | Ligne 95: | ||
Astuce : installez binutils-avr via apt pour obtenir l' | Astuce : installez binutils-avr via apt pour obtenir l' | ||
- | Tapez : avr-size name-of.hex | + | Tapez : |
+ | avr-size name-of.hex | ||
- | Le PWM logiciel nécessite quelques calculs et du code programme supplémentaire pour fonctionner, | + | Le PWM logiciel nécessite quelques calculs et du code programme supplémentaire pour fonctionner, |
**Conclusion | **Conclusion | ||
Ligne 121: | Ligne 122: | ||
Faisons la synthèse | Faisons la synthèse | ||
- | Pour le plaisir (ci-dessus), | + | Pour le plaisir (ci-dessus), |
issue128/tutoriel2.1515763989.txt.gz · Dernière modification : 2018/01/12 14:33 de auntiee