CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Severe problem with printf and interrupt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Zer0flag
Guest







Severe problem with printf and interrupt
PostPosted: Wed Apr 16, 2008 4:17 am     Reply with quote

Hi all!

In my program I have implemented a temperature PI Controller routine in a timer interrupt in order to achieve equidistant calls. So there are a couple of calculations (including 32 bit divisions and multiplications) as well as reads and writes to the SPI bus in this interrupt. I know one should not put so much stuff in the interrupt but I didn't find a possibility to implement equidistant calls in the main loop and there are no other critical interrupts so I did it that way.

Now I notice a severe problem with printf trying to output a 32 bit integer in the main program as long as the PI Controller interrupt is enabled.

The following code:
Code:

printf("%Lu", (int32) 0);


outputs most of the time 0 but from time to time it outputs something else. I also tested the whole thing with a 32 bit variable (instead of the hardcoded 0) and with numbers other than zero and the problem persists. Strangely with int16 I don't see the problem.

If I disable the PI Controller interrupt around printf everything works ok.

Printf is not called in my interrupt but I think there might be some functions used by printf that are also used in my interrupt (e.g. some calculation functions) that are not reentrant or something and they get messed up when the interrupt occurs at a specific point in time during the printf.

Any help is welcome!

Best regards,
Zer0flag
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Apr 16, 2008 11:21 am     Reply with quote

Are you using the hardware UART or a software emulated UART? Prove this by posting the chip number and the #use rs232 line.

In case you are using the hardware UART, than notice that on most chips this is double buffered. This could be a partial explanation of why a 2-byte int16 always succeeds and a larger message sometimes fails.
Zer0flag
Guest







PostPosted: Wed Apr 16, 2008 2:34 pm     Reply with quote

ckielstra wrote:
Are you using the hardware UART or a software emulated UART? Prove this by posting the chip number and the #use rs232 line.

In case you are using the hardware UART, than notice that on most chips this is double buffered. This could be a partial explanation of why a 2-byte int16 always succeeds and a larger message sometimes fails.


Hi!

This is the PIC and UART stuff:

Code:

#include <18F4620.H>
#use rs232(baud=19200, parity=N, bits=8, stop=1, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


The problem is that calling printf with another function like:

Code:

printf(bputc,"%Lu", (int32) 0);


or

Code:

printf(lcd_putc,"%Lu", (int32) 0);


results in the same error.

sprintf() also gives the error described in my first post.
itoa() in contrast does not give an error.

Regards,
Zer0flag
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group