|
|
View previous topic :: View next topic |
Author |
Message |
jaikumar
Joined: 15 Dec 2006 Posts: 109
|
Help. Why does this program print garbage? |
Posted: Fri Jul 09, 2010 3:28 am |
|
|
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
|
This code works. But Still prints garbage now and then. |
Posted: Fri Jul 09, 2010 4:06 am |
|
|
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: 19552
|
|
Posted: Fri Jul 09, 2010 4:09 am |
|
|
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
|
Now it works. |
Posted: Fri Jul 09, 2010 4:15 am |
|
|
Did exactly as Ttelmah advised, working perfectly.
Thanks Ttelmah. |
|
|
|
|
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
|