| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 
 | #include <16F876A.h>
#include <float.h>
#include <math.h>
//#device *=16 ADC=8 //16bit pointer, 8-bit ADC
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,PUT,NOWRT,BROWNOUT,NOLVP
#define TRISA 0b00000000
#define TRISB 0b00000000
#define TRISC 0b00000000
#define Port_Ai 0b00000000 //Initial valua for Port A
#define Port_Bi 0b00000000 //Initial valua for Port B
#define Port_Ci 0b00000000 //Initial valua for Port C
typedef struct {
    int1 bit0;
    int1 bit1;
    int1 bit2;
    int1 bit3;
    int1 bit4;
    int1 bit5;
    int1 bit6;
    int1 bit7;
} flags;
//Important time variables
float ts, ta, tb, t0, tmag; //time related variable
//Important phase variables
float phase, phaset1; //to identify sector number
float phaset2, thetha, thetha1, thetha2; //to calculate related time variables
float modin; //modulation index
float ZZ; //to convert angles larger than 360 degrees into the scale of 0 to 360 degrees
//Important voltage related variables
float vref, vdc; //to calculate modulation index
//Other variables
int8 i, j, h; //counter
float nsamp, result1; //condition for one complete cycle
int8 n; //sector number
//Variables for Rounding up Process
float result, integral, integral1, XX; // variables for rounding up process
int8 YY, integral2;
float testa1;
//Interrupt Bit
flags sys01;
#define flg_10ms sys01.bit0
//Arrays of Outputs of Each Sector (1 to 6)
int8
outta1[8]={0b00000000,0b00000100,0b00000110,0b00000111,0b00000111,0b00000110,0b00000100,
0b00000000}; //test mar
int8
outta2[8]={0b00000000,0b00000010,0b00000110,0b00000111,0b00000111,0b00000110,0b00000010,
0b00000000}; //test mar
int8
outta3[8]={0b00000000,0b00000010,0b00000011,0b00000111,0b00000111,0b00000011,0b00000010,
0b00000000}; //test mar
int8
outta4[8]={0b00000000,0b00000001,0b00000011,0b00000111,0b00000111,0b00000011,0b00000001,
0b00000000}; //test mar
int8
outta5[8]={0b00000000,0b00000001,0b00000101,0b00000111,0b00000111,0b00000101,0b00000101,
0b00000000}; //test mar
int8
outta6[8]={0b00000000,0b00000100,0b00000101,0b00000111,0b00000111,0b00000101,0b00000100,
0b00000000}; //test mar
int8 timema[8];
//Initialization
void init(){
    setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
    // enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
    enable_interrupts(INT_TIMER1);
}
//Function to transfer output from array to output port
void showout(){
 
    if(n == 1)
    {
        for(j=0;j<8;j++)
        {
            output_b(outta1[j]);
            delay_ms(timema[j]);
        }
    }
    if(n == 2)
    {
        for(j=0;j<8;j++)
        {
            output_b(outta2[j]);
            delay_ms(timema[j]);
        }
    }
    if(n == 3)
    {
        for(j=0;j<8;j++)
        {
            output_b(outta3[j]);
            delay_ms(timema[j]);
        }
    }
    if(n == 4)
    {
        for(j=0;j<8;j++)
        {
            output_b(outta4[j]);
            delay_ms(timema[j]);
        }
    }
    if(n == 5)
    {
        for(j=0;j<8;j++)
        {
            output_b(outta5[j]);
            delay_ms(timema[j]);
        }
    }
    if(n == 6)
    {
        for(j=0;j<8;j++)
        {
            output_b(outta6[j]);
            delay_ms(timema[j]);
        }
    }
}
 
//Function to round up ta, tb and t0
void roundup()
{
    result=modf(XX,&integral);
    YY = integral;
    if ((result >=0) && (result <0.5))
    {
        YY = YY + 0;
    }
    else{
        if ((result >= 0.5) && (result < 1))
        {
            YY = YY + 1;
        }
    }
}
//Function to identifying sector number
void idsec()
{
    for(i=1;i<=6;i++)
    {
        phaset1 = i*3.142/3;
        phaset1 = phaset1 - phase;
        phaset2 = phase - (i - 1)*3.142/3;
        if((phaset1 >=0) && (phaset2 >=0))
        {
            n = i;
        }
    }
}
//Function to collect and re-insert rounded up numbers for important time variables
void rdjob()
{
    XX = ta;
    roundup();
    ta = YY;
    XX = tb;
    roundup();
    tb = YY;
    XX = t0;
    roundup();
    t0 = YY;
}
//Function to obtain input details
void in_put()
{
    vdc = 10;
    vref = 10;
    //fundamental frequency of 10 Hz (100 samples)
    phase = 2*3.142*h*0.01;
}
// Calculation of time ta, tb, ts and t0
void tcal()
{
    ts = 1000/100;
    thetha = phase;
    thetha1 = (n*3.142/3) - thetha;
    thetha2 = thetha - (n - 1)*3.142/3;
    tmag = (sqrt(3))*(modin)*ts/4; //moin=vref1/vdc1
    ta = (tmag)*(sin(thetha1)); //test time
    tb = (tmag)*(sin(thetha2)); //test time
    t0 = ts - ta - tb;
    t0 = t0/2;
    rdjob();
    //Putting times into timema array
    timema[0]=t0;
    timema[3]=t0;
    timema[4]=t0;
    timema[7]=t0;
    timema[1]=ta;
    timema[6]=ta;
    timema[2]=tb;
    timema[5]=tb;
}
//Main function
void main()
{
    Mainloop:
 
    nsamp = 100;
 
    result1=modf(nsamp,&integral1);
 
    integral2=integral1+1;
 
    h=1;
    do
    {
 
        in_put();
        idsec();
        tcal();
        h++;
    } while (h < integral2);
 
    goto Mainloop;
 
} | 
Partager