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

PIC18F45K22 Issue without ICD set to TRUE

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



Joined: 26 Dec 2010
Posts: 4
Location: Connecticut

View user's profile Send private message Visit poster's website

PIC18F45K22 Issue without ICD set to TRUE
PostPosted: Thu Dec 30, 2010 7:53 pm     Reply with quote

I'm new to PIC programming and the CCS compiler, though I have been a programmer on DOS and Windows for decades. My first project is proving the learning curve still exists.

I strip the program down to its simplest form. I have code that monitors 2 input pins and sets a digital pattern on 4 output pins.

The code works as long as the #Device ICD=TRUE is present. If it is commented out (to save code space in the future) then the chip does not communicate to the LCD or respond to any input stimulus.

My compiler is version 4.116. My code is below.
I am running the PIC with the PLL on and a 16 MHZ xtal.
MCLR has a pull up resistor.

I am betting its something obvious but I appreciate any assistance anyone can offer.

Main.H
Code:

#include <18F45K22.h>
#device ICD=TRUE               //Comment out this line kills all functions
#device adc=8
#FUSES NOWDT
#FUSES WDT1                     //Watch Dog Timer uses 1:1 Postscale
#FUSES H4                       //High speed with 4x PLL
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No 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 MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODELAYINTOSC         
#FUSES PLLEN                    //Enable PLL 4x Clock
#FUSES PRIMARY_ON           
#FUSES TIMER3C0             
#FUSES CCP2D2               
#FUSES WDT_NOSLEEP

#use delay(clock=64Mhz, crystal)


tools.c
Code:

//****************************************************************************
//
//  Global Pin Definitions
//
//****************************************************************************
//I/O Pin Constants
#define Range_Select_Bit0_PIN    PIN_A0   //Range Control Bit 0
#define Range_Select_Bit1_PIN    PIN_A1   //Range Control Bit 1
#define Range_Select_Bit2_PIN    PIN_A2   //Range Control Bit 2

#define Master_Range_Select_PIN  PIN_A3   //Master Range Control Bit

#define Select_Key_Pin           PIN_B0   //Select Key Bit
#define Up_Key_Pin               PIN_B1   //Up Key Bit
#define Down_Key_Pin             PIN_B2   //Down Key Bit

#define ADC_Ready_Pin            PIN_B3   //ADC Ready Bit
#define ADC_Standby              PIN_A7   //ADC Standby Bit

//****************************************************************************
//
//  Global Control Constants
//
//****************************************************************************
//Range Select Constants
Const int RANGE_1    = 0;  //Range 1 Index
Const int RANGE_2    = 1;  //Range 2 Index
Const int RANGE_3    = 2;  //Range 3 Index
Const int RANGE_4    = 3;  //Range 4 Index
Const int RANGE_5    = 4;  //Range 5 Index
Const int RANGE_6    = 5;  //Range 6 Index
Const int RANGE_7    = 6;  //Range 7 Index
Const int RANGE_8    = 7;  //Range 8 Index

//****************************************************************************
//
//  Global Control Variables
//
//****************************************************************************
//Present Variables
int     P_Selected_Range;     //Present Selected Range
//****************************************************************************
//
//  Functions and Procedures
//
//****************************************************************************
//*************************
//  Port Setup Functions
//*************************
void SetUpAllIO(void)
{
//Make All Analog Pins Digital
#byte ANSELA = 0xF38
#byte ANSELB = 0xF39
#byte ANSELC = 0xF3A
#byte ANSELD = 0xF3B
#byte ANSELE = 0xF3C
ANSELA = 0;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;

//Set Fast IO for All
  #USE FAST_IO (All)

//Set Input and Output Assignments
  //Set A7,A4,A3,A2,A1,A0 as Output (0), A6,A5 as Input (1)
  SET_TRIS_A(0b01100000);
  //Set B all as Input (1)
  SET_TRIS_B(0b11111111);
  //Set .C6,C5,.C3... as Output (0), C7,..C4.... as Input (1)
  SET_TRIS_C(0b10010111);
  //Set D6 as Output (0), All others Input (1)
  SET_TRIS_D(0b10111111);
}

