 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Thu Jan 10, 2013 9:03 am |
|
|
I see Ckielstra has already posted the glaring error.
The size will depend more on the variable types involved than anything else. Your large says says to me that almost certainly the 'flag' 'B_alarms', is declared as possibly an int. Wastes storage space _and time_. Use an int1, and a single instruction can set/clear it. I'd suggest that both speed and size may well be improved by looking carefully at the actual data types used throughout.
At the very least, if you do put maths in an if statement, you should bracket it. Otherwise even a fractional misunderstanding (by either you, or the compiler), of the precedence rules will lead to disaster.
Best Wishes |
|
 |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu Jan 10, 2013 9:29 am |
|
|
| Ttelmah wrote: |
At the very least, if you do put maths in an if statement, you should bracket it. Otherwise even a fractional misunderstanding (by either you, or the compiler), of the precedence rules will lead to disaster.
Best Wishes |
As this is an airfield application, that "disaster" might conceivably be of the "massive fireball with many maimed and injured" variety. That's why I asked about the coding standards that might have to be applied to this code.
Another reason for not including such things in if statements is that the order of evaluation is not guaranteed. Indeed lazy evaluation may mean code you think is being executed, such as increments, may not actually be happening at all depending on the inputs to the condition. In other words you could introduce data-dependant faults which are difficult to debug, test and verify. What do you expect this code will do to i if A_Flag is false?
| Code: |
int i = 0;
int An_Array[10] = { 0,0,0,0,0,0,0,0,0,0};
...
while (i < 10)
{
if (A_Flag && An_Array[i++]++ < 100)
{
output_toggle(PIN_A2);
}
};
|
Pointers are, of course, a part of C that commonly introduces errors. Safety critical C standards often depricates pointers entirely due to their inherent coding "dangers". Pointer code is often more difficult to read and therefore maintain than its indexed counterpart, further increasing the likelyhood of bugs.
RF Developer |
|
 |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Thu Jan 10, 2013 10:50 am |
|
|
| Quote: | | almost certainly the 'flag' 'B_alarms', is declared as possibly an int. |
I very often use boolean types :
| Code: | #define BOOLEAN int1
Boolean B_Alarme=FALSE; |
OK for " maths in an if statement"  |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Thu Jan 10, 2013 12:09 pm |
|
|
CCS has a major bug with int1 types, when used in certain logic constructs. This has been reported to them, and they have said they will fix this in the next release. Problem is that they don't correctly handle propagation to the required higher type, when a construct is used with (say) an int1 on the left, and a higher type on the right. However the same construct works correctly with the higher type on the left.
A big caveat.....
Best Wishes |
|
 |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Mon Jan 14, 2013 3:15 am |
