CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Frequency limit using SMT gated counter mode

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JerryR



Joined: 07 Feb 2008
Posts: 181

View user's profile Send private message

Frequency limit using SMT gated counter mode
PostPosted: Thu Sep 25, 2025 12:44 pm     Reply with quote

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
   }
}

gaugeguy



Joined: 05 Apr 2011
Posts: 345

View user's profile Send private message

PostPosted: Thu Sep 25, 2025 2:23 pm     Reply with quote

instead of using long, use int16 or int32 for the variable size depending on what you want.
JerryR



Joined: 07 Feb 2008
Posts: 181

View user's profile Send private message

PostPosted: Thu Sep 25, 2025 2:42 pm     Reply with quote

gaugeguy:

ABSOLUTELY! What was I thinking? long truncated results to 16 bits.

Thanks so much
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group