Possible Bug with Calculation on incrementing variable by 1
Posted: Mon Dec 07, 2015 12:29 am
Hello
I'm using Flowcode 6 with PIC pack.
The chip I'm using is PIC12F675
Internal Oscillator 4MHz, no clock out
Power Up Timer enabled
MCLR internal (IO pin active)
NO Brown Out Detect
NO Watchdog timer
NO Code Protection
For the purpose I'm trying to accomplish I possibly detected
a bug in the Calculation Function, when I try to increment a
variable by 1 ( variable = variable + 1 ) .
The descriptions the program and problems are bellow.
Flowcode flowchart, asm file and function timing diagram (this last one for how it should behave,
not for how it is actually behaving) are attached for better understanding.
NOTE: I did not used the Flowcode help/report a bug because there I can not attach the
files I needed to attach here.
Program Description (as it should behave)
Simplified description:
When a push button is clicked, a time window will start.
All clicks on the push button, within this time window, will be counted
(but only the 1st click starts the time window and the subsequent ones will
not restart it).
The time window ends by itself and at its end the program will see how many clicks occurred.
Only two clicks on the push button, during the started time window, will produce an output action
(when the time window ends) to an output pin: It will be complemented
(if it was 0 it will be set to 1 and if it was 1 it will be set to 0).
Any other number of push button clicks, during the same time window, will not produce any output action.
Every time the time window ends, besides producing an output action or not, all is (re)started from the
beginning and the program will be again waiting for new push button clicks.
More detailed description:
All variables are initialized as 0
Pin A5 (GP4) is initialized as 0
Pin A5 (GP5) is initialized as 1
Every time the push button (active low) connected to GP2/INT is clicked,
an interrupt (at the falling edge) is generated.
The program jumps to the INT0_Service macro where CameraControlPulseCount
variable is incremented by 1 to count the push button clicks and, at the 1st one, timer1
( internal clock source and a 1:8 prescaler, 1.907Hz ) is started (to create a time window
of 1.048 seconds as we will see).
On each timer1 interrupt TMR1_INT_COUNTER variable is incremented and, when it reaches
value 2 (1.048 seconds passed) timer1 interrupt will be disabled, gate pulse stopped, and its
registers loaded with 0 (to prepare for the next cycle).
After this the program will see if during the time window there was 2 clicks on the push button.
* If CameraControlPulseCount=2 BuzzerFlag will be set to 1
* If CameraControlPulseCount is different from 2 BuzzerFlag will be set to 0
After this all variables associated with timer1 counting will be set to 0
On main program, GP5 output is controlled
* if BuzzerFlag is 1, GP5 will be set to 0 (to sound an active low buzzer)
* if BuzzerFlag is 0, GP5 will be set to 1 (to stop sound an active low buzzer)
Also, as a mere time window visual reference, during timer1 counting
GP4 will be set to 1 to activate a LED (through TimerIsCounting variable).
The problem:
Simulated through Flowcode 6 simulator:
It indicates that apparently all is working well and CameraControlPulseCount variable starts
at 0 and is incremented correctly by 1 on each push button click .
Simulated through PIC Simulator IDE from oshonsoft.com :
When the 1st click occurs in the push button (the moment timer1 starts to count),
CameraControlPulseCount variable (PIC user register address 42 - found through .asm),
which was been initiated as a 0 from the beginning of the main program, is incremented
immediately by 2, instead of being incremented to 1 (jumping from value 0 to value 2).
After this wrong increment, the subsequent ones are correctly incremented by 1 .
Real PIC behavior:
If push button is clicked only once within the time window, at its end GP5 is cleared,
sounding the active low buzzer.
This is coherent with PIC Simulator IDE from oshonsoft.com as, for this to occur this way,
CameraControlPulseCount variable needed to be incremented from 0 to 2 with this only one push button click.
It would need 2 push button cklicks within time windows for GP5 to be cleared and sound the active low buzzer,
and not only one.
The most strange part is if I initialize CameraControlPulseCount with a value of 1 or more
from the beginning (when variables are initialized in main program), it is correctly incremented by
1 on each and every push button click.
.
I'm using Flowcode 6 with PIC pack.
The chip I'm using is PIC12F675
Internal Oscillator 4MHz, no clock out
Power Up Timer enabled
MCLR internal (IO pin active)
NO Brown Out Detect
NO Watchdog timer
NO Code Protection
For the purpose I'm trying to accomplish I possibly detected
a bug in the Calculation Function, when I try to increment a
variable by 1 ( variable = variable + 1 ) .
The descriptions the program and problems are bellow.
Flowcode flowchart, asm file and function timing diagram (this last one for how it should behave,
not for how it is actually behaving) are attached for better understanding.
NOTE: I did not used the Flowcode help/report a bug because there I can not attach the
files I needed to attach here.
Program Description (as it should behave)
Simplified description:
When a push button is clicked, a time window will start.
All clicks on the push button, within this time window, will be counted
(but only the 1st click starts the time window and the subsequent ones will
not restart it).
The time window ends by itself and at its end the program will see how many clicks occurred.
Only two clicks on the push button, during the started time window, will produce an output action
(when the time window ends) to an output pin: It will be complemented
(if it was 0 it will be set to 1 and if it was 1 it will be set to 0).
Any other number of push button clicks, during the same time window, will not produce any output action.
Every time the time window ends, besides producing an output action or not, all is (re)started from the
beginning and the program will be again waiting for new push button clicks.
More detailed description:
All variables are initialized as 0
Pin A5 (GP4) is initialized as 0
Pin A5 (GP5) is initialized as 1
Every time the push button (active low) connected to GP2/INT is clicked,
an interrupt (at the falling edge) is generated.
The program jumps to the INT0_Service macro where CameraControlPulseCount
variable is incremented by 1 to count the push button clicks and, at the 1st one, timer1
( internal clock source and a 1:8 prescaler, 1.907Hz ) is started (to create a time window
of 1.048 seconds as we will see).
On each timer1 interrupt TMR1_INT_COUNTER variable is incremented and, when it reaches
value 2 (1.048 seconds passed) timer1 interrupt will be disabled, gate pulse stopped, and its
registers loaded with 0 (to prepare for the next cycle).
After this the program will see if during the time window there was 2 clicks on the push button.
* If CameraControlPulseCount=2 BuzzerFlag will be set to 1
* If CameraControlPulseCount is different from 2 BuzzerFlag will be set to 0
After this all variables associated with timer1 counting will be set to 0
On main program, GP5 output is controlled
* if BuzzerFlag is 1, GP5 will be set to 0 (to sound an active low buzzer)
* if BuzzerFlag is 0, GP5 will be set to 1 (to stop sound an active low buzzer)
Also, as a mere time window visual reference, during timer1 counting
GP4 will be set to 1 to activate a LED (through TimerIsCounting variable).
The problem:
Simulated through Flowcode 6 simulator:
It indicates that apparently all is working well and CameraControlPulseCount variable starts
at 0 and is incremented correctly by 1 on each push button click .
Simulated through PIC Simulator IDE from oshonsoft.com :
When the 1st click occurs in the push button (the moment timer1 starts to count),
CameraControlPulseCount variable (PIC user register address 42 - found through .asm),
which was been initiated as a 0 from the beginning of the main program, is incremented
immediately by 2, instead of being incremented to 1 (jumping from value 0 to value 2).
After this wrong increment, the subsequent ones are correctly incremented by 1 .
Real PIC behavior:
If push button is clicked only once within the time window, at its end GP5 is cleared,
sounding the active low buzzer.
This is coherent with PIC Simulator IDE from oshonsoft.com as, for this to occur this way,
CameraControlPulseCount variable needed to be incremented from 0 to 2 with this only one push button click.
It would need 2 push button cklicks within time windows for GP5 to be cleared and sound the active low buzzer,
and not only one.
The most strange part is if I initialize CameraControlPulseCount with a value of 1 or more
from the beginning (when variables are initialized in main program), it is correctly incremented by
1 on each and every push button click.
.