|
|
Dear friend
Hope the WE was good enough
I largely took advantage of all your comments, but ... it still doesn't work properly.
I have :
| Code: | #use RS232(BAUD=38400,BITS=8,PARITY=N,STOP=1,ENABLE=PIN_C5, UART1, ERRORS)
#PRIORITY TIMER3,EXT
#INT_TIMER3 high
void Counter()
{
set_timer3(L_Period);
L_Compteur++;
B_Count=TRUE; //doit donc battre le quart de milliseconde
}
/*************************************************************************************
Se déclenche à chaque flanc de la boucle de courant
Temps mesuré 220 µs
**************************************************************************************/
#INT_EXT high
void Boucle()
{
L_Compteur_Flanc=L_Compteur;
disable_interrupts(INT_RDA);
B_Compteur_Flanc=TRUE;
}
/************************************************************************
se produit lors de l'arrivée de caractère sur la ligne RS485
temps mesuré : 4 µs
*************************************************************************/
#INT_RDA
void Receive()
{
char c;
c=getc();
if(I_UART_In_Pos==0 && c==I_NumeroEsclave) // on commence à prendre en compte seulement si c'est bien la bonne adresse
{
INStr[I_UART_In_Pos++]=c;
B_UART_OK=FALSE;
}
else if(I_UART_In_Pos>0 && I_UART_In_Pos<UART_MAX)
{
INStr[I_UART_In_Pos++]=c;
B_UART_OK=(I_UART_In_Pos>=UART_MAX);
}
L_Cycle_UART=1; // en prévision du Time_out
}
/************************************************************************************
Cette routine est appelée quand l'interruption de boucle de courant a eu lieu
Elle analyse les temps et détermine quelle led doit être allumée ou éteinte
dure 190 µs à 220µs
*************************************************************************************/
void Flanc_pulse()
{ ...
treatment
B_Compteur_Flanc=FALSE;
enable_interrupts(INT_RDA);
}
//-------------------------------------------------------------------------------Main
void main()
{
int i;
initialisation();
while(TRUE)
{
restart_wdt(); // resetter le watch dog !
if (B_Count) Comptage();
if(B_Compteur_Flanc) Flanc_Pulse(); // ca ne marche pas, perturbé par INT_RDA !
if(B_UART_OK) GetData(); // charger le query ModBus
if(B_Check) Check_Alarm(); // Vérifier s'il y a une alarme à afficher
if(SFLS.Changed)
{
treatment
SFLS.Changed=FALSE;
}
}
} |
The treatment (Flanc_pulse routine) is perturbated by the UART !!!
I try to place spies in the code to understand what is happening, but without success for the time being. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Mon Jan 14, 2013 8:13 am |
|
|
Several steps back.
First, what compiler version?. I don't see this being said at any point, and when I compiled one of the routines when you quoted a 'size', using 4.140, I got a size that was vastly smaller for the routine, whatever I did with the numbers. Hence possibility rears, that you are using a compiler that has a problem.
Then, I've got to get you to understand what #PRIORITY does. #PRIORITY, _has nothing whatsoever to do with high priority interrupts_....
#PRIORITY sets the order the interrupt handlers are 'polled', inside the individual interrupt levels. If it is not used, they are polled in the order they are declared. Your #PRIORITY statement does nothing. The problem is that this statement, 'predates' chips that had hardware priorities, so the name can be misleading.
Now, as posted, the UART RX can't interfere with Flanc_Pulse code. The only way it will interfere, is if the TIMER3 interrupt contains anything at all that accesses the UART. What it can interfere with is latency to when the Flanc_Pulse code is called. Are you transmitting with the UART?. If so, then 'enable' is your problem. This fundamentally alters the way the UART works. It was not mentioned or shown before. With this, the _transmission_ routines, _have_ to wait till the byte is sent, so they can turn off the enable line. Far better to do this yourself, either using external hardware to control the line, or enabling it before you TX, and then using a counter in one of your timers, to turn it off.
I don't think anybody suggested even in passing, moving the timer3 code into the main loop.
Best Wishes |
|
 |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Jan 14, 2013 11:33 am |
|
|
| Quote: |
By experience from 8 to 31
|
As an aside, if you are mapping simply a status for each lamp, it might be convenient to bit map each lamp to the bits of a 32 bit unsigned INT - instead of an int arrary[]
If that is the situation it makes testing for any "fails" as simple as coding
if (my32int) {take action};
to see which bits are set by the lighting modbus checker routine.
I am perfectly willing to admit that i might misunderstand the intent of array[], but thats my 2 cents worth. |
|
 |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Tue Jan 15, 2013 2:50 am |
|
|
| Quote: | | what compiler version? | My PCH version is 4.14
I am afraid my problem is due to I have taken out the Flanc_Pulse routine from the INT_RDA and put it in main() routine.
| Code: | | if(B_Compteur_Flanc) Flanc_Pulse(); |
Because, in this case, when I send Modbus infos with UART, inside GetData() routine, it takes 19ms to send 57 characters which is longer that the delay between 2 INT_EXT calls (one edge at 10ms and one at 4ms). That means Flanc_Pulse routine which is in charge to treat each INT_EXT measurement, in reality "forget" to treat one during GetData()
When I put back Flanc_Pulse in the INT_EXT, the problem disappears. The only thing is the timebase Timer3 is pertubated by then 220µs duration of Flanc_Pulse (in theory 250µs + 220µs), which can introduce an error in the time measurment. Maybe I can try to introduce a compensation in Flanc_Pulse ? (eg L_Compteur += Compensation)
I also have implemented a regulation of L_period (related to Timer3 period) regarding 14ms measurement.
| Code: | // Mesure de largeur de pulse-------------------------------------------------
BOOLEAN Proximite(int Valeur,int Reference)
{
if((Valeur<Reference-INCERTITUDE)||(Valeur>Reference+INCERTITUDE))
{
return(FALSE);
}
else
{
return(TRUE);
if(Valeur<Reference-2)
{
L_Period--;
}
else if (Valeur>Reference+2)
{
L_Period++;
}
}
} |
So I hope this can reduce the effect described.
| Quote: | | if you are mapping simply a status for each lamp | Yes it is the case
| Code: | struct SFLS_Type
{
int length;
int defaut[32]; // manages the delay of faulty lamp display
int Led_Verte[32]; // used as boolean
int Led_Rouge[32];
boolean Alarm;
boolean MA, MI, HI;
boolean Local;
Boolean Changed;
} SFLS; |
Thanks |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Tue Jan 15, 2013 8:14 am |
|
|
4.14, is not a legitimate compiler version number.
Use INT_TBE, to send your modbus data. You don't have to wait for it to send. Remember there is a character of output buffering (just like the RX, just under two charcaters). Control the enable line yourself. Set it, Load the first character into the transmit buffer enable TBE, and load the rest into a serial buffer and get out of the routine. When you load the last character, turn off TBE, and set a timer interrupt to trigger two byte times later. When this fires turn off the enable.
This way your other jobs can be going on while you send.
You need to be using interrupts _throughout_, rather than mixing trying to stay in routines, with doing other things in interrupts.
Best Wishes |
|
 |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Tue Jan 15, 2013 8:59 am |
