Hi, Ken
IF you want your Thing to work properly ...
You HAVE to use QUICK Interrupts and not " On Interrupt" ... especially if using a LCD.
So, Have a look to asm interrupts or Darrel Instant Interrupts ...
Alain
Hi, Ken
IF you want your Thing to work properly ...
You HAVE to use QUICK Interrupts and not " On Interrupt" ... especially if using a LCD.
So, Have a look to asm interrupts or Darrel Instant Interrupts ...
Alain
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
Why not just let hardware handle most all of this for you?
This will be a LOT faster than interrupting on every single pulse, and countingCode:' include your hardware/config stuff here Revs VAR BYTE ' holds up to 255 revolutions ANSEL = 0 ' disable A/D so RA4 is digital CMCON = 7 ' disable comparators OPTION_REG = %00110001 ' RA4 = count input ' inc count every 4th falling edge with 1:4 prescale ' assigned to TMR0 Mainloop: TMR0 = 0 ' clear count before pause PAUSE 20 ' Adjust this to set how long it counts revolutions here Revs = TMR0 ' TMR0 counts 1 rev for every 4 pulses LCDOUT $FE,1,"Revs: ",DEC Revs ' do other stuff here, then return to get more counts GOTO Mainloop
in software. You should be able to give the wheel a pretty good spin without
missing anything.
HI, I am not able to make this code work, always leaves me with ORevs = 1 on the LCD.
I also change the oscillation to 20 Mhz but did not see any difference.
ken
' Internal Oscillation program
' LCD
' Main loop display the counts and the interrupt routine counts
'/////////////////////////
'// Define section //
'/////////////////////////
INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language
@ DEVICE pic16F88, INTRC_OSC, MCLR_ON
OSCCON=$60 ' use internal 4MHZ osc
ANSEL = 0 : ADCON1 = 7
CCP1CON = 0 'CCP1 module off
'/////////////////////////
'// LCD configuration //
'/////////////////////////
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 0 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000
pause 1000
'/////////////////////////
'// PIN configuration //
'/////////////////////////
TRISA = %11111111 ' Set PORTA.4 (TOCKI) to input for timer0
TRISB = %00001111 ' Set PORTB
PortA = 0
'///////////////////////////////////////////////
'// Variable Declaration and initialization //
'///////////////////////////////////////////////
Revs var word
ORevs var word
ANSEL = 0 ' disable A/D so RA4 is digital
CMCON = 7 ' disable comparators
OPTION_REG = %00110001 ' RA4 = count input
' inc count every 4th falling edge with 1:4 prescale
' assigned to TMR0
Mainloop:
TMR0 = 0 ' clear count before pause
PAUSE 20 ' Adjust this to set how long it counts revolutions here
Revs = TMR0 ' TMR0 counts 1 rev for every 4 pulses
ORevs = Revs +1
LCDOUT $FE,1,"ORevs: ",DEC ORevs
GOTO Mainloop
end
As Joe already mentioned earlier, RA4 T0CKI is a Schmitt Trigger input. If your sensor doesn't output a minimum voltage of 0.8 * VDD for logic 1 and 0.2 * VDD or less for logic 0, then the Schmitt Trigger input isn't seeing the correct logic levels, and TMR0 isn't counting your pulses.
Here's a quick little test program to show how it works when RA4/T0CKI is connected to an I/O-pin to increment the counter.
It works great, but only as long as the input signal meets the threshold levels the Schmitt Trigger input buffer on RA4/T0CKI expects.Code:@ DEVICE pic16F88, INTRC_OSC, CCPMX_ON, MCLR_OFF DEFINE HSER_BAUD 2400 Loops VAR BYTE Revs var BYTE OSCCON=$60 ' use internal 4MHZ osc ANSEL = 0 ' disable A/D so RA4 is digital CMCON = 7 ' disable comparators TRISA.4=1 ' RA4/T0CKI = input (Timer0 clock input) PORTB.5=1 ' USART TX pin idles high PORTB.0=1 ' clock output pin to RA4 starts high TRISB.0=0 ' clock out pin to RA4/T0CKI = output TRISB.5=0 ' USART TX = output ANSEL = 0 ' disable A/D so RA4 is digital CMCON = 7 ' disable comparators OPTION_REG = %10110001 ' clk on RA4/T0CKI, falling edge, 1:4 prescale Mainloop: TMR0 = 0 ' clear count before each pass ' output 8 clocks on RB0 to RA4/T0CKI FOR Loops = 0 TO 7 PORTB.0=1 ' RB0 connects to RA4/T0CKI PAUSE 5 PORTB.0=0 ' counter increments on falling edge PAUSE 5 NEXT Loops Revs = TMR0 ' get TMR0 count IF OPTION_REG.0 THEN HSEROUT ["Counts at 1:4 prescale: ",dec Revs,13,10] ELSE HSEROUT ["Counts at 1:2 prescale: ",dec Revs,13,10] ENDIF OPTION_REG.0 = OPTION_REG.0 ^ 1 ' toggle 1:4 or 1:2 prescale GOTO Mainloop end
Last edited by Bruce; - 20th February 2009 at 16:32. Reason: Quckie test program
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.
Hi
well I am only getting about 3volt, so 0.8 * 5v = 4v, one volt away from triggering. I will a simple amplifier or inverter to the output of my sensor and feed that to the chip
ken
I think I am getting there but just not quite.
your program works with the loop for incrementing. I removed the loop , because I have a sensor output, The output is going through an inverter, so now I get either 0 or 5v going into RA4.
What I now see on the LCD is the incrementation going up high without any look at the input. it goes up to a 1000 in about 3 seconds with the sensor laying there not moving:
Here is the code I have, mainly Bruce,s code with the addition of the LCD and external oscillation.
@ DEVICE pic16F88, CCPMX_ON, MCLR_OFF
DEFINE OSC 20 'use external 20mhz crystal
ANSEL = 0 ' disable A/D so RA4 is digital
CMCON = 7 ' disable comparators
TRISA.4=1 ' RA4/T0CKI = input (Timer0 clock input)
ANSEL = 0 ' disable A/D so RA4 is digital
CMCON = 7 ' disable comparators
OPTION_REG = %10110001 ' clk on RA4/T0CKI, falling edge, 1:4 prescale
Revs var BYTE
ORevs var word
Counter var word
'/////////////////////////
'// LCD configuration //
'/////////////////////////
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 1 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 0 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits) '4 therefore put wire at 4, 5, 6 and 7 of LCD
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000
pause 1000
Counter =0
Mainloop:
TMR0 = 0 ' clear count before each pass
' output 8 clocks on RB0 to RA4/T0CKI
'instead, input from sensor
Revs = TMR0 ' get TMR0 count
Counter = Revs + 1
IF OPTION_REG.0 THEN
LCDOUT $FE,1,"Counter1: ", DEC Counter
ELSE
lcdout $FE,$C0, "Counter2: ", DEC Counter
ENDIF
OPTION_REG.0 = OPTION_REG.0 ^ 1 ' toggle 1:4 or 1:2 prescale
GOTO Mainloop
end
Last edited by lerameur; - 20th February 2009 at 21:25.
Bookmarks