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

Help. Why does this program print garbage?

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



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

Help. Why does this program print garbage?
PostPosted: Fri Jul 09, 2010 3:28 am     Reply with quote

This program prints the required values and also lot of garbage. The printf works without problems for the below line:
printf("Hello World");

PCWHD ver 4.108.

Thanks for any HELP.


Code:

#include <main.h>


int32 Ticker;
int16 bres;


void do_1sec_event()
{
Ticker++;
if (Ticker > 21600000) {Ticker = 0;}
}


#int_TIMER1
void TIMER1_isr()                         
{
     
   bres += 65536;   // add 65536 ticks to bresenham total

   if(bres >= 1000000)   // if reached 1 second!
   {
      bres -= 1000000;   // subtract 1 second, retain error
      do_1sec_event();   // update clock, etc
   }

}



void main()
{
     
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(A0_VR_A1_VR);
   setup_vref(VREF_LOW | 12);   
   setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 );
   enable_interrupts( INT_TIMER1 );           
   enable_interrupts( GLOBAL );
 
Do
  {

    printf("%lu\r\n",Ticker);
   
  }while(1);
   

}
jaikumar



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

This code works. But Still prints garbage now and then.
PostPosted: Fri Jul 09, 2010 4:06 am     Reply with quote

Code:

#include <main.h>


int32 Ticker=0;
int32 bres=0;


#int_TIMER1
void TIMER1_isr()                         
{
 
   bres += 65536;   // add 65536 ticks to bresenham total

   if(bres >= 1000000)   // if reached 1 second!
   {
      bres -= 1000000;   // subtract 1 second, retain error
      if (Ticker < 21600000) Ticker++;
   }
 
}



void main()
{
     
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(A0_VR_A1_VR);
   setup_vref(VREF_LOW | 12);   
   setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 );
   enable_interrupts( INT_TIMER1 );           
   enable_interrupts( GLOBAL );
   Ticker = 0;
   bres = 0;
 
While(TRUE)
  {

    printf("TMR1 = %lu\n\r", Ticker);
    delay_ms(500);
   
  }
   

}
Ttelmah



Joined: 11 Mar 2010
Posts: 19257

View user's profile Send private message

PostPosted: Fri Jul 09, 2010 4:09 am     Reply with quote

A number of things:
1) You don't show your RS232 setup. Is this a hardware RS232, or a software port. If software, the individual characters _will_ get corrupted by interrupts occurring. You need to disable interrupts for each transmitted character (DISABLE_INTS option in the RS232 setup). Doesn't apply if this is a hardware RS232 port.
2) You need to retrieve the 'ticker', then print it. Problem here is that printing is slow, and the printout, will work character by character 'through' the data, which is a changing value, giving garbage.
3) For the same reason, but involving a faster time interval, when you retrieve the ticker, interrupts should be disabled for the transfer.
So, something like:
Code:

int32 local_ticker;

    //Then in the main
    Do {
        disable_interrupts(GLOBAL);
        local_ticker=Ticker;
        enable_interrupts(GLOBAL);
        printf("%lu\r\n",local_ticker);
    } while (TRUE);


Best Wishes
jaikumar



Joined: 15 Dec 2006
Posts: 109

View user's profile Send private message

Now it works.
PostPosted: Fri Jul 09, 2010 4:15 am     Reply with quote

Did exactly as Ttelmah advised, working perfectly.

Thanks Ttelmah.
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