For your information i am using timer one interrupt to act as a counter(measuring period) and On interrupt external interrupt ,rising edge detection for RB0 to count the slotted wheel while slicing through the sensor.
Microcontroller i am using
IC16F877 4MHZ
This is the picbasic compiler i am using:
https://oshonsoft.com/pic.html
This is the sensor i am using :
http://oeiwcsnts1.omron.com/pdfcatal.nsf/0/3D4B80C095EC4F4886256C5600712077/$File/D21EESX4700902.pdf?OpenElement
this is the code:
Dim interrupt_count As Byte
Dim timeroffset As Word
Dim tmroffset As Word
Dim count1 As Word 'RB0 interrupt count
EEPROM 21, 0, 0, 0, 0,
TRISA = %0000000
TRISC = 0
OPTION_REG.6 = 1 'RB0/INT edge triggered (Rising)
OPTION_REG.7 = 0 ' PORTB pull up enabled by individual port latch value
INTCON.INTE = 0 'External interrupt for RB0 disable!
INTCON.7 = 1 'Global interrupt enabled!
Symbol t1ck0 = T1CON.4 ' Timer1 Input Clock Prescale Select bits
Symbol t1ck1 = T1CON.5 ' Timer1 Input Clock Prescale Select bits
Symbol sensor = PORTD.0 'sensor
'WaitMs 3000
Define LCD_LINES = 2
Define LCD_CHARS = 8
Define LCD_BITS = 4 'allowed values are 4 and 8 - the number of data interface lines
Define LCD_DREG = PORTB
Define LCD_DBIT = 4 '0 or 4 for 4-bit interface, ignored for 8-bit interface
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 1
Define LCD_EREG = PORTB
Define LCD_EBIT = 3
Define LCD_RWREG = PORTB 'set to 0 if not used, 0 is default
Define LCD_RWBIT = 2 'set to 0 if not used, 0 is default
Lcdinit
WaitMs 2 'wait For lcd To start
Lcdcmdout 0x0e 'Underline cursor on
Lcdcmdout LcdClear
Lcdcmdout LcdLine1Home
Lcdout "RPM" 'Clear LCD display
Lcdcmdout LcdLine2Home
Lcdout "Ver1.0"
timeroffset = 15535 ' interrput occurs every 50 ms(0.05sec)
t1ck0 = 0 'prescaler 1:1 selected
t1ck1 = 0
PIE1.0 = 1 'enable TMR1 interupts
INTCON.6 = 1 'enable all unmasked interrupts
INTCON.7 = 1 'enable Global interrupts
PIR1.0 = 0 'reset interupt flag
T1CON.0 = 0 'stop the timer
TMR1H = timeroffset.HB 'timersethigh 'load a start up value into the timer
TMR1L = timeroffset.LB 'timersetlow
T1CON.0 = 1 'start the timer
INTCON.INTE = 1 'External interrupt for RB0 enable!
main:
end
On Interrupt
If INTCON.1 = 1 Then 'RB0 External interrupt count!
count1 = count1 + 1
Write 21, count1.LB
Write 22, count1.HB
INTCON.INTF = 0 'RB0 External Interrupt flag bit cleared!
Endif
If PIR1.0 = 1 Then 'TMR1F - Timer 1 overflow interrupt flag bit ;1=overflow
INTCON.INTE = 0 'External interrupt for RB0 disable!
T1CON.0 = 0 'stop the timer
TMR1H = timeroffset.HB 'timersethigh 'load a start up value into the timer
TMR1L = timeroffset.LB 'timersetlow
PIR1.0 = 0 'reset TMR1 interupt flag
interrupt_count = interrupt_count + 1 'timer 1 interrupt count
If interrupt_count = 20 Then '1 sec elapsed!
interrupt_count = 0
Gosub menu1
count1 = 0
Write 21, count1.LB
Write 22, count1.HB
T1CON.0 = 1 'start the timer
INTCON.INTE = 1 'External interrupt for RB0 enable!
Endif
Endif
T1CON.0 = 1 'start the timer
INTCON.INTE = 1 'External interrupt for RB0 enable!
Resume 'return back to where set timer was called
Enable 're enable interrupts
menu1:
Lcdcmdout LcdClear
Lcdout "RPM"
Lcdcmdout 0xc0
Lcdout "RPM:" 'count
Gosub update1
Return
update1:
Read 21, count1.LB
Read 22, count1.HB
Lcdcmdout LcdLine2Pos(5)
count1 = count1 * 5 '12 slot used (1 minute Total count)
Lcdout #count1 ' RPM
Return
what my program going to do is at 1st it will start the 0.05 sec timer,and RB0 will start generating pulse while the wheel is rotating ,so after 2 times timer overflow which is 1 sec the total number of slot passing through the sensor since the timer was started is now being stored and display to the LCD.the LCD will refresh almost every sec i can said.This process is repeated........
Anyone has any idea can share with me and the others ,after study my code above.So fell free to share anything ok! Enjoy ur Day!