PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
wiring_analog.c
Go to the documentation of this file.
1 
10 #include <Arduino.h>
11 
12 static const int Ctrduty = 6;
13 static const int Dutyperiod = 0x1010101;
14 
15 // Assume Propeller ASC for analogRead for now.
16 // Note the code below is an Arduino-fied version of code given
17 // to me by Michael Swartzendruber in the Parallax forum.
18 
19 #define SE_0 0x08 // ch0 single ended
20 #define SE_1 0x09
21 #define SE_2 0x0A
22 #define SE_3 0x0B
23 #define SE_4 0x0C
24 #define SE_5 0x0D
25 #define SE_6 0x0E
26 #define SE_7 0x0F
27 
28 #define D_01 0x00 // 0+/1- differential
29 #define D_10 0x01 // 1+/0-
30 #define D_23 0x02 // 2+/3-
31 #define D_32 0x03 // 3+/2-
32 #define D_45 0x04 // 4+/5-
33 #define D_54 0x05 // 5+/4-
34 #define D_67 0x06 // 6+/7-
35 #define D_76 0x07 // 7+/6-
36 
37 static const int adcChannel2SE[8] = {SE_0, SE_1, SE_2, SE_3, SE_4, SE_5, SE_6, SE_7};
38 static const int clockDelay = 400;
39 
40 // pins for the Propeller ASC A/D convertor.
41 volatile int cs;
42 volatile int clk;
43 volatile int dio;
44 
45 void init( int cspin, int clkpin, int diopin )
46 {
47  cs = cspin;
48  clk = clkpin;
49  dio = diopin;
50 
51  pinMode( cspin, OUTPUT );
52  digitalWrite( cspin, HIGH );
53 
54  pinMode( clkpin, OUTPUT );
55  digitalWrite( clkpin, LOW );
56 }
57 
58 void finalize()
59 {
60  pinMode( cs, INPUT );
61  pinMode( clk, INPUT );
62  pinMode( dio, INPUT );
63 }
64 
65 int do_adc_cmd( int mux )
66 {
67  int level = 0;
68  int counter;
69 
70  // Read MCP3208 channel
71  // -- mux is encoded mux bits for channel/mode
72 
73  digitalWrite( cs, LOW ); // activate adc
74  pinMode( dio, OUTPUT ); // dio is output
75 
76  // output mux bits, MSBFIRST
77 
78  for ( counter = 0; counter < 5; counter++ )
79  { // send mux bits
80  digitalWrite(dio, (mux & 0x10) ? HIGH : LOW);
81  delayMicroseconds( clockDelay );
82  mux <<= 1;
83  digitalWrite( clk, HIGH ); // clock the bit
84  delayMicroseconds( clockDelay );
85  digitalWrite( clk, LOW );
86  }
87 
88  // input data bits, MSBPOST
89 
90  pinMode( dio, INPUT ); // dio is input
91 
92  level = 0; // clear work var
93 
94  for ( counter = 0; counter < 13; counter++ )
95  { // null + 12 data bits
96  digitalWrite( clk, HIGH ); // clock the bit
97  delayMicroseconds( clockDelay );
98  digitalWrite( clk, LOW );
99  delayMicroseconds( clockDelay );
100  level = ( level << 1 ) | digitalRead( dio ); // input data bit
101  }
102 
103  digitalWrite( cs, HIGH ); // de-activate adc
104 
105  return( level & 0x0fff );
106 }
107 
108 // -- ch is channel, 0 to 7
109 // -- mode is 0 for single-ended, 1 for differential
110 int read_adc_channel( int ch_mode )
111 {
112  long adc_cmd;
113 
114  adc_cmd = 0x10 | ch_mode ; // create mux bits
115 
116  return do_adc_cmd( adc_cmd );
117 }
118 
119 int analogRead(uint8_t adcChannel)
120 {
121  init( 27, 25, 26 );
122  int value = read_adc_channel(adcChannel2SE[adcChannel]);
123 
124  value = map(value, 0, 4095, 0, 1023);
125 
126  return value;
127 }
128 
129 void analogReference(uint8_t mode)
130 {
131 }
132 
133 void analogWrite(uint8_t pin, int duty)
134 {
135  CTRA = 0;
136  FRQA = (Dutyperiod * duty);
137  CTRA = ((Ctrduty << 26) + pin);
138 }
FRQA
#define FRQA
Counter A frequency register.
Definition: propeller1.h:169
Arduino.h
Provides Arduino types and functions on the Propeller.
CTRA
#define CTRA
Counter A control register.
Definition: propeller1.h:165