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

PIC18F4520 delay issue

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



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PIC18F4520 delay issue
PostPosted: Fri Feb 23, 2007 5:55 am     Reply with quote

Hi!

I'm running a PIC18F4520 with a 10 MHz xtal and x4 pll enabled. There is only a LED on PIN_A0 to test if the uC is alive. Compiler version is 4.025.

My oscilloscope confirms a nice 10 MHz sine wave on osc1 and osc2 pins.
But PIN_A0 toggles about every 7.5 us instead 500 ms as expected.

Here is the code:

Code:

#include <18F4520.h>

#FUSES WDT64                    //Watch Dog Timer uses 1:64 Postscale
#FUSES H4                       //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV25                   //Brownout reset at 2.5V
#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 IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOLPT1OSC                //Timer1 configured for higher power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES XINST                    //Extended set extension and Indexed Addressing mode enabled

#use delay(clock=40M, oscillator=10M, RESTART_WDT)       
#use rs232(baud = 9600, parity = N, xmit = RS232_TX, rcv = RS232_RX, bits = 8, stream = SFC, enable = PIN_B1, restart_wdt)


void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
   setup_wdt(WDT_ON);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_PWM_FULL_BRIDGE|CCP_SHUTDOWN_ON_INT0|CCP_SHUTDOWN_AC_L|CCP_SHUTDOWN_BD_L);
   setup_ccp2(CCP_PWM);
   set_pwm1_duty(512);
   set_pwm2_duty(512);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   setup_oscillator(False);

   for (;;) {
      output_toggle(PIN_A0);
      delay_ms(500);
      restart_wdt();
   }
}


It's not the first time a use this MCU.... what do I miss?

Thanks,
Marco / iw2nzm
ckielstra



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

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 7:06 am     Reply with quote

Code:
   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
Is this the actual program you are testing with? You activate a lot of interrupts but in the posted code there are no interrupt handling routines, this could cause all kind of problems.

What happens when you change the code to disable the watchdog? (add fuse NOWDT and change the call to setup_wdt() ).

Code:
#use delay(clock=40M, oscillator=10M, RESTART_WDT)
This has changed in v4.xxx and I don't know how well tested this is. Test what happens when you revert to
Code:
#use delay(clock=40000000, RESTART_WDT)
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 9:44 am     Reply with quote

ckielstra wrote:
Is this the actual program you are testing with? You activate a lot of interrupts but in the posted code there are no interrupt handling routines, this could cause all kind of problems.


The actual program contains empty handling routines like this:

Code:

#int_TIMER0
void  TIMER0_isr(void)
{

}


Below I copy the whole code, there also a lot of #define but they should not be a trouble.

Quote:

What happens when you change the code to disable the watchdog? (add fuse NOWDT and change the call to setup_wdt() ).


Nothing. I already tried this.

Quote:

Code:
#use delay(clock=40M, oscillator=10M, RESTART_WDT)
This has changed in v4.xxx and I don't know how well tested this is. Test what happens when you revert to
Code:
#use delay(clock=40000000, RESTART_WDT)


As before, I have just tried again with no difference.
The PIC seems alive because the clock is running and I can change the I/O lines.

I also disabled interrupts, nothing to do.

Bye
Marco / iw2nzm

the whole code:

Code:

#include <18F4520.h>

#FUSES NOWDT                    //Watch Dog Timer uses 1:64 Postscale
#FUSES H4                       //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV25                   //Brownout reset at 2.5V
#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 IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOLPT1OSC                //Timer1 configured for higher power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES XINST                    //Extended set extension and Indexed Addressing mode enabled

#use delay(clock=40000000, RESTART_WDT)       
#define ADC_DATA_IN         PIN_A0
#define ADC_DATA_OUT         PIN_A1
#define ADC_CS               PIN_A2
#define ADC_CK               PIN_A3
#define ADC_EOC            PIN_A4
#define COMPASS_ENB         PIN_A5
#define COMPASS_DATA_READY PIN_B0
#define RS485_ENB            PIN_B1
#define DAC_LOAD            PIN_B2
#define DAC_LDAC            PIN_B3
#define A0                  PIN_B4
#define A1                  PIN_B5
#define A2                  PIN_B6
#define RTC_DATA            PIN_B7
#define COMPASS_RX         PIN_C0
#define PWM2               PIN_C1
#define PWM1               PIN_C2
#define SPI_CLK            PIN_C3
#define SPI_DI               PIN_C4
#define SPI_DO               PIN_C5
#define RS232_TX            PIN_C6
#define RS232_RX            PIN_C7
#define BUS_D0               PIN_D0
#define BUS_D1               PIN_D1
#define BUS_D2               PIN_D2
#define BUS_D3               PIN_D3
#define BUS_D4               PIN_D4
#define BUS_D5               PIN_D5
#define BUS_D6               PIN_D6
#define BUS_D7               PIN_D7
#define OSD_CS               PIN_E0
#define OSC_SCL            PIN_E1
#define OSC_SDA            PIN_E2
#use rs232(baud = 9600, parity = N, xmit = RS232_TX, rcv = RS232_RX, bits = 8, stream = SFC, enable = RS485_ENB, restart_wdt)


#int_TIMER0
void  TIMER0_isr(void)
{

}

#int_TIMER1
void  TIMER1_isr(void)
{

}

#int_TIMER2
void  TIMER2_isr(void)
{

}

#int_EXT
void  EXT_isr(void)
{

}

#int_RDA
void  RDA_isr(void)
{

}

#int_SSP
void  SSP_isr(void)
{

}



void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
   setup_wdt(WDT_ON);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_16,255,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_ccp1(CCP_PWM_FULL_BRIDGE|CCP_SHUTDOWN_ON_INT0|CCP_SHUTDOWN_AC_L|CCP_SHUTDOWN_BD_L);
   setup_ccp2(CCP_PWM);
   set_pwm1_duty(512);
   set_pwm2_duty(512);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_SSP);
   disable_interrupts(GLOBAL);
   //setup_oscillator(False);

   for (;;) {
      output_toggle(PIN_A0);
      delay_ms(500);
      //restart_wdt();
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 10:50 am     Reply with quote

Quote:
#FUSES XINST //Extended set extension and Indexed Addressing mode enabled

CCS doesn't support the extended instruction set. Change that
fuse to NOXINST.
Ttelmah
Guest







PostPosted: Fri Feb 23, 2007 10:51 am     Reply with quote

Two things.
As shown, add a 'getc', to the INT_RDA handler. Alone of the interrupts shown, this one, if enabled, and the serial input line drops, will cause the PIC to hang completely, if the character is not read in the handler. Though you have the global interrupt disabled, this is worth doing to avoid potential problems.
Turn off the XINST fuse.
Though the new compiler, may be starting to use the extended instructions, this instruction would cause problems for the older compilers, and I'd not trust the new compilers on this....

Best Wishes
iw2nzm



Joined: 23 Feb 2007
Posts: 55

View user's profile Send private message

PostPosted: Fri Feb 23, 2007 11:37 am     Reply with quote

PCM programmer wrote:

CCS doesn't support the extended instruction set. Change that
fuse to NOXINST.


This was my fault. Rolling Eyes
Now it works fine. Thank you and thanks to Ttelmah too.

Marco / iw2nzm
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