Search This Blog

Friday, November 30, 2018

Lightning Impulse Signal : Li Full and Li Chop simulation


Lightning Impulse Signal : Li Full and Li Chop simulation Code

Generate Li Full and Li Chop array points using Code (VB.net)

Li Full : T1 (Time to peak) 1.2uS
              T2 (Time to peak) 50uS
               Amplitude : 1 unit

Code:

        Dim k0 As Integer = 100
        Dim k1 As Single = 2.47
        Dim k2 As Single = 1 / 68.23
        Dim I As Single = 0
        IMPULSE_ARY(0) = 0
        Dim t As Single = 0.01

        ' IMPULSE_ARY(0-124)=0
        For I = 125 To 152
            IMPULSE_ARY(I) = 4.2 * t ^ 3
            t = t + 0.01
        Next
        t = 0.04
       ' exponential 
        For I = 150 To SAMPLES
            IMPULSE_ARY(I) = 1 / 0.9643 * (1 - Math.E ^ (-k1 * t)) * (Math.E ^ (-k2 * t))
            'IMPULSE_ARY(I) =  Math.E ^ (-k1 * t) - (Math.E ^ (-k2 * t))
            t = t + 0.01
        Next
        For I = 0 To SAMPLES Step 4
            IMPULSE_ARY(I) = IMPULSE_ARY(I) + (Rnd(1) - 0.5) * 0.0004
        Next
       ' plot this array and get 



















--------------------------------------------------------------------------------------------------------------------------

Li Chopped : T1 (Time to peak) 1.2uS
              Tc (Time to peak) 3.88uS
               Amplitude : 1 unit

Code:

        Dim k0 As Integer = 100
        Dim k1 As Single = 2.47
        Dim k2 As Single = 1 / 68.23
        Dim I As Single = 0
        Dim t As Single = 0.01
        ' IMPULSE_ARY(0-124)=0       
        For I = 125 To 152
            IMPULSE_ARY(I) = 4.2 * t ^ 3
            t = t + 0.01
        Next
        t = 0.04
        For I = 152 To 4000
            IMPULSE_ARY(I) = 1 / 0.9643 * (1 - Math.E ^ (-k1 * t)) * (Math.E ^ (-k2 * t))
            t = t + 0.01
        Next
        Dim A As Single = 450
        Dim B As Single = A + 27
        t = 0.01 * B
        Dim E_VAL As Single
        For I = B To B + 25
            IMPULSE_ARY(I) = IMPULSE_ARY(I) * Math.Cos(2 * (t - (B / 100))) * (Math.E ^ (-0.2 * (t - (B / 100))))
            t = t + 0.01
        Next
        t = t - 0.01
        For I = B + 25 To 4000
            E_VAL = (0.95 * Math.E ^ (-4 * (t - (B + 25) / 100))) + (0.05 * Math.E ^ (-0.3 * (t - (B + 25) / 100)))

            IMPULSE_ARY(I) = IMPULSE_ARY(B + 25) * E_VAL * Math.Cos(3 * (t - (B + 25) / 100))
            t = t + 0.01
        Next
        For I = 0 To 4000 Step 4
            IMPULSE_ARY(I) = IMPULSE_ARY(I) + (Rnd(1) - 0.5) * 0.0006
        Next
    ' plot this array and get



Thursday, November 29, 2018

ANALOG COMPARATOR C CODE cvAVR for Atmega32/Atmega16

ANALOG COMPARATOR C CODE cvAVR for Atmega32/Atmega16

Chip type               : ATmega32
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
*****************************************************/

#include
#include
// Alphanumeric LCD Module functions
// its customization
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include
void main(void)
{
    PORTA=0x00;
    DDRA=0x00;
    PORTB=0x00;
    DDRB=0x00;
    PORTC=0x00;
    DDRC=0xFF;
    PORTD=0x00;
    DDRD=0xFF;

Analog_init();
// LCD module initialization
lcd_init(16);

while (1)
      {
          if (ACSR.5 == 1)  {
                PORTD =0xFF;
            }
            else {
                PORTD =0x00;
            }
          delay_ms(100);
      };
}


//---------------------------- ANALOG COMPARATOR ----------------------//
void Analog_init(void) {
 
   ACSR |= (1 << ACBG); // enable internal reference voltage 1,1V 
   ACSR |= (1 << ACIE); // enable comparator interrupt

}

void Analog_end(void) {
 
   ACSR |= (1 << ACD);
}

ISR (ANALOG_COMP_vect) { 

   int ac1 = 0;
   int ac2 = 0;
   int k = 0;

   if (ACO == 0) {
     
      ac2 = ac1;

      ac1 = ACO;
   }
   else {
     
      ac2 = ac1;

      ac1 = ACO;
   }
   k++;

   if (k==6) {
     
      ac1=ACO;
     
      if (ac2==ac1) {
       
         DDRC <<1 nbsp="" p="">      }
      else {
       
         DDRC <<1 nbsp="" p="">         DDRC +=1;
      }
    k=1;
   }
 
   Analog_end();
 
   get_number(DDRC); 
}
//-------------------------- END ANALOG COMPARATOR----------------------//


Tuesday, November 27, 2018

BITS-wilp old PAPER

Few BITS WILP old  PAPER for M. TECH SOFTWARE SYSTEMS
Comprehensive and Regular Exams


SPWM - ATSAM3X8E Arduino UNO PWM Setup code

ATSAM3X8E Arduino UNO PWM Setup code

