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

booting problems with serial

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



Joined: 19 Jul 2008
Posts: 16

View user's profile Send private message

booting problems with serial
PostPosted: Fri Jan 29, 2010 8:27 pm     Reply with quote

When I don't have serial plugged into my board, my pic chip boots great everytime. When I do have serial plugged in, the chip boots correctly about 2/3 of the time. If I plug the serial in after booting the chip, it works fine. This could be a problem on my board, but I've checked thoroughly and it seems ok. Is it possible that there's something in my code that is causing trouble?

This is a PIC18F2620-I/SP.

Thanks,
Richard
Code:

#include "C:\Documents and Settings\RichardJohnson\Desktop\submersible\2620\2620stepper1_28_10.h"

#use delay(clock=20,000,000)
#use rs232 (baud = 9600, parity = N, xmit = PIN_C6, rcv = PIN_C7, bits = 8, ERRORS)
#define KEYHIT_DELAY 500
#define RAND_MAX 1000
#include <STDLIB.H>
#include <math.h>

int1 whichStepper = 1;
int8 whichSerial = 0;
int1 loopOnOff = 0;
int8 serial0 = 0;
int8 serial1 = 0;
int1 serialOn = 0;
//int8 iPins = 0;
int16 timer = 0;
int8 switcher = 0;
int8 posAdder = 30;
int8 delayAdder = 40;
int8 resetLocAdder = 50;
int8 onOffAdder = 60;
int8 loopAdder  = 70;

typedef struct {
   int8 thisMotor;
   int16 delay;
   int16 counter;
   int16 loc;
   int16 newLoc;
   int16 pins[4];
   int8 steps[4][4];
   int8 modLoc;
   int1 motorOn;
   int16 timer;
   int1 loopOnOff;
   int16 loopDelay;
   int16 locL;
   int16 locH;
   int1 loopSwitch;
}stepperStruct;

stepperStruct stepper0;
stepperStruct stepper1;
stepperStruct stepper2;
stepperStruct stepper3;
stepperStruct stepper4;

void setpins(stepperStruct *structIn);
void stepperTimer(stepperStruct *structIn);
void setLoc(int8 serial0, int8 serial1, stepperStruct *structIn);
void setDelay(int8 serial0, int8 serial1, stepperStruct *structIn);
void resetLoc(int8 serial0, int8 serial1, stepperStruct *structIn);
void updateStepper(stepperStruct *structIn);
void pinsOff(stepperStruct *structIn);
void motorOnOff(int8 serial0, int8 serial1, stepperStruct *structIn);
void misc(int8 serial_0, int8 serial_1);
void loopsOnOff(int8 serial_0, int8 serial_1, stepperStruct *structIn);
void loop(stepperStruct *structIn);
void setVariables(stepperStruct *structIn, int16 counter, int8 loc, int8 newLoc, int8 delay, int1 motorOn, int16 timer, int1 loopOnOff,
   int16 loopDelay, int8 locL, int8 locH, int1 loopSwitch);


#INT_RTCC
void servocounter()
{
   stepper0.timer++;
   stepper1.timer++;
   stepper2.timer++;
   stepper3.timer++;
   stepper4.timer++;
   stepper0.counter++;
   stepper1.counter++;
   stepper2.counter++;
   stepper3.counter++;
   stepper4.counter++;
   timer++;
   set_timer0(60535);
}

#INT_RDA
void serialfunction()
{
   serialOn = 1;
   serial0 = getc();
}

