# C30 Craziness... Wots going on here?

Status
Not open for further replies.

#### krazy

##### New Member
Hey everybody,

I'm trying to code my Microchip dsPIC33FJ with some fairly simple code. I don't know why it's not working though.

Here is my code:
Code:
	void initS2P(void)
{
TRISA = 0x00;  // init all of A register as outputs
LATA = 0x00;   // clear that bad boy out, just to be sure
}

void clockSR()
{
PORTAbits.RA2 = 1;
PORTAbits.RA2 = 0;
}

void updateLEDArray(int shift[8])
{
int bitNum;

for(bitNum = 7; bitNum == 0; bitNum--)
{
PORTAbits.RA3 = shift[bitNum]; // put bit on SER
clockSR();                     // clock SRCLK
}
PORTAbits.RA4 = 1;  // clock REGCLK
PORTAbits.RA4 = 0;
}

initS2P();

int bitArray[8];

bitArray[0] = 0;
bitArray[1] = 1;
bitArray[2] = 0;
bitArray[3] = 1;
bitArray[4] = 0;
bitArray[5] = 1;
bitArray[6] = 0;
bitArray[7] = 0;

updateLEDArray(bitArray);
But, for some reason, none of it is working? I can manually switch the pins. Commands like:
PORTAbits.RA2 = 0;
PORTAbits.RA2 = 1;
would leave RA2 high.

Additionally, I can copy and paste those lines hundreds of times and it works.

Oddly, though, when I simplify it into functions, it stops working.

For some reason, when I de-abstract the code and make it one long main, it then stops working again.

Any ideas?

#### Pommie

##### Well-Known Member
The code in this for loop will never be executed.
Code:
    for(bitNum = 7; bitNum == 0; bitNum--)
Change BitNum to signed and the above to,
Code:
    for(bitNum = 7; bitNum > 0; bitNum--)
Mike.

#### fermat7

##### Member
try that:

for(bitNum = 7; bitNum > 0; bitNum--)

#### Pommie

##### Well-Known Member
try that:

for(bitNum = 7; bitNum > 0; bitNum--)
But it only works if bitNum is signed.

An alternative for unsigned is,
for(bitNum = 7; bitNum < 8; bitNum--)

Not obvious why but it should work.

Mike.

#### smanches

##### New Member
But it only works if bitNum is signed.
Why is that? It should work with unsigned as well. At no point is the number going negative.

#### Pommie

##### Well-Known Member
Whoops, that code should have been,
Code:
    for(bitNum = 7; bitNum [COLOR="Red"]>[/COLOR]= 0; bitNum--)
in which case if it's not signed then it can never be less than zero.

Mike.

#### kchriste

##### New Member
Forum Supporter
For some reason, when I de-abstract the code and make it one long main, it then stops working again.
I think arhi has it figured. Check the assembler listing for a working scenario and a non-working one. I'll bet there are extra instructions in the working code that provide a bit of a delay between pin toggles. Using LATAbits instead of PORTAbits as mentioned by arhi is the way to go anyway because it totally avoids the classic read-modify-write bug.

Status
Not open for further replies.