Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

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:

Answer:
------------
Even though I can't duplicate the issue on the part I'm using there is a published exchange instruction errata on this part which is used in the "sin".function in the standard math library, <math.h>.

Step1: Add the following errata math library file to your MPLAB project window under the Library File folder:
C:\Program Files\Microchip\MPLAB C30\errata-lib\libm-coff.a
Step2: Lose the clock switch .s and .h file from your project. Use the C30 built-in macro's instead.

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
 
Status
Not open for further replies.

Latest threads

Back
Top