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 CCS Technical Support

Simple source code = Erratic behaviour

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



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

Simple source code = Erratic behaviour
PostPosted: Sun Mar 07, 2010 9:17 am     Reply with quote

Hello guys,
I created a simple application which measures input frequency+duty cycle and it outputs the same frequency but modified duty cycle according to the ADC reading (it extends the duty cycle).
The problem is that it occasionally prints garbage, stops to work, restarts.
I have tried different crystals (HS, H4), a microcontroller with/without TinyBootloader. The problem is not caused by power supply either.

Compiler v4.104

Does anybody have a clue?
Thanks in advance

Code:

#include <18F1220.h>
#device adc=8
//#device HIGH_INTS=true

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOEBTR                     //Memory protected from table reads
#FUSES NOEBTRB                    //Boot block protected from table reads
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPB                      //Boot Block Code Protected
#FUSES NOWRTB                   //Boot block not write protected

#use delay(clock=20000000)
#use rs232(baud=115200,errors,parity=N,UART1,bits=8)

#define MAX_FLASH getenv("PROGRAM_MEMORY")
#define LOADER_SIZE   0xFF   //tinybld size + a bit more (200 bytes is enough)
#build(reset=0x0000:0x0007)
#org MAX_FLASH-LOADER_SIZE , MAX_FLASH-1 {}//void boot_loader(void) {}

#use fast_io(a)
#use fast_io(b)

#define trisa 0b00000001
#define trisb 0b00011000

#define PWM_OUT PIN_A2

#define DEBUG 1

unsigned int8 PercentToAdd=0;
int8 SecondTimerFlag=false;

int8 DetectRising=0;

unsigned int16 StartTime=0;
unsigned int16 AddedDuty=0;
unsigned int16 Period=0;
unsigned int16 Duty=0;

#int_TIMER0
void TIMER0_isr(void)
{
   SecondTimerFlag=true;
}

#int_TIMER3 //FAST
void TIMER3_isr(void)
{
   output_high(PWM_OUT);
   disable_interrupts(int_timer3);
}

#int_CCP1
void ccp()
{
   unsigned int16 tmr3val,ccpval;
   ccpval=CCP_1;
   
   if(!DetectRising)
   {//falling
      output_low(PWM_OUT);
      Period=ccpval-StartTime;
      StartTime=ccpval;
      disable_interrupts(int_timer3);
      DetectRising=1;
      setup_ccp1(CCP_CAPTURE_RE);
   }
   else
   {
      Duty=ccpval-StartTime;
      DetectRising=0;
      setup_ccp1(CCP_CAPTURE_FE);
     
      if(PercentToAdd<=3)
      {
         output_high(PWM_OUT);
      }
      else
      {
         tmr3val=Period-Duty;
         AddedDuty=((unsigned int32)tmr3val*PercentToAdd)/100L;       
         set_timer3(-AddedDuty);
         clear_interrupt(int_timer3);
         enable_interrupts(int_timer3);   
      }
   }
}
#zero_ram
void main()
{
   unsigned int8 adcval;
   setup_wdt(WDT_OFF);
   
   set_tris_a(trisa);
   set_tris_b(trisb);
   
   port_b_pullups(TRUE);
   
   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_32|ADC_TAD_MUL_4);
   
   set_adc_channel(0);
   if(DEBUG)printf("init\n");
   delay_ms(10);
   
   setup_timer_0(T0_INTERNAL|T0_DIV_64);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
   setup_timer_2(T2_DIV_BY_16,250,15);
   setup_timer_3(T3_INTERNAL|T3_DIV_BY_4);
   
   setup_ccp1(CCP_CAPTURE_FE);
   enable_interrupts(int_ccp1);
   enable_interrupts(int_timer0);
   enable_interrupts(global);
   
   while(1)
   {
      if(SecondTimerFlag)
      {
         SecondTimerFlag=false;
         adcval=read_adc();
         PercentToAdd=((unsigned int16)adcval*100L)/256;
         if(DEBUG)printf("%%%u T%lu dut%lu tmr%lu\n",PercentToAdd,Period,Duty,AddedDuty);
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 1:13 pm     Reply with quote

Quote:

The problem is that it occasionally prints garbage, stops to work, restarts

Consider these possible problems:

Hardware:

1. Improper MCLR circuit.
2. No bypass capacitors (100 nF, ceramic) on the Vdd pins.
3. Poorly regulated power supply.
4. Incorrect crystal circuit.
5. Poor solder joints (cracked, intermittent connections).

One quick way to check the hardware is to use a factory-built eval board
rather than your own breadboard. If the problem goes away, there is
likely something wrong with your breadboard.

Software:

1. Enabling GLOBAL interrupts while inside an #int_xxx routine.
2. Enabling the XINST fuse in an 18F PIC.
3. Writing past the end of an array, thus over-writing other variables.
4. Watchdog timer enabled, but restart_wdt() is not called often enough.

One quick way to find if it's a software problem is to start commenting
out parts of your code and see if the problem goes away. Disable
interrupts. Comment out any "weird" things that you're doing, such as
#build, High Ints, FAST ints, etc.
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Sun Mar 07, 2010 1:37 pm     Reply with quote

thanks for the response.
I am sure this is not HW related, I have tried different crystal oscillators, MCLR is disabled,

>>Comment out any "weird" things that you're doing, such as #build, High ints, FAST ints, etc.
I have already tried those.

>>XINST fuse
It is not possible to use "#fuses NOXINST" keyword when using PIC18F1220.
>>WDT
The watchdog is also disabled.

I am going to comment out some parts of the source code tomorrow. Anyway, the source code is very short - there is nothing which would cause those troubles (e.g. there are no arrays at all...). I highly suspect there is something wrong with the compiler.
I am also going to convert the project to, say, PIC18F2420.

However, can anyone replicate the issue?

Cheers
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

Sorted out
PostPosted: Mon Mar 08, 2010 9:07 am     Reply with quote

I have changed the PIC to 18F2420 and problem is sorted out.
Ttelmah
Guest







PostPosted: Mon Mar 08, 2010 9:43 am     Reply with quote

Obvious thing. Revision B1 silicon.
Look at the errata sheet.

Best Wishes
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Mon Mar 08, 2010 9:51 am     Reply with quote

Ttelmah wrote:
Obvious thing. Revision B1 silicon.
Look at the errata sheet.
Best Wishes

omg, piece of crap.
thanks
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