Article Index

 

Page 319

from machine import mem32,Pin
from time import sleep_ms

led = Pin(2,mode=Pin.OUT)
GPIOSet = 0x60004008  #Change to 0x3FF44008 for the ESP32
GPIOClear = 0x6000400C #Change to 0x3FF4400C for the ESP32
mask = 1<<2
while True:
    mem32[GPIOSet] = mask
    sleep_ms(500)
    mem32[GPIOClear] = mask
    sleep_ms(500)

Page 319

from machine import mem32,Pin
from time import sleep_ms
@micropython.native
def blink():
    GPIOSet = 0x60004008  #Change to 0x3FF44008 for the ESP32
    GPIOClear = 0x6000400C #Change to 0x3FF4400C for the ESP32
    mask = 1<<2
    while True:
        mem32[GPIOSet] = mask
        mem32[GPIOClear] = mask
led = Pin(2,mode=Pin.OUT)
blink()

Page 323

from machine import Pin
import machine

def gpio_setgroup(value, mask):
    machine.mem32[0x60004004] = machine.mem32[0x60004004] & ~mask | value & mask
  #Change to 0x3FF44004 for the ESP32

pin = Pin(2, Pin.OUT)
pin = Pin(4, Pin.OUT)
value1 = 1 << 2 | 0 << 4
value2 = 0 << 2 | 1 << 4
mask = 1 << 2 | 1 << 4
while True:
    gpio_setgroup(value1, mask)
    gpio_setgroup(value2, mask)

Page 326 ESP32 only

from machine import Pin, DAC, mem32
from time import sleep

def _reg_get(adr):
    return mem32[adr]

def _reg_set( adr,value, mask):
    mem32[adr] = mem32[adr] & ~mask | value & mask

def enableSin(chan,f):
    if chan<1 or chan>2:
        return
    setFreq(chan, f)
    setPhase(chan,0x2)
    #enable tone
    _reg_set(0x3FF48898, 0x10000, 0x10000)
    #select caannel
    if chan==1:
        _reg_set(0x3FF4889c, 1<<24,0x1<<24)
    else:
        _reg_set(0x3FF4889c, 1<<25, 0x1 <<25)


def setFreq(chan,f):
    if chan<1 or chan>2:
        return
    step = int(f*65536/8000000) & 0xFFFF
    _reg_set(0x3FF48898, step, 0x000FF)

def setPhase(chan,p):
    _reg_set(0x3FF4889c, p << (20 + 2 * (chan – 1)), 0x03 << (20 + 2 * (chan - 1)))


def setScale(chan,s):
    _reg_set(0x3FF4889c, s << (16 + 2 * (chan - 1)),0x03 << (16 + 2 * (chan - 1)))

def setOff(chan,off):
    _reg_set(0x3FF4889c, off << (8 * (chan - 1)),0xFF << (8 * (chan - 1)))

dac1 = DAC(Pin(26))

enableSin(2,30000)
setScale(2,0x0)
setOff(2,0x0)
while(True):
    sleep(0.001)
    setPhase(2,0x3)
    sleep(.001)
    setPhase(2, 0x2)

Page 328

import ntptime
from machine import RTC
import network
from machine import Pin, Timer
from time import sleep_ms

def setup(country, ssid, key):
    network.country(country)
    wifi = network.WLAN(network.STA_IF)
    wifi.active(True)
    wifi.disconnect()
    LED = Pin(2, Pin.OUT)
    LED.on()
    timeout = 20000
    wifi.connect(ssid,key)
    timer = Timer(1)
    timer.init(period=200, mode=Timer.PERIODIC,callback=lambda t:LED.value(not LED.value()))    
    s = network.STAT_IDLE
    while timeout>0:
        print("connecting",s)
        s = wifi.status()
        if s != network.STAT_CONNECTING:
            break
        sleep_ms(100)
        timeout = timeout-100    
    if(s==network.STAT_GOT_IP):
         timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:LED.value(not LED.value()))        
    else:
        timer.deinit()
        LED.on()
    return wifi
   
wifi=setup("UK", "dlink3","hawkhawk")
print("Connected")
print(wifi.ifconfig())

ntptime.host="pool.ntp.org"
ntptime.timeout=1000
try:
    ntptime.settime()
except Exception:
    print("NTP server not available")
    pass
rtc = RTC()
print(rtc.datetime())

Page 330

from machine import lightsleep
from time import sleep
for i in range(10):
    print("starting sleep",i)
    lightsleep(1000)
    print ("back from sleep",i)

Page 331 What you see as the output of this depends on the IDE you are using

from machine import deepsleep
from time import sleep
for i in range(10):
    print("starting sleep",i)
sleep(2)
print('I am going to sleep')
deepsleep(1000)

Page 331 

What you see as the output of this depends on the IDE you are using

from machine import deepsleep, RTC
from time import sleep
sleep(3)
rtc = RTC()
if len(rtc.memory())==0:
    start=0
else:
    start = int.from_bytes(rtc.memory(),"big")
for i in range(start,start+10):
    print(i)
print("starting sleep")
rtc.memory((i+1).to_bytes(4,"big"))
deepsleep(1000)

Page 333 

What you see as the output of this depends on the IDE you are using

from machine import lightsleep,Pin
import esp32

wakePin = Pin(2,Pin.IN,pull=None)
esp32.wake_on_ext0(wakePin,esp32.WAKEUP_ANY_HIGH)
for i in range(10):
    print("starting sleep",i)
    lightsleep()
    print ("back from sleep",i)

Page 335

What you see as the output of this depends on the IDE you are using

from machine import WDT
from time import sleep
sleep(4)
print("starting")
wdt = WDT(timeout=4000)
wdt.feed()
while True:
    sleep(0.1)
    print("still running")
    pass

Page 336

What you see as the output of this depends on the IDE you are using

from machine import deepsleep
from time import sleep
from esp32 import NVS
sleep(3)
start = 0
nvsState = NVS("state")
try:
    start = nvsState.get_i32("start")
except OSError:
    pass
for i in range(start,start+10):
    print(i)
print("starting sleep")
nvsState.set_i32("start",i+1)
nvsState.commit()
deepsleep(1000)
 

Page 344

import machine
import os
sd =machine.SDCard(slot=2, sck=18, miso=4, mosi=14, cs=5, freq=400)
print(sd)

os.mount(sd, "/sd")
f = open('/sd/Hello.txt', 'w')
f.write('Hello World')
f.close()

print(os.listdir('/sd'))

f = open('/sd/Hello.txt', 'r')
s=f.read()
f.close()
print(s)