|
|
View previous topic :: View next topic |
Author |
Message |
margo
Joined: 23 Sep 2008 Posts: 1
|
funky code startup |
Posted: Tue Sep 23, 2008 8:22 am |
|
|
When I step through the following code the the beginning seems to be randomly jumping around. I think this is causing a problem because i always have to do a soft reset before the other software that i am using will connect to the pic. Is this a problem with the compiler or my code? I am using MPLAB IDE Version 8.14.00.00 with the CCS C compiler. any suggestions would be great
thanks!!
Here is the sequence of jumps of where the arrow stops when i step through the code in debugger mode.
bracket after void main()
a line in the sting.h file
Global variable declaration
Global variable declaration
Global variable declaration
SET_TRIS_C( 0x00 );
send_string( "1.01" );
a line in void toggle_led()
SET_TRIS_E( 0xFF );
from here it will sequentially step through the program
here is the code
Code: |
#include <16F877A.H>
#device ADC=10
#fuses HS,WDT,NOPUT,BROWNOUT,LVP,NOWRT,NOPROTECT
#use delay(clock=20000000,restart_wdt)
#define send_string(data) \
sprintf(response,data); \
send_string_engine(response);
#include <string.h>
#define MaxPressure 107 //40 psi
#define MinPressure 85 //30 psi
#define MaxVacuum 44 //22 in/Hg
#define MinVacuum 70 //24 in/Hg
#define WR PIN_B2
#define RD PIN_B1
#define SND PIN_E1
#define TXE PIN_E2
#define RXF PIN_B4
#define LED PIN_E0
#define WRL PIN_B5
#define PUMP1 PIN_A2 //AD OUT for Vacuum
#define PUMP2 PIN_A5 //AD OUT for Pressure
#define PULSER PIN_B0 //To pulse and hold bjt
#define IND PIN_A4 //Indicator Lights
#define BOTTLEADDRESS 0x00
#define NONE 0xFF
#define MaxBottleRows 8
#define MaxValveBytes 32
#define ExtraBytes 3
#define NumPressure 2
/**********************************************************
Global Definition
**********************************************************/
short led_state;
char response[30];
int remainder;
int buffer[50];
int j;
int ValveBytes = MaxValveBytes;
int Valves[MaxValveBytes];
int Pressure;
int Vacuum;
int BottleStates[MaxBottleRows];
int BottleRows = 0;
int tempBottleStates[MaxBottleRows];
long counter = 0;
long hundred_mss;
long i;
long longDelay;
/**********************************************************
Prototype Definition
**********************************************************/
int read_usb();
void send(int thebyte);
void send_string_engine( char* data );
void receive_packet();
void strobe_SND();
void strobe_WRL();
void address(int n);
void toggle_led();
void initialize();
void delay_one_ms(long n);
void sendState();
void write_valves();
int read_AD(int channel);
void readBottleSensors();
void pump_control();
void longDelay_processor();
void PumpTimerISR();
/**********************************************************
MAIN
**********************************************************/
void main()
{
//1111 1111 port E idle state: 7654 3210 1=input 0=output
SET_TRIS_E( 0xFF ); //all input
//1111 1111 port D idle state: 7654 3210 1=input 0=output
SET_TRIS_D( 0xFF ); //all input
//1111 0000 port C idle state: 7654 3210 1=input 0=output
SET_TRIS_C( 0x00 ); //all output
//1111 1111 port B idle state: 7654 3210 1=input 0=output
SET_TRIS_B( 0xFE ); //all input
//1111 0011 port A idle state: 7654 3210 1=input 0=output
SET_TRIS_A( 0x03 ); //four in four out
while(!input(RXF)) //purge USB FIFO
{
output_low(RD);
output_high(RD);
}
setup_ccp1(CCP_OFF); //CCP1 off
setup_ccp2(CCP_OFF); //CCP2 off
setup_adc_ports( AN0_AN1_AN3 );//ANALOG 0
setup_adc( ADC_CLOCK_DIV_32 );//enable the A/D converter
// clear_interrupt(int_timer1);
// setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); //20M/(4*8*65536)
// enable_interrupts(global);
// disable_interrupts(INT_TIMER1);
// set_timer1(0); //restarts timer
output_high(WR);//init USB WR
output_high(RD);//init USB RD
output_high(SND);//init SND
output_high(LED);//turn off LED
address(NONE);
output_low(WRL);
output_high(PUMP1);
output_high(PUMP2);
for(i=0; i<10; i++)
{
RESTART_WDT();
delay_ms(40);
output_high(LED);
output_high(WRL);
delay_ms(40);
output_low(LED);
output_low(WRL);
}//leave the LED on
initialize(); //home
for(;;)
{
RESTART_WDT();
//byte received in FIFO buffer...
if(!input(RXF))
{
receive_packet();
switch (buffer[1]){
case 1: //Return Firmware version
send_string( "1.01" );
buffer[1] = 0;
break;
case 2: //Run Custom Sequence
ValveBytes = buffer[0] - ExtraBytes;
longDelay = buffer[2]*256 + buffer[3];
BottleRows = buffer[4];
for (i=0;i<ValveBytes;i++)
{
Valves[i] = buffer[i+4];
}
write_valves();
readBottleSensors();
sendState();
delay_one_ms(longDelay);
//longDelay_processor();
buffer[1] = 0;
break;
case 3: //Read Bottle, Pressure, Liquid Sensor
BottleRows = buffer[4];
readBottleSensors();
sendState();
buffer[1] = 0;
break;
default:
send_string( "ERROR: Invalid OP Code Read" );
break;
}//end of switch
}//end of if(!RXF)
toggle_led();
//pump_control();
}//end of for(;;)
}//end of main()
|
|
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 23, 2008 9:07 am |
|
|
It is not 'randomly jumping around'. It is initialising the variables.
All 'static' variables, are initialised to zero, when the code starts (there is one in string.h). Then all global variables with initialisation values, are set to these values (you have several). Finally any local variables in main, with initialised values are set (you don't have any of these).
It doesn't really 'jump' (no physical 'jump' instructions), but the debugger display will move to each location involved as the variables are initialised.
This is not the problem.
A number of comments. If you want to use TRIS, then you need to set fast_io mode. Otherwise your TRIS statements are a waste of time, and likely to cause problems.
Enable the PUT fuse. NOPUT, is only reliable,if you have a very fast rising power rail, or an external MCLR circuit.
If RXF, stays low for more than 18mSec, you may well have a watchdog timeout in the loop checking this....
You have told the code to restart the watchdog in delays, then do it yourself as well in several places. Why?.
Best Wishes |
|
|
|
|
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
|