PDA

View Full Version : RPM With Timer1



mel4853
- 27th June 2009, 00:38
I'm using a 16f687 and trying to get the timer1 working. I'm using a code snipet from mister e. It works great with a 16f688 but can't get it to work with the 16f687.

@wTimer1 = TMR1L
wTimer1 var word EXT ' timer1 setup
Speed:
wTimer1=0 ' clear Timer1
T1CON = %00000111 ' start timer1
pause 1000 ' acquisition time
T1CON = %00000110 ' stop timer1
pause 100
return

The rest of the code works fine just this part doesn't. Also if I change wtimer1 to 4 it displays the 4 so I know it's getting here??????

Bruce
- 27th June 2009, 01:02
Do you have an external clock input on T1CKI? You have Timer1 configured
for external clocks.

mel4853
- 27th June 2009, 01:11
Yes on RA5, sorry forgot to put that.

Bruce
- 27th June 2009, 01:23
I can't see anything in the small code fragment you posted that
would keep Timer1 from counting.

Assuming you have pulses on RA5 during your pause time - it should
work the same as on your 16F688. I suspect there's something either
different in the rest of your code, a bad connection, or a fried pin.

mel4853
- 27th June 2009, 02:42
I have pulses on RA5 and here's the code.

Include "modedefs.bas"

'DEFINES-----------------------------------------------------------------------
define OSC 4
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

'SETUP REGISTERS---------------------------------------------------------------
TRISA=%110000 'RA4,RA5 as Input
ANSEL=%00001000 'AN3 Analog In
ANSELH=$00 'All pins digital out
TRISB=$00 'All pins output
TRISC=$00 'All pins output
CM1CON0.7=0 'Shut off comparators
CM2CON0.7=0 'Shut off comparators
ADCON0.7=1 'Right justify result

'SETUP VARIBLES----------------------------------------------------------------
DPIN var PORTC.1 ' Shift data pin
CPIN var PORTC.2 ' Shift clock pin
LPIN var PORTB.4 ' Latch pin
adval var word ' Create adval to store result
volt var byte ' Battery level
cvolt var byte ' Charge voltage
lvolt var byte ' Load voltage
LBatt var byte ' Low battery alarm
@wTimer1 = TMR1L
wTimer1 var word EXT ' timer1 setup
A595 var byte ' Alarm out byte
C595 var byte ' Charge out byte
L595 var byte ' Load out byte

; Initialize your Hardware first, set CONFIGs, OSC, Turn off A/D etc
;----[ Change these to match your LCD ]---------------------------------------
LCD_DB4 VAR PORTC.3
LCD_DB5 VAR PORTC.6
LCD_DB6 VAR PORTC.7
LCD_DB7 VAR PORTB.7
LCD_RS VAR PORTC.5
LCD_E VAR PORTC.4
LCD_Lines CON 2 ' # of Lines on LCD, 1 or 2 (Note: use 2 for 4 lines)
LCD_DATAUS CON 50 ' Data delay time in us
LCD_COMMANDUS CON 2000 ' Command delay time in us

INCLUDE "LCD_AnyPin.pbp" ; *** Include MUST be AFTER LCD Pin assignments ****
PAUSE 1000 : LCDOUT $FE,1 : PAUSE 250 ; Initialize LCD (You may not need this,
; but some displays are picky)
;------------------------------------------------------------------------------

Voltsetup:
lvolt=48 ' Set load dump to 14.8 volts
cvolt=22 ' Set charge to 12.2 volts
LBatt=16 ' Set low battery alarm to 11.6 volts

Start:
lcdout $FE,1,"RMLDESIGN VER1.0"
lcdout $FE,$C0," WINDCONTROLLER"
pause 4000
lcdout $FE,1
gosub getad
adval=adval+100
LCDOUT $FE,$80,"V=",dec (adval/10), ".", dec adval dig 0
lcdout $FE,$88,"RPM=",dec4 wTimer1*9
if volt>cvolt then gosub Load 'On Start Up Put On Load If Volts High

