I tried it @ 20mhz with code below, I have no provision to increment the timer right now but it does change the outputs on RB0 & RB1 when you switch RB7
Code:
@MyConfig = _HS_OSC & _WDT_OFF & _PWRTE_ON
@ __config MyConfig
DEFINE OSC 20
Symbol OUT_PIN = PORTB.0 'Output to solenoid RB0
Symbol LED = PORTB.1 'Control button to RB1
Symbol CNTL_BUTTON = PORTB.7 'Control button to RB7
spacing var byte
temp var byte
TRISA=$FF
TRISB = $FC ' RB0 and RB1 outputs, others input
portB = 0
'* * * * * * * Set initial value to variables * * * * * * * * * * *
'
spacing = 15
LED = 0
temp=1
' Program starts with gosub to Wait_Button
main:
gosub wait_button
LED = 1 ' on return turn on LED
pause 2000 ' Wait for two seconds
if CNTL_BUTTON = 1 then spacing = 30
goto direct 'If button is released before 2 secs
'code goes directly into the default spacing
temp=0
gosub wait_button 'If button is pressed more than 2 secs,
'it gets caught here and waits for release
LED = 0
pause 1000 ' Wait for 100 ms
repeata:
spacing = spacing +5 'Increase the spacing count by 2 every time
toggle LED: pause 1000: toggle LED
if spacing = 45 then spacing = 20 'Till it reaches maximum and reset to minimum
temp=1
gosub wait_button 'Access the push button state and repeat if short press
pause 2000 ' Delay for 2 seconds
if CNTL_BUTTON = 1 then goto repeata
LED = 1
OUT_PIN = 0
'Calculate the value to be loaded into TMR0 for counting at RA4 connected to the encoder output
'Use a multiplier to convert the spacing in cm to number of pulses to count
'Say if the distance measuring wheel's dia is 40 cm then its circumference = 3.14*40 appr = 125 cm
'For one revolution ie 125 cm distance traveled = 500 pulse outputs of encoder
'Correspondingly 500/125 = 4 pulses/cm distance, therefore set the count as 255 - (spacing * 4)
direct:
spacing = 255 - (spacing * 4)
OPTION_REG = %00110000 ' TMR0, counter set option_reg<5> clear for timer
' Option_Reg<4> set for falling edge clear for
' rising edge detection.
ON INTERRUPT GOTO ISR ' Interrupt service routine
TMR0 = spacing
OUT_PIN = 0
INTCON = %10100000 ' Enable TMR0 overflow interrupt
'This is the interrupt service routine, ISR. The program jumps here whenever TMR0 overflow occurs
DISABLE 'Disable interrupts
ISR: 'Entry point of the ISR
OUT_PIN = 1
TMR0 = spacing
INTCON.2 = 0 ' Reset timer interrupt flag
RESUME ' Resume main program
ENABLE ' Enable interrupts
wait_button:
if CNTL_BUTTON && 1 =temp then wait_button
return
end
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Bookmarks