Word
Posted: 27 Aug 2008 20:03
Hi Dany
Thanks for your replies. I try and change it.
Thanks for your replies. I try and change it.
Development Tools, Compilers, Books
https://forum.mikroe.com/
No.The Counter command makes counting pulses easy.I think Dany has answered you question about your code.By saying I have to do it the hard way round, is that with a counter
Code: Select all
sub procedure LCD_BarGraph_Draw(dim Row as byte, dim Column_Start as byte, dim Column_End as byte, dim Value as word)
Well, I have never worked with the CCP either, it is on my list to try it out...DDJ wrote:Dany
Would it not be easier to use the CCP module here? I have never worked with it, but from a few posts it seems less complicated. What do you think ? The CD4093 puts out square wave pulses. Is it easy to measure these with CCP ?
Thanks
Code: Select all
'(*
' * Project name:
'
' * Copyright:
' (c) mikroElektronika, 2006
' * Revision History:
' 20050914 (MJ):
' - initial release.
' * Description:
' This code demonstrates using Pulse-In Function for counting pulses in given
' time period. This a blocking call. This demo waits for input signal
' (high level) to start counting pulses. It counts pulses for 1000 miliseconds.
' Then it prints results on LCD which is connected on PORTB.
' * Test configuration:
' MCU: P16F887
' Dev.Board: EasyPIC5
' Oscillator: HS, 8.000 MHz
'module pulsein_demo.mbas
' SW: mikroBasic v5.0 or higher
' * NOTES:
' Prototype:
' function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
'
' PORT - assigns port for pulse-in function
' PIN - assigns pin for pulse-in function
' STATE - defines logical state for counting pulses (LOW = 0 or HIGH = 1)
' DURATION - Time in ms during which the pulses are being count
'
' This function returns number of pulses in given time (DURATION), on given
' PORT and PIN.
' Tested on EasyPIC3 with P16F877A. With few modification it can be used
' with almost every PIC MCU.
' *)
program pulsein_demo
dim LCD_RS as sbit at RB4_bit ' Lcd module connections
LCD_EN as sbit at RB5_bit
LCD_D4 as sbit at RB0_bit
LCD_D5 as sbit at RB1_bit
LCD_D6 as sbit at RB2_bit
LCD_D7 as sbit at RB3_bit
LCD_RS_Direction as sbit at TRISB4_bit
LCD_EN_Direction as sbit at TRISB5_bit
LCD_D4_Direction as sbit at TRISB0_bit
LCD_D5_Direction as sbit at TRISB1_bit
LCD_D6_Direction as sbit at TRISB2_bit
LCD_D7_Direction as sbit at TRISB3_bit ' End Lcd module connections
dim text as string [16] ' Variable text is of string type
dim counter,
msec,
num_impulse as word
txt as char[10]
dim tmp as word ' Temporary variable
old as byte
sub procedure interrupt
counter = counter + 1 ' Increment value of counter on every interrupt
if counter = 1 then ' Has 1ms elapsed?
counter = 0
msec = msec + 1 ' Increment msec on every 1 ms
end if
TMR0 = 0
INTCON = 0x20 ' Clear the TMR0IF flag Enable TMRO interrupt
end sub
sub function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
' Previous pin state value
tmp = 0 ' Clear temporary variable
msec = 0
while TestBit(port, pin) = state ' Wait for pulses
wend ' Note that the program will stop here
' if there are no pulses
' Note that the program will stop here
while TestBit(port, pin) <> state ' Wait for rising (falling) edge
wend ' if there are no pulses
' Clear number of ms
old = TestBit(port, pin) ' Get current pin state
' if there are no pulses
while duration > msec ' Check if duration is reached
if (TestBit(port, pin)) <> old then ' Check if pin state has changed
tmp = tmp + 1
old = TestBit(port, pin) ' Get current pin state
end if
wend
result = (tmp >> 1) + 1 ' Assign result value
end sub
main:
OPTION_REG = 2 ' Assign prescaler to TMR0 (1:8)
PORTB = 0xFF
TRISB = %0000000
ANSEL = 0
ANSELH = 0
PORTD = 0
TRISD = %1000000
Lcd_Init() ' LCD display initialization
Lcd_Cmd(_LCD_CURSOR_OFF) ' LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR) ' LCD command (clear LCD)
text = "Puls meting" ' Define the first message
Lcd_Out(1,1,text) ' Write the first message in the first line
text = "Tijd : " ' Define the first message
Lcd_Out(2,1,text) ' Write the first message in the first line
while 1
counter = 0
TMR0 = 0
INTCON = 0xA0 ' Enable TMRO interrupt
msec = 0 ' Clear msec
num_impulse = 0 ' Clear num_impulse
CM1CON0.C1ON = 0 ' Comparators off
CM2CON0.C2ON = 0
num_impulse = Pulse_In(PORTD, 0, 1, 1000) ' Count HI pulses on PORTD.0, for 1000ms
WordToStr(num_impulse, txt) ' Convert results to string for LCD output
'WordToStr(num_impulse, txt) ' Convert results to string for LCD output
Lcd_Out(2,6, txt) ' Write the first message in the first line Lcd_Out(2, 1, txt)
wend ' Wait forever (stop)
end.
Code: Select all
'(*
' * Project name:
'
' * Copyright:
' (c) mikroElektronika, 2006
' * Revision History:
' 20050914 (MJ):
' - initial release.
' * Description:
' This code demonstrates using Pulse-In Function for counting pulses in given
' time period. This a blocking call. This demo waits for input signal
' (high level) to start counting pulses. It counts pulses for 1000 miliseconds.
' Then it prints results on LCD which is connected on PORTB.
' * Test configuration:
' MCU: P16F887
' Dev.Board: EasyPIC5
' Oscillator: HS, 8.000 MHz
'module pulsein_demo.mbas
' SW: mikroBasic v5.0 or higher
' * NOTES:
' Prototype:
' function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
'
' PORT - assigns port for pulse-in function
' PIN - assigns pin for pulse-in function
' STATE - defines logical state for counting pulses (LOW = 0 or HIGH = 1)
' DURATION - Time in ms during which the pulses are being count
'
' This function returns number of pulses in given time (DURATION), on given
' PORT and PIN.
' Tested on EasyPIC3 with P16F877A. With few modification it can be used
' with almost every PIC MCU.
' *)
program pulsein_demo
dim LCD_RS as sbit at RB4_bit ' Lcd module connections
LCD_EN as sbit at RB5_bit
LCD_D4 as sbit at RB0_bit
LCD_D5 as sbit at RB1_bit
LCD_D6 as sbit at RB2_bit
LCD_D7 as sbit at RB3_bit
LCD_RS_Direction as sbit at TRISB4_bit
LCD_EN_Direction as sbit at TRISB5_bit
LCD_D4_Direction as sbit at TRISB0_bit
LCD_D5_Direction as sbit at TRISB1_bit
LCD_D6_Direction as sbit at TRISB2_bit
LCD_D7_Direction as sbit at TRISB3_bit ' End Lcd module connections
dim counter,
msec,
num_impulse as word
txt as char[10]
sub procedure interrupt
inc(counter) ' Increment value of counter on every interrupt
if counter = 1 then ' Has 1ms elapsed?
counter = 0 ' Clear the counter
inc(msec) ' Increment msec on every 1 ms
end if
TMR0 = 5
ClearBit(INTCON, T0IF) ' Clear the TMR0IF flag
SetBit(INTCON, T0IE) ' Enable TMR0 interrupt
end sub
sub function Pulse_In(dim byref port as byte, dim pin, state as byte, dim duration as word)as word
dim tmp as word ' Temporary variable
old as byte ' Previous pin state value
while TestBit(port, pin) = state ' Wait for pulses
wend ' Note that the program will stop here
' if there are no pulses
while TestBit(port, pin) <> state ' Wait for rising (falling) edge
wend ' Note that the program will stop here
' if there are no pulses
tmp = 0 ' Clear temporary variable
msec = 0 ' Clear number of ms
old = TestBit(port, pin) ' Get current pin state
while duration > msec ' Check if duration is reached
if (TestBit(port, pin)) <> old then ' Check if pin state has changed
inc(tmp) ' Increment number of pulses
old = TestBit(port, pin) ' Get current pin state
end if
wend
result = (tmp >> 1) + 1 ' Assign result value
end sub
main:
OPTION_REG = 1 ' Assign prescaler to TMR0 (1:8)
TRISB = $00 ' Designate PORTB as output
PORTB = $FF ' Initialize PORTB
TRISD = $FF ' Designate PORTD as input (for signal)
counter= $00 ' Initialize the counter
TMR0 = 5
INTCON = $A0 ' Enable TMRO interrupt
'ANSEL = 0
ANSELH = 0
msec = 0 ' Clear msec
num_impulse = 0 ' Clear num_impulse
ADCON1 = $FF ' PORTB all digital
CM1CON0 = $06 ' Comparators off
CM2CON0 = $06
CM2CON1 = $06
Lcd_Init() ' Initialize LCD connected to PORTB
Lcd_Cmd(_LCD_CURSOR_OFF) ' Send command to LCD "clear display"
Lcd_Cmd(_LCD_CLEAR) ' Send command "cursor off"
Lcd_Out(1, 1, "Waiting... ") ' Print txt to LCD
num_impulse = Pulse_In(PORTD, 0, 1, 1000) ' Count HI pulses on PORTD.0, for 1000ms
WordToStr(num_impulse, txt) ' Convert results to string for LCD output
Lcd_Out(1, 1, "Number of pulses") ' Print txt to LCD
Lcd_Out(2, 1, txt) ' Print number of pulses to LCD
while 1 = 1
wend ' Wait forever (stop)
end.