//*************************
//  Range Functions
//*************************
void  SetRange(int iRange)
{
   P_Selected_Range = iRange;
   Switch(P_Selected_Range) {
      Case RANGE_1 :
           //  Set Ouptut Controls 
             output_low(Range_Select_Bit2_PIN);
             output_low(Range_Select_Bit1_PIN); 
             output_low(Range_Select_Bit0_PIN);
             output_low(Master_Range_Select_PIN);
             Break;
      Case RANGE_2 :
           //  Set Ouptut Controls   
             output_low(Range_Select_Bit2_PIN);
             output_low(Range_Select_Bit1_PIN); 
             output_high(Range_Select_Bit0_PIN);
             output_low(Master_Range_Select_PIN);
             Break;
      Case RANGE_3 :
           //  Set Ouptut Controls
             output_low(Range_Select_Bit2_PIN);
             output_high(Range_Select_Bit1_PIN); 
             output_low(Range_Select_Bit0_PIN);
             output_low(Master_Range_Select_PIN);
             Break;
      Case RANGE_4 :
           //  Set Ouptut Controls   
             output_low(Range_Select_Bit2_PIN);
             output_high(Range_Select_Bit1_PIN); 
             output_high(Range_Select_Bit0_PIN);
             output_low(Master_Range_Select_PIN);
             Break;
      Case RANGE_5 :
           //  Set Ouptut Controls   
             output_high(Range_Select_Bit2_PIN);
             output_low(Range_Select_Bit1_PIN); 
             output_low(Range_Select_Bit0_PIN);
             output_low(Master_Range_Select_PIN);
             Break;   
      Case RANGE_6 :
           //  Set Ouptut Controls
             output_high(Range_Select_Bit2_PIN);
             output_low(Range_Select_Bit1_PIN); 
             output_high(Range_Select_Bit0_PIN);
             output_high(Master_Range_Select_PIN);
             Break;
      Case RANGE_7 :
           //  Set Ouptut Controls
             output_high(Range_Select_Bit2_PIN);
             output_high(Range_Select_Bit1_PIN); 
             output_low(Range_Select_Bit0_PIN);
             output_high(Master_Range_Select_PIN);
             Break;
       Case RANGE_8 :
            //  Set Ouptut Controls
             output_high(Range_Select_Bit2_PIN);
             output_high(Range_Select_Bit1_PIN); 
             output_high(Range_Select_Bit0_PIN);
             output_high(Master_Range_Select_PIN);
              Break;   
      default:
         P_Selected_Range = RANGE_1;
         //  Set Ouptut Controls
           output_low(Range_Select_Bit2_PIN);
           output_low(Range_Select_Bit1_PIN); 
           output_low(Range_Select_Bit0_PIN);
           output_high(Master_Range_Select_PIN);
           break;
      }//Case
}//SetRange

//*************************
//  Key Functions
//*************************
void  ProcessUpKey(void)
{   
  //Debounce 20 mS
    delay_ms(20);
  //Check if sill down
    If (!Input(Up_Key_Pin)) {
      //Wait for Key Release
        Do {} While (!Input(Up_Key_Pin));
      //Increment Range
        If (P_Selected_Range==RANGE_8)
          P_Selected_Range = RANGE_1;
        Else
          P_Selected_Range = P_Selected_Range+1;
      //Select Range
        SetRange(P_Selected_Range);
    }//If (!Input(Up_Key_Pin))
}//ProcessUpKey


void  ProcessDownKey(void)
{
  //Debounce 20 mS
    delay_ms(20);
  //Check if sill down
    If (!Input(Down_Key_Pin)) { 
      //Wait for Key Release
        Do {} While (!Input(Down_Key_Pin));
      //Decrement Range
         If (P_Selected_Range==RANGE_1)
           P_Selected_Range = RANGE_8;
         Else
           P_Selected_Range = P_Selected_Range-1;
      //Select Range
        SetRange(P_Selected_Range);
    }//If (!Input(Down_Key_Pin))
}//ProcessDownKey

void  SetupSystem(void)
{
  setup_adc_ports(NO_ANALOGS|VSS_VDD);
  setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
  setup_spi(SPI_SS_DISABLED); 
  setup_spi2(SPI_SS_DISABLED);
  setup_wdt(WDT_OFF);
  setup_timer_0(RTCC_INTERNAL);
  setup_timer_1(T1_DISABLED);
  setup_timer_2(T2_DISABLED,0,1);
  setup_timer_4(T4_DISABLED,0,1);
  setup_ccp1(CCP_OFF);
  setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
  SetUpAllIO();
  //Delay 600 mS
    delay_ms(600);
  //Set Starting Range to No Range
    SetRange(RANGE_1);
   
}//SetupSystem


main.c
Code:

#include <main.h>
#include <tools.c>

void main()
{
  //  Setup Meter
    SetupSystem();
   
  //Main Outer Reset Loop
    while(TRUE){

      //Check Keys 
        If (!Input(Up_Key_Pin))
          ProcessUpKey();
        If (!Input(Down_Key_Pin))
          ProcessDownKey();

    }//Main Loop End

}// Program End
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 30, 2010 8:45 pm     Reply with quote

See if this thread helps.
http://www.ccsinfo.com/forum/viewtopic.php?t=28356

If not, compile your program with and without the #device ICD=TRUE
line. Compare the fuses listed at the end of the .LST file for each
compilation.

The reason for doing this is because when you invoke the
#device ICD=TRUE statement, the compiler will automatically disable
certain fuse settings. Disabling these fuses may be what allows your
program to run.

If you compare the fuses for the two compilations, it may tell you which
fuse setting is the critical one.
BRathbunSpaceElectronics



Joined: 26 Dec 2010
Posts: 4
Location: Connecticut