Main:
gosub Speed
gosub Getad
adval=adval+100
LCDOUT $FE,$80,"V=",dec (adval/10), ".", dec adval dig 0
lcdout $FE,$88,"RPM=",dec4 wTimer1*9
if volt<LBatt then Alarm 'Show Low Battery
if volt>lvolt then Load 'Put charge to load
If volt<cvolt then Charge 'Charge battery
goto main

Speed:
wTimer1=0 ' clear Timer1
T1CON = %00000111 ' start timer1
pause 1000 ' acquisition time
T1CON = %00000110 ' stop timer1
pause 100
return

Getad:
adcin 3,adval 'Get A/D value
if adval> 355 Then adval=355 'Range between 100 & 355 then minus
if adval< 100 then adval=100 '100 to get 0=10 & 255=35.5
adval=adval-100
volt=adval
return

Alarm:
a595=%00001001
Shiftout DPIN, CPIN, MSBFIRST,[A595] ' send byte to 74hc595
pulsout lpIN,10
high portb.6
lcdout $FE,$C0,"*LOWBATT CHARGE*"
goto main

Load:
l595=%00100100
Shiftout DPIN, CPIN, MSBFIRST,[L595]
pulsout lpin,10
high portb.5
lcdout $FE,$C0," *ON LOAD* "
goto main

Charge:
c595=%00010010
Shiftout DPIN, CPIN, MSBFIRST,[C595]
pulsout lpin,10
high portb.6
lcdout $FE,$C0," *CHARGING* "
goto main

Bruce
- 27th June 2009, 06:40
It might work once you fix a bunch of errors. Look through your code slowly, line by line.

if volt>cvolt then gosub Load. This calls Load, but Load doesn't RETURN. It lands on a
GOTO Main.

if volt If volt T1CON = %00000111 ' start timer1 ? I'm not sure what this does, but it looks
pretty odd...;o}

volt=adval ' volt is a byte. adval is a word?

Your Main routine has a RETURN instead of a GOTO Main. This, along with the GOSUB Load
with no RETURN is going to screw up the stack.

Go through it all slowly. There may be more potential show-stoppers....;o}

mel4853
- 27th June 2009, 14:39
The only part that doesn't work is the timer1. I have it displying everything except rpm. Timer1 is not counting. As for the if volt statements, all of those lines didn't copy in. They're all there in my code. It's just the Speed part always read 0.

Bruce
- 27th June 2009, 20:31
Why ask for help - if you're not going to listen or learn from it?

mel4853
- 27th June 2009, 20:56
What do you mean by that????

Bruce
- 27th June 2009, 22:52
Did you fix the problem areas I pointed out above?

Can you post your code, that's still not working, showing the fixes?

mel4853
- 27th June 2009, 23:23
Can't post code. It shows everything correct on reply screen then I hit preview post and it jumbles Speed into Main routine?? Tried three times and now I see the if volt if volt deal. Thats because of the jumbling it did.WOW!! Speed: should read as post 1 does.
Yes, I did go thru and change what you had said. As for the T1CON I didn't change because that is where the problem is. It just displays 0 all the time??This works on the 16f688 but not the 16f687?? On the datasheet T1CON bit 7 I don't understand?? I can't test anymore tonight had to pick up garage for company tonight, but can still read posts.

Bruce
- 27th June 2009, 23:43
Use the code tags when posting code to help keep formatting.

See this page for an example http://www.picbasic.co.uk/forum/misc.php?do=bbcode#code

Just replace value with the code you paste into the text area.

mel4853
- 28th June 2009, 16:53
It's still jumbling the main with speed when I hit preview. It all works except the speed routine.

Speed:
wTimer1=0 ' clear Timer1
T1CON.0=1 ' start timer1
pause 1000 ' acquisition time
T1CON.0=0 ' stop timer1
pause 100
return

I call this from a gosub in the main routine. I know it gets here because if I put a number in wtimer1= it will display that number.

Bruce
- 28th June 2009, 16:56
Try placing an LCDOUT just below T1CON.0=0 in your Speed routine to see if it prints
the value in Timer1.

Does it work now?

mel4853
- 29th June 2009, 21:50
Bruce,
Sorry to waste your time. My fault on that one. Forgot one resistor. Everything works just fine now. Thank you for the help!!!!!!!

Mike