-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.fs
61 lines (54 loc) · 1.14 KB
/
adc.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
\ this is from the flashforth tutorial
-read-adc
marker -read-adc
\ Registers of interest on the ATmega328P
$78 constant adcl
$79 constant adch
$7a constant adcsra
$7b constant adcsrb
$7c constant admux
$7e constant didr0
\ Bit masks
%10000000 constant mADEN
%01000000 constant mADSC
%00010000 constant mADIF
: adc.clear.iflag ( -- )
mADIF adcsra mset \ clear by writing 1
;
: adc.init ( -- )
$3f didr0 c! \ Disable digital inputs 5 through 0
$40 admux c! \ AVcc as ref , right - adjust result , channel 0
$06 adcsra c! \ single conversion mode , prescaler 64
mADEN adcsra mset \ enable ADC
adc.clear.iflag
;
: adc.close ( -- )
mADEN adcsra mclr
adc.clear.iflag
;
: adc.wait ( -- )
begin mADSC adcsra mtst 0= until
;
: adc.select ( u -- )
adc.wait
$0f and \ channel selected by lower nibble
admux c@ $f0 and \ fetch upper nibble
or admux c!
;
: adc@ ( -- u )
adc.wait
mADSC adcsra mset
adc.wait
adcl c@ adch c@ #8 lshift or
adc.clear.iflag
;
: adc.test ( -- ) \ Exercise the analog converter
adc.init
begin
0 adc.select adc@ ." adc0 " u.
1 adc.select adc@ ." adc1 " u.
cr
#500 ms
key? until
adc.close
;