View user's profile Send private message Visit poster's website

PIC18F45K22 Runaway Inter
PostPosted: Sun Jan 02, 2011 3:30 pm     Reply with quote

Thanks PCM Programmer, I was able to track it down to the change below.

Code:


Fuses with ICD=TRUE

Configuration Fuses:
   Word  1: F200   HSH PLLEN PRIMARY_ON FCMEN IESO
   Word  2: 0019   NOPUT NOBROWNOUT BORV20 NOWDT WDT1
   Word  3: BD00   CCP2C1 NOPBADEN CCP3B5 NODELAYINTOSC TIMER3C0 CCP2D2 MCLR
   Word  4: 0000   NOSTVREN NOLVP NOXINST DEBUG
   Word  5: C00F   NOPROTECT NOCPB NOCPD
   Word  6: E00F   NOWRT NOWRTC NOWRTB NOWRTD
   Word  7: 400F   NOEBTR NOEBTRB

Fuses without ICD=TRUE

Configuration Fuses:
   Word  1: F200   HSH PLLEN PRIMARY_ON FCMEN IESO
   Word  2: 0119   NOPUT NOBROWNOUT BORV20 WDT_NOSLEEP WDT1
   Word  3: BD00   CCP2C1 NOPBADEN CCP3B5 NODELAYINTOSC TIMER3C0 CCP2D2 MCLR
   Word  4: 0081   STVREN NOLVP NOXINST NODEBUG
   Word  5: C00F   NOPROTECT NOCPB NOCPD
   Word  6: E00F   NOWRT NOWRTC NOWRTB NOWRTD
   Word  7: 400F   NOEBTR NOEBTRB



As you can see, the Word 4 fuses are different. The DEBUG and Stack Overflow FUSES have changed and the stack overflow fuse is the one that is causing the PIC to appear lockup.

This indicates to me a stack overrun is the ultimate source of my locked up. Having not much PIC experience my guess is I have an interrupt executing without the proper interrupt routine, thus casing the stack overflow to trip. I am not implicitly enabling any interrupts in my code but my thought is that one of the functions in my setup procedure is enabling an interrupt.

Any thoughts? Thanks,

Brandon
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 3:54 pm     Reply with quote

Quote:
Fuses with ICD=TRUE

Configuration Fuses:
Word 1: F200 HSH PLLEN PRIMARY_ON FCMEN IESO
Word 2: 0019 NOPUT NOBROWNOUT BORV20 NOWDT WDT1
Word 3: BD00 CCP2C1 NOPBADEN CCP3B5 NODELAYINTOSC TIMER3C0 CCP2D2 MCLR
Word 4: 0000 NOSTVREN NOLVP NOXINST DEBUG
Word 5: C00F NOPROTECT NOCPB NOCPD
Word 6: E00F NOWRT NOWRTC NOWRTB NOWRTD
Word 7: 400F NOEBTR NOEBTRB

In the section above, the ICD=TRUE is disabling the Watchdog timer.

Quote:

Fuses without ICD=TRUE

Configuration Fuses:
Word 1: F200 HSH PLLEN PRIMARY_ON FCMEN IESO
Word 2: 0119 NOPUT NOBROWNOUT BORV20 WDT_NOSLEEP WDT1
Word 3: BD00 CCP2C1 NOPBADEN CCP3B5 NODELAYINTOSC TIMER3C0 CCP2D2 MCLR
Word 4: 0081 STVREN NOLVP NOXINST NODEBUG
Word 5: C00F NOPROTECT NOCPB NOCPD
Word 6: E00F NOWRT NOWRTC NOWRTB NOWRTD
Word 7: 400F NOEBTR NOEBTRB

In the section above, the Watchdog timer is enabled, and at a very
rapid rate.

Solution: Edit the #fuses your original code. Delete all references
to "WDT" or anything that uses it (in the fuses). Then add NOWDT to
the fuses. Re-compile and test.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 4:00 pm     Reply with quote

Interrupts have to be explicitly enabled. I don't see this in your code.

I'm a bit surprized about automatic disabling of stack overflow reset with ICD. I remember, that I have traced a stack overflow under PCH V 3.xx, so the setting seemed to be compatible with ICD operation at times.

P.S.:
Quote:
In the section above, the Watchdog timer is enabled, and at a very rapid rate.

Yes, sounds more plausible. Simply remove the #FUSE WDT1 setting, that cancels #FUSE NOWDT. If you intended to use a
watchdog timer, select a suitable rate.
BRathbunSpaceElectronics



Joined: 26 Dec 2010
Posts: 4
Location: Connecticut

View user's profile Send private message Visit poster's website

Thanks ...
PostPosted: Mon Jan 03, 2011 9:24 am     Reply with quote

My troubleshooting skills lead me astray, turns out the WDT fuses were the issue. I'm up and running, and thanks for the help.

I'm now off to research RS232 interrupt issues.

Thanks again for all the help.

Brandon
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