Article Index

Page 180

from machine import ADC,Pin
adc = ADC(Pin(32)) #Use GPIO5 for S3
print(adc.read())
print(adc.read_u16())
print(adc.read_uv()/1000000)

Page 182

from machine import ADC,Pin
import time
adc = ADC(Pin(32),atten=ADC.ATTN_11DB)#change to 5 for the EPS32 S3

t = time.ticks_us()
for i in range(10000):
    pass
print(time.ticks_diff(time.ticks_us(), t)/10000)

t = time.ticks_us()
for i in range(10000):
    m=adc.read()
print(time.ticks_diff(time.ticks_us(), t)/10000)

t = time.ticks_us()
for i in range(10000):
    m=adc.read_u16()
print(time.ticks_diff(time.ticks_us(), t)/10000)

t = time.ticks_us()
for i in range(10000):
    m=adc.read_uv()
print(time.ticks_diff(time.ticks_us(), t)/10000)

Page 184 Only works on ESP32

from machine import Pin, DAC
import math
dac1 = DAC(Pin(26))
wave = []
for i in range(0,255):
    wave.append(int(127*math.sin(2*math.pi/256*i))+128)
while(True):
     for i in range(0,255):
         dac1.write(wave[i])

Page 185 Only works on ESP32

 

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

def _gpio_get(adr):
    return mem32[adr]

def _gpio_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
    _gpio_set(0x3FF48898, 0x10000, 0x10000)
    #select channel
    if chan==1:
        _gpio_set(0x3FF4889c, 1<<24,0x1<<24)
    else:
        _gpio_set(0x3FF4889c, 1<<25, 0x1 <<25)
    #set pad
    pad=0x3FF48484+4*(chan-1)
    _gpio_set(pad, 0x20200, 0x23A00)

def setFreq(chan,f):
    if chan<1 or chan>2:
        return
    step=int(f*65536/8000000) & 0xFFFF
    _gpio_set(0x3FF48898, step, 0x000FF)
def setPhase(chan,p):
    _gpio_set(0x3FF4889c, p << (20 + 2 * (chan – 1)),0x03 << (20 + 2 * (chan - 1)))

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

def setOff(chan,off):
    _gpio_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)