Hardware: Mega2560
IDE: AS7
I'm having issues declaring the correct type of variable, and probably other issues I'm not aware of.
Short Term Objective: Read PORTA, invert the entire port values, write the result to PORTC.
I thought I could read PORTA and assign the value to the variable 'portAread', invert it using ~(portAread), and then just write that value to PORTC. That is not how it is done, apparently.
Long Term Objective: Read PORTA, compare to previous read using bitwise operators. Then depending on the result, write it to either PORTC or PORTL.
I can write the binary value of portAread to the serial monitor, but can't seem to invert it and/or write it to PORTC.
The ports are wired together: C0 (output) is connected to A0 (input), C1 to A1, etc. I'm doing this to demonstrate the functionality of the code.
The way I've written my code, I thought I would see the value of portAread cycle between 0b00000000 and 0b11111111. That is not happening - it stays at 0b11111111.
Any direction is very appreciated.
byte pinState = LOW;
byte lastPinState = HIGH;
unsigned int portAread;
byte pin = HIGH;
unsigned long firstTime;
unsigned long secondTime;
unsigned long resultTime;
int count = 0;
void setup()
{
//lcd.begin(8, 1);
Serial.begin(57600);
DDRA = B0000000; // Port A is INPUT
PORTA = B11111111; // Sets pull-up
DDRC = B11111111; // Port C is OUTPUT
PORTC = B00000000; // Clear Port C
pinMode(13, OUTPUT);
}
void loop()
{
// Visual confirmation it's not hung.
pin = !pin;
digitalWrite(13,pin);
firstTime = micros();
// ***** Suspected problem is with next 3 lines, and probably the way the variable was declared. **********
portAread = PORTA; // Read PORTA into variable
portAread = ~(portAread); //Invert values.
PORTC = portAread; // Write variable to PORTC
secondTime = micros();
resultTime = secondTime - firstTime;
Serial.print(count);
Serial.print("\t");
Serial.print(portAread, BIN);
Serial.print("\t");
Serial.println(resultTime);
++count;
delay(1500);
}