2 Channel, 50Mhz Frequency Counter, PIC24FJ

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

2 Channel, 50Mhz Frequency Counter, PIC24FJ

#1 Post by rmteo » 13 Sep 2008 19:18

Credit to Florin (yo2lio) for the original code. I have made 2 modifications. First, the obvious one, is that it is a 2-channel frequency counter. Second, the update rate is 0.25sec instead of 1sec. You should be able to run it on the smallest PIC24FJ. It needs only 2146 ROM and 48 RAM.

Code: Select all

program FREQCNT2

include "DSPIC_aditional_string_library"

dim F_out1, F_out2 as dWord
    data_available as boolean
    lcd_txt as string[16]

sub procedure Timer1Int org $1A ' interrupt at 1s
  IFS0 = IFS0 and $FFF7     ' Clear TMR1IF
  T2CON.15 = 0              ' Stop Timer2/3
  W10 = TMR2                ' Save Timer2/3
  W11 = TMR3HLD
  TMR3HLD = 0               ' Clear Timer2/3
  TMR2 = 0
  T2CON.15 = 1              ' Start Timer2/3
  F_out1 = (W11 << 16) or W10
  F_out1 = F_out1 << 5
  T4CON.15 = 0              ' Stop Timer4/5
  W10 = TMR4                ' Save Timer4/5
  W11 = TMR5HLD
  TMR5HLD = 0               ' Clear Timer4/5
  TMR4 = 0
  T4CON.15 = 1              ' Start Timer4/5
  F_out2 = (W11 << 16) or W10
  F_out2 = F_out2 << 5
  data_available = True
end sub

main:
  AD1PCFG = 0xFFFF
  RPINR3 = 8            ' Channel 1 input on RP8
  RPINR4 = 9            ' Channel 2 input on RP9
  data_available = False
  T2CON = $000A ' 32 bit Timer2/3 , External clock from pin T2CK (on the rising edge) , prescaler 1as1
  TMR3 = 0
  TMR2 = 0
  T4CON = $000A ' 32 bit Timer4/5 , External clock from pin T4CK (on the rising edge) , prescaler 1as1
  TMR4 = 0
  TMR5 = 0
  T1CON = $0010           ' Timer1 ON, internal clock FCY, prescaler 1 as 64
  TMR1 = 0
  PR1 = 62500             ' interrupt at 0.25sec
  IPC0  = IPC0 or  $1000  ' Interrupt priority level = 1
  IFS0  = IFS0 and $FFF7  ' Clear TMR1IF
  IEC0  = IEC0 or  $0008  ' Enable Timer1 interrupts
  T1CON.15 = 1            ' Start Timer1
  T2CON.15 = 1            ' Start Timer2
  T4CON.15 = 1            ' Start Timer4

  Lcd_Init(PORTB, 7,6,5,4, PORTB, 2,1,3)
  Lcd_Cmd(LCD_CURSOR_OFF)
  Lcd_Cmd(LCD_CLEAR)
  while true
    if data_available then
      data_available = False
      LongWord2StrWithZeros(F_out1,lcd_txt)
      lcd_txt[0] = "1"
      lcd_txt[1] = ":"
      Str_Cat(lcd_txt," Hz")
      Lcd_Out(1,1,lcd_txt)  ' Display Channel 1
      LongWord2StrWithZeros(F_out2,lcd_txt)
      lcd_txt[0] = "2"
      lcd_txt[1] = ":"
      Str_Cat(lcd_txt," Hz")
      Lcd_Out(2,1,lcd_txt)  ' Display Channel 2
    end if
  wend
end.
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#2 Post by yo2lio » 13 Sep 2008 19:43

:D
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

oscill
Posts: 14
Joined: 05 Feb 2006 00:28
Location: Russia
Contact:

#3 Post by oscill » 22 Jan 2009 09:10

Regard
try to compile the this a code and have an error in line:
RPINR3 = 8 ' Channel 1 input on RP8
RPINR4 = 9 ' Channel 2 input on RP9

