Need help with what appears to be compiler bug...

jbmillard

Member
I very rarely yell bug when it comes to compilers, but I think this is one. It has to do with clock switching and the sin() function. I submitted a web ticket with MicroChip on this two months ago, but have heard nothing.

I'm running it on a PIC24HJ128GP204. The problem is that after a clock switch the sin() function returns garbage (> 1.0). If I call

Code:
__asm__ volatile("bset.b 0x02C8,#0");

after the clock switch the sine function works normally. I learned of this fix from a year old post on the MicroChip forums. I just wish I knew why it worked. The TRISB register is at address 0x02C8 if I'm reading the special functions register correctly.

Here's a screen shot that shows the problem:

**broken link removed** (it's pretty big which is why I didn't inline it). As you can see, the sin() function is returning 6.469921. If I uncomment the line of asm, it works normally.

Personally, I would love to have someone find something stupid I did, so I could quickly fix it. I am currently using doubles but have them set up as four bytes. I have tried it with floats and doubles and I get the same result.

I have a test program (which gave me the screenshot) if anyone is interested.

Here's the version of the compiler I'm using:

Language tool versions: pic30-as.exe v3.22, pic30-gcc.exe v3.22, pic30-ld.exe v3.22, pic30-ar.exe v3.22

I upgraded to the latest when I first saw this problem. No change.

Thanks!

Brad
 
Last edited:
I just heard from MicroChip tech support and this is the answer in case anybody is interested:


and the clock switch code now looks like:

Code:
__builtin_write_OSCCONH(NOSC_FRCPLL);  //Only NOSC bits are R/W 
__builtin_write_OSCCONL(OSCCON | 0x1); //Initiate clock switch
while(OSCCONbits.COSC != NOSC_FRCPLL); //Wait until clock switch complete
while (OSCCONbits.LOCK != 1);

This does appear to fix the problem.

Brad
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…