' EXAMPLE 14 ' ' PROPELLER FREQUENCY COUNTER: External Frequency Source '********************************************************************************** 'IMPORTANT: This example may require an understanding of Example: 13 '********************************************************************************** 'WHAT'S NEW IN THIS EXAMPLE: ' SPECIAL PURPOSE PROPELLER REGISTERS: CTRA, FRQA, AND PHSA. Using these ' registers, an incoming frequency source is sampled for one second. The ' result is then displayed on a Parallax LCD. '********************************************************************************** 'DIFFICULTY LEVEL: Intermediate '********************************************************************************** 'PURPOSE: The purpose of this example is to show how to create a frequency counter ' using the Propeller's special purpose registers and display a frequency ' count in Hz on an LCD. 'Submitted by Dave Scanlan, April 25, 2006 'File: Example14_FreqCtrLCD_ExternalSource.spin '********************************************************************************** 'CORRECT OUTPUT: After sampling an incoming digital wave, the result will be ' displayed as below: ' 4x20 LCD Module ' ││ ' │ PROPELLER │ ' │ FREQUENCY COUNTER │ ' │ │ ' │ FREQ:XXXXXXXXXXXHz │ ' ││ '********************************************************************************** CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 LCD_Pin = 0 'The LCD is connected to pin A0. LCD_Baud = 19_200 'Baud LCD_Lines = 4 'The number of lines on this LCD In = %0 On = 1 Off = 0 ' VAR Long Frequency ' OBJ LCD: "debug_lcd" 'Creates the object LCD from "debug_lcd" ' PUB Start Repeat MeasureFrequency DisplayFrequency ' PUB MeasureFrequency | Pin Pin := 5 'Pulses are sampled on this pin. DirA[Pin] := In CTRA := 0 'Clear CTRA settings CTRA := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (Pin) 'Trigger to count rising ' edge on Pin A5 FRQA := 1000 'Count 1000 pulses on each trigger PHSA := 0 'Clear accumulated value WaitCNT( 80_000_000 + CNT ) 'Wait for 1 second Frequency := PHSA / 1000 'Calculate Freq based on the sampling ' duration, 1000 ms (1 second) PUB DisplayFrequency ' If LCD.Start(0, 19_200, 4 ) If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines) 'Initialize the LCD object LCD.Cursor(Off) 'Set cursor off LCD.Backlight(On) 'Set backlight on LCD.Cls 'Clear the LCD screen LCD.Str(string(" PROPELLER")) 'Display string on LCD LCD.Newline LCD.Str(string(" FREQUENCY COUNTER")) 'Display string on LCD LCD.Gotoxy(2,3) 'Gotoxy(Col,Row) LCD.Str(string("FREQ:")) 'Display string on LCD LCD.Gotoxy(7,3) LCD.DecF(Frequency, 8) 'Display Freq. as a decimal. LCD.Gotoxy(15,3) LCD.Str(string("Hz")) 'Display string on LCD '********************************************************************************** 'ADDITIONAL INFORMATION: ' -- The code for sampling an incoming wave is an adaptation from ' Dr. Martin Hebel's original code in the object, BS2.Function.spin. ' -- The results of this counter agree closely with the readings from an HP 5314A ' frequency counter with deviations of 0 to 6 Hz at 2MHz. ' -- For this example, the amplitude was 3 volts. Until, the max input voltage, ' has been "firmly" established for the Propeller, I would proceed with ' caution. Remember, this processor is a 3.3VDC device. ' -- For analog signals, a pre-conditioning Schmitt trigger is suggested. ' Resource: http://en.wikipedia.org/wiki/Schmitt_trigger 'FORMAT FOR: LCD.DecF(Frequency, 8) ' LCD.DecF(Value, Field_Width) ' Prints signed decimal value in fixed-width field