issue130:tutoriel3
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 | ||
issue130:tutoriel3 [2018/02/28 09:52] – d52fr | issue130:tutoriel3 [2018/03/05 15:16] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 2: | Ligne 2: | ||
Many USB-to-serial adapters will work. Common chipsets are CH340, CP210x, FTDI, PL2303, but any other chipset will work as well. Importantly, | Many USB-to-serial adapters will work. Common chipsets are CH340, CP210x, FTDI, PL2303, but any other chipset will work as well. Importantly, | ||
+ | |||
+ | Dans le dernier numéro, je vous montrais comment faire varier la luminosité d'une LED avec un potentiomètre. Dans ce numéro, nous lirons les valeurs du potentiomètre en utilisant une conversion analogique/ | ||
+ | |||
+ | Beaucoup d' | ||
**Serial communication and hardware | **Serial communication and hardware | ||
Ligne 8: | Ligne 12: | ||
For the program flow, it is further important if the communication is blocking (the program must halt until the data is fully received or transmitted) or non-blocking (the data is buffered and the program need not halt until data is transmitted or received, then the data would be processed). Some microcontrollers have a Universal Synchronous and Asynchronous Receiver-Transmitter (USART) implemented. The ATmega328p (the one on the Arduino UNO R3) for example has a USART on PIN2 for receiving and PIN3 for sending serial data.** | For the program flow, it is further important if the communication is blocking (the program must halt until the data is fully received or transmitted) or non-blocking (the data is buffered and the program need not halt until data is transmitted or received, then the data would be processed). Some microcontrollers have a Universal Synchronous and Asynchronous Receiver-Transmitter (USART) implemented. The ATmega328p (the one on the Arduino UNO R3) for example has a USART on PIN2 for receiving and PIN3 for sending serial data.** | ||
+ | |||
+ | Communication série et matériel | ||
+ | |||
+ | Dans une communication série, les octets de données sont envoyés bit par bit sur une ligne dédiée de réception et de transmission. Voyez en fin d' | ||
+ | |||
+ | Pour le déroulement du programme, il est important de savoir, pour la suite, si la communication est bloquante (le programme s' | ||
**Great Cow BASIC has a solution to get the hardware USART to work with a few lines of code. Additionally, | **Great Cow BASIC has a solution to get the hardware USART to work with a few lines of code. Additionally, | ||
Ligne 14: | Ligne 24: | ||
An example of using the hardware USART in the ATmega328p is shown on the previous page.** | An example of using the hardware USART in the ATmega328p is shown on the previous page.** | ||
+ | |||
+ | Great Cow BASIC a une solution pour faire fonctionner l' | ||
+ | |||
+ | Paramétrer l' | ||
+ | |||
+ | Un exemple de l' | ||
**Setup for Serial operations using Software | **Setup for Serial operations using Software | ||
Some microcontrollers do not have a hardware USART; which is true for the ATtiny13a I am using for these examples. Another reason could be the need of an additional serial connection without changing the microcontroller. In order to receive and send data, the serial communication could be achieved in software as well. For me, this would be quite a hard task to program, but, luckily, Great Cow BASIC has two methods for serial communication. For comparison, I will provide an example for each, and observe the differences in the two methods. Great Cow BASIC also has two methods for Serial operations using software, and these are both shown below. For simplicity, I provide the examples for sending values over the serial line. If you want to receive data, go right ahead and add the few missing lines of code.** | Some microcontrollers do not have a hardware USART; which is true for the ATtiny13a I am using for these examples. Another reason could be the need of an additional serial connection without changing the microcontroller. In order to receive and send data, the serial communication could be achieved in software as well. For me, this would be quite a hard task to program, but, luckily, Great Cow BASIC has two methods for serial communication. For comparison, I will provide an example for each, and observe the differences in the two methods. Great Cow BASIC also has two methods for Serial operations using software, and these are both shown below. For simplicity, I provide the examples for sending values over the serial line. If you want to receive data, go right ahead and add the few missing lines of code.** | ||
+ | |||
+ | Paramétrer des opérations série en utilisant le logiciel | ||
+ | |||
+ | Certains microcontrôleurs n'ont pas d' | ||
**Option 1 - ‘Legacy Software Serial Method’: | **Option 1 - ‘Legacy Software Serial Method’: | ||
This legacy method was implemented in 2007 - and is a popular method for software serial operations. This method (top right) can handle up to three different serial lines, with baud rates between 300 and 19,200.** | This legacy method was implemented in 2007 - and is a popular method for software serial operations. This method (top right) can handle up to three different serial lines, with baud rates between 300 and 19,200.** | ||
+ | |||
+ | Option 1 - « Méthode série par logiciel hérité » | ||
+ | |||
+ | Cette méthode héritée a été implémentée en 2007, et c'est une méthode populaire pour les opérations série par logiciel. Cette méthode (en haut à droite) peut gérer jusqu' | ||
**Option 2 - ‘Optimised Method’: | **Option 2 - ‘Optimised Method’: | ||
Ligne 28: | Ligne 52: | ||
If you compare the code examples, keep in mind that the ATMega328p on the Arduino comes with a 16 MHz Resonator, so the system clock is at 16 MHz. The ATtiny13a comes with a 9.6 MHz integrated systems clock; but, by default, the clock is divided (read: limited) by 8 to a mere 1.2 MHz. To change that, it would be necessary to change the fuses, which I did not want to do in this set of articles – I wanted to keep things simple.** | If you compare the code examples, keep in mind that the ATMega328p on the Arduino comes with a 16 MHz Resonator, so the system clock is at 16 MHz. The ATtiny13a comes with a 9.6 MHz integrated systems clock; but, by default, the clock is divided (read: limited) by 8 to a mere 1.2 MHz. To change that, it would be necessary to change the fuses, which I did not want to do in this set of articles – I wanted to keep things simple.** | ||
+ | |||
+ | Option 2 - « Méthode optimisée » | ||
+ | |||
+ | Cette méthode (en bas à droite) utilise une bibliothèque complémentaire pour Great Cow BASIC, de Frank Steinberg, et c'est un pilote logiciel série optimisé écrit en assembleur portable. « Assembleur portable » signifie que cette méthode optimisée supporte les microcontrôleurs AVR et PIC. Cette méthode fournit une vitesse de transmission plus élevée (par ex., un AVR tournant à 1 MHz peut gérer jusqu' | ||
+ | |||
+ | Si vous comparez les exemples de code, gardez en tête que l' | ||
**Software prerequisites to work with the serial adapter | **Software prerequisites to work with the serial adapter | ||
Ligne 44: | Ligne 74: | ||
sudo chmod 666 / | sudo chmod 666 / | ||
+ | |||
+ | Prérequis logiciels pour travailler avec un adaptateur série | ||
+ | |||
+ | Si vous ne devez utiliser ni un adaptateur série, ni du logiciel pour terminal série, sur votre ordinateur, voici quelques brèves explications pour faire fonctionner une communication série. Il y a de bonnes chances que votre ordinateur ait les pilotes nécessaires intégrés : | ||
+ | |||
+ | Branchez votre adaptateur série au port USB et tapez : | ||
+ | |||
+ | dmesg | grep tty | ||
+ | |||
+ | Vous devriez voir une ligne de ce genre : | ||
+ | |||
+ | [...] usb 6-2: cp210x converter now attached to ttyUSB0 (...convertisseur cp210x maintenant branché sur ttyUSB0) | ||
+ | |||
+ | Maintenant vous savez que le circuit de votre adaptateur est supporté (dans mon cas c'est un cp210x) et qu'il est assigné à ttyUSB0. Ensuite, donnez à votre utilisateur Linux l' | ||
+ | |||
+ | sudo chmod 666 / | ||
**Give your superuser password if prompted. | **Give your superuser password if prompted. | ||
Then, if you do not have a serial terminal installed, I suggest putty, as it is easy to use and easily installed. A sudo apt install putty is all you need to do. Then start putty and change the serial line to / | Then, if you do not have a serial terminal installed, I suggest putty, as it is easy to use and easily installed. A sudo apt install putty is all you need to do. Then start putty and change the serial line to / | ||
+ | |||
+ | Saisissez votre mot de passe de super-utilisateur, | ||
+ | |||
+ | Ensuite, si aucun terminal série n'est installé, je suggère putty, car il est facile à utiliser et facilement installé. Vous n'avez qu'à faire un sudo apt install putty. Ensuite, lancez putty et modifiez la ligne série pour / | ||
**Breadboard circuitry | **Breadboard circuitry | ||
Compile the program and flash it to the ATtiny13a with your hardware programmer of choice. The USB-to-serial adapter should have +5V or +3V, and GND, to power up the microcontroller. Then, hook up the receiving line from the adapter to the sending PIN (PB1) of the ATtiny13a. Hook up the Poti to +5V or +3V, and GND as well, and the middle pin to the PIN ADC2 (PB4).** | Compile the program and flash it to the ATtiny13a with your hardware programmer of choice. The USB-to-serial adapter should have +5V or +3V, and GND, to power up the microcontroller. Then, hook up the receiving line from the adapter to the sending PIN (PB1) of the ATtiny13a. Hook up the Poti to +5V or +3V, and GND as well, and the middle pin to the PIN ADC2 (PB4).** | ||
+ | |||
+ | Circuit de la plaque d' | ||
+ | |||
+ | Compilez le programme et flashez-le dans le ATtiny13a avec le programmeur matériel de votre choix. Pour alimenter le microcontrôleur, | ||
**Conclusion | **Conclusion | ||
- | After some experimentation, | + | After some experimentation, |
+ | |||
+ | Conclusion | ||
+ | |||
+ | Après quelques essais, il apparaît que la méthode série du logiciel hérité produit un programme trop gros - trop gros pour le ATtiny13a. Ma version de code présentée au-dessus se compile sur 1 016 octets. Pourquoi est-ce un problème ? D' | ||
- | But, all is not lost! Serial communication on the ATtiny13a is still feasible. The optimized serial software compiled to around 368 bytes and worked like a charm. With the optimised software, we have a working solution to get serial I/O to work; even with the small program memory device like the ATtiny13a!! | + | **But, all is not lost! Serial communication on the ATtiny13a is still feasible. The optimized serial software compiled to around 368 bytes and worked like a charm. With the optimised software, we have a working solution to get serial I/O to work; even with the small program memory device like the ATtiny13a!! |
Serial communications is an important method for exchanging data. Great Cow BASIC makes the configuration and use very simple. Serial communications is easy - it is useful for debugging and to communicate key messages to the users. It can also be used for inter-microcontroller communications, | Serial communications is an important method for exchanging data. Great Cow BASIC makes the configuration and use very simple. Serial communications is easy - it is useful for debugging and to communicate key messages to the users. It can also be used for inter-microcontroller communications, | ||
+ | |||
+ | Mais tout n'est pas perdu ! La communication série avec le ATtiny13a est toujours réalisable. Le logiciel série optimisé est compilé sur environ 360 octets et fonctionne à merveille. Avec le logiciel optimisé, nous avons une solution fonctionnelle pour faire marcher les entrées/ | ||
+ | |||
+ | Les communications série sont une importante méthode d' | ||
**GitHub Repo | **GitHub Repo | ||
Evan Venn (Anobium...my mentor with respect to these articles) suggested to open a GitHub repository for the source code, and, I must admit, this was a good idea. If you want to download the sources instead of copy-pasting it, you can now check it out with git or an SVN client. See https:// | Evan Venn (Anobium...my mentor with respect to these articles) suggested to open a GitHub repository for the source code, and, I must admit, this was a good idea. If you want to download the sources instead of copy-pasting it, you can now check it out with git or an SVN client. See https:// | ||
+ | |||
+ | Le dépôt Github | ||
+ | |||
+ | Evan Venn (Anobium... mon mentor pour ce qui concerne ces articles) me suggérait d' | ||
**References | **References | ||
Ligne 73: | Ligne 139: | ||
I wish to thank Evan Venn (Anobium) from the Great Cow BASIC Team for his insights and valuable hints.** | I wish to thank Evan Venn (Anobium) from the Great Cow BASIC Team for his insights and valuable hints.** | ||
+ | Références | ||
+ | Communications série : https:// | ||
+ | Remerciements | ||
+ | Je souhaite remercier Evan Venn (Anobium) de l' | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
**Corrections | **Corrections | ||
- | In the last issue a formula got lost, for completeness: | + | In the last issue a formula got lost, for completeness: |
- | + | ||
+ | Corrections | ||
+ | Dans l' | ||
- | Serial Comms - A Few Definitions and Notes: | + | **Serial Comms - A Few Definitions and Notes: |
by Michael Kennedy and Gord Campbell | by Michael Kennedy and Gord Campbell | ||
Ligne 101: | Ligne 165: | ||
The simplest RS-232 implementations handle two-way communications between just two devices (say, Dev-A and Dev-B). The cabling has just three wires: ground, data pulses going from Dev-A to Dev-B, and data pulses from Dev-B to Dev-A. The cable is normally up to about 15-20 metres, but could be up to a few hundred metres, max. The hardware at both ends could be microcomputers, | The simplest RS-232 implementations handle two-way communications between just two devices (say, Dev-A and Dev-B). The cabling has just three wires: ground, data pulses going from Dev-A to Dev-B, and data pulses from Dev-B to Dev-A. The cable is normally up to about 15-20 metres, but could be up to a few hundred metres, max. The hardware at both ends could be microcomputers, | ||
+ | |||
+ | Communications série - Quelques définitions et notes : | ||
+ | |||
+ | par Michael Kennedy et Gord Campbell | ||
+ | |||
+ | Ces notes se concentrent sur quelques fonctionnalités principales des implémentations classiques du RS-232, mais quelques autres variantes sont aussi mentionnées. Elles ont pour but de compléter l' | ||
+ | |||
+ | « RS-232 » (Recommended Standard 232 - norme recommandée n° 232) est un système de communication populaire. À l' | ||
+ | |||
+ | Les implémentations les plus simples du RS-232 gèrent des communications bidirectionnelles entre uniquement deux appareils (disons, DEV-A et Dev-B). Le câblage n'a que trois fils : une masse, des impulsions de données de Dev-A vers Dev-B, et des impulsions de données de Dev-B vers Dev-A. Le câble a normalement une longueur jusqu' | ||
**The speed of the data-pulses must normally be set, in advance, on both devices, and must be based on how quickly both devices can transmit and receive the pulses, on the cable length and quality, the overall electrical environment, | **The speed of the data-pulses must normally be set, in advance, on both devices, and must be based on how quickly both devices can transmit and receive the pulses, on the cable length and quality, the overall electrical environment, | ||
Ligne 110: | Ligne 184: | ||
• Dev-A might send a single Parity bit (Odd/ | • Dev-A might send a single Parity bit (Odd/ | ||
• Dev-A sends one, or one-and-half, | • Dev-A sends one, or one-and-half, | ||
+ | |||
+ | La vitesse des impulsions de données doit être pré-positionnée sur les deux dispositifs et doit être basée sur la vélocité de ces deux appareils pour émettre et recevoir des impulsions, sur la longueur et la qualité du câble, l' | ||
+ | |||
+ | Pour envoyer un seul octet de DEv-A vers Dev-B : | ||
+ | • Dev-B doit être prêt à accepter l' | ||
+ | • Dev-A émet une impulsion d'« avertissement », appelé Start-bit (bit de début), pour réveiller Dev-B ! | ||
+ | • Ensuite, Dev-A émet les bits de l' | ||
+ | • Dev-A peut envoyer un unique Parity bit (bit de parité - pair, impair, aucun). | ||
+ | • Dev-A envoie un, ou un et demi, ou deux, Stop-bits (bits d' | ||
**So, typically, a single byte might involve the transfer of about 10-12 bits/ | **So, typically, a single byte might involve the transfer of about 10-12 bits/ | ||
Ligne 116: | Ligne 199: | ||
SIMPLEX/ | SIMPLEX/ | ||
+ | |||
+ | Ainsi, typiquement, | ||
+ | |||
+ | UART : l' | ||
+ | |||
+ | SIMPLEX/ | ||
**Further, less common, variations on RS-232... | **Further, less common, variations on RS-232... | ||
Ligne 122: | Ligne 211: | ||
Additional Control wires: In some implementations, | Additional Control wires: In some implementations, | ||
+ | |||
+ | D' | ||
+ | |||
+ | USART : deux fils peuvent être utilisés pour envoyer les bits de données de Dev-A à Dev-B et deux autres pour les bits de données Dev-B vers Dev-A. Le second fil de chaque paire contient des impulsions d' | ||
+ | |||
+ | Fils de contrôle additionnels : dans certaines implémentations, | ||
**RS-422, Longer cables: Slightly different devices and cabling arrangements can be adopted where longer cable runs, and/or higher speeds, are needed. The cable contains two wires for each of the data-pulses runs, and two more wires for each of the clock-pulses - like standard twisted-pair LAN cables. | **RS-422, Longer cables: Slightly different devices and cabling arrangements can be adopted where longer cable runs, and/or higher speeds, are needed. The cable contains two wires for each of the data-pulses runs, and two more wires for each of the clock-pulses - like standard twisted-pair LAN cables. | ||
Ligne 128: | Ligne 223: | ||
Error Handling!: If you’re writing “serial” software, and/or taming serial hardware, for private educational or entertainment purposes, then you probably won’t be over-worried if a few errors arise now-and-then. For example, if you’re sending data, serially, to a display, or controlling a few LEDs, etc, you might chose to not bother if the device is present or not, or powered on, or fully “reset” after a power-on, or ready to send/ | Error Handling!: If you’re writing “serial” software, and/or taming serial hardware, for private educational or entertainment purposes, then you probably won’t be over-worried if a few errors arise now-and-then. For example, if you’re sending data, serially, to a display, or controlling a few LEDs, etc, you might chose to not bother if the device is present or not, or powered on, or fully “reset” after a power-on, or ready to send/ | ||
+ | |||
+ | RS-422, des câbles plus longs : des dispositifs et un arrangement du câblage légèrement différents peuvent être adoptés là où de plus grandes distances de câblage et/ou de plus grandes vitesses sont nécessaires. Le câble contient deux fils pour chaque liaison d' | ||
+ | |||
+ | RS-485, des dispositifs multiples : différents CI peuvent être déployés là où, par exemple, plus de deux dispositifs (voire 10, 20, 30...) peuvent partager le(s) même(s) câble(s) et où, par exemple, ils dialoguent tous avec un dispositif « serveur/ | ||
+ | |||
+ | Gestion des erreurs ! : Si vous écrivez du logiciel « série » et/ou apprivoisez un matériel série, pour des besoins éducatifs ou ludiques, vous n' | ||
**However, if your RS-232 app is used in an MRI/ | **However, if your RS-232 app is used in an MRI/ | ||
Ligne 136: | Ligne 237: | ||
• Dev-B: Got that, 123 bytes in total, checksum=xxx, | • Dev-B: Got that, 123 bytes in total, checksum=xxx, | ||
• Dev-A: Excellent – thank you, over-and-out.** | • Dev-A: Excellent – thank you, over-and-out.** | ||
+ | |||
+ | Cependant, si votre appli RS-232 est utilisée dans un scanner à résonance magnétique nucléaire ou un tomographe, et qu'il contrôle les doses de radiation, les positions et les moteurs, etc., et que vous êtes le malade, alors vous devrez réellement gérer toutes les conditions ci-dessus ! Ensuite, vous devrez probablement utiliser certains signaux/ | ||
+ | • Les deux dispositifs devront utiliser les fils de contrôle pour s' | ||
+ | • Dev-A dit : « Hello, je veux vous envoyer une alerte/ | ||
+ | • Dev-B : « Oui, envoyez-le. » | ||
+ | • Dev-A : Le voici, .... xxxxx..... | ||
+ | • Dev-B : Bien reçu, 123 octet au total, somme de contrôle = xxxx, merci. | ||
+ | • Dev-A : Excellent - merci, terminé. | ||
**At all of the above stages: | **At all of the above stages: | ||
Ligne 145: | Ligne 254: | ||
So, to ring that bell, or turn on that emergency light, or rotate that stepper motor, a single “Print.....” statement might evolve into a very complicated 500-line “LED_On()” function! | So, to ring that bell, or turn on that emergency light, or rotate that stepper motor, a single “Print.....” statement might evolve into a very complicated 500-line “LED_On()” function! | ||
+ | |||
+ | À toutes les étapes précédentes : | ||
+ | • L' | ||
+ | • Le récepteur devrait avoir des timeouts qui tournent, pour s' | ||
+ | • Toutes les lignes de handshaking devraient être vérifiées continuellement, | ||
+ | • Si des erreurs mineures se produisent, des renvois devraient être tentés probablement, | ||
+ | • La gestion des erreurs majeures/ | ||
+ | |||
+ | Ainsi, pour sonner l' | ||
**Finally: For lots of good, general, details, see: https:// | **Finally: For lots of good, general, details, see: https:// | ||
Ligne 154: | Ligne 272: | ||
Michael Kennedy and Gord Campbell** | Michael Kennedy and Gord Campbell** | ||
+ | |||
+ | Pour terminer : voyez de nombreux et bons détails généraux sur : https:// | ||
+ | |||
+ | Si vous implémentez des communications RS-232 sur un dispositif particulier (UART, uP, CPU, etc), alors regardez les détails des datasheets des constructeurs des matériels. Normalement, | ||
+ | |||
+ | Bonne chance ! | ||
+ | |||
+ | Michael Kennedy et Gord Campbell |
issue130/tutoriel3.1519807973.txt.gz · Dernière modification : 2018/02/28 09:52 de d52fr