View Full Version : Please help me understand this code
lilimike
- 28th April 2010, 21:00
Hi,
From the code bellow, I am expecting to have the DEBUG line running only when the variable "ReadFlag" is set and it does, so far so good.
But once it ran, I clear ReadFlag and it should become set again ONLY if I=10 and this should happen only after the Interrupt has ran 10 times right? Well it is not happening.
In my DEBUG line "I" shows up as 0,2,4,5 randomly
I am not using variable "I" anywhere else other than:
I VAR BYTE
I = 0at the top of my program.
Can someone help me understand this?
Goto main
Main:
DO while readflag = 1
debug dec Fvalue," i=",dec i," R=",dec readflag,10,13 ; Display on terminal if ReadFlag = 1
readflag = 0 ; do not update until the next 10 readings
Fvalue = 0
LOOP
goto main
'---[INT - interrupt handler]------Signal found at RB0, take a reading
SignalFound:
T1CON.0 = 0 ; Stop timer1
i = i + 1
FValue = TMR1H * 256 + TMR1L + fvalue ; Add 10 readings to FValue
if i = 10 then
fvalue = fvalue / 10 ; get the average of one reading
i = 0 ; Reset counter
ReadFlag = 1 ; FValue is ready to read
endif
TMR1H = 21 ; reset timer1 to 60mS
TMR1L = 167
T1CON.0 = 1 ; Start timer1
@ INT_RETURN
Thanks
Mike
lilimike
- 28th April 2010, 22:18
Getting even more confusing, I tried:
DO while I = 0
And still the DEBUG line shows up even when: I > 0
:confused:
Bruce
- 29th April 2010, 02:19
In my DEBUG line "I" shows up as 0,2,4,5 randomly
You probably have several timer interrupts while it's inside your DO LOOP, so I ends up with unexpected values.
Example: With i = i + 1 being first in your timer interupt, it's changing the value of i on every interrupt, and sometimes even during the process of PBP trying to output everything with DEBUG.
When DEBUG actually starts, your interrupt is still changing I before PBP outputs it.
Jerson
- 29th April 2010, 02:24
Try this. Changes are in Bold
'---[INT - interrupt handler]------Signal found at RB0, take a reading
SignalFound:
T1CON.0 = 0 ; Stop timer1
i = i + 1
FValue = TMR1H * 256 + TMR1L + fvalue ; Add 10 readings to FValue
if i >= 10 then ' COMPARISON CHANGED TO >=
fvalue = fvalue / i ; get the average of one reading CHANGED HERE
i = 0 ; Reset counter
ReadFlag = 1 ; FValue is ready to read
endif
TMR1H = 21 ; reset timer1 to 60mS
TMR1L = 167
T1CON.0 = 1 ; Start timer1
@ INT_RETURN
lilimike
- 29th April 2010, 21:06
Hi Bruce and Jersen,
That is really fast !
Jerson,
I was able to observe the proper values by moving the DEBUG line inside the INT handler.
Is there a more efficient way to observe variables values within a program?
It seams like LCDOUT and DEBUG are taking too long to operate and are giving false values.
Thank you
Mike
lilimike
- 2nd May 2010, 03:18
Bruce,
I came across this link you wrote Entitled "Using MicroCode Studio ICD
With The PIC16F628 Microcontroller" http://www.rentron.com/PicBasic/MCS_X3.htm
Would this enable me to view each step of a program as well as where the PIC timer is at while executing a specific line of code?
Mike
Bruce
- 2nd May 2010, 03:52
Hi Mike,
Yep. MCS+ ICD is very handy if you have the paid version of MCS+. But if you're trying to view real-time timer values, I would go with MPLAB/MPSIM.
By the time you LCDOUT, SEROUT, whatever, the timer is long past the value you're displaying.
What precisely is it you need to do?
lilimike
- 2nd May 2010, 04:32
I didn't think I could see real time values with MPSIM using a program written with PBP!
I will give it a try, I guess I have to invest a little time learning the Stimulus properly, I have done very little assembly and was able to test buttons triggering LEDs but when I started going deeper into this I kind of got distracted with PBP.
I am building an ultrasonic distance measurement device, I have seen some circuits here and there but I really want to fully understand what I am doing and build my own. I don't have any code to show just yet because I have started to isolate the different parts of my code and it doesn't look like any program right now.
What I am trying to achieve is using Daryl's Interrupts.
Using TMR1_INT I send HPWM and using INT_INT I detect the echo from the Rx on RB0.
I keep trying to tell myself that the PIC is running fast but I am always amazed with how much I underestimate the speed at witch it operates.
I think I am not too far from something, just wish I could slow down everything and observe what is going on during each operation of code (other than the operation itself)
I guess MPSIM is probably the way to go?
Mike
lilimike
- 2nd May 2010, 12:23
I got PBP running with MPSIM and now I finely see what is happening and when, thanks for this info.
Mike
lilimike
- 3rd May 2010, 04:30
I don't know what's going on here!
Using MPSIM with watch window
I have TIMER1 running, I can observe that TMR1H=218 and TMR1L=8
testa is a word variable.
I execute this operation:
testa = TMR1H
and I see that testa = 218
Then I do this:
testa = TMR1H * 256 + TMR1L
However now testa = 8 instead of 55816
What am I missing?
PS: Sorry for mistyping Darrel's name in post #8, I know a Daryl and it just came out without thinking!
Mike
lilimike
- 3rd May 2010, 14:00
When I send the value of testa on a DEBUG line I get the proper value so it must be a setting in MPSIM somewhere.
I can't figure out why there would be such a setting though!
Mike
Bruce
- 3rd May 2010, 14:26
Which PIC are you using?
Some with 16-bit read/write modes for timers require you to read low byte first, then high byte.
lilimike
- 3rd May 2010, 14:36
I am using 16F628A
In the Watch properties of testa I changed the size to 16 bits and that solved it.
Now how can I get the value of each array data in the watch window?
let say I have a variable called ArrayVariable(x)
I would like to read ArrayVariable(1) and ArrayVariable(2)
The only choice I get from the list is _ArrayVariable
Thanks
Mike
Bruce
- 3rd May 2010, 18:42
Click View, File Registers, and scroll down until you see _ArrayVariable. This will be
_ArrayVariable[0]. After that will be the rest of the array.
lilimike
- 3rd May 2010, 18:50
I am all set, thanks so much!
Mike
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.