Hi,
I'm having this weird problem whereby I am able to debug, and to step
through the program, but it just ignores/skips some of the lines.
In the code below, I'll show the skipped lines with \\<<< at the end.
Any ideas, please?
Thanks,
Cat
#include <inttypes.h>
#include <avr/io.h>
#include <stdio.h>
volatile uint8_t iMotorPosition[9] = {0, 0b110110, 0b100110, 0b110101,
0b010101, 0b111001, 0b101001, 0b111010, 0b011010};
volatile uint8_t iOldInput = 0;
uint8_t iBitsChanged = 0; //moved here because AVRstudio gives "Location not
valid" when inside "ReadInput"
int8_t iStepX = 1; //The steps will become negative when direction changes
int8_t iStepY = 1; //or I'll set them according to the actual HI/LO value
int8_t iStepZ = 1;
int8_t iCurrentStepX = 1;
int8_t iCurrentStepY = 1;
int8_t iCurrentStepZ = 1;
void SetupPorts (void);
void ReadInput(void);
int main (void)
{
SetupPorts(); \\<<<
while(1)
{
ReadInput();
}
}
void ReadInput(void)
{
uint8_t iNewInput = PIND & 63; //<<<Read port D and mask (keep) only the
lower 6 bits \\<<<
if (iNewInput == iOldInput)
return;
//Set Step signs
iStepX = bit_is_set(iNewInput, 0) ? 1 : -1;
iStepY = bit_is_set(iNewInput, 2) ? 1 : -1;
iStepZ = bit_is_set(iNewInput, 4) ? 1 : -1;
//Find out which bits changed and act accordingly
iBitsChanged = iNewInput ^ iOldInput; //We use "^" (XOR) to check which
bits have changed
iOldInput = iNewInput; //Save New input as Old for next time
//Mask direction bits in iBitsChanged MAYBE NOT NEEDED
iBitsChanged &= 0b101010;
if (bit_is_set(iBitsChanged, 1)) //If X-step changed
{
if (bit_is_set(iNewInput, 1)) //And if it changed to HI
{
iCurrentStepX +=iStepX;
if (iCurrentStepX == 9)
iCurrentStepX = 1;
if (iCurrentStepX == 0)
iCurrentStepX = 8;
PORTA = iMotorPosition[iCurrentStepX];
}
}
if (bit_is_set(iBitsChanged, 3)) //If Y-step changed
{
if (bit_is_set(iNewInput, 3)) //And if it changed to HI
{
iCurrentStepY +=iStepY;
if (iCurrentStepY == 9)
iCurrentStepY = 1;
if (iCurrentStepY == 0)
iCurrentStepY = 8;
PORTB = iMotorPosition[iCurrentStepY];
}
}
if (bit_is_set(iBitsChanged, 5)) //If Z-step changed
{
if (bit_is_set(iNewInput, 5)) //And if it changed to HI
{
iCurrentStepZ +=iStepZ;
if (iCurrentStepZ == 9)
iCurrentStepZ = 1;
if (iCurrentStepZ == 0)
iCurrentStepZ = 8;
PORTC = iMotorPosition[iCurrentStepZ];
}
}
}
void SetupPorts(void)
{
DDRD = 0; //PortD ->Input
DDRA = 0xFF; //PortA ->Output X
DDRB = 0xFF; //PortB ->Output Y
DDRC = 0xFF; //PortC ->Output Z
}