Audio Frequency Counter


Results 1 to 9 of 9

Threaded View

  1. #4
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default Help required

    Hello Steve,

    I have run into some problem using your code for a 16F628A with a 16x2 LCD.

    I am basically making a audio frequency counter, which would then be used to control a set of 8 LED's depending upon the Input frequency on PORTB.

    Keeping this in mind I tried to output the frequency on the LCD but I get jumping figures at high speed. I believe , I have to introduce a delay between the display. Secondly , connecting 10K to ground on Pin#3 with 1K in series , as your circuit says, I don't get any reading. Which means , I need some impedence matching or may be a signal conditioner.

    My audio source is from my desktop computer , audio out.

    my code so far -
    Code:
    @   __CONFIG  _HS_OSC & _MCLRE_OFF  &  _LVP_OFF & _WDT_ON & _PWRTE_ON  & _BODEN_ON
    DEFINE OSC 20
    
                ;76543210           
        TRISB = %00000000			
        TRISA = %11110000
     
    DEFINE LCD_DREG PORTB 			'Define PIC port used for LCD Data lines
    DEFINE LCD_DBIT 0 			    'Define first pin of portb connected to LCD DB4
    DEFINE LCD_RSREG PORTB 			'Define PIC port used for RS line of LCD
    DEFINE LCD_RSBIT 5 			    'Define Portb pin used for RS connection
    DEFINE LCD_EREG PORTB 			'Define PIC prot used for E line of LCD
    DEFINE LCD_EBIT 4 			    'Define PortB pin used for E connection
    DEFINE LCD_BITS 4 			    'Define the 4 bit communication mode to LCD
    DEFINE LCD_LINES 2 			    'Define using a 2 line LCD
    'DEFINE LCD_COMMANDUS 2000 		'Define delay between sending LCD commands
    'DEFINE LCD_DATAUS 50 			'Define delay time between data sent.
    
        
        ' Interrupt and register definition
        ' ---------------------------------
        '
        OPTION_REG = %1111000   ' TMR0 clock source : RA4/T0CKI
                                ' increment on low to high transition
                                ' Prescaler assign to WDT
                                ' WDT rate 1:1
                                '
        INTCON = %10100000      ' Enable global interrupt
                                ' Disable EE write interrupt
                                ' Enable TMR0 overflow interrupt
        CMCON = 7            ' disable analog comparator
        VRCON = 0            'turns off Vref for min current 
    
            
        ' Variable definition
        ' -------------------
        '
        DisplayPort  var PORTB   ' Port for 7 Segments     
        ClockInput   var PORTA.4 ' Input pin for signal
        _7Seg1       con 14      ' enable more significant 7 segment display
        _7Seg2       con 13      ' enable mid significant 7 segment display
        _7Seg3       con 11      ' enable less significant 7 segment display
        Digit_1	     var byte    ' Hundreds digit
        Digit_2	     var byte    ' Tenth digit
        Digit_3	     var byte    ' Unit digit
        ToBeDisplay	 var word    ' Result of count to be send to 7 segment display
        Display      var byte    ' Temp variable
        DisplayLoop  var byte    ' 
        Delay        var word    ' Variable for Delay loop
        OverFlowVar  var word	 '
        Thousands	 var bit	 ' Flag for count >= 1000 & < 10 000 
        TenThousands var bit	 ' Flag for count >= 10 000
    
        ' Variable and software initialisation
        ' ------------------------------------
        '
        tobedisplay = 0 ' set initial value of count
        TMR0 = 0        ' reset prescaller 
        on interrupt goto SetVarToBeDisplay
        
    MainLoop:
    
        ' MainLoop
        ' ---------
        '
        ' 1. display the result of the count on RA4 pin
        ' 2. refresh display
        ' 3. reset Timer0 
        ' 4. reload prescaler.
        '
        ' Duration of the procedure : 1 sec 
        '           fine tuned by DelayBetweenEachDisplay Sub
        '
        ' Looping 1 sec and get results of the pulse count in
        ' TMR0 + OverFlowVar
        '
    DisplayRefresh:
    	'
        ' Testing amount of count
        ' -----------------------
        '
        ' Get the result of count and place decimal point flag
        ' on the according 7 segments
        '
        If tobedisplay>=1000 then
        	tobedisplay=tobedisplay/10
        	if tobedisplay>=1000 then
        		tobedisplay=tobedisplay/10
        		Thousands=0
        		TenThousands=1
        	else
        		TenThousands=0
        		thousands=1
        	endif
         else
        	thousands=0
        	tenthousands=0
        endif
        
        LCDOut $FE,$C0, "COUNT: ",DEC tobedisplay , "    " 
        gosub DelayBetweenEachDigit
        tobedisplay = OverFlowVar + TMR0
        OverFlowVar = 0 ' Reset OverFlowVar
        TMR0 = 0        ' reset prescaller
    
        goto DisplayRefresh
        goto DisplayRefresh
        goto DisplayRefresh
        goto DisplayRefresh
        goto DisplayRefresh
        goto DisplayRefresh
    
    
    DelayBetweenEachDigit:
    
        ' DelayBetweenEachDigit
        ' ---------------------
        ' Produce delay of about 3 mSec 
        '
        ' Fine tuned with MPLAB StopWatch to get MainLoop = 1 sec
        '
    	for delay=1 to 307
    	    @ nop
    	next
    	@ nop
    	@ nop
    	@ nop
    	@ nop
    	@ nop
    	@ nop
    	@ nop
        return
    
    
        disable
    SetVarToBeDisplay:
        '
        ' SetVarToBeDisplay
        ' -----------------
        ' interrupt routine of TMR0 overflow
        '
        ' Reset prescaller
        ' Reset overflow flag
        '
        OverFlowVar = OverFlowVar + 256 
        INTCON.2 = 0 ' clear overflow flag
        TMR0 = 0     ' reload TMR0
        resume
    enable
    Last edited by charudatt; - 27th July 2009 at 20:01. Reason: pin number change

Similar Threads

  1. frequency counter help and pointers
    By comwarrior in forum General
    Replies: 2
    Last Post: - 30th June 2009, 11:51
  2. Replies: 14
    Last Post: - 26th September 2007, 05:41
  3. Frequency Counter
    By Asmith in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 22nd June 2007, 15:54
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. Frequency Counter using PIC and PicBasic
    By PICtron in forum mel PIC BASIC Pro
    Replies: 31
    Last Post: - 28th January 2005, 06:20

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts