-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp3008.fs
executable file
·55 lines (38 loc) · 1.38 KB
/
mcp3008.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
marker mcp3008.fs
CheckI2c 0= [IF] cr cr .( ERROR. The needed I2c interface is NOT activated! ok ) abort cr [then]
\ To Read data from a MCP3008 connected to a RPI for Gforth through the Spi interface
\ Tested under Jessie
needs Common-extensions.f
needs unix/libc.fs
needs wiringPi.fs \ From: https://github.com/kristopherjohnson/wiringPi_gforth/blob/master/wiringPi.fs
1000000 constant spiSpeed 0 constant spiMcp3008 \ SpiChannel of the Mcp3008
8 constant #Channels 3.3e fconstant vRef \ When vRef is connected to 3.3V
0 value fdSpi 1023e fconstant #StepsAdc
8 constant CHAN_CONFIG_SINGLE 0 constant CHAN_CONFIG_DIFF
#StepsAdc 100e f/ fconstant (Adc%)
: SetupMcp3008 ( spiChannel spiSpeed - fd ) wiringPiSPISetup dup ?ior ;
: initMcp3008 ( - ) spiMcp3008 spiSpeed SetupMcp3008 to fdSpi ;
: Adc@ ( spiChannel ADcChannel - RawData )
upad off 1 upad c! CHAN_CONFIG_SINGLE or 4 lshift upad 1+ c!
upad 3 wiringPiSPIDataRW ?ior
upad 1+ c@ 3 and 8 lshift upad 2 + c@ or ;
: Adc% ( RawData - %OfTotal ) s>f (Adc%) f/ ;
: .Adc ( spiChannel - )
#Channels 0 do dup cr i . i Adc@ . loop drop ;
initMcp3008
\\\ Eg:
0 value fdAdc
0 spiSpeed SetupMcp3008 to fdAdc
0 2 Adc@ cr cr . cr
0 .Adc abort
\\\ Output:
224
0 0
1 56
2 224
3 19
4 32
5 67
6 96
7 148
\\\