|
|
| Ttelmah wrote: | 4.14, is not a legitimate compiler version number.
|
Glups !
My About popup shows :
IDE Version 4.119
PCB Version 4.119
PCM Version 4.119
PCH Version 4.140 |
|
 |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Tue Jan 15, 2013 9:06 am |
|
|
You mean this code is a bad method ?
| Code: | /***************************************************************************
Envoyer par la RS485
temps mesuré 10,5ms pour 10 octets à 9600 Bdx(avec l'écho)
2,55ms à 38400 Bdx
1,75 ms à 57600 Bdx
***************************************************************************/
void Echo(int *OUT,int Len)
{
int i=0;
long C;
//Len est la longueur à envoyer sans compter le CRC
C=CRC16(OUT,Len); // ajouter le CRC16
OUT[Len]= (int)C;
OUT[Len+1]=(int)(C>>8);
for(i=0; i<Len+2; i++) putc(OUT[i]);
} |
I mean I do not understand how you load the transmit buffer and the serial buffer ! |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Tue Jan 15, 2013 9:46 am |
|
|
The extra zero makes all the difference in the number.
EX_STISR.C
Except for adding the control of the enable line.
You were saying you were sending stuff, and taking longer than the time between two RX characters, sitting in the routine with INT_RDA disabled.
Best Wishes |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jan 15, 2013 3:51 pm |
|
|
| Jean FOUGERON wrote: | You mean this code is a bad method ?
| Code: | /***************************************************************************
Envoyer par la RS485
temps mesuré 10,5ms pour 10 octets à 9600 Bdx(avec l'écho)
2,55ms à 38400 Bdx
1,75 ms à 57600 Bdx
***************************************************************************/
void Echo(int *OUT,int Len)
{
int i=0;
long C;
//Len est la longueur à envoyer sans compter le CRC
C=CRC16(OUT,Len); // ajouter le CRC16
OUT[Len]= (int)C;
OUT[Len+1]=(int)(C>>8);
for(i=0; i<Len+2; i++) putc(OUT[i]);
} |
I mean I do not understand how you load the transmit buffer and the serial buffer ! | As
Ttelmah pointed out, have a look at the EX_STISR.c program in your PICC examples directory. Just copy/paste and no more worries.
Then another thing I don't like about your function is that your Echo function is writing into the buffer that it gets passed as a parameter. Dangerous! How should the calling function now that you are going to add two more bytes to the buffer? This is unusual and I don't see it documented in your function header.
Here is another way to write your function that does not write into the buffer it got as parameter and also results into shorter code. Note how I've already replaced the call to putc() by bputc() from EX_STISR.c. | Code: | void Echo(int *OUT,int Len)
{
int8 i;
int16 C;
for(i=0; i<Len; i++)
{
bputc( OUT[i] );
}
//Len est la longueur à envoyer sans compter le CRC
C = CRC16(OUT, Len); // ajouter le CRC16
bputc( (int8)C );
bputc( (int8)(C>>8) );
} |
|
|
 |
Jean FOUGERON
Joined: 30 Nov 2012 Posts: 110 Location: France
|
|
Posted: Wed Jan 16, 2013 5:05 am |
|
|
| ckielstra wrote: |
Just copy/paste and no more worries.
| Code: | void Echo(int *OUT,int Len)
{
int8 i;
int16 C;
for(i=0; i<Len; i++)
{
bputc( OUT[i] );
}
//Len est la longueur à envoyer sans compter le CRC
C = CRC16(OUT, Len); // ajouter le CRC16
bputc( (int8)C );
bputc( (int8)(C>>8) );
} |
|
Thanks
I did it
One question anyway,
how works this line ?
| Code: | | while(ni==t_next_out); |
Does'nt it go to an infinite loop ? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Wed Jan 16, 2013 5:25 am |
|
|
It'll only happen if your buffer is full.
At this point it'll sit (interrupt is enabled here), and wait for one character to send. Then the condition will no longer be true, and it can continue.
Provided your buffer is large enough, it'll never happen.
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
|