View previous topic :: View next topic |
Author |
Message |
dc_eng
Joined: 22 Jul 2009 Posts: 8
|
Interesting problem here... |
Posted: Wed Jul 22, 2009 6:13 am |
|
|
Hi to all.
I am working on a piece of code that drives me crazy..
My uC is PIC18F4620, My compiler verison is 4.068. My code was working perfectly until I add some more functions. Then suddenly it stops functioning.
My uC is communicating with other uC with same type via rs485. My transmitting uC is working without problem. And code on that uC is fixed. I am not changing it.
On receiver side which causes problem. When I download an older version of my code it works. So, there is no electrical problem. Problem is software.
My original program was much longer but I delete most of the functions in order to debug where the problem is. Finally I delete all functions except the one that receives rs485 data.
So, there is 2 functions 1 main and 1 rs485 receiving function.
But my code was still not functioning.
Than I start to delete UNUSED variables since I delete all other functions. Than suddenly it start to work.
So I work around and here is what I conclude.
If I declare a variable like below it works
INT MODBUS_PACKET[17];
If I declare a variable like below it won't
INT MODBUS_PACKET[18];
You are probably saying that I am using all RAM. But my RAM usage is %6.
All the same program except 1 more integer that changes everything.
Also I must say that the variable above has nothing to do with my code. Compiler warns me that that variable is never being used.
Does anyone has experienced this kind of problem.
thanks.
dc |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jul 22, 2009 9:47 am |
|
|
Yes ![Razz](images/smiles/icon_razz.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 22, 2009 12:07 pm |
|
|
Quote: |
Finally I delete all functions except the one that receives rs485 data. |
Post this small program. Make sure it compiles with no errors before
you post it. |
|
![](templates/subSilver/images/spacer.gif) |
sirius
Joined: 27 Jun 2009 Posts: 16 Location: Bulgaria
|
|
Posted: Wed Jul 22, 2009 5:09 pm |
|
|
Wayne_ ,please explain this,in case you have an explanation,that makes sense of course .I'm curious about this,and mainly how to avoid it.Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
dc_eng
Joined: 22 Jul 2009 Posts: 8
|
|
Posted: Thu Jul 23, 2009 1:03 am |
|
|
Hi again,
I am posting my code below.
Code: |
#include "D:\P_FILES\PROJECTS\XXXXXX\XXXXXX_WBLOCK\Software\MW_73.h"
#include <string.h>
#include <stdlib.h>
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#DEFINE VIB_PULSE PIN_C3
#DEFINE Q4_IN2 PIN_D0
#DEFINE Q4_IN1 PIN_D1
#DEFINE Q3_IN2 PIN_D2
#DEFINE Q3_IN1 PIN_D3
#DEFINE Q2_IN2 PIN_D4
#DEFINE Q2_IN1 PIN_D5
#DEFINE Q1_IN2 PIN_D6
#DEFINE Q1_IN1 PIN_D7
#DEFINE HSENSOR_UP PIN_C1
#DEFINE HSENSOR_DOWN PIN_C2
#DEFINE SERIALO PIN_E1
#DEFINE SERIALI PIN_E2
#DEFINE RS485_1TX PIN_C5
#DEFINE RS485_1RX PIN_C4
#DEFINE RS485_2TX PIN_B0
#DEFINE RS485_2RX PIN_C6
#DEFINE RS485_TXEN PIN_C7
#DEFINE D_TX PIN_A2
#DEFINE D_RX PIN_A3
#DEFINE RESET_BAR PIN_A0
#DEFINE RDY_BAR PIN_A5
#DEFINE SCLK PIN_A1
#DEFINE BN_2E0 PIN_B5
#DEFINE BN_2E1 PIN_B4
#DEFINE BN_2E2 PIN_B3
#DEFINE BN_2E3 PIN_B2
#DEFINE BN_2E4 PIN_B1
INT SOFTWARE_VERSION;
INT32 GENERAL_PARAMETERS;
INT1 COMMUNICATION_TYPE,SEQUENTIAL_DISCHARGE,AVERAGE_CONTROL,ON_START_BOTH_UP_DOWN;
INT1 AUTOMATIC_VIBRATOR_INTENSITY;
INT NUMBER_OF_DEFFERENT_PRODUCTS;
INT DISCHARGE_PER_PACKAGE;
INT1 DISCHARGE_OF_MAIN_DOOR,FEEDING_MACHINE,HOLD_MAIN_DOOR_ON_MULTIPLE_DISCHARGE;
INT NUMBER_OF_CHANNELS;
INT REJECT_SIGNAL;
INT MAIN_DOOR;
INT16 TARGET_WEIGHT;
INT16 UPPER_LIMIT;
INT16 LOWER_LIMIT;
INT16 C1TARGET_WEIGHT,C2TARGET_WEIGHT,C3TARGET_WEIGHT,C4TARGET_WEIGHT;
INT C1MAX_DEVIATION;
INT C2MAX_DEVIATION;
INT C3MAX_DEVIATION;
INT C4MAX_DEVIATION;
INT32 DISABLED_CHANNELS; // 0-> CHANNEL IS DEAD, 1-> CHANNEL LIVE
INT SEQUENTIAL_DISCHARGE_DELAY;
INT REMOVE_TARE_PERIOD;
INT DISCHARGE_NUMBER_UP_TO_TARE=0; // IN EVERY DISCHARGE CHANNEL FUNCTION THIS MUST BE CHECKED
INT NOT_IN_COMBINATION_LIMIT;
INT FILLING_PERCENTAGE;
INT STEP_DELAY_MOTOR_UP;
INT STEP_DELAY_MOTOR_DOWN;
INT HOLD_TIME_MOTOR_UP;
INT HOLD_TIME_MOTOR_DOWN;
INT CENTER_POINT_OF_MOTOR_UP;
INT CENTER_POINT_OF_MOTOR_DOWN;
INT VIB_TO_CUP_UP_DELAY;
INT CUP_UP_TO_CUP_DOWN_DELAY;
INT CUP_DOWN_TO_MAIN_DOOR_DELAY;
INT AD_SETTLING_TIME;
INT CHANNEL_ID;
INT SLOW_DISCHARGE_ANGLE;
INT VIB_INTENSITY[29];
INT VIB_DURATION[29];
INT STEP_DELAY_MOTOR_MAIN_A;
INT STEP_DELAY_MOTOR_MAIN_B;
INT STEP_DELAY_MOTOR_MAIN_C;
INT STEP_DELAY_MOTOR_MAIN_D;
INT HOLD_TIME_MOTOR_MAIN_A;
INT HOLD_TIME_MOTOR_MAIN_B;
INT HOLD_TIME_MOTOR_MAIN_C;
INT HOLD_TIME_MOTOR_MAIN_D;
INT ANGLE_OF_MOTOR_MAIN_A;
INT ANGLE_OF_MOTOR_MAIN_B;
INT ANGLE_OF_MOTOR_MAIN_C;
INT ANGLE_OF_MOTOR_MAIN_D;
INT16 AD_COEFF =0;
INT PROGRAM_ID;
INT16 PID_OFFSET;
INT STEP_CNTUP =0;
INT STEP_CNTDW =0;
INT VIB_CNT =0;
INT PHASE_UP =0; // 1,2,3 OR 4
INT PHASE_DW =0; // 1,2,3 OR 4
INT PHASE_VIB =0;
INT STEP_OUTPUT =0B11111111;
INT16 DELAY_TICKS_UP;
INT16 DELAY_TICKS_DOWN;
INT16 DELAY_TICKS_VIB;
INT16 DELAY_TICKS_AD;
INT16 TIMER0_CNT;
INT16 TARGET_TIMER;
INT16 TIMER0_IS;
INT32 DATA485;
INT FUNC485;
INT32 CHID485;
INT32 DATA485_;
INT FUNC485_;
INT32 CHID485_;
// MODBUS VARIABLES
INT MODBUS_PACKET[9];
INT8 AD_COM_REG =0;
INT8 AD_STATUS_REG =0;
INT32 AD_DATA_REG =0;
INT32 AD_DATA_REGT =0;
INT32 AD_MODE_REG =0;
INT32 AD_CONF_REG =0;
INT32 AD_FILTER_REG =0;
INT8 AD_DAC_REG =0;
INT32 AD_OFFSET_REG =0;
INT32 AD_GAIN_REG =0;
INT32 AD_TEST_REG =0;
INT32 AD_DATA_REGZERO =0;
INT32 AD_DATA_REGAV =0;
CHAR U_DATA[10];
CHAR P_NAME[15];
INT1 RCV_TOGGLE;
INT CNT1;
// RECEIVING RS485 DATA
VOID RECV_RS485() {
INT CNT1;
CHID485=0;
FUNC485=0;
DATA485=0;
RCV_TOGGLE = 1;
FOR (CNT1 = 0 ; CNT1<=30 ; CNT1++) {
CHID485 = CHID485 | INPUT_STATE(RS485_1RX); // 0
CHID485 = CHID485<<1;
WHILE (INPUT_STATE(RS485_2RX)==RCV_TOGGLE) { }
RCV_TOGGLE^=1;
}
CHID485 = CHID485 | INPUT_STATE(RS485_1RX); // 0
WHILE (INPUT_STATE(RS485_2RX)==RCV_TOGGLE) { }
RCV_TOGGLE^=1;
FOR (CNT1 = 0 ; CNT1<=6 ; CNT1++) {
FUNC485 = FUNC485 | INPUT_STATE(RS485_1RX); // 1
FUNC485 = FUNC485<<1;
WHILE (INPUT_STATE(RS485_2RX)==RCV_TOGGLE) { }
RCV_TOGGLE^=1;
}
FUNC485 = FUNC485 | INPUT_STATE(RS485_1RX);
WHILE (INPUT_STATE(RS485_2RX)==RCV_TOGGLE) { }
RCV_TOGGLE^=1;
FOR (CNT1 = 0 ; CNT1<=30 ; CNT1++) {
DATA485 = DATA485 | INPUT_STATE(RS485_1RX); // 16
DATA485 = DATA485<<1;
WHILE (INPUT_STATE(RS485_2RX)==RCV_TOGGLE) { }
RCV_TOGGLE^=1;
}
// LAST BIT DO NOT WAIT
DATA485 = DATA485 | INPUT_STATE(RS485_1RX); // 31
IF (CHANNEL_ID >= 1) {
PRINTF("%LU ",CHID485);
PRINTF("%U ",FUNC485);
PRINTF("%LU \n\r",DATA485);
}
} // VOID RECV_RS485()
void main() {
// 76543210
SET_TRIS_A(0B00101000);
SET_TRIS_B(0B00111110);
SET_TRIS_C(0B01010110);
SET_TRIS_D(0B00000000);
SET_TRIS_E(0B00000100);
OUTPUT_A(0B00000000);
OUTPUT_B(0B00000000);
OUTPUT_C(0B00000000);
OUTPUT_D(0B11111111);
OUTPUT_E(0B00000000);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(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_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
WHILE (1) {
PRINTF("W..\n\r");
WHILE ( !INPUT_STATE(RS485_2RX) ) { }
RECV_RS485();
}
} // void main() {
|
Also my header code.
Code: |
#include <18F4620.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 BROWNOUT //Reset when brownout detected
#FUSES BORV43 //Brownout reset at 4.3V
#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 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 XINST //Extended set extension and Indexed Addressing mode enabled
#FUSES noPBADEN //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=115200,parity=N,xmit=PIN_E1,rcv=PIN_E2,bits=8,invert)
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 23, 2009 1:09 am |
|
|
This fuse will cause the program to behave in erratic ways. Change the
fuse to NOXINST. Do this in all your programs. |
|
![](templates/subSilver/images/spacer.gif) |
dc_eng
Joined: 22 Jul 2009 Posts: 8
|
|
Posted: Thu Jul 23, 2009 1:39 am |
|
|
I have changed it before, there was no difference.
But you are right. Compiler also warns that that feature is not supported.
Yesterday suddenly code start to work, so I put back all functions back.
Again there was no problem.
After playing around the for a while again it suddenly stop functioning.
But it is so strange that I am curious, maybe I was wrong, maybe I was downloading the working code. I do not know.
Also the problem can be vista too. ![Confused](images/smiles/icon_confused.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 23, 2009 1:43 am |
|
|
Quote: | I have changed it before, there was no difference. |
This implies that after you saw that "there was no difference", you set
it to XINST again. This is wrong. Never use XINST under any
circumstances. Always use NOXINST.
Quote: |
Yesterday suddenly code start to work, so I put back all functions back. |
You need to find out what made it suddenly start working. You need to
go back and trace through those steps again, and debug the problem. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jul 23, 2009 2:04 am |
|
|
sirius wrote: | Wayne_ ,please explain this,in case you have an explanation,that makes sense of course .I'm curious about this,and mainly how to avoid it.Thanks! |
dc_eng stated that by changing the array size he could make it fail or work, this usually indicates memory corruption as changing the size of the array moves variables around in memory. Although the corruption is always there (Unless it is the size of that array that is causing the problem (overrun)) it may not be noticeable depending on what variables are being corrupted.
I initially posted "Yes" and nothing else because dc_eng did not give us any information we could work with so I was being just as vague.
Now he has posted some code it is not quite so obvious, he also keeps changning the code from one extreme to the other without actually tracking down the problem
"Yesterday suddenly code start to work, so I put back all functions back."
"After playing around the for a while again it suddenly stop functioning. "
And what does this mean "Also the problem can be vista too." ?
The code is also hard to read as it contains alot of unused variables, A LOT OF UPPERCASE TEXT , and a lot of global variables!
I will still endevour to try and find the problem based on what is given as others are doing as well ![Smile](images/smiles/icon_smile.gif) |
|
![](templates/subSilver/images/spacer.gif) |
dc_eng
Joined: 22 Jul 2009 Posts: 8
|
|
Posted: Thu Jul 23, 2009 2:33 am |
|
|
Hi,
I try the following. I have increase my array to 1000,
INT MODBUS_PACKET[1000];
Than from top I start to delete unused variables 5-6 lines at a time.
Than suddenly something happened.
Compiler gave an error on my function, but I was just deleting unused variables. Also error was a syntax error.
So I re-compile without making any difference, than compiler says OK there is no problem.
And my function start to work.
Up to that compiler error function was not functioning because I have increased variables.
Can it be because of any hidden characters or so?
Or like wayne_ said because of capital letters
Wayne my laptop is 12" so I need big characters ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jul 23, 2009 3:14 am |
|
|
Using capitals shouldn't affect your program in this case
Just makes it uncomfortable to read. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jul 23, 2009 3:19 am |
|
|
dc_eng wrote: | Hi,
I try the following. I have increase my array to 1000,
INT MODBUS_PACKET[1000];
|
Did it work ?
Quote: |
Than from top I start to delete unused variables 5-6 lines at a time.
Than suddenly something happened.
Compiler gave an error on my function, but I was just deleting unused variables. Also error was a syntax error.
So I re-compile without making any difference, than compiler says OK there is no problem.
And my function start to work.
Up to that compiler error function was not functioning because I have increased variables.
|
?
Quote: |
Can it be because of any hidden characters or so?
|
Unlikely.
Quote: |
Or like wayne_ said because of capital letters
|
No
Quote: |
Wayne my laptop is 12" so I need big characters ![Very Happy](images/smiles/icon_biggrin.gif) |
LOL
Show us the code, tell us what change you made to make it work. Put the last thing you did back in, does it fail ? |
|
![](templates/subSilver/images/spacer.gif) |
dc_eng
Joined: 22 Jul 2009 Posts: 8
|
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jul 23, 2009 5:32 am |
|
|
So far I have found the following
Good output
2 43 0
Bad output
10 192 0
The second value should not be possible !
FUNC485=0;
Then read 6 bits into FUNC485.
192 = 11000000 (NEEDS 8 bits!)
This may be explaind by FUNC485 being inverted after the intialisation and before reading 6 zeros in.
The binary representation of the values also indicate that the bad data is actually part of the good data shifted.
1010 (10) 11000000 (192 ?) >> 4 = xxxx (2) 101011 (43)
It doesn't quite add because if the first 4 bits == 2 then you would get 101010 () for the 1st bad value.
Can you post the memory allocation of your variables, .lst file I think. |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jul 23, 2009 5:44 am |
|
|
Also try using an int16 and an int32 for your dummy var which causes the problem and post your findings (work No work)
Also move the location of the dummy var, again post your findings. |
|
![](templates/subSilver/images/spacer.gif) |
|