void main()
{
   setup_comparator (NC_NC_NC_NC);
   setup_timer_0 (RTCC_DIV_1|RTCC_INTERNAL);
   enable_interrupts (INT_rda);
   enable_interrupts (INT_RTCC);
   enable_interrupts (GLOBAL);
   
   delay_ms(500);
   
   output_high(PIN_B7);
   
   if(whichStepper == 0)
   {
      stepper0.thisMotor = 0;
      stepper1.thisMotor = 1;
      stepper2.thisMotor = 2;
      stepper3.thisMotor = 3;
      stepper4.thisMotor = 4;
   }
   else
   {
      stepper0.thisMotor = 5;
      stepper1.thisMotor = 6;
      stepper2.thisMotor = 7;
      stepper3.thisMotor = 8;
      stepper4.thisMotor = 9;
   }
   
   if(whichStepper == 0)
   {
      stepper0.pins[0] = 31763; stepper0.pins[1] = 31762; stepper0.pins[2] = 31764; stepper0.pins[3] = 31765;//c3, c2, c4, c5
      stepper1.pins[0] = 31754; stepper1.pins[1] = 31755; stepper1.pins[2] = 31752; stepper1.pins[3] = 31753;//b2, b3, b0, b1
      stepper2.pins[0] = 31748; stepper2.pins[1] = 31749; stepper2.pins[2] = 31760; stepper2.pins[3] = 31761;//a4, a5, c0, c1
      stepper3.pins[0] = 31747; stepper3.pins[1] = 31746; stepper3.pins[2] = 31745; stepper3.pins[3] = 31744;//a3, a2, a1, a0
      stepper4.pins[0] = 31756; stepper4.pins[1] = 31757; stepper4.pins[2] = 31759; stepper4.pins[3] = 31758;//b4, b5, b7, b6
   }
   else
   {
      stepper0.pins[0] = 31744; stepper0.pins[1] = 31746; stepper0.pins[2] = 31745; stepper0.pins[3] = 31747;//A0, A2, A1, A3
      stepper1.pins[0] = 31758; stepper1.pins[1] = 31756; stepper1.pins[2] = 31759; stepper1.pins[3] = 31757;//B6, B4, B7, B5
      stepper2.pins[0] = 31748; stepper2.pins[1] = 31755; stepper2.pins[2] = 31754; stepper2.pins[3] = 31749;//A4, B3, B2, A5
      stepper3.pins[0] = 31753; stepper3.pins[1] = 31752; stepper3.pins[2] = 31764; stepper3.pins[3] = 31765;//B1, B0, C4, C5
      stepper4.pins[0] = 31760; stepper4.pins[1] = 31761; stepper4.pins[2] = 31762; stepper4.pins[3] = 31763;//C0, C1, C2, C3
   }

   //setVar(   struct, count, loc, newLoc, delay, motorOn, timer, loopOnOff, loopDelay, locL, locH, loopSwitch)
   setVariables(&stepper0, 0,  1,   2,     10,     0,       0,     0,         100,       0,    250,  0);
   setVariables(&stepper1, 0,  1,   2,     10,     0,       0,     0,         100,       0,    250,  0);
   setVariables(&stepper2, 0,  1,   2,     10,     0,       0,     0,         100,       0,    250,  0);
   setVariables(&stepper3, 0,  1,   2,     10,     0,       0,     0,         100,       0,    250,  0);
   setVariables(&stepper4, 0,  1,   2,     10,     0,       0,     0,         100,       0,    250,  0);
     
   delay_ms (1000);
   
   //output_high(PIN_B4);
   //output_high(PIN_B5);
   //output_high(PIN_B6);
   output_low(PIN_B7);
   //stepper0.motorOn = 1;
   //stepper3.motorOn = 1;
     
   //stepper0.newLoc = 250;
   
   WHILE (true)
   {
      if (serialOn == 1)
      {
         //output_toggle(PIN_B7);
         if(whichSerial == 0)
         {
            //output_toggle(PIN_B6);
            if(serial0 < 30)
            {
               misc(serial0, serial1);
            }
            else if(serial0 < 40)
            {
               //output_toggle(PIN_B7);
               setLoc(serial0, serial1, &stepper0);
               setLoc(serial0, serial1, &stepper1);
               setLoc(serial0, serial1, &stepper2);
               setLoc(serial0, serial1, &stepper3);
               setLoc(serial0, serial1, &stepper4);
            }
            else if(serial0 < 50)
            {
               //output_toggle(PIN_B7);
               setDelay(serial0, serial1, &stepper0);
               setDelay(serial0, serial1, &stepper1);
               setDelay(serial0, serial1, &stepper2);
               setDelay(serial0, serial1, &stepper3);
               setDelay(serial0, serial1, &stepper4);
            }
            else if(serial0 < 60)
            {
               resetLoc(serial0, serial1, &stepper0);
               resetLoc(serial0, serial1, &stepper1);
               resetLoc(serial0, serial1, &stepper2);
               resetLoc(serial0, serial1, &stepper3);
               resetLoc(serial0, serial1, &stepper4);
            }
            else if(serial0 < 70)
            {
               //output_toggle(PIN_B6);
               motorOnOff(serial0, serial1, &stepper0);
               motorOnOff(serial0, serial1, &stepper1);
               motorOnOff(serial0, serial1, &stepper2);
               motorOnOff(serial0, serial1, &stepper3);
               motorOnOff(serial0, serial1, &stepper4);
            }
            else if(serial0 < 80)
            {
               loopsOnOff(serial0, serial1, &stepper0);
               loopsOnOff(serial0, serial1, &stepper1);
               loopsOnOff(serial0, serial1, &stepper2);
               loopsOnOff(serial0, serial1, &stepper3);
               loopsOnOff(serial0, serial1, &stepper4);
               }
         }
         serialOn = 0;
      }
      loop(&stepper0);
      loop(&stepper1);
      loop(&stepper2);
      loop(&stepper3);
      loop(&stepper4);
      //stepperTimer(&stepper0);
      updateStepper(&stepper0);
      //updateStepper(&stepper1);
      updateStepper(&stepper2);
      updateStepper(&stepper3);
      //updateStepper(&stepper4);
   }
}

