I try to build a function generator (preferably a sine) with an R-2R ladder and a Arduino Leonardo by Borderless Electronics.
For performance reasons one should use portd
instead of digitalWrite
.
However the signal is not at all what I want (just noise). So upon further investigation I found that the digital pins were never on HIGH
. Pin 6 worked flawlessly. Furthermore I have tested the two failing pins with simple test programms and they behaved exactly as I would have expected.
Here is the code I use
int sine[255];
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
float x;
float y;
for(int i=0;i<255;i++)
{
x=(float)i;
y=sin((x/255)*2*PI);
sine[i]=int(y*128)+128;
}
}
void loop()
{
for (int i=0;i<255;i++)
{
PORTD=sine[i];
delayMicroseconds(10);
}
}
I tested this with two Arduinos of the exactly same type.
Question: Why are pins 5 and 7 never on HIGH
?
Edit: I have simplified everything down to one statement in the loop: portd = x;
I vary x manually
expected findings:
x -> #pin that is on high
0 -> None
1 -> 0
2 -> 1
4 -> 2
8 -> 3
...
128 -> 7
actual findings
x -> #pin that is on high
0 -> None
1 -> 3
2 -> 2
4 -> 0
8 -> 1
16 -> 4
32 -> None
64 -> None
128-> 6
I test this by simply connecting an oscilloscope to the output pins. The behaviour is extremely strange since the pins seem to be in random order, some even missing to portd
. Furthermore portd = B11111111
, according to the manual, is equivalent to portd = 255;
and should set all pins on HIGH
. But in my case it sets every pin to HIGH
, except pins 5 and 7 are on LOW
.