Article Index

Page 173

from machine import mem32
def getVCal():
    VC=(mem32[0x3FF5_A010]>>8)& 0x1F
    if(VC & 0x10 ):
        VC=-(VC & 0x0F)
    return 1100+VC*7

print(getVCal())

Page 177

from machine import ADC,Pin
import time
adc = ADC(Pin(32),atten=ADC.ATTN_11DB)

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 178

import esp32
import time
while(True):
    m = esp32.hall_sensor()+esp32.hall_sensor()+esp32.hall_sensor()+esp32.hall_sensor()
    m = m/4
    print(m)
    time.sleep(0.5)

Page 181

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 181

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)