void updateStepper(stepperStruct *structIn)
{
   if(structIn->motorOn > 0)
   {
      //output_high(PIN_B6);
      stepperTimer(structIn);
   }
   else
   {
      if(structIn->thisMotor == 5)
      {
         //output_low(PIN_B6);
         //output_low(PIN_B7);
      }
      pinsOff(structIn);
   }
}

void stepperTimer(stepperStruct *structIn)
{
   if(structIn->thisMotor == 5)
   {
      //output_high(PIN_B6);
   }
   if(structIn->counter > structIn->delay)
   {
      if(structIn->newLoc > structIn->loc)
      {
         //output_high(PIN_B6);
         structIn->loc++;
         structIn->modLoc++;
         if(structIn->modLoc == 4)
         {
            structIn->modLoc = 0;
         }
         //setpins(structIn);
      }
      else if(structIn->newLoc < structIn->loc)
      {
         //output_high(PIN_B7);
         structIn->loc--;
         structIn->modLoc--;
         if(structIn->modLoc > 3)
         {
            structin->modLoc = 3;
         }
         //setpins(structIn);
      }
      setpins(structIn);
      structIn->counter = 0;
   }
}

void setpins(stepperStruct *structIn)
{
   int8 iPins = 0;
   for(iPins=0;iPins<4;iPins++)
   {
      output_bit(structIn->pins[iPins], structIn->steps[iPins][structIn->modLoc]);
   }
}

void setLoc(int8 serial_0, int8 serial_1, stepperStruct *structIn)
{
   if(serial_0 == structIn->thisMotor+posAdder)
   {
      //output_toggle(PIN_B7);
      structIn->newLoc = serial_1;
   }
}

void motorOnOff(int8 serial_0, int8 serial_1, stepperStruct *structIn)
{
   if (serial_0 == structIn->thisMotor+onOffAdder)
   {
      //output_toggle(PIN_B7);
      structIn->motorOn = (int1)serial_1;
   }
}

void setDelay(int8 serial_0, int8 serial_1, stepperStruct *structIn)
{
   if(serial_0 == structIn->thisMotor+delayAdder)
   {
      //output_toggle(PIN_B7);
      structIn->delay = serial_1;
   }
}

void resetLoc(int8 serial_0, int8 serial_1, stepperStruct *structIn)
{
   if(serial_0 == structIn->thisMotor+resetLocAdder)
   {
      structIn->loc = serial_1;
      structIn->newLoc = serial_1;
   }
}

void loopsOnOff(int8 serial_0, int8 serial_1, stepperStruct *structIn)
{
   if(serial_0 == structIn->thisMotor+loopAdder)
   {
      structIn->loopOnOff = serial_1;
   }
}

void pinsOff(stepperStruct *structIn)
{
   int8 iPins = 0;
   for(iPins=0;iPins<4;iPins++)
   {
      output_low(structIn->pins[iPins]);
   }
}

void misc(int8 serial_0, int8 serial_1)
{
   switch (serial_0) {
      case 15: loopOnOff = serial_1; break;
   }
}

