MCP6002
1002 Bytes
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
#include <SPI.h>
const int PIN_CS = 10;
const int GAIN_1 = 0x1;
const int GAIN_2 = 0x0;
void setup()
{
Serial.begin(300);
pinMode(PIN_CS, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV2);
}
//assuming single channel, gain=2
void setOutput(unsigned int val)
{
byte lowByte = val & 0xff;
byte highByte = ((val >> 8) & 0xff) | 0x10;
PORTB &= 0xfb;
SPI.transfer(highByte);
SPI.transfer(lowByte);
PORTB |= 0x4;
}
void setOutput(byte channel, byte gain, byte shutdown, unsigned int val)
{
byte lowByte = val & 0xff;
byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;
PORTB &= 0xfb;
SPI.transfer(highByte);
SPI.transfer(lowByte);
PORTB |= 0x4;
}
void loop()
{
//high-res sine wave
for (int i=0; i < 256; i+=1)
{
//setOutput(0, GAIN_2, 1, i);
double a = (((double)i*(22/7))/127.5);
double b = ((sin(a)*0.5)+0.5)*255;
setOutput((int)b);
//Serial.println((int)b);
Serial.println(analogRead(A0));
}
}