Thursday, August 23, 2007

Adding an SD/MMC card - step 2

I wrote some code to test my wiring and voltage dividing resistor setup. It allows me to toggle each of the pins (10 - 13) using the serial port of the PC. All of the wiring and voltage dividers seem to be working.

Now for the real work. The question is: do I write my own code or try to reuse other code. AVRLib has MMC code which can be adapted to SD very easily, but AVRLib may not integrate with the Arduino development environment. There are other AVR examples out there. I even found one that does FAT format and IDE, which would be useful if I were connecting up a compact flash card interface because compact flash behaves like an IDE drive.



Test Code




int incomingByte = '1';
int prevByte = 0;

int pinArray[] = {
10, 11, 12, 13
};
int pinCount = 4;
int LastPinVals[] = {
LOW, LOW, LOW, LOW };
int count = 0;

void setup()
{
for (count=0;count<pinCount;count++) {
pinMode(pinArray[count], OUTPUT);
}

reset_pins();

Serial.begin(9600);
Serial.println("Start");
}

void reset_pins()
{
for (count=0;count<pinCount;count++) {
digitalWrite(pinArray[count], LOW);
}
}

int pinToIdx(int pin)
{
int i;
for (i = 0; i < pinCount; i++)
{
if (pinArray[i] == pin)
{
return i;
}
}
return 0;
}

void toggle(int pin)
{
digitalWrite(pin, ! LastPinVals[pinToIdx(pin)]);
LastPinVals[pinToIdx(pin)] = ! LastPinVals[pinToIdx(pin)];
}

int byteToPin(int byteVal)
{
return pinArray[byteVal - '1'];
}

void loop()
{
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte >= '1' && incomingByte <= '4')
{
if (prevByte != incomingByte)
{
Serial.print("Got ");
Serial.println(incomingByte);
toggle(byteToPin(incomingByte));
prevByte = incomingByte;
}
}
}
}


Labels: ,

2 Comments:

At 2:32 PM, Blogger Rob said...

Hmm, no asserts!

So, have you thought about what Test First looks like in this world?

 
At 3:27 PM, Blogger Unknown said...

Hey.

Nice blog you have.

Im trying to adapt the AVRlib MMC library to SD but im still having some problems.

Could you help me out and tell me what changes i have to do to adapt this library to SD?

I would really appreciate that.

My email is miguel.casillas@gmail.com

Hope you can help me.

 

Post a Comment

<< Home