Frequency measurement

Thread Starter

RG23

Joined Dec 6, 2010
304
I got the following part of code to work

It works for 2,3,5,6,7, etc......but not 2.2,2.3, 4.1 etc

Rich (BB code):
label1: btfsc PORTC,5
        goto label2
        btfsc FLAGS,6   ; debounce -\_
 return     ; exit routine, no action
 incf count1,1
 movf count1,0  ; get count
 addlw DEBOUNCE_TIME ; CNT>16?
 btfss CARRY
 return     ; no, so continue process
 bsf  FLAGS,6   ; set PRESSED flag
 bsf  FLAGS,0   ; set FLAGS.0 to update LCD
 
        movf X,0
        movwf K
        movwf Out1
                 
        movf Y,0
        movwf Q
        
        incf Y
        movf Y,0
        movwf Out2
        
        movf Q,0
        sublw '9'
        btfsc  STATUS,Z
        call loop1
        
label2: 
        btfss FLAGS,6   ; FLAG set? _/- debounce
 goto label3   ; no so done
 decf count1,1  
 btfss ZERO   ; CNT=0?
 return     ; no so done
 bcf  FLAGS,6   ; yes so clear FLAG
     
                         
label3:
 clrf count1   ; clear counter, still bouncing
 return
loop1:  movlw '0'
        movwf Y
        movlw '0'
        movwf Out2
        incf X
        incf Out1
        movf K,0
        sublw   '9'
        btfsc  STATUS,Z
        call loop2
        return  
loop2:  
        movlw '0'
        movwf X
        movlw '0'
        movwf Out1
        return
 
Last edited by a moderator:

tom66

Joined May 9, 2009
2,595
Yes, that's because that's not using the reciprocal method.

Are you allowed to use the "C" language? It would be much easier compared to assembly.

If you want a quick fix then change the code so it counts for a 10 second period instead of 1 second.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
to be honest I still dont know to measure the time between two consecutive edges then determining time and taking reciprocal to find the frequency

I am saying this despite so many posts. I apologize but its the truth

I made the following attempt......

I connected a switch to one of the ports(PORTC,5) and then when I connected function generator I got the readings on LCD display but as per my code I could read 2,3,..... and not decimal

To your point of using 10 seconds, I need to reset the counter after every one second so it wont help
 

Thread Starter

RG23

Joined Dec 6, 2010
304
i humbly request any one to paste a small portion of the code that involves the use of timer storing the time value between two consecutive edges and taking reciprocal to find frequency and display on lCD

the pdf files posted earlier were too complicated for me to understand
 

tom66

Joined May 9, 2009
2,595
to be honest I still dont know to measure the time between two consecutive edges then determining time and taking reciprocal to find the frequency

I am saying this despite so many posts. I apologize but its the truth

I made the following attempt......

I connected a switch to one of the ports(PORTC,5) and then when I connected function generator I got the readings on LCD display but as per my code I could read 2,3,..... and not decimal

To your point of using 10 seconds, I need to reset the counter after every one second so it wont help
Do you know how to start a timer and get it to count?
 

Thread Starter

RG23

Joined Dec 6, 2010
304
yes i know how to define the timer

In fact I have defined a 10 ms timer and obtained 1 second interval from it
in my code

Its just that I dont know how to measure the time between two consecutive rising or falling edges of incoming signal

In my code which i got to execute with the help of switch connected to one of the ports

if i press the switch once per second it displays one and so on

but to get the value in decimal like 2.3 or 2.5 etc I think I have to use a different logic which I still couldn't figure out

Thanks for your help
 

tom66

Joined May 9, 2009
2,595
yes i know how to define the timer

In fact I have defined a 10 ms timer and obtained 1 second interval from it
in my code

Its just that I dont know how to measure the time between two consecutive rising or falling edges of incoming signal

In my code which i got to execute with the help of switch connected to one of the ports

if i press the switch once per second it displays one and so on

but to get the value in decimal like 2.3 or 2.5 etc I think I have to use a different logic which I still couldn't figure out

Thanks for your help
What you need to do is get some code working which waits for the input to change. So, the chip starts up and waits for the signal to go high. When it does this, it starts the timer at zero. It then waits for it to go low, and finally high again. As soon as the pin goes high a second time, it stops the timer and records the value. The value in the timer is the period of the waveform. The process can be repeated for more measurements. Get this part working, then you can work on frequency.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
Now I get the correct number of counts corresponding to the frequency I set on the generator

For eg

50 when 2 Hz

33 when 3 Hz

Now I want to display the corresponding frequency as to the number of counts

If anyone has an idea please let me know

Thanks
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I have got the desired number of counts on LCD corresponding to the frequency on the generator

Now I want to divide 100 by those number of counts to see the frequency on LCD
 

Markd77

Joined Sep 7, 2009
2,806
Have a look at my post #23 in this thread. It's easiest in assembler to use integer division, instead of 100/count which would just give the result 2 or 3, etc, if you do 10000/count you would get a result like 210, 343, etc. You then just need to put the decimal point in the right place so the LCD reads 2.10, 3.43, etc.
 

Markd77

