Video URL:
http://www.youtube.com/watch?v=Idwc6j7JQPoAs a chill-out thingy on a lazy Sunday afternoon after ACMC07, i made a control patch and circuit for a square waveform generator. Actually, it did help me get over the conference and the subsequent 14.5 hour drive home. I'm pretty happy with the sonic results. If you are interested in getting one of the cheap MCP42100 digital pots from Futurlec working quickly, my code below is "quite well" commented.
In the video, the left hand sliders control the looping pitches and the right hand sliders control an amplitude graph over time (every point along the horizontal axis represents 20ms).
[expand / collapse code]
/* Controling a 555 Timer IC Square Wave Generator via an SPI-based Pot
by Sebastian Tomczak
25 June 2007, Adelaide, Australia
*/
#define SLAVESELECT 10//ss
#define SPICLOCK 13//sck
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
// command bytes
byte cmdWritePot1 = B00010001; // write data to pot 1
byte cmdWritePot2 = B00010010; // write data to pot 1
byte serIn1;
byte serIn2;
void spi_transfer(byte data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<
{
};
}
// Setup Function Begins Here
void setup()
{
Serial.begin(57600);
byte clr;
pinMode(DATAOUT,OUTPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
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)
*/
clr = SPSR;
clr = SPDR;
delay(10);
}
void write_pot_1_1(byte potValue1_1)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,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(SLAVESELECT,HIGH); // disable spi pot
}
void write_pot_1_2(byte potValue1_2)
{
// spi pot chip 1
digitalWrite(SLAVESELECT,LOW); // enable chip
// 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(SLAVESELECT,HIGH); // disable spi pot
}
// Main Loop Begins Here
void loop()
{
if(Serial.available() > 1) {
serIn1 = Serial.read();
serIn2 = Serial.read();
write_pot_1_1(serIn1);
write_pot_1_2(serIn2);
}
}
2 comments:
I was so happy to stumble on this post -- I was trying for a while to find a good example of doing SPI with the arduino. In fact, I'm even planning on using it for controlling an MCP42100. However, it looks like something weird happened to the code in spi_transfer. The condition in the while loop isn't valid and even looking at your source I couldn't figure out what it was supposed to say. Any clues?
yeah, sorry about that! Happens when you copy and paste code from arduino into blogger...
You can get the actual code here:
http://milkcrate.com.au/_other/downloads/projects/555control/555control.txt
Post a Comment