Crude low frequency counters with Raspberry Pi Pico
Crude low frequency counter
Used two state machines to generate two square waves as signal sources. Frequency counter measured these two frequencies.
Frequency counter input Pico pin 20 (GPIO-15)
40 Hz sq.wave signal source on Pico pin 21 (GPIO-16)
25 kHz sq.wave signal source on Pico pin 22 (GPIO-17)
25 kHz was maximum frequency that could be measured.
With 25 kHz input, reading varied from 25000 Hz to 25004 Hz
Number of interrupts in one second were counted to get frequency.
MicroPython code:
# Filename CrudeFc1.py
import time
from machine import Pin
import rp2
# global variable
H2L = 0
# Interrupt handler (ISR)
def h2l( void ):
global H2L
H2L = H2L + 1
def frequency():
global H2L
fin.irq ( handler = None ) # Int. disabled
H2L = 0
fin.irq ( handler = h2l, trigger=Pin.IRQ_FALLING ) #Int. enabled
time.sleep(1) # gate time of ONE SECOND
fin.irq ( handler = None ) # Int. disabled
return(H2L)
#### square waves start
# 25 kHz square wave
@rp2.asm_pio ( set_init = rp2.PIO.OUT_LOW )
def kHz25( ):
wrap_target( )
set( pins, 0 ) # 1 clock
set( pins, 1 ) # 1 clock
wrap( ) # zero clock
# 40 Hz square wave
@rp2.asm_pio( set_init=rp2.PIO.OUT_LOW )
def Hz40():
wrap_target( )
set( pins, 0 ) [24] # 25 clocks
set( pins, 1 ) [24] # 25 clock
wrap( ) # zero clock
# Square wave on GP 16 ( Pico pin 21 )
sm2 = rp2.StateMachine( 1, Hz40, freq=2000, set_base=machine.Pin(16) )
sm2.active(1)
# Square wave on GP17 ( Pico pin 22 )
sm1 = rp2.StateMachine( 0, kHz25, freq=50000, set_base=machine.Pin(17) )
sm1.active(1)
### square waves end
# GP15 is set as input with internal pull-up resistor
fin = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_UP )
# High to low transition on fin pin associated with h2l ISR
# fin.irq ( handler = h2l, trigger=Pin.IRQ_FALLING )
# clock set at 125 MHz
machine.freq(125000000)
print('CPU frequency', machine.freq(), 'Hertz')
time.sleep(1)
while True:
f=frequency()
print('frequency=',f)
### Frequency counter end
### End of file CrudeFc1.py
#####################
### End of file CrudeFc1.py
#####################
This screen capture shows the running program with 25 kHz input.
Then input was changed to 40 Hz .
Arduino IDE for Paspberry Pi Pico
$ sudo apt install arduino
Arduino 1.8.13 was installed.
Used this Arduino Board manager for Raspberry Pi Pico
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
Board manager URL was set up from menu
File --> Preferences -->Additional Board Manager URLs
This screen capture shows the steps followed.
Raspberry PiPico/RP2040 version 2.2.0 failed to work for me; installed the next lower version.
Arduino sketch for crude frequency counter.
////////////////////////////////////////// Filename CrudeFc2.ino
volatile uint32_t H2L = 0;
// GPIO15 ( Pico pin 20 ) is frequency input.
// Amplitude of input signal is below 3.3 Volt
const uint8_t fin = 15;
// Interrupt Service Routine
void h2l ( )
{
H2L = H2L + 1;
}
void setup() {
// initialize GPIO15 as input
pinMode(fin, INPUT_PULLDOWN);
// Falling edge on fin fires h2l ISR
attachInterrupt ( digitalPinToInterrupt( fin ), h2l, FALLING);
Serial.begin(9600);
}
void loop() {
if (H2L != 0) {
detachInterrupt ( fin ); // Interrupt disabled
Serial.print(H2L);
Serial.println(" Hertz");
H2L = 0;
attachInterrupt ( digitalPinToInterrupt( fin ), h2l, FALLING);
// Interrupt enabled
}
delayMicroseconds(1000000); // gate time of one second
}
// End of CrudeFc2.ino
///////////////////////////////////////
Comments
Post a Comment