Ioannis,
Not sure I understand.... it will toggle LED1 once every second which means it will blink at 0.5Hz - isn't that what I wrote?
Barry,
Excellent! I wouldn't check IntCount in the main routine like that. Instead I'd check IntCount in the ISR and set a flag (UpdateDisplay or whatever). The main routine then checks this flag, executes the code and resets the flag. The reason for this is that when the code grows the Main routine may "miss" the when IntCount=0 (depending on what else it has to do and how long it takes). If you instead use the flag/semaphore method the Main routine WILL see that it's time to update display - when it gets around to it.
OK, now that you have a stable time base and the pulses counted in hardware it's time to add in the PID again. The ISR would look something like:
Code:
ISR:
T1CON.0 = 0 ' Stop timer
TMR1_Temp.HighByte = TMR1H ' Get current "time"
TMR1_Temp.LOWBYTE = TMR1L
TMR1_Temp = TMR1_Temp + TMR1_Reload ' Add reload value to get correct interval
TMR1H = TMR1_Temp.HIGHBYTE ' Move result back to timer
TMR1L = TMR1_Temp.LOWBYTE
T1CON.0 = 1 ' Start timer
' Get the current motor velocity
newCount = TMR0 - oldCount
oldCount = TMR0
' Calculate error and run PID
pid_Error = SetPoint - newCount
GOSUB PID
IntCount = IntCount + 1 ' Increment interrupt counter
If IntCount = 9 THEN
UpdateDisplay = 1
IntCount = 0
ENDIF
@ INT_RETURN
Your setpoint in this case is pulses per interrupt period, not actual frequency in Hz.
And the Main routine perhaps something like:
Code:
UpdateDisplay VAR BIT
Main:
If UpdateDisplay THEN
'LCDOUT.....
'LCDOUT.....
UpdateDisplay = 0
Goto Main
This is untested but I'm sure you'll get the idea.
/Henrik.
Bookmarks