[expand / collapse code]
// define spi bus pins
#define SLAVESELECT 10//ss
#define SPICLOCK 13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
// MCP42100 command bytes
byte cmdWritePot1 = B00010001; // write data to pot 1
byte cmdWritePot2 = B00010010; // write data to pot 2
// incoming serial data bytes
byte ser1;
byte ser2;
byte ser3;
byte ser4;
// spi output
void spi_transfer(byte data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<
{
};
}
// Setup Function Begins Here
void setup()
{
Serial.begin(9600);
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
// setup the spi register
SPCR = B01010000;
/*
SPCR bits:
7: SPIEE - enables SPI interrupt when high
6: SPE - enable SPI bus when high
5: DORD - LSB first when high, MSB first when low
4: MSTR - arduino is in master mode when high, slave when low
3: CPOL - data clock idle when high if 1, idle when low if 0
2: CPHA - data on falling edge of clock when high, rising edge when low
1: SPR1 - set speed of SPI bus
0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
*/
// clear old spi data
clr = SPSR;
clr = SPDR;
delay(10);
Serial.print(47);
}
void write_pot_1_1(byte potValue1_1)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,LOW); // "enable" chip 1
digitalWrite(2,LOW); // enable chip 1
// set value for pot A
spi_transfer(cmdWritePot1); // command byte out to select pot and init write
spi_transfer(potValue1_1); // 8 bit pot value
digitalWrite(2,HIGH); // disable chip 1
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 1
}
void write_pot_1_2(byte potValue1_2)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,LOW); // "enable" chip 1
digitalWrite(2,LOW); // enable chip 1
// set value for pot B
spi_transfer(cmdWritePot2); // command byte out to select pot and init write
spi_transfer(potValue1_2); // 8 bit pot value
digitalWrite(2,HIGH); // disable chip 1
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 1
}
void write_pot_2_1(byte potValue2_1)
{
// spi pot chip 2
digitalWrite(SLAVESELECT,LOW); // "enable" chip 2
digitalWrite(3,LOW); // enable chip 2
// set value for pot A
spi_transfer(cmdWritePot1); // command byte out to select pot and init write
spi_transfer(potValue2_1); // 8 bit pot value
digitalWrite(3,HIGH); // disable chip 2
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 2
}
void write_pot_2_2(byte potValue2_2)
{
// spi pot chip 2
digitalWrite(SLAVESELECT,LOW); // "enable" chip 2
digitalWrite(3,LOW); // enable chip 2
// set value for pot B
spi_transfer(cmdWritePot2); // command byte out to select pot and init write
spi_transfer(potValue2_2); // 8 bit pot value
digitalWrite(3,HIGH); // disable chip 2
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 2
}
// Main Loop Begins Here
void loop()
{
if(Serial.available() > 0) {
ser1 = Serial.read();
write_pot_1_1(ser1);
write_pot_1_2(ser1);
write_pot_2_1(ser1);
write_pot_2_2(ser1);
Serial.print(47);
Serial.flush();
}
}
// define spi bus pins
#define SLAVESELECT 10//ss
#define SPICLOCK 13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
// MCP42100 command bytes
byte cmdWritePot1 = B00010001; // write data to pot 1
byte cmdWritePot2 = B00010010; // write data to pot 2
// incoming serial data bytes
byte ser1;
byte ser2;
byte ser3;
byte ser4;
// spi output
void spi_transfer(byte data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<
{
};
}
// Setup Function Begins Here
void setup()
{
Serial.begin(9600);
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
// setup the spi register
SPCR = B01010000;
/*
SPCR bits:
7: SPIEE - enables SPI interrupt when high
6: SPE - enable SPI bus when high
5: DORD - LSB first when high, MSB first when low
4: MSTR - arduino is in master mode when high, slave when low
3: CPOL - data clock idle when high if 1, idle when low if 0
2: CPHA - data on falling edge of clock when high, rising edge when low
1: SPR1 - set speed of SPI bus
0: SPR0 - set speed of SPI bus (00 is fastest @ 4MHz, 11 is slowest @ 250KHz)
*/
// clear old spi data
clr = SPSR;
clr = SPDR;
delay(10);
Serial.print(47);
}
void write_pot_1_1(byte potValue1_1)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,LOW); // "enable" chip 1
digitalWrite(2,LOW); // enable chip 1
// set value for pot A
spi_transfer(cmdWritePot1); // command byte out to select pot and init write
spi_transfer(potValue1_1); // 8 bit pot value
digitalWrite(2,HIGH); // disable chip 1
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 1
}
void write_pot_1_2(byte potValue1_2)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,LOW); // "enable" chip 1
digitalWrite(2,LOW); // enable chip 1
// set value for pot B
spi_transfer(cmdWritePot2); // command byte out to select pot and init write
spi_transfer(potValue1_2); // 8 bit pot value
digitalWrite(2,HIGH); // disable chip 1
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 1
}
void write_pot_2_1(byte potValue2_1)
{
// spi pot chip 2
digitalWrite(SLAVESELECT,LOW); // "enable" chip 2
digitalWrite(3,LOW); // enable chip 2
// set value for pot A
spi_transfer(cmdWritePot1); // command byte out to select pot and init write
spi_transfer(potValue2_1); // 8 bit pot value
digitalWrite(3,HIGH); // disable chip 2
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 2
}
void write_pot_2_2(byte potValue2_2)
{
// spi pot chip 2
digitalWrite(SLAVESELECT,LOW); // "enable" chip 2
digitalWrite(3,LOW); // enable chip 2
// set value for pot B
spi_transfer(cmdWritePot2); // command byte out to select pot and init write
spi_transfer(potValue2_2); // 8 bit pot value
digitalWrite(3,HIGH); // disable chip 2
digitalWrite(SLAVESELECT,HIGH); // "disable" spi pot chip 2
}
// Main Loop Begins Here
void loop()
{
if(Serial.available() > 0) {
ser1 = Serial.read();
write_pot_1_1(ser1);
write_pot_1_2(ser1);
write_pot_2_1(ser1);
write_pot_2_2(ser1);
Serial.print(47);
Serial.flush();
}
}
// define spi bus pins
#define SLAVESELECT 10//ss
#define SPICLOCK 13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
// MCP42100 command bytes
byte cmdWritePot1 = B00010001; // write data to pot 1
byte cmdWritePot2 = B00010010; // write data to pot 2
// incoming serial data bytes
byte ser1;
byte ser2;
byte ser3;
byte ser4;
// spi output
void spi_transfer(byte data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<
ser1 = Serial.read();
write_pot_1_1(ser1);
write_pot_1_2(ser1);
write_pot_2_1(ser1);
write_pot_2_2(ser1);
Serial.print(47);
Serial.flush();
}
}
}
0 comments:
Post a Comment