JerryR
Joined: 07 Feb 2008 Posts: 181
|
Frequency limit using SMT gated counter mode |
Posted: Thu Sep 25, 2025 12:44 pm |
|
|
This group has helped me a bunch and I'm stuck on this issue:
The setup:
I'm using a PIC16F1619 in gated counter mode as a frequency counter. The microcontroller uses an external 16 MHz crystal. I'm suppling a stable square-wave to C1 (mapped to SMT1SIG), and an external RTCC supplying a stable 1Hz signal (500ms high, 500ms low) to C3 (mapped to SMT1WIN). All signals are square and reliable
The issue:
SMT seems to work in gated counter mode correctly with SMT1SIG input up to around 130,000 Hz. past this limit, frequency is inaccurate. I should be able to count up 2^24 hertz (~16Mhz), however, based on the SMT TIMER's 24 bit width. I'd be happy with half that.
So the problem maybe in the way I'm using the gate signal logic. I should be gating pulses into the timer for 500ms. I should have another 500ms to service the LCD (which should be enough time).
Code attached. Anyone see where I'm going wrong here. Thanks!
Code: |
#include <16f1619.h>
#use delay(clock=16000000)
#include <flex_lcd.c>
#include <MCP79411_driver.c>
#fuses HS, NOWDT, NOPLLEN, NOPROTECT, NOLVP, NOBROWNOUT
#define SMT_INPUT PIN_C1 //Signal input pin
#define SMT_GATE_PIN PIN_C3 // Gate signal pin for SMT
//select pin functions
#pin_select SMT1SIG=SMT_INPUT
#pin_select SMT1WIN=SMT_GATE_PIN
int32 Freq= 0;
int8 hrs,min,sec= 0;
//=============================================================================
void setup_smt_gated_frequency() {
// Configure SMT1 module
// Enable SMT1, select Gated Counter Mode
// SMT_ENABLED: 1, SMT_MODE_GATED_COUNTER: 10
setup_smt1(SMT_ENABLED | SMT_MODE_GATED_COUNTER | SMT_WIN_ACTIVE_HIGH);
// Wait for the peripheral to stabilize
delay_ms(50);
}
//===========================================================================
//===========================================================================
long measure_frequency()
{
long count = 0;
int8 status;
smt1_reset_timer(); //SMT1STAT = 0x00 AFTER
smt1_start();
// Reset and start the SMT counter
while (input(SMT_GATE_PIN) == 0); //wait here until gate signal goes HIGH
while (input(SMT_GATE_PIN) == 1); //wait here until gate signal goes LOW
// Stop the SMT counter
smt1_stop(); ////SMT1STAT = 0x00 AFTER
// Read the 24-bit SMT counter value
count = smt1_read(SMT_TMR_REG);
return count; //return count as is SMT_TMR_REG
}
//===========================================================================
void main(void)
{
long frequency;
//initialize RTCC
rtc_Init();
lcd_init();
setup_smt_gated_frequency();
while(TRUE)
{
frequency= measure_frequency(); //measure pulses during gate period (0.5sec)
//now have approx. 0.5 sec while gate low to service LCD
lcd_putc("\f"); //clear screen
printf(lcd_putc,"%lu",frequency); //show frequency on LCD
}
}
|
|
|