void loop(stepperStruct *structIn)
{
   if(structIn->loopOnOff > 0)
   {
      //output_toggle(PIN_B6);
      if(structIn->timer > structIn->loopDelay)
      {
         //output_toggle(PIN_B6);
         if(structIn->loopSwitch < 1)
         {
            //output_toggle(PIN_B7);
            structIn->newLoc = structIn->locL;
            structIn->loopSwitch = 1;
         }
         else
         {
            //output_toggle(PIN_B7);
            structIn->newLoc = structIn->locH;
            structIn->loopSwitch = 0;
         }
         structIn->timer = 0;
      }
   }
}

void setVariables(stepperStruct *structIn, int16 counter, int8 loc, int8 newLoc, int8 delay, int1 motorOn, int16 timer, int1 loopOnOff,
   int16 loopDelay, int8 locL, int8 locH, int1 loopSwitch)
{
   structIn->counter = counter;
   structIn->loc = loc;
   structIn->newLoc = newLoc;
   structIn->delay = delay;
   structIn->motorOn = motorOn;
   structIn->timer = timer;
   structIn->loopOnOff = loopOnOff;
   structIn->loopDelay = loopDelay;
   structIn->locL = locL;
   structIn->locH = locH;
   structIn->loopSwitch = loopSwitch;
   structIn->modLoc = 0;
   
   structIn->steps[0][0] = 1; structIn->steps[0][1] = 0; structIn->steps[0][2] = 0; structIn->steps[0][3] = 1;
   structIn->steps[1][0] = 0; structIn->steps[1][1] = 1; structIn->steps[1][2] = 1; structIn->steps[1][3] = 0;
   structIn->steps[2][0] = 1; structIn->steps[2][1] = 1; structIn->steps[2][2] = 0; structIn->steps[2][3] = 0;
   structIn->steps[3][0] = 0; structIn->steps[3][1] = 0; structIn->steps[3][2] = 1; structIn->steps[3][3] = 1;

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 29, 2010 9:15 pm     Reply with quote

Troubleshoot it.

1. Comment out the line that enables Global interrupts.
Re-compile, test, and see if you still get the problem.

2. If you do, then un-comment that line. Add a while(1) loop
immediately after the line that enables global interrupts.
In the loop, test your 'serialOn' flag with an if() statement.
If it's set, then display the value on a terminal window, using "%X"
in a printf statement. This will tell you if you're getting some
unexpected character upon start-up.

Also, post the contents of the .h file. If it doesn't contain your #fuses
then post that statement also.

Post your compiler version.

Post if you built this board yourself or if you bought it. If you bought it,
post the manufacturer and part number. If you built it, did you follow
all standard design practices ? i.e., good power and ground, regulated
power supply, 100 nF (0.1 uF) caps on all Vdd pins, using proper MCLR
circuit, as recommended by CCS and Microchip, etc.
rwjzownts



Joined: 19 Jul 2008
Posts: 16

View user's profile Send private message

PostPosted: Sat Jan 30, 2010 2:17 pm     Reply with quote

Thanks for the advice. I commented out the interrupts, and the problem still happens, so I think it must be in the board.

I designed the board, and had it fabricated by pcbfabexpress. I've taken care of everything that you mention, except the capacitor for vdd. I'll do that from now on.

My compiler version is 4.88.

Here's my .h file

Code:
#include <18F2620.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOBROWNOUT                 //Reset when brownout detected
#FUSES BORV21                   //Brownout reset at 2.1V
#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 NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS)
rwjzownts



Joined: 19 Jul 2008
Posts: 16

View user's profile Send private message

PostPosted: Sat Jan 30, 2010 3:21 pm     Reply with quote

I've added the capacitors to both of the chips running this code, and that has solved the booting problem. However, one now has serious timing problems. It is running about 20 times too slow. Any ideas why this would be happening?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 31, 2010 12:19 am     Reply with quote

Make a test program that blinks an LED once per second. By looking at
the LED, you should be able to easily tell if the program is running
correctly. The LED must have a series resistor (you can use 470 ohms).
Code:

#include <18F2620.h>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=20000000)

//======================================
void main(void)
{

while(1)
  {
   output_high(PIN_B0);   
   delay_ms(100);   // Do a short blink once per second
   output_low(PIN_B0);
   delay_ms(900);
  }

}
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