this there is adjusting the register ADC?
I looked datasheet PIC24FJ and has not seen this.

who can me to help?

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 22 Jan 2009 09:20

oscill wrote:Regard
try to compile the this a code and have an error in line:
RPINR3 = 8 ' Channel 1 input on RP8
RPINR4 = 9 ' Channel 2 input on RP9

this there is adjusting the register ADC?
I looked datasheet PIC24FJ and has not seen this.

who can me to help?
Depending of MCU type ....

What MCU do you try to use ?
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

oscill
Posts: 14
Joined: 05 Feb 2006 00:28
Location: Russia
Contact:

#5 Post by oscill » 22 Jan 2009 09:53

Thank you
I use PIC24FJ64GA002

You may give link where look adjustment?

P.S.This register there is dsPIC33
Last edited by oscill on 22 Jan 2009 10:12, edited 1 time in total.
My problem - my bad english

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#6 Post by yo2lio » 22 Jan 2009 10:09

Program compile ok at me :

0:0 S-100 Success (Release build) - Timestamp: 11:07:14 AM
0:0 W-101 Used ROM: 2146 (4%) Used RAM: 48 (1%)
0:0 W-102 Free ROM: 41881 (96%) Free RAM: 4048 (99%)
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

oscill
Posts: 14
Joined: 05 Feb 2006 00:28
Location: Russia
Contact:

#7 Post by oscill » 22 Jan 2009 10:27

Hmm, OK

At night, try once again

I should like that yo2lio your messages on forum much,much I help, thank you. :D
My problem - my bad english

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#8 Post by rmteo » 22 Jan 2009 16:49

oscill wrote:Regard
try to compile the this a code and have an error in line:
RPINR3 = 8 ' Channel 1 input on RP8
RPINR4 = 9 ' Channel 2 input on RP9

this there is adjusting the register ADC?
I looked datasheet PIC24FJ and has not seen this.

who can me to help?
Check the data sheet:
9.4 Peripheral Pin Select

A major challenge in general purpose devices is providing
the largest possible set of peripheral features while
minimizing the conflict of features on I/O pins. The challenge
is even greater on low pin count devices similar
to the PIC24FJ64GA family. In an application that
needs to use more than one peripheral multiplexed on
single pin, inconvenient workarounds in application
code or a complete redesign may be the only option.

The peripheral pin select feature provides an alternative
to these choices by enabling the user&#146;s peripheral
set selection and their placement on a wide range of
I/O pins. By increasing the pinout options available on
a particular device, users can better tailor the
microcontroller to their entire application, rather than
trimming the application to fit the device.

The peripheral pin select feature operates over a fixed
subset of digital I/O pins. Users may independently
map the input and/or output of any one of many digital
peripherals to any one of these I/O pins.
Peripheral pin
select is performed in software and generally does not
require the device to be reprogrammed. Hardware
safeguards are included that prevent accidental or
spurious changes to the peripheral mapping once it has
been established.
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

oscill
Posts: 14
Joined: 05 Feb 2006 00:28
Location: Russia
Contact:

#9 Post by oscill » 29 Jan 2009 08:29

All work! I have done the mistake .
I have else question? As inaccuracy of the measurement of the frequency?
If F in = 27.333100 c what accuracy this will.

Thank you for help.
My problem - my bad english

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#10 Post by yo2lio » 29 Jan 2009 09:47

Depending of your MCU clock accuracy.

The resolution is 4 Hz .

question for rmteo , why this:

Code: Select all

  F_out1 = F_out1 << 5 
I don't understand ... must be:

Code: Select all

  F_out1 = F_out1 << 2
if time base is 0.25 sec instead of 1 sec , resolution is 4 Hz instead of 1 Hz and counters value must be multiplied with 4 instead of 32 ...???
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#11 Post by rmteo » 02 Feb 2009 01:25

Florin, you are correct, it should be leftshifted by 2 and not by 5. I was playing with even shorter time bases and forgot to change the code. :twisted:
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”