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 support@ccsinfo.com

problem measuring ac line freq

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



Joined: 23 Jul 2018
Posts: 20

View user's profile Send private message

problem measuring ac line freq
PostPosted: Mon Jul 30, 2018 11:48 pm     Reply with quote

Code:
#include <18f252.h>
#DEVICE ADC=10
#use delay (clock=20M)
#fuses NOLVP, NOPROTECT, HS, NOWDT, NOBROWNOUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

//!#define MODBUS_TYPE MODBUS_TYPE_SLAVE
//!#define MODBUS_SERIAL_RX_BUFFER_SIZE 40                                       
//!#define MODBUS_SERIAL_BAUD 38400               
//!
//!#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA                   
//!#define MODBUS_SERIAL_TX_PIN  PIN_F3                                 
//!#define MODBUS_SERIAL_RX_PIN  PIN_F2
//!#define MODBUS_SERIAL_ENABLE_PIN   PIN_C3   // Controls DE pin for RS485
//!
//!#include "modbus.c"
//!
//!#define MODBUS_ADDRESS 0x01

long temp = 0;
int1 int_flag = FALSE;
int1 edge_flag = TRUE;
int1 pulse_flag = FALSE;
static unsigned int16 counter = 0;
unsigned int16 ccp_delta = 0;
static unsigned int32 dc_counter = 0;
//!unsigned int16 freq  = 0;
unsigned int8 timer_counter = 0;//, timer3_counter = 0;
static unsigned int32 dc_volts = 0;
const int reg_num = 3;
unsigned int16 hold_regs[reg_num]={0x00,0x00,0x00};

#INT_CCP1
void ccp_isr()
{
   if(edge_flag)
   {
      counter = CCP_1;
      setup_CCP1(CCP_CAPTURE_FE);
      edge_flag = FALSE;
   }
   else
   {
      ccp_delta = CCP_1 - counter;
      setup_CCP1(CCP_CAPTURE_RE);
      edge_flag = TRUE;
      pulse_flag = TRUE;
   }
}

#INT_CCP2
void ccp2_isr()
{
   dc_counter++;   
}

#INT_TIMER3
void timer1_isr()
{
   set_timer3(0);
      timer_counter++;
      if(timer_counter == 10) //reading counter after 1 sec
      {
         int_flag = TRUE;
      }
}

void main()
{
   static unsigned int32 dc = 0;
   static unsigned int16 pulse  = 0;
   static unsigned int16 freq = 0;
   
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(ALL_ANALOG);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
   setup_timer_3(T3_INTERNAL | T3_DIV_BY_8);
//!   set_timer1(65535); // T = 1/fosc/4 * prescalar * (65535 - tmr)

   clear_interrupt(INT_CCP1);
   setup_CCP1(CCP_CAPTURE_RE);
   enable_interrupts(INT_CCP1);
   
   clear_interrupt(INT_CCP2);
   setup_CCP2(CCP_CAPTURE_DIV_16);
   enable_interrupts(INT_CCP2);
   
   clear_interrupt(INT_TIMER3);
   enable_interrupts(INT_TIMER3);
   
   enable_interrupts(GLOBAL);
   
//!   modbus_init();
   
   while(1)
   {
     
      set_adc_channel(0);
      delay_us(100);
      temp = (read_adc()*4.88)/10;

      if(int_flag)
      {
         dc_volts = dc_counter;
         dc_counter = 0;
         timer_counter = 0;
         int_flag = FALSE;
      }
     
      if(pulse_flag)
      {
      disable_interrupts(INT_CCP1);
      pulse = ccp_delta;
      enable_interrupts(INT_CCP1);
      freq = (6250000/(pulse*2));
      pulse_flag = FALSE;
      }
     
      dc = ((dc_volts*6631)/100000); delay_us(200); //6631   
     
//!      hold_regs[0] = temp;
//!      hold_regs[1] = freq;
//!      hold_regs[2] = dc;
     
//!      //MODBUS CODE//
//!      if(modbus_kbhit())
//!      {
//!         //check address against our address, 0 is broadcast
//!         if((modbus_rx.address == MODBUS_ADDRESS) || modbus_rx.address == 0)
//!         {     
//!            switch(modbus_rx.func)
//!            {                                             
//!               case FUNC_READ_HOLDING_REGISTERS:
//!               if(modbus_rx.data[0] || modbus_rx.data[2] ||  modbus_rx.data[1]>=reg_num ||
//!                  modbus_rx.data[3]+modbus_rx.data[1]>reg_num)
//!               {                         
//!                  modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
//!               }
//!               else                                                 
//!               {   
//!                  modbus_read_holding_registers_rsp(MODBUS_ADDRESS,(modbus_rx.data[3]*2),
//!                  hold_regs+modbus_rx.data[1]);
//!               }                                                                     
//!               break;                                     
//!               
//!               default:    //We don't support the function, so return exception
//!               modbus_exception_rsp(MODBUS_ADDRESS,modbus_rx.func,ILLEGAL_FUNCTION);
//!            }                                     
//!         }
//!      }
//!      //MODBUS CODE END//

      printf("temp: %lu, freq: %lu, dc: %lu\r\n", temp, freq, dc);
   }
}