Joined Sep 7, 2009
2,806
I suppose if there are a fairly small range of values for count you could use a lookup table, ie.

Tables have some complications so it is probably worth reading AN556 from the Microchip site.

The result is intentionally 10 times too big so you need to add a decimal point.
Rich (BB code):
movf count, W
call table

table
addwf PCL, F
retlw D'255'     ;count=0   255 because result too big
retlw D'255'     ;count=1   255 because result too big
retlw D'255'     ;count=2   255 because result too big
retlw D'255'     ;count=3   255 because result too big
retlw D'250'     ;count=4
retlw D'200'     ;count=5
retlw D'167'     ;count=6
retlw D'143'     ;count=7
retlw D'125'     ;count=8
retlw D'111'     ;count=9
retlw D'100'     ;count=10
retlw D'91'     ;count=11
retlw D'83'     ;count=12
retlw D'77'     ;count=13
retlw D'71'     ;count=14
retlw D'67'     ;count=15
retlw D'63'     ;count=16
retlw D'59'     ;count=17
retlw D'56'     ;count=18
retlw D'53'     ;count=19
retlw D'50'     ;count=20
retlw D'48'     ;count=21
retlw D'45'     ;count=22
retlw D'43'     ;count=23
retlw D'42'     ;count=24
retlw D'40'     ;count=25
retlw D'38'     ;count=26
retlw D'37'     ;count=27
retlw D'36'     ;count=28
retlw D'34'     ;count=29
retlw D'33'     ;count=30
retlw D'32'     ;count=31
retlw D'31'     ;count=32
retlw D'30'     ;count=33
retlw D'29'     ;count=34
retlw D'29'     ;count=35
retlw D'28'     ;count=36
retlw D'27'     ;count=37
retlw D'26'     ;count=38
retlw D'26'     ;count=39
retlw D'25'     ;count=40
retlw D'24'     ;count=41
retlw D'24'     ;count=42
retlw D'23'     ;count=43
retlw D'23'     ;count=44
retlw D'22'     ;count=45
retlw D'22'     ;count=46
retlw D'21'     ;count=47
retlw D'21'     ;count=48
retlw D'20'     ;count=49
retlw D'20'     ;count=50
retlw D'20'     ;count=51
retlw D'19'     ;count=52
retlw D'19'     ;count=53
retlw D'19'     ;count=54
retlw D'18'     ;count=55
retlw D'18'     ;count=56
retlw D'18'     ;count=57
retlw D'17'     ;count=58
retlw D'17'     ;count=59
retlw D'17'     ;count=60
retlw D'16'     ;count=61
retlw D'16'     ;count=62
retlw D'16'     ;count=63
retlw D'16'     ;count=64
retlw D'15'     ;count=65
retlw D'15'     ;count=66
retlw D'15'     ;count=67
retlw D'15'     ;count=68
retlw D'14'     ;count=69
retlw D'14'     ;count=70
retlw D'14'     ;count=71
retlw D'14'     ;count=72
retlw D'14'     ;count=73
retlw D'14'     ;count=74
retlw D'13'     ;count=75
retlw D'13'     ;count=76
retlw D'13'     ;count=77
retlw D'13'     ;count=78
retlw D'13'     ;count=79
retlw D'13'     ;count=80
retlw D'12'     ;count=81
retlw D'12'     ;count=82
retlw D'12'     ;count=83
retlw D'12'     ;count=84
retlw D'12'     ;count=85
retlw D'12'     ;count=86
retlw D'11'     ;count=87
retlw D'11'     ;count=88
retlw D'11'     ;count=89
retlw D'11'     ;count=90
retlw D'11'     ;count=91
retlw D'11'     ;count=92
retlw D'11'     ;count=93
retlw D'11'     ;count=94
retlw D'11'     ;count=95
retlw D'10'     ;count=96
retlw D'10'     ;count=97
retlw D'10'     ;count=98
retlw D'10'     ;count=99
retlw D'10'     ;count=100
retlw D'10'     ;count=101
retlw D'10'     ;count=102
retlw D'10'     ;count=103
retlw D'10'     ;count=104
retlw D'10'     ;count=105
retlw D'9'     ;count=106
retlw D'9'     ;count=107
retlw D'9'     ;count=108
retlw D'9'     ;count=109
retlw D'9'     ;count=110
retlw D'9'     ;count=111
retlw D'9'     ;count=112
retlw D'9'     ;count=113
retlw D'9'     ;count=114
retlw D'9'     ;count=115
retlw D'9'     ;count=116
retlw D'9'     ;count=117
retlw D'8'     ;count=118
retlw D'8'     ;count=119
retlw D'8'     ;count=120
retlw D'8'     ;count=121
retlw D'8'     ;count=122
retlw D'8'     ;count=123
retlw D'8'     ;count=124
retlw D'8'     ;count=125
retlw D'8'     ;count=126
retlw D'8'     ;count=127
retlw D'8'     ;count=128
retlw D'8'     ;count=129
retlw D'8'     ;count=130
retlw D'8'     ;count=131
retlw D'8'     ;count=132
retlw D'8'     ;count=133
retlw D'7'     ;count=134
retlw D'7'     ;count=135
retlw D'7'     ;count=136
retlw D'7'     ;count=137
retlw D'7'     ;count=138
retlw D'7'     ;count=139
retlw D'7'     ;count=140
retlw D'7'     ;count=141
retlw D'7'     ;count=142
retlw D'7'     ;count=143
retlw D'7'     ;count=144
retlw D'7'     ;count=145
retlw D'7'     ;count=146
retlw D'7'     ;count=147
retlw D'7'     ;count=148
retlw D'7'     ;count=149
retlw D'7'     ;count=150
retlw D'7'     ;count=151
retlw D'7'     ;count=152
retlw D'7'     ;count=153
retlw D'6'     ;count=154
retlw D'6'     ;count=155
retlw D'6'     ;count=156
retlw D'6'     ;count=157
retlw D'6'     ;count=158
retlw D'6'     ;count=159
retlw D'6'     ;count=160
retlw D'6'     ;count=161
retlw D'6'     ;count=162
retlw D'6'     ;count=163
retlw D'6'     ;count=164
retlw D'6'     ;count=165
retlw D'6'     ;count=166
retlw D'6'     ;count=167
retlw D'6'     ;count=168
retlw D'6'     ;count=169
retlw D'6'     ;count=170
retlw D'6'     ;count=171
retlw D'6'     ;count=172
retlw D'6'     ;count=173
retlw D'6'     ;count=174
retlw D'6'     ;count=175
retlw D'6'     ;count=176
retlw D'6'     ;count=177
retlw D'6'     ;count=178
retlw D'6'     ;count=179
retlw D'6'     ;count=180
retlw D'6'     ;count=181
retlw D'5'     ;count=182
retlw D'5'     ;count=183
retlw D'5'     ;count=184
retlw D'5'     ;count=185
retlw D'5'     ;count=186
retlw D'5'     ;count=187
retlw D'5'     ;count=188
retlw D'5'     ;count=189
retlw D'5'     ;count=190
retlw D'5'     ;count=191
retlw D'5'     ;count=192
retlw D'5'     ;count=193
retlw D'5'     ;count=194
retlw D'5'     ;count=195
retlw D'5'     ;count=196
retlw D'5'     ;count=197
retlw D'5'     ;count=198
retlw D'5'     ;count=199
retlw D'5'     ;count=200
retlw D'5'     ;count=201
retlw D'5'     ;count=202
retlw D'5'     ;count=203
retlw D'5'     ;count=204
retlw D'5'     ;count=205
retlw D'5'     ;count=206
retlw D'5'     ;count=207
retlw D'5'     ;count=208
retlw D'5'     ;count=209
retlw D'5'     ;count=210
retlw D'5'     ;count=211
retlw D'5'     ;count=212
retlw D'5'     ;count=213
retlw D'5'     ;count=214
retlw D'5'     ;count=215
retlw D'5'     ;count=216
retlw D'5'     ;count=217
retlw D'5'     ;count=218
retlw D'5'     ;count=219
retlw D'5'     ;count=220
retlw D'5'     ;count=221
retlw D'5'     ;count=222
retlw D'4'     ;count=223
retlw D'4'     ;count=224
retlw D'4'     ;count=225
retlw D'4'     ;count=226
retlw D'4'     ;count=227
retlw D'4'     ;count=228
retlw D'4'     ;count=229
retlw D'4'     ;count=230
retlw D'4'     ;count=231
retlw D'4'     ;count=232
retlw D'4'     ;count=233
retlw D'4'     ;count=234
retlw D'4'     ;count=235
retlw D'4'     ;count=236
retlw D'4'     ;count=237
retlw D'4'     ;count=238
retlw D'4'     ;count=239
retlw D'4'     ;count=240
retlw D'4'     ;count=241
retlw D'4'     ;count=242
retlw D'4'     ;count=243
retlw D'4'     ;count=244
retlw D'4'     ;count=245
retlw D'4'     ;count=246
retlw D'4'     ;count=247
retlw D'4'     ;count=248
retlw D'4'     ;count=249
retlw D'4'     ;count=250
retlw D'4'     ;count=251
retlw D'4'     ;count=252
retlw D'4'     ;count=253
retlw D'4'     ;count=254
retlw D'4'     ;count=255
 
Last edited:

Thread Starter

RG23

Joined Dec 6, 2010
304
Following is part of my program:

/////10 ms subroutine

incf count209,1 /////count209 to count 100 Hz pulses
movf count209,0
addlw 0x06
btfss STATUS,DC
addlw -0x06
addlw 0x60
btfss STATUS,C
addlw -0x60
movwf count209


/////// Subroutine for incoming pusle

btfsc PORTC,5
movlw 0xCE ;return LCD to Line 2 Char 8
movwf PORTD
call SND_CMD////////display position on LCD
movf count209,0
call bcd ///////bcd is the subroutine for BCD to ASCII
movlw h'00'
movwf count209

When I connect the function generator to PORTC,5

I get count209 as 20 corresponding to 5Hz on generator and so on correctly

similarly 50 counts for 2Hz etc

As I mentioned earlier now I want to see the freq in place of counts
 
Top