| Ligne 202 : |
Ligne 202 : |
| | | | |
| | <syntaxhighlight lang="python"> | | <syntaxhighlight lang="python"> |
| − | # Modifier l'adresse de MOD-IO2 d'Olimex vers 0x22 | + | # Test PWM sur le MOD-IO2 d'Olimex avec un ESP8266 sous MicroPython |
| | # | | # |
| − | # Shop: http://shop.mchobby.be/product.php?id_product=1408 | + | # Shop: http://shop.mchobby.be/product.php?id_product=1409 |
| | # GitHub: https://github.com/mchobby/esp8266-upy/tree/master/modio2 | | # GitHub: https://github.com/mchobby/esp8266-upy/tree/master/modio2 |
| | | | |
| | from machine import I2C, Pin | | from machine import I2C, Pin |
| | + | from time import sleep_ms |
| | from modio2 import MODIO2 | | from modio2 import MODIO2 |
| | | | |
| | i2c = I2C( sda=Pin(2), scl=Pin(4) ) | | i2c = I2C( sda=Pin(2), scl=Pin(4) ) |
| − | brd = MODIO2( i2c, addr=0x1 ) | + | brd = MODIO2( i2c ) # Adresse par défaut=0x21 |
| − | brd.change_address( 0x22 ) | + | |
| | + | print( "GPIO 5 - IN") |
| | + | brd.gpios.pin_mode( 5, Pin.IN ) |
| | + | |
| | + | print( "GPIO 6 - PWM" ) |
| | + | brd.gpios.pwm( gpio=6, cycle=0 ) |
| | + | |
| | + | cycle=0 |
| | + | while cycle<255: |
| | + | val = brd.gpios.analog( 5, raw = True ) |
| | + | cycle = val // 4 # de 0..1023 a 0..254 |
| | + | if cycle >= 254: # Assure 100% du cycle utile |
| | + | cycle = 255 |
| | + | brd.gpios.pwm( 6, cycle ) |
| | + | print( "val=%s -> cycle=%s" %(val,cycle) ) |
| | + | sleep_ms( 1000 ) |
| | + | |
| | + | print( "That's the end folks") |
| | + | </syntaxhighlight> |
| | + | |
| | + | Ce qui affiche le résultat suivant à l'écran pendant que l'on exécute le script: |
| | + | <syntaxhighlight lang="python"> |
| | + | MicroPython v1.9.4-8-ga9a3caad0 on 2018-05-11; ESP module with ESP8266 |
| | + | Type "help()" for more information. |
| | + | >>> |
| | + | >>> import test2pwm |
| | + | GPIO 5 - IN |
| | + | GPIO 6 - PWM |
| | + | val=698 -> cycle=174 |
| | + | val=698 -> cycle=174 |
| | + | val=620 -> cycle=155 |
| | + | val=614 -> cycle=153 |
| | + | val=597 -> cycle=149 |
| | + | val=496 -> cycle=124 |
| | + | val=494 -> cycle=123 |
| | + | val=701 -> cycle=175 |
| | + | val=880 -> cycle=220 |
| | + | val=978 -> cycle=244 |
| | + | val=979 -> cycle=244 |
| | + | val=982 -> cycle=245 |
| | + | val=1023 -> cycle=255 |
| | + | That's the end folks |
| | + | >>> |
| | </syntaxhighlight> | | </syntaxhighlight> |
| | | | |