hey all, i am trying to read ac line frequency but there is a problem when I'm varying AC volts (using a variac) the frequency that I'm reading is also changing. Same thing is happening on simulation as well. Can someone take a look at my code and tell me what i am doing wrong ? i have also attached schematic. Thanks in advance.

Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Jul 31, 2018 12:29 am     Reply with quote

You need to get rid of the capacitor on the input side. Use a bridge rectifier to feed the opto. Capacitors introduce lag. How this delays the opto switching point will vary with the input voltage.
Simplest circuit is:
<http://www.emergingtechs.org/p/zer.html>
Obviously adjust the resistor used based on the voltage involved. Your 10K total, suggests you are measuring a relatively low voltage, the example given, is working directly from mains.
This is surprisingly good.
A slightly better circuit is this one:
<https://www.edn.com/design/analog/4368740/Mains-driven-zero-crossing-detector-uses-only-a-few-high-voltage-parts>

Beware in all cases the operational voltage rating of the resistors involved.
bliztkrieg



Joined: 23 Jul 2018
Posts: 20

View user's profile Send private message

PostPosted: Tue Jul 31, 2018 12:41 am     Reply with quote

Ttelmah wrote:
You need to get rid of the capacitor on the input side. Use a bridge rectifier to feed the opto. Capacitors introduce lag. How this delays the opto switching point will vary with the input voltage.
Simplest circuit is:
<http://www.emergingtechs.org/p/zer.html>
Obviously adjust the resistor used based on the voltage involved. Your 10K total, suggests you are measuring a relatively low voltage, the example given, is working directly from mains.
This is surprisingly good.
A slightly better circuit is this one:
<https://www.edn.com/design/analog/4368740/Mains-driven-zero-crossing-detector-uses-only-a-few-high-voltage-parts>

Beware in all cases the operational voltage rating of the resistors involved.


Hey thanks for your response. I have got things working like they should but i have one little problem, when i cut off the AC supply the frequency should drop down to 0 right ? Well, it is not, it is showing me the last calculated value anything that i may have missed ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 31, 2018 1:51 am     Reply with quote

bliztkrieg wrote:

I have one little problem, when i cut off the AC supply the frequency
should drop down to 0 right ? Well, it is not, it is showing me the last
calculated value anything that i may have missed ?

Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=56439&start=5
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Jul 31, 2018 1:51 am     Reply with quote

No, it's not going to drop to zero.
The CCP doesn't trigger, so the last calculated value remains.

You need to add some sort of timeout to the loop. If 'pulse_flag' is not set in (say) twenty times round the loop, then set frequency to zero:
Code:

    int8 loopcnt=0;



   //then in the loop

      if(pulse_flag)
      {
         disable_interrupts(INT_CCP1);
         pulse = ccp_delta;
         enable_interrupts(INT_CCP1);
         freq = (6250000/(pulse*2));
         pulse_flag = FALSE;
         loopcnt=0;
      }
      else
      {
          if (loopcnt>=20)
              freq=0;
          else
              ++loopcnt;
      }

Every time pulse_flag is seen, loopcnt is set back to zero.
If it gets to 20 (the loop always takes over 100uSec), it'll set freq to zero. So >0.02 seconds without a pulse being seen.
bliztkrieg



Joined: 23 Jul 2018
Posts: 20

View user's profile Send private message

PostPosted: Tue Jul 31, 2018 3:30 am     Reply with quote

Ttelmah wrote:
No, it's not going to drop to zero.
The CCP doesn't trigger, so the last calculated value remains.

You need to add some sort of timeout to the loop. If 'pulse_flag' is not set in (say) twenty times round the loop, then set frequency to zero:
Code:

    int8 loopcnt=0;



   //then in the loop

      if(pulse_flag)
      {
         disable_interrupts(INT_CCP1);
         pulse = ccp_delta;
         enable_interrupts(INT_CCP1);
         freq = (6250000/(pulse*2));
         pulse_flag = FALSE;
         loopcnt=0;
      }
      else
      {
          if (loopcnt>=20)
              freq=0;
          else
              ++loopcnt;
      }

Every time pulse_flag is seen, loopcnt is set back to zero.
If it gets to 20 (the loop always takes over 100uSec), it'll set freq to zero. So >0.02 seconds without a pulse being seen.


Thanks it worked like a charm :D
kx9



Joined: 05 Nov 2013
Posts: 3
Location: portland OR

View user's profile Send private message

measuring 60hz frequency
PostPosted: Tue Sep 04, 2018 12:55 pm     Reply with quote

You might want to add a hysteresis gate and divide by 2 FF after opto. This will clean up the slow edges, filter noisy signals and will make the input signal voltage intolerant.
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