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?
 
I didn't understand what exactly is not working, but use LATAbits instead of PORTAbits for setting the output value.
 
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.
 
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.
 
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.
 
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.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…