I have used this code for SPWM (sine wave PWM) with dead time
sine wave table generated separately and duty cycle of PWM is updated after particular interrupt.
Only setting up pwm with top, inverting with dead time is given below.
---------------------------------------------------------------------------------
void pwmSetup() 
  {
    //Configure PWM channels 0,1,2,3 PWML0,PWMH0,PWML1,PWMH1,PWML2,PWMH2,PWML3,PWMH3),
( arduino pins P34,P35,P36,P37,P38,P39,P40,P41)

    REG_PIOC_PDR = 0x3FC;  //B1111111100,
    REG_PIOC_ABSR = REG_PIOC_ABSR | 0x3FCu; //B1111111100,
    REG_PMC_PCER1 = REG_PMC_PCER1 | 16;

 //Register 1 (activate clock for PWM, id36, bit5 of PMC_PCSR1)
    REG_PWM_ENA = REG_PWM_SR | B1111; //PWM Enable Register | PWM
    PMC->PMC_PCER1 |= PMC_PCER1_PID36;                // PWM power ON
    PWM->PWM_DIS = PWM_DIS_CHID2;                         //

      PWM->PWM_CH_NUM[2].PWM_CMR = PWM_CMR_CPRE_CLKB;   
      PWM->PWM_CH_NUM[2].PWM_CPRD = 2000;                   //
      PWM->PWM_CH_NUM[2].PWM_CDTY = 100;                    //
      PWM->PWM_IER1 = PWM_IER1_CHID2;                       //
 
      PWM->PWM_ENA = PWM_ENA_CHID2;                         //

      REG_PIOC_PDR = 0x3FC;                     //B1111111100, PIO
      REG_PIOC_ABSR = REG_PIOC_ABSR | 0x3FCu;   //B1111111100,
 
      REG_PMC_PCER1 = REG_PMC_PCER1 | 16; //Peripheral Clock Enable
      REG_PWM_ENA = REG_PWM_SR | B1111;

PWM_SCM_SYNC5 | PWM_SCM_SYNC4 | PWM_SCM_SYNC3 | PWM_SCM_SYNC2 |
PWM_SCM_SYNC1 | PWM_SCM_SYNC0; // Set the PWM channels as sync
   
      REG_PWM_CMR0 = 0x10100; //Channe0 Mode Register: Dead Time
      REG_PWM_CMR1 = 0x10100; //Channe1 Mode Register: Dead Time
      REG_PWM_CMR2 = 0x10100; //Channe2 Mode Register: Dead Time
      REG_PWM_CMR3 = 0x10100; //Channe3 Mode Register: Dead Time

      //dead time calc according to if clk is 84Mhz
      // 0xFC00FC  - 3us       // 0xA800A8  - 2us    // 0x540054  -
      //       1us      //0x1500150  -  4us        //  0x01680168   - 5us   make   all same
      REG_PWM_DT0 = 0xFC00FC;  // 0xA800A8; // 2us  ;//0x1500150;
      REG_PWM_DT1 = 0xFC00FC;  // 0xA800A8;   ;//0x01680168;//
      REG_PWM_DT2 = 0xFC00FC;  // 0xA800A8;   ;//0xA800A8;
      REG_PWM_DT3 = 0xFC00FC;  // 0xA800A8;   ;//0xA800A8;
 
// top has to decide what should be your interupt rate
      REG_PWM_CPRD0 = top; //Cha0nne0 Period Register
      REG_PWM_CPRD1 = top; //Channe1 Period Register
      REG_PWM_CPRD2 = top; //Channe2 Period Register
      REG_PWM_CPRD3 = top; //Channe3 Period Register


      PWM->PWM_IER1 = PWM_IER1_CHID2;                       //
//Interrupt on Channel 2 Counter match
      NVIC_EnableIRQ(PWM_IRQn);                             //

  }

Monday, November 26, 2018

CRC C Code

// CRC C CODE USED FOR MODBUS COMMUNICATION
unsigned int CRC(unsigned char intSend[], int len)
{
int int_c = 0xFFFF;
int int_l;

int int_i=0, int_j=0;

for (int_i = 0; int_i < len; int_i++)
{
//intSend[int_i] = intSend[int_i] & 0x00FF;

int_c = int_c ^  intSend[int_i]  ;
for ( int_j = 0; int_j < 8; int_j++)
{
int_l = int_c & 0x0001;  // Mask of LSB
int_c = int_c >> 1;
int_c = int_c & 0x7FFF;  //0x7FFF
if (int_l == 1)
int_c = int_c ^ 0xA001;
}
}
//int_crc_byte_a = int_c & 0x00FF;
//int_crc_byte_b = (int_c >> 8) & 0x00FF;
return int_c;
}

Saturday, November 24, 2018

PWM Atmega32:Inverter Mode Register Settings

SPWM

  // INVERTED MODE
TCCR1A = _BV(COM1A0) | _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);

// NON INVERTED MODE                    // set on match
// TCCR1A =   _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(WGM12);

/* start with a duty */
OCR1A = 0;
OCR1B = 0;

/* enable interrupt on timer overflow */
TIMSK = _BV(TOIE1);
/* enable interrupt      */
 sei();

TCCR1B |= _BV(CS00);

These can be used as bipolar settings.
Use sine table entries and update OCR1A/B after a particular time/interupt.

Sine wave points dc (Duty cycle array) (top - TOP for timer)
for(j=0;j {
   val = (j  * PI)/points;
   val = 1000 * sin(val) + 1000;
   val = val/2000;
   val = (val-0.5)* percentage + 0.5;
   dc[j]=top * val;
}