PDA

View Full Version : hardware counting while software runs



Archangel
- 1st October 2006, 01:46
Hi All,
Can someone tell me how to set up the PIC to count a signal or pulse in the background while a program is running? I am attempting to do the following:
run a counter which counts down from some number - say 50, and at the same time,count an encoder pulse which will subtract from the count the software counter is counting, it is a time vs mechanical count. I have the counter set up to work properly but have no idea how to interface the hardware (encoder ?) into the counter loop, in fact I do not know how to interface the (Timers ? ) so as to allow PIC to work independantly of the Main Loop. RTFM has not been too useful so far. To complicate matters I have fallen in between ISPs so I have limited access to internet, at Mom's House, so untill I find another ISP, I have to drive over here ;) Since good ol' % $ $ (name omitted) offered me Hi speed DSL for 17.95 which they couldn't deliver and lo speed for 35.99 and wouldn't discount.GRRRRR . . . . anyway anyone got Ideas how to do this?
Thank You in advance,
Joe

sayzer
- 1st October 2006, 03:35
Here I have a sample code for rotary encoder using two Timer modules in counter mode.

Reference Signal of encoder is connected to Timer0, and one of pulse signals is connected to Timer1.





'Edit: the code is for 16F877-20Mhz.

@ DEVICE PIC16F877, HS_OSC
@ DEVICE PIC16F877, WDT_ON
@ DEVICE PIC16F877, PROTECT_OFF
@ DEVICE PIC16F877, CPD_OFF
@ DEVICE PIC16F877, PWRT_ON
@ DEVICE PIC16F877, BOD_ON


DEFINE OSC 20

'=======Add tris registers here... =========
'I skipped the part where you need to set your registers.....
'..
'...
'....
'========Timer0 Settings - 8bit===================
OPTION_REG = %00111000
' Set 1:1 prescaler & assign to WDT
' Set TMR0 clock to external input on RA4/T0CKI.
' Connect Rerefence signal of encoder to this pin.
' Increment on high-to-low transition on RA4/T0CKI pin

'=====Timer1 settings - 16Bit ====================
T1CON = %00000111 ' 1:1 in counter mode.
'RC0 = Encoder input (RC0/T1OSO/T1CKI (on rising edge).
'Connect One of the pulse signals of encoder to this pin.)
INTCON.7 = 0 ' Disable interrupts

Encoder var word
RefPoint var word
RefBit var word
TourNo var word

Encoder = 0
TMR1L = 0 ' Clear TMR counter
TMR1H = 0
Refpoint = 0
Refbit = 0
TMR0 = 0
Tourno = 0


Start:
LCDOUT $fe,1,"Encoder:",DEC4 Encoder
LCDOUT $fe,$c0,"Tour :",DEC4 Tourno

Encoder.highbyte = TMR1H 'Load Lower byte of Timer1.
Encoder.lowbyte = TMR1L 'Load Higher byte of Timer1.

Refpoint = TMR0 'Load TMRO value.

if Refbit + 1 = Refpoint then ' Check: Do we have an increment?If yes, then
Tourno = Tourno + 1 ' Add up tour numbers.
TMR1H = 0 ' Since the encoder completed one tour, clear timer1 Lower byte.
TMR1L = 0 ' Since the encoder completed one tour, clear timer1 Higher byte.
Encoder = 0 ' Since the encoder completed one tour, clear timer1 holder, too.
endif

RefBit = Refpoint ' Make eqaul before the next TMR0 assignment



Goto start

mister_e
- 1st October 2006, 10:35
And depending wich PIC you're using you may have 1,2,3,... Timer/Counter who can work with external Clock. Be sure the PIC you select provide you at least 2 of these with external Clock Source.

Or there's still this interrupt on something stuff.

@Sayzer

@ DEVICE HS_OSC,WDT_ON,PROTECT_OFF,CPD_ON,PWRT_ON,BOD_ON

You know what i want to say now :D

sayzer
- 1st October 2006, 13:57
Yeap, I forgot to say that the code above is for 16F877-20Mhz.

Also,



@Sayzer

@ DEVICE HS_OSC,WDT_ON,PROTECT_OFF,CPD_ON,PWRT_ON,BOD_ON


I know what you mean Steve! I will never forget.

Archangel
- 2nd October 2006, 01:31
Thanks,
I will take this code home and chew on it a while . . . . I am not manipulating data in an lcd, rather I am using it to control hardware.something like so:

x var byte

main:
if porta.0 !=0 then
pause50
DoSomthing
else
endif
goto main


DoSomething:
for x = 50 to 0 step - 1
goto encoder
portB.5 low
next x
return

encoder:
Portb.5 high ' to power up encoder
some code here to count encoder subtracting code from var X
return

sayzer
- 2nd October 2006, 03:26
.....


main:
if porta.0 !=0 then
pause50
DoSomthing 'is a goto not a gosub
else
endif
goto main


DoSomething:
for x = 50 to 0 step - 1
goto encoder 'another one here
portB.5 low
next x
return

encoder:
Portb.5 high ' to power up encoder
some code here to count encoder subtracting code from var X
return


I guess this was a quick example.
Check "goto" !