This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553: Ac Extension box (defense Project)

Part Number: MSP430G2553

Dear Sir,

One of my client doing Ac Extension box (defense Project). Now developing 5 boards for testing ... each board read frequency counts varied. we try that lot of way but still same issue coming .... Other parameters reading properly...

we are using 1 MHz clock for all , and timer A using SMCLK(1MHZ)  capture mode (TA1.0)

Please give the solution . i am also attached circuit and codes.

   

Best Regards,

Muthu S 

//************************Project Name : JUNCTION BOX **************************
//************************Start Date   : 09/11/17     **************************
//************************Author  Name : B.Hariprasath & Enphiniti Team*********
//************************Client Name  : Enphiniti Engineering ****************
//******************************************************************************
//Version History
//Version 1.0   09/11/17
//Low voltage and high voltage read via adc
//low earthleak and high earth leak read via adc
//low load current and high load current read via adc or i/o pins
//low frequency and high frequency read via timer capture method (1Mhz frequency)
//==============================================================================
             //Header file added
#include <msp430g2553.h>
#include "main.h"
#include <math.h>
//==============================================================================
           // Pre define variable added

//board 1 frequency 
#define  LOW_FREQUENCY   21276 
#define  HIGH_FREQUENCY  18867 // Timer capture falling edge
/*
//board 2 frequency
#define  LOW_FREQUENCY   21276 
#define  HIGH_FREQUENCY  18867 // Timer capture falling edge

//board 3 frequency 
#define  LOW_FREQUENCY   21276 
#define  HIGH_FREQUENCY  18867 // Timer capture falling edge
//board 4 frequency
#define  LOW_FREQUENCY   21276 
#define  HIGH_FREQUENCY  18867 // Timer capture falling edge

//board 5 frequency
#define  LOW_FREQUENCY   21276 
#define  HIGH_FREQUENCY  18867 // Timer capture falling edge
*/
//==============================================================================
#define  LOW_VOLTAGE     620   //low voltage -200v ,ADC count Max =1023  
#define  HIGH_VOLTAGE    790   // High voltage-250V
#define  SET_LOAD        475  // Max load 16A 
#define  EARTHLEAK       650   // Earth Lead MAx 5v

#define  RESET_SWITCH    P1IN & BIT5
//==============================================================================
                 // interrupt 
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void);
#pragma vector=TIMER1_A0_VECTOR
__interrupt void Timer1_A (void);
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void);
//==============================================================================
            //software bool variable
_Bool SET_FREQUENCY;
_Bool VOLTAGE_RELAY,EARTHLEAK_RELAY,LOAD_RELAY,FREQUENCY_RELAY;
_Bool OVER_LOAD_STATUS;
//==============================================================================
          //Variable declaration
unsigned int Adc_dataout[6],temp_voltage,temp_earth;
unsigned char Adc_count,capture_count;
unsigned int avr_earthvalue, avr_load, avr_voltage;
unsigned int frequency_value,over_load,voltage,earth_leak;
unsigned int freq_timer_count,first_rise_count,second_rise_count,buffer[4];
long total_frequency;

//long first_frequency,second_frequency; 
//==============================================================================
int main()
{
    
    WDTCTL = WDTPW + WDTHOLD;
    System_Initialize();
    variable_inialize();
    
   capture_count=0;
    //Power on Initialize
    RELAY_OFF;
    UNDER_VOLTAGE_LED_OFF;
    OVER_VOLTAGE_LED_OFF;
    EARTH_LEAKAGE_LED_OFF;
    UNDER_FREQUENCY_LED_OFF;
    OVER_FREQUENCY_LED_OFF;
    OVER_LOAD_LED_OFF;
    ALL_STATUS_LED_ON;
    time_delay(3000);    
    Adc_4channel_read();
    Adc_4channel_read();
    Adc_4channel_read();
    RELAY_ON;

    
   SET_FREQUENCY=0;
   freq_timer_count=0;
   CCTL1 = CCIE;
    
    while(1)
    {
      
       if(!(RESET_SWITCH))  // its overload reset switch
           OVER_LOAD_STATUS=0;
         
       time_delay(50);
       adc_data_monitoring();
   //    time_delay(100);
      
       
       if(!(OVER_LOAD_STATUS))
       {
        if(SET_FREQUENCY)
         {
           SET_FREQUENCY=0;
          frequency_validate();
           
         }
       }  
    
    }
  
} 
//==============================================================================
void frequency_validate(void)
{
  
    if(frequency_value < HIGH_FREQUENCY)
    {
       RELAY_OFF;
       OVER_FREQUENCY_LED_ON;
       UNDER_FREQUENCY_LED_OFF;
       ALL_STATUS_LED_OFF;
       FREQUENCY_RELAY=1;
      
    }
    
    else if(frequency_value > LOW_FREQUENCY)
    {
       RELAY_OFF;
       OVER_FREQUENCY_LED_OFF;
       UNDER_FREQUENCY_LED_ON;
       ALL_STATUS_LED_OFF;
       FREQUENCY_RELAY=1;
      
    }
  
    else //if((frequency_value > HIGH_FREQUENCY)&&(frequency_value < LOW_FREQUENCY))
    {
      if((VOLTAGE_RELAY)||(EARTHLEAK_RELAY)||(LOAD_RELAY))
      {
        RELAY_OFF;
        ALL_STATUS_LED_OFF;
        
      }
      else
      {
         RELAY_ON;
         ALL_STATUS_LED_ON;
      }
      FREQUENCY_RELAY=0;
      OVER_FREQUENCY_LED_OFF;
      UNDER_FREQUENCY_LED_OFF;
  
    }
   
   
}
//==============================================================================
void time_delay(int invalue) 
{	

	 unsigned int i;
	do
	{
        	for (i=0;i<500;i++);

	}while(--invalue);

}
//------------------------------------------------------------------------------
void variable_inialize(void)
{
  
  Adc_dataout[0] =0;
  Adc_count=0;
  avr_earthvalue=avr_load= avr_voltage=0;//avr_frequency=0;
  frequency_value=0;
  freq_timer_count=0;
  SET_FREQUENCY=0;
  OVER_LOAD_STATUS=0;
  VOLTAGE_RELAY=EARTHLEAK_RELAY=LOAD_RELAY=FREQUENCY_RELAY=0;
  capture_count=0;
  //first_frequency=second_frequency=0;
   first_rise_count = second_rise_count = 0;
   temp_voltage=0;
   temp_earth=0;
   total_frequency=0;
}

//==============================================================================
void System_Initialize()
{
  Port_Initialize();
  Clock_Initialize();
  TimerA_Initialize();
  Adc_10init();
   
}
//==============================================================================
void  Port_Initialize(void)
{
    P1SEL = 0x00;
    P1DIR = 0xC0;  
    P1REN |= BIT5;
    
 
    
    P2SEL = 0x01;
    P2DIR = 0xFE;
 
}
//==============================================================================
void Clock_Initialize(void)
{
 
  if (CAL_BC1_1MHZ==0xFF)	// If calibration constant erased
  {											
    while(1);                // do not load, trap CPU!!	
  }
  
  DCOCTL = 0;                // Select lowest DCOx and MODx settings
  BCSCTL1 = CAL_BC1_1MHZ;    // Set DCO
  DCOCTL =  CAL_DCO_1MHZ;   // smclk - 12Mhz Aclk -12MHZ
}
//==============================================================================
void TimerA_Initialize(void) //timer set 100us contmode
{
  
    TA1CTL |= TASSEL_2 + MC_2 ;    // SMCLK + Continuous mode
    TA1CCTL0 |= CM_1 + CCIS_0 +SCCI+ SCS + CAP + CCIE ;    // Raising Edge + CCI0A + Sync + Capture Mode + Interrupt enable
   
    _BIS_SR(GIE); //SMCLK active + Interrupts
} 
//==============================================================================
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0(void)
{
/*
  freq_timer_count++;
  freq_timer_count &= 0x03;
  
  if(freq_timer_count == 1)
  {
       first_rise_count=TA1CCR0;
  }
  else if(freq_timer_count == 2)
  { 
     second_rise_count = TA1CCR0;

     TA1CCR0=0;
     TA1CTL &= ~TAIFG;
     TA1CCTL0 &= ~CCIFG;
     frequency_value = second_rise_count - first_rise_count;
     SET_FREQUENCY=1;
  } 
*/
     
    first_rise_count = TA1CCR0;    //TACCR1;
    
    if(first_rise_count < second_rise_count)
    {
        buffer[capture_count++] = ((0xFFFF - second_rise_count) + first_rise_count);
    }
    else
    {
        buffer[capture_count++] = first_rise_count - second_rise_count;            
    }
    second_rise_count = first_rise_count;    
    
    if (capture_count>4)
    {
     
    
     buffer[0] = buffer[3];
     for(char i=0;i<4;i++)
     total_frequency += buffer[i];
     
     frequency_value = total_frequency / 4;
     SET_FREQUENCY=1;
     buffer[0] = 0;
     total_frequency=0;
     capture_count=0;
    }
    
    
      TA1CTL &= ~TAIFG;
      TA1CCTL0 &= ~CCIFG;   
  
}
//==============================================================================
void Adc_10init(void)
{
  
  ADC10CTL1 = INCH_5 + CONSEQ_1;            // A5/A4/A3/A2/A1/A0, single sequence
  ADC10CTL0 = ADC10SHT_2 | MSC | ADC10ON;   //+ ADC10IE;
  ADC10DTC1 = 0x06;                         // 5 conversions
  ADC10AE0 |= 0x1F;                         // P1.5,4,3,2,1,0 ADC10 option select
  __delay_cycles(75);
      
  
}
//==============================================================================

void Adc_4channel_read(void)
{
   ADC10CTL0 |= ENC + ADC10SC;      // Sampling and conversion start
   while (ADC10CTL1 & BUSY);          // Wait if ADC10 core is active
   ADC10SA = (int)Adc_dataout;           // Data buffer start
   ADC10CTL0 &= ~ENC;
   adc_delay();
   adc_delay();
   adc_delay();
}
//==============================================================================
void adc_delay(void)
{
  unsigned char i;
  for(i=0;i<15;i++)
  {
        _NOP();
        _NOP();
        _NOP();
        _NOP();
        _NOP();
        
  }     
}
//==============================================================================
void adc_data_monitoring(void)
{
    //VOLTAGE_RELAY=EARTHLEAK_RELAY=LOAD_RELAY=FREQUENCY_RELAY=0;


   if(Adc_count < 16)
   {
         Adc_4channel_read();
         
         if(Adc_dataout[5] > temp_voltage)
         {
            temp_voltage = Adc_dataout[5];
            avr_voltage    = Adc_dataout[5];   //A0

         }
                   
        // avr_voltage    += Adc_dataout[5];   //A0
         avr_load  += Adc_dataout[2];   //A3
         //avr_earthvalue += Adc_dataout[1];   // A4
         if(Adc_dataout[1] > temp_earth)
         {
           temp_earth = Adc_dataout[1];
           avr_earthvalue =Adc_dataout[1]; 
         }
       //avr_frequency    += Adc_dataout[0]; // A5
         Adc_count++;
   }
   else
   {
        Adc_count=0;
        temp_voltage=0;
        temp_earth=0;
        over_load  = avr_load >> 4;
        earth_leak  = avr_earthvalue;     //avr_earthvalue >> 2;
        voltage = avr_voltage  ;  //;>> 3;
       if(!(OVER_LOAD_STATUS))
       {
         if(over_load > SET_LOAD)
         {
           ALL_STATUS_LED_OFF;
           OVER_LOAD_LED_ON;
           OVER_LOAD_STATUS =1;
           LOAD_RELAY=1;
         }
         else
         {
           if((VOLTAGE_RELAY)||(EARTHLEAK_RELAY)||(FREQUENCY_RELAY))
            { 
               RELAY_OFF;
               ALL_STATUS_LED_OFF;
            }
           else
            {
             RELAY_ON;
             ALL_STATUS_LED_ON;
            }
            LOAD_RELAY=0;
            OVER_LOAD_LED_OFF;
         } 
       }
         
        // earth leakage checking
        
        
        if(earth_leak > EARTHLEAK)
        {
           RELAY_OFF;
           ALL_STATUS_LED_OFF;
           EARTH_LEAKAGE_LED_ON;
           EARTHLEAK_RELAY=1;
        }
        else
        {
           if((LOAD_RELAY)||(VOLTAGE_RELAY)||(FREQUENCY_RELAY))
           { 
             RELAY_OFF;
             ALL_STATUS_LED_OFF;
           }
           else
            {
              RELAY_ON;
              ALL_STATUS_LED_ON;
            }   
             EARTHLEAK_RELAY=0;   
             EARTH_LEAKAGE_LED_OFF;
             
        }
      
     //over & under voltage checking
        
        
        if((voltage  > LOW_VOLTAGE )&& (voltage < 670)&&(voltage > 100))  // low  voltage -200v
          {
          
           RELAY_OFF;
           ALL_STATUS_LED_OFF;
           UNDER_VOLTAGE_LED_ON;   
           OVER_VOLTAGE_LED_OFF;
           VOLTAGE_RELAY=1;
          }
        else if ((voltage > HIGH_VOLTAGE) && ( voltage < 840))// high voltage  250 sample value
          {
             RELAY_OFF;
             ALL_STATUS_LED_OFF; 
             OVER_VOLTAGE_LED_ON;
             UNDER_VOLTAGE_LED_OFF;
             VOLTAGE_RELAY=1;
          }
        else
          {
            if((LOAD_RELAY)||(EARTHLEAK_RELAY)||(FREQUENCY_RELAY))
             {
               RELAY_OFF;
               ALL_STATUS_LED_OFF;
             }
            else
             {
              RELAY_ON;
              ALL_STATUS_LED_ON;
             }
            VOLTAGE_RELAY=0;
            UNDER_VOLTAGE_LED_OFF;   
            OVER_VOLTAGE_LED_OFF;
          
          }
        
        
        // average value should be cleared 
        //avr_frequency =0;  
        avr_load =0; 
        avr_earthvalue=0;
        avr_voltage=0;
        
        
    }
      
        
 }
  
//==============================================================================
5314.main.h

 

**Attention** This is a public forum