Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Micro c to ccs c code conversion

Status
Not open for further replies.

sahu

Member
pl help me about code conversion Micro c to ccs c.
PHP:
/*
*  PIC16F873A Pin Configuration
* 
* O = Output
* I = Input
*
*                           ________
*                      MCLR | 1  28 | RB7 --> (O) == DIGIT1 CC 
*          ADC1(I) --> AN0  | 2  27 | RB6 --> (O) == G CA
*          ADC2(I) --> AN1  | 3  26 | RB5 --> (O) == F CA
* DIGIT2 CC == (O) <-- RA2  | 4  25 | RB4 --> (O) == E CA
*          ADC3(I) --> AN3  | 5  24 | RB3 --> (O) == D CA
*    Delay_SW  (I) --> RA4  | 6  23 | RB2 --> (O) == C CA
* DIGIT3 CC == (O) <-- RA5  | 7  22 | RB1 --> (O) == B CA
*                      Vss  | 8  21 | RB0 --> (O) == A CA
*                      OSC1 | 9  20 | Vdd
*                      OSC2 |10  19 | Vss
*        RL4 == (O) <-- RC0 |11  18 | RC7 --> (O) == LED_HILO
*        RL3 == (O) <-- RC1 |12  17 | RC6 --> (O) == LED_NORMAL
*        RL2 == (O) <-- RC2 |13  16 | RC5 --> (0) == LED_DELAY
*        RL1 == (O) <-- RC3 |14  15 | RC4 --> (0) ==
*                           ---------
* Program description and features:
*    - Voltage Stabilizer
*    - Auto calibration save upon first manual calibration
*    - Saved in internal EEPROM
*    - Display on 3-digit 7 SEGMENT display
*/

sbit Relay1 at RC3_bit;
sbit Relay2 at RC2_bit;
sbit Relay3 at RC1_bit;
sbit Relay4 at RC0_bit;

sbit LED_DELAY at RC5_bit;
sbit LED_NORMAL at RC6_bit;
sbit LED_HILO at RC7_bit;

sbit C2 at RA2_bit;
sbit C3 at RA5_bit;
sbit C1 at RB7_bit;

sbit Delay_SWITCH at RA4_bit;
const unsigned char ADC_0 = 0;//Vin_CHANNEL
const unsigned char ADC_1 = 1;//CalibratePot_CHANNEL
const unsigned char ADC_3 = 3;//CalibrateSw_CHANNEL

volatile unsigned char IntCount;
volatile unsigned char flagReg;
sbit flg2ms at flagReg.B0;
sbit flgAC at flagReg.B1;
sbit change at flagReg.B2;
sbit normal at flagReg.B3;
sbit flgStart at flagReg.B0;

#define PRESSED 0
#define UNPRESSED 1
const unsigned char EE_ADDRESS = 0x06;  // EEPROM address for saving calibration
const unsigned char CALIBRATED = 123;

float Kc;                               // "Constant" for calibration
unsigned int Kc_INT;                    // To save to EEPROM, = Kc * 10000
unsigned int Kc_INT_HIGH;               // High byte for Kc_INT
unsigned int Kc_INT_LOW;                // Low byte for Kc_INT
float Voltage_FLOAT;
unsigned int Voltage_INT;
unsigned int oldVoltage;

unsigned char SEGMENT;
const unsigned char COMMON_CATHODE = 0;
const unsigned char COMMON_ANODE = 1;
const unsigned char COMMON = COMMON_CATHODE;

float calibrator;

#define INTAP_LOW 0                         // 165V
#define INTAP_MED 1                         // 190V
#define INTAP_HI  2                         // 240V

#define OUTTAP_LOW 0                        // 215V
#define OUTTAP_HI  1                        // 240V

unsigned char INPUT_TAPPING;
unsigned char OUTPUT_TAPPING;

unsigned char old_INPUT_TAPPING;
unsigned char old_OUTPUT_TAPPING;

unsigned char oldInput;

unsigned int V_DIFFERENCE;

#define STARTED 1
#define FIRST_TIME 0

#define HYSTERESIS 3

const unsigned char AnodeDriveSegment[10] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152};
/*  These are the values that need to be sent to the seven segment [A..G] for
*      a common anode display. These values have been obtaned using the
*      Mikroelektronika seven segment editor provided as part of the
*      mikroC/mikroBASIC/mikroPASCAL compiler.
*  The corresponding values for the common cathode display are:
*      CathodeDriveSegment = 255 - AnodeDriveSegment;
*/

unsigned char DriveSegment[10];

void UpdateDisplay(void){
     unsigned int SendVal;
     unsigned char Digit;
     if (flg2ms){
        SEGMENT++;
        if (SEGMENT > 3) SEGMENT = 1;

        C1 = 0; C2 = 0; C3 = 0;

        switch (SEGMENT){
               case 1:
                    Digit = (unsigned char) (Voltage_INT / 100);
                    break;
               case 2:
                    Digit = (unsigned char) ((Voltage_INT / 10) % 10);
                    break;
               case 3:
                    Digit = (unsigned char) (Voltage_INT % 10);
                    break;
        }

        SendVal = DriveSegment[Digit] & 0x7F; // Make bit 7 zero

        PORTB = SendVal;

        switch (SEGMENT){
               case 1:
                    C1 = 1;
                    break;
               case 2:
                    C2 = 1;
                    break;
               case 3:
                    C3 = 1;
                    break;
        }
        flg2ms = 0;              // Clear flag
     }
}

void InitIO(void){
     PORTB = 0;
     TRISB = 0;
     PORTC = 0;
     TRISC = 0;
     ADCON1 = 4;            // RA0, RA1 and RA3 analog, rest digital
     PORTA = 0;
     TRISA = 0x1B;          // Inputs for ADC
     CMCON = 7;             // Disable comparator
}

void InitTimer2(void){
     T2CON = 3;
/*   Prescale 1:16
*   Timer off for now
*/
     PR2 = 124;
/*   With prescale 1:16 and at 4MHz clock,
*   this gives 2ms period.
*/
     TMR2IF_bit = 0;
     TMR2IE_bit = 1;       // Turn on TMR2 interrupt
     GIE_bit = 1;          // Turn on global interrupts
     PEIE_bit = 1;         // Turn on peripheral interrupts
     TMR2ON_bit = 1;       // Start TMR2
}

void InitDisplay(void){
     unsigned char i;
     if (COMMON == COMMON_ANODE){
        for (i = 0; i < 10; i++){
            DriveSegment[i] = AnodeDriveSegment[i];
        }
     }
     else{
          for (i = 0; i < 10; i++){
              DriveSegment[i] = 255 - AnodeDriveSegment[i];
          }
     }
}

void interrupt(void){
     if (TMR2IF_bit == 1){
     // Configure for 2ms interrupt
        flg2ms = 1;
        IntCount++;
        if (IntCount > 200){
           flgAC = 1;
           IntCount = 0;
        }
        TMR2IF_bit = 0;
     }
}

void DoTheCalibrationAndSave(void){
     unsigned char i;
     unsigned int CPot;

     while (ADC_Get_Sample(ADC_3) < 512){      // While Calibrate switch is PRESSED
           //Kc = (float)((ADC_Get_Sample(ADC_1) * 2.0) / 1023); // Get the calibration "scaling factor" between 0.00 and 2.00
           CPot = ADC_Get_Sample(ADC_1)/2 + 128;
           Kc = (float)(CPot * 2.0)/1023.0;

           Kc = (float) ((float)Kc * 10000.0);
           Kc_INT = (unsigned int) Kc;
           Kc = (float) ((float)Kc_INT / 10000.0);

           Voltage_FLOAT = (float)((float)ADC_Get_Sample(ADC_0) * 5.0 * 101.0 * Kc) / (1023.0 * 1.3836);

           UpdateDisplay();

           //Voltage_FLOAT = Voltage_FLOAT + 1.5;
           /* Use 100k and 1k resistors for voltage division
            * Divide by 1.3836 to convert from DC voltage to AC
            */
            Voltage_INT = (unsigned int) Voltage_FLOAT;

     }
     // Button has been released - so now, it's time to save the value of Kc
     //Kc = (float) ((float)Kc * 10000.0);
     //Kc_INT = (unsigned int) Kc;
     Kc_INT_HIGH = (Kc_INT >> 8);
     Kc_INT_LOW = (Kc_INT & 0xFF);
     //Kc = (float) ((float)Kc_INT / 10000.0);
     EEPROM_Write(EE_ADDRESS, CALIBRATED);      // Write value to indicate that calibration has been completed
     EEPROM_Write(EE_ADDRESS + 1, (unsigned char) Kc_INT_HIGH);
     EEPROM_Write(EE_ADDRESS + 2, (unsigned char) Kc_INT_LOW);
     /* Data has been saved to EEPROM as:
      * At address EE_ADDRESS, value CALIBRATED has been saved to indicate that calibration HAS occurred
      * Kc_INT = (unsigned int) (Kc * 10000);
      * Kc_INT_HIGH = upper byte of Kc_INT; Kc_INT_LOW = lower byte of Kc_INT;
      * At address EE_ADDRESS + 1, value of Kc_INT_HIGH is saved.
      * At address EE_ADDRESS + 2, value of Kc_INT_LOW is saved.
      */
}

void Calibrate(void){
     unsigned char EERead;
     EERead = EEPROM_Read(EE_ADDRESS);
     if (EERead == CALIBRATED){       // If Calibration has been done
        if (ADC_Get_Sample(ADC_3) < 512){          // If Calibrate switch is PRESSED
           DoTheCalibrationAndSave();
        }
        else{
             // Calibration has been done and Calibrate switch is UNPRESSED, meaning no new calibration will be done
             // So just get existing saved value of Kc from EEPROM and proceed
             Kc_INT_HIGH = EEPROM_Read(EE_ADDRESS + 1);
             Kc_INT_LOW = EEPROM_Read(EE_ADDRESS + 2);
             Kc_INT = (unsigned int) ((Kc_INT_HIGH << 8) | (Kc_INT_LOW)); // Put Kc_INT_HIGH and Kc_INT_LOW into a 16-bit variable
             Kc = (float) ((float)Kc_INT / 10000.0); // Get the float value of Kc
             Voltage_FLOAT = (float)((float)ADC_Get_Sample(ADC_0) * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0);
             //Voltage_FLOAT = Voltage_FLOAT + 1.5;
             Voltage_INT = (unsigned int) Voltage_FLOAT;
        }
     }
     else{
          while (ADC_Get_Sample(ADC_3) > 512);      // While Calibrate switch is UNPRESSED
          DoTheCalibrationAndSave();
     }
}

void Delay(void){
/*   If Delay Switch is pressed (Delay_SWITCH == 0), long Delay
*   Otherwise, short Delay
*/

unsigned char timeKeeper;

LED_DELAY = 1;

if (Delay_SWITCH == UNPRESSED){
    for (timeKeeper = 0; timeKeeper < 250; timeKeeper ++){
        delay_ms(720);          // 3 minutes Delay
    }
}
else{
      delay_ms(2000);         // 2 seconds delay
}
LED_DELAY = 0;
}

void StabilizeVoltage(void){
     unsigned int ADS;
     unsigned char V_LOW_CUT;
     if (flgAC){
        ADS = ADC_Get_Sample(ADC_0);
        Voltage_FLOAT = (float)((float)ADS * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0);
        //Voltage_FLOAT = Voltage_FLOAT + 1.5;
        Voltage_INT = (unsigned int) Voltage_FLOAT;

        if (normal == 1){
           V_LOW_CUT = 125;
        }
        else{
             V_LOW_CUT = 135;
        }

        if (Voltage_INT < V_LOW_CUT){              /*    Low Cut Check    */
           normal = 0;
           LED_HILO = 1;
           LED_NORMAL = 0;
           INPUT_TAPPING = INTAP_LOW;
           OUTPUT_TAPPING = OUTTAP_HI;
           oldVoltage = V_LOW_CUT;
           flgStart = FIRST_TIME;
        }
        else if (Voltage_INT > 270){         /*    High Cut Check   */
           LED_HILO = 1;
           LED_NORMAL = 0;
           normal = 0;
           INPUT_TAPPING = INTAP_HI;
           OUTPUT_TAPPING = OUTTAP_LOW;
           oldVoltage = 270;
        }
        else{                                /* Normal --> Stabilize */
            normal = 1;
            LED_HILO = 0;
            LED_NORMAL = 1;
            if (Voltage_INT > oldVoltage){
               V_DIFFERENCE = Voltage_INT - oldVoltage;
            }
            else{
                 V_DIFFERENCE = oldVoltage - Voltage_INT;
            }
            if (V_DIFFERENCE >= HYSTERESIS){
                if (Voltage_INT < 157){
                   INPUT_TAPPING = INTAP_LOW;
                   OUTPUT_TAPPING = OUTTAP_HI;
                   oldVoltage = 157;
                }
                else if (Voltage_INT < 175){
                   INPUT_TAPPING = INTAP_LOW;
                   OUTPUT_TAPPING = OUTTAP_LOW;
                   oldVoltage = 175;
                }
                else if (Voltage_INT < 183){
                   INPUT_TAPPING = INTAP_MED;
                   OUTPUT_TAPPING = OUTTAP_HI;
                   oldVoltage = 183;
                }
                else if (Voltage_INT < 202){
                   INPUT_TAPPING = INTAP_MED;
                   OUTPUT_TAPPING = OUTTAP_LOW;
                   oldVoltage = 202;
                }
                else if (Voltage_INT < 235){
                   INPUT_TAPPING = INTAP_HI;
                   OUTPUT_TAPPING = OUTTAP_HI;
                   oldVoltage = 235;
                }
                else{
                   INPUT_TAPPING = INTAP_HI;
                   OUTPUT_TAPPING = OUTTAP_LOW;
                   oldVoltage = 215;
                }
            }
        }
        if (INPUT_TAPPING == INTAP_LOW){
           Relay1 = 0;
           Relay2 = 0;
        }
        else if (INPUT_TAPPING == INTAP_MED){
             Relay1 = 1;
             Relay2 = 0;
        }
        else{
             Relay1 = 1;
             Relay2 = 1;
        }

        if (OUTPUT_TAPPING == OUTTAP_LOW){
           Relay3 = 1;
        }
        else{
             Relay3 = 0;
        }

        if (normal == 1){
           Relay4 = 1;        // Main relay on; so output on
        }
        else{
             Relay4 = 0;        // Main relay off; so output off
        }
        flgAC = 0;
     }
}

void main() {
     InitIO();
     ADC_Init();
     InitTimer2();
     InitDisplay();
     Calibrate();
     Delay();
     while(1){
         UpdateDisplay();
         StabilizeVoltage();
     }
}
 
Hi tech to CCS should be easy enough, very cheeky to want it done more than once! I can understand Mikro C being a bit difficult to port, but CCS has a good forum try there.
This is for main voltage isnt it?? No way should you be messing with connecting a pic to line voltage until you at least have an idea what your doing.
 
No problem
Its generate hex , which are lower memory compression than micro c .
So!!! You don't need the C file conversion.... Just the hex file.... I have told you before, I don't have time to do other peoples work for them.... I do not work for this site... I help... Help being the operative word....
 
Some people just dont get it! Ruins it for the rest of us. BTW did you pay for the 3 compilers? Yeah of course you did silly me
 
An attempt of porting a code from a compiler to another compiler depends entirely of the user. You have to read the manual for each compiler before porting.

CCS and MikroC has almost the same code size after compiling. Both compilers are very good in the quality and support.

I've recently ported an FFT code from a C compiler for PC to an ARM Cortex-M4 processor (GCC compiler). Not an easy task, but doable.
 
I cant ake up mind about GCC, Its free and I like that, no hastle so far and I like that. But for some reason I keep fireing up Kiel compiler in demo mode with 32K limit! why would I do that??? I do like Kiel uVision tho
 
So!!! You don't need the C file conversion.... Just the hex file.... I have told you before, I don't have time to do other peoples work for them.... I do not work for this site... I help... Help being the operative word....
no no , sir
i have need c file.
xc8 or hi tech
 
An attempt of porting a code from a compiler to another compiler depends entirely of the user. You have to read the manual for each compiler before porting.

CCS and MikroC has almost the same code size after compiling. Both compilers are very good in the quality and support.

I've recently ported an FFT code from a C compiler for PC to an ARM Cortex-M4 processor (GCC compiler). Not an easy task, but doable.
dear sir , u may wrong here CCS and MikroC has almost the same code size after compiling.
pl check here https://www.sonsivri.to/forum/index.php?topic=920.0
 
Why change them???? just compile and use the hex! I am completely lost as to why you want to change the compiler??? If you have compiled both then whats the issue?
Am I being really stupid and missing something obvious??
 
This compiles But I have no idea if it works... The display and led's simulate fine..

C:
/*
*  PIC16F873A Pin Configuration
* 
* O = Output
* I = Input
*
*                           ________
*                      MCLR | 1  28 | RB7 --> (O) == DIGIT1 CC 
*          ADC1(I) --> AN0  | 2  27 | RB6 --> (O) == G CA
*          ADC2(I) --> AN1  | 3  26 | RB5 --> (O) == F CA
* DIGIT2 CC == (O) <-- RA2  | 4  25 | RB4 --> (O) == E CA
*          ADC3(I) --> AN3  | 5  24 | RB3 --> (O) == D CA
*    Delay_SW  (I) --> RA4  | 6  23 | RB2 --> (O) == C CA
* DIGIT3 CC == (O) <-- RA5  | 7  22 | RB1 --> (O) == B CA
*                      Vss  | 8  21 | RB0 --> (O) == A CA
*                      OSC1 | 9  20 | Vdd
*                      OSC2 |10  19 | Vss
*        RL4 == (O) <-- RC0 |11  18 | RC7 --> (O) == LED_HILO
*        RL3 == (O) <-- RC1 |12  17 | RC6 --> (O) == LED_NORMAL
*        RL2 == (O) <-- RC2 |13  16 | RC5 --> (0) == LED_DELAY
*        RL1 == (O) <-- RC3 |14  15 | RC4 --> (0) ==
*                           ---------
* Program description and features:
*    - Voltage Stabilizer
*    - Auto calibration save upon first manual calibration
*    - Saved in internal EEPROM
*    - Display on 3-digit 7 SEGMENT display
*/
#include<XC.h>

#define _XTAL_FREQ 20000000
#pragma config BOREN = OFF, CPD = OFF, DEBUG = OFF, WRT = OFF, FOSC = HS, WDTE = OFF, CP = OFF, LVP = OFF, PWRTE = OFF

#define Relay1 RC3
#define Relay2 RC2
#define Relay3 RC1
#define Relay4 RC0

#define LED_DELAY RC5
#define LED_NORMAL RC6
#define LED_HILO RC7

#define C2  RA2
#define C3  RA5
#define C1  RB7

#define Delay_SWITCH  RA4
const unsigned char ADC_0 = 0x00;    //Vin_CHANNEL
const unsigned char ADC_1 = 0x08;    //CalibratePot_CHANNEL
const unsigned char ADC_3 = 0x18;    //CalibrateSw_CHANNEL

volatile unsigned char IntCount;
typedef union {
    struct{
        unsigned  flgStart    :1;
        unsigned  normal    :1;
        unsigned  flgAC        :1;
        unsigned  change    :1;
    };
    struct{
        unsigned  flg2ms    :1;       
    };   
}flagRegbits;

volatile flagRegbits flagReg ;

#define PRESSED 0
#define UNPRESSED 1
const unsigned char EE_ADDRESS = 0x06;  // EEPROM address for saving calibration
const unsigned char CALIBRATED = 123;

float Kc;                               // "Constant" for calibration
unsigned int Kc_INT;                    // To save to EEPROM, = Kc * 10000
unsigned int Kc_INT_HIGH;               // High byte for Kc_INT
unsigned int Kc_INT_LOW;                // Low byte for Kc_INT
float Voltage_FLOAT;
unsigned int Voltage_INT;
unsigned int oldVoltage;

unsigned char SEGMENT;
const unsigned char COMMON_CATHODE = 0;
const unsigned char COMMON_ANODE = 1;
const unsigned char COMMON = COMMON_CATHODE;

float calibrator;

#define INTAP_LOW 0                         // 165V
#define INTAP_MED 1                         // 190V
#define INTAP_HI  2                         // 240V

#define OUTTAP_LOW 0                        // 215V
#define OUTTAP_HI  1                        // 240V

unsigned char INPUT_TAPPING;
unsigned char OUTPUT_TAPPING;

unsigned char old_INPUT_TAPPING;
unsigned char old_OUTPUT_TAPPING;

unsigned char oldInput;

unsigned int V_DIFFERENCE;

#define STARTED 1
#define FIRST_TIME 0

#define HYSTERESIS 3

const unsigned char AnodeDriveSegment[10] = {192, 249, 164, 176, 153, 146, 130, 248, 128, 152};
/*  These are the values that need to be sent to the seven segment [A..G] for
*      a common anode display. These values have been obtaned using the
*      Mikroelektronika seven segment editor provided as part of the
*      mikroC/mikroBASIC/mikroPASCAL compiler.
*  The corresponding values for the common cathode display are:
*      CathodeDriveSegment = 255 - AnodeDriveSegment;
*/

unsigned char DriveSegment[10];

void UpdateDisplay(void){
     unsigned int SendVal;
     unsigned char Digit;
     if (flagReg.flg2ms){
        SEGMENT++;
        if (SEGMENT > 3) SEGMENT = 1;

        C1 = 0; C2 = 0; C3 = 0;

        switch (SEGMENT){
               case 1:
                    Digit = (unsigned char) (Voltage_INT / 100);
                    break;
               case 2:
                    Digit = (unsigned char) ((Voltage_INT / 10) % 10);
                    break;
               case 3:
                    Digit = (unsigned char) (Voltage_INT % 10);
                    break;
        }

        SendVal = DriveSegment[Digit] & 0x7F; // Make bit 7 zero

        PORTB = SendVal;

        switch (SEGMENT){
               case 1:
                    C1 = 1;
                    break;
               case 2:
                    C2 = 1;
                    break;
               case 3:
                    C3 = 1;
                    break;
        }
        flagReg.flg2ms = 0;              // Clear flag
     }
}

void ADC_Init(void){

    ADCON1 = 0x84;            // RA0, RA1 and RA3 analog, rest digital
}

int ADC_Get_Sample(unsigned char CH){
    int res = 0;
    ADCON0 = 0x41;
    ADCON0 |= CH;
    ADON = 1;
    __delay_ms(1);
    GO = 1;
    while(GO)
    res = (int)ADRESH <<8;
    res+= ADRESL;
    return res;

}

void InitIO(void){
     PORTB = 0;
     TRISB = 0;
     PORTC = 0;
     TRISC = 0;    
     PORTA = 0;
     TRISA = 0x1B;          // Inputs for ADC
     CMCON = 7;             // Disable comparator
}

void InitTimer2(void){
     T2CON = 3;
/*   Prescale 1:16
*   Timer off for now
*/
     PR2 = 124;
/*   With prescale 1:16 and at 4MHz clock,
*   this gives 2ms period.
*/
     TMR2IF = 0;
     TMR2IE = 1;       // Turn on TMR2 interrupt
     GIE = 1;          // Turn on global interrupts
     PEIE = 1;         // Turn on peripheral interrupts
     TMR2ON = 1;       // Start TMR2
}

void InitDisplay(void){
     unsigned char i;
     if (COMMON == COMMON_ANODE){
        for (i = 0; i < 10; i++){
            DriveSegment[i] = AnodeDriveSegment[i];
        }
     }
     else{
          for (i = 0; i < 10; i++){
              DriveSegment[i] = 255 - AnodeDriveSegment[i];
          }
     }
}

void interrupt ISR(void){
     if (TMR2IF == 1){
     // Configure for 2ms interrupt
        flagReg.flg2ms = 1;
        IntCount++;
        if (IntCount > 200){
           flagReg.flgAC = 1;
           IntCount = 0;
        }
        TMR2IF = 0;
     }
}

void DoTheCalibrationAndSave(void){
     unsigned char i;
     unsigned int CPot;

     while (ADC_Get_Sample(ADC_3) < 512){      // While Calibrate switch is PRESSED
           //Kc = (float)((ADC_Get_Sample(ADC_1) * 2.0) / 1023); // Get the calibration "scaling factor" between 0.00 and 2.00
           CPot = ADC_Get_Sample(ADC_1)/2 + 128;
           Kc = (float)(CPot * 2.0)/1023.0;

           Kc = (float) ((float)Kc * 10000.0);
           Kc_INT = (unsigned int) Kc;
           Kc = (float) ((float)Kc_INT / 10000.0);

           Voltage_FLOAT = (float)((float)ADC_Get_Sample(ADC_0) * 5.0 * 101.0 * Kc) / (1023.0 * 1.3836);

           UpdateDisplay();

           //Voltage_FLOAT = Voltage_FLOAT + 1.5;
           /* Use 100k and 1k resistors for voltage division
            * Divide by 1.3836 to convert from DC voltage to AC
            */
            Voltage_INT = (unsigned int) Voltage_FLOAT;

     }
     // Button has been released - so now, it's time to save the value of Kc
     //Kc = (float) ((float)Kc * 10000.0);
     //Kc_INT = (unsigned int) Kc;
     Kc_INT_HIGH = (Kc_INT >> 8);
     Kc_INT_LOW = (Kc_INT & 0xFF);
     //Kc = (float) ((float)Kc_INT / 10000.0);
     eeprom_write(EE_ADDRESS, CALIBRATED);      // Write value to indicate that calibration has been completed
     eeprom_write(EE_ADDRESS + 1, (unsigned char) Kc_INT_HIGH);
     eeprom_write(EE_ADDRESS + 2, (unsigned char) Kc_INT_LOW);
     /* Data has been saved to EEPROM as:
      * At address EE_ADDRESS, value CALIBRATED has been saved to indicate that calibration HAS occurred
      * Kc_INT = (unsigned int) (Kc * 10000);
      * Kc_INT_HIGH = upper byte of Kc_INT; Kc_INT_LOW = lower byte of Kc_INT;
      * At address EE_ADDRESS + 1, value of Kc_INT_HIGH is saved.
      * At address EE_ADDRESS + 2, value of Kc_INT_LOW is saved.
      */
}

void Calibrate(void){
     unsigned char EERead;
     EERead = eeprom_read(EE_ADDRESS);
     if (EERead == CALIBRATED){       // If Calibration has been done
        if (ADC_Get_Sample(ADC_3) < 512){          // If Calibrate switch is PRESSED
           DoTheCalibrationAndSave();
        }
        else{
             // Calibration has been done and Calibrate switch is UNPRESSED, meaning no new calibration will be done
             // So just get existing saved value of Kc from EEPROM and proceed
             Kc_INT_HIGH = eeprom_read(EE_ADDRESS + 1);
             Kc_INT_LOW = eeprom_read(EE_ADDRESS + 2);
             Kc_INT = (unsigned int) ((Kc_INT_HIGH << 8) | (Kc_INT_LOW)); // Put Kc_INT_HIGH and Kc_INT_LOW into a 16-bit variable
             Kc = (float) ((float)Kc_INT / 10000.0); // Get the float value of Kc
             Voltage_FLOAT = (float)((float)ADC_Get_Sample(ADC_0) * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0);
             //Voltage_FLOAT = Voltage_FLOAT + 1.5;
             Voltage_INT = (unsigned int) Voltage_FLOAT;
        }
     }
     else{
          while (ADC_Get_Sample(ADC_3) > 512);      // While Calibrate switch is UNPRESSED
          DoTheCalibrationAndSave();
     }
}

void Delay(void){
/*   If Delay Switch is pressed (Delay_SWITCH == 0), long Delay
*   Otherwise, short Delay
*/

unsigned char timeKeeper;

LED_DELAY = 1;

if (Delay_SWITCH == UNPRESSED){
    for (timeKeeper = 0; timeKeeper < 10; timeKeeper ++){
        __delay_ms(720);          // 3 minutes Delay
    }
}
else{
      __delay_ms(2000);         // 2 seconds delay
}
LED_DELAY = 0;
}

void StabilizeVoltage(void){
     unsigned int ADS;
     unsigned char V_LOW_CUT;
     if (flagReg.flgAC){
        ADS = ADC_Get_Sample(ADC_0);
        Voltage_FLOAT = (float)((float)ADS * 5.0 * 101.0 * Kc) / (1.3836 * 1023.0);
        //Voltage_FLOAT = Voltage_FLOAT + 1.5;
        Voltage_INT = (unsigned int) Voltage_FLOAT;

        if (flagReg.normal == 1){
           V_LOW_CUT = 125;
        }
        else{
             V_LOW_CUT = 135;
        }

        if (Voltage_INT < V_LOW_CUT){              /*    Low Cut Check    */
           flagReg.normal = 0;
           LED_HILO = 1;
           LED_NORMAL = 0;
           INPUT_TAPPING = INTAP_LOW;
           OUTPUT_TAPPING = OUTTAP_HI;
           oldVoltage = V_LOW_CUT;
           flagReg.flgStart = FIRST_TIME;
        }
        else if (Voltage_INT > 270){         /*    High Cut Check   */
           LED_HILO = 1;
           LED_NORMAL = 0;
           flagReg.normal = 0;
           INPUT_TAPPING = INTAP_HI;
           OUTPUT_TAPPING = OUTTAP_LOW;
           oldVoltage = 270;
        }
        else{                                /* Normal --> Stabilize */
            flagReg.normal = 1;
            LED_HILO = 0;
            LED_NORMAL = 1;
            if (Voltage_INT > oldVoltage){
               V_DIFFERENCE = Voltage_INT - oldVoltage;
            }
            else{
                 V_DIFFERENCE = oldVoltage - Voltage_INT;
            }
            if (V_DIFFERENCE >= HYSTERESIS){
                if (Voltage_INT < 157){
                   INPUT_TAPPING = INTAP_LOW;
                   OUTPUT_TAPPING = OUTTAP_HI;
                   oldVoltage = 157;
                }
                else if (Voltage_INT < 175){
                   INPUT_TAPPING = INTAP_LOW;
                   OUTPUT_TAPPING = OUTTAP_LOW;
                   oldVoltage = 175;
                }
                else if (Voltage_INT < 183){
                   INPUT_TAPPING = INTAP_MED;
                   OUTPUT_TAPPING = OUTTAP_HI;
                   oldVoltage = 183;
                }
                else if (Voltage_INT < 202){
                   INPUT_TAPPING = INTAP_MED;
                   OUTPUT_TAPPING = OUTTAP_LOW;
                   oldVoltage = 202;
                }
                else if (Voltage_INT < 235){
                   INPUT_TAPPING = INTAP_HI;
                   OUTPUT_TAPPING = OUTTAP_HI;
                   oldVoltage = 235;
                }
                else{
                   INPUT_TAPPING = INTAP_HI;
                   OUTPUT_TAPPING = OUTTAP_LOW;
                   oldVoltage = 215;
                }
            }
        }
        if (INPUT_TAPPING == INTAP_LOW){
           Relay1 = 0;
           Relay2 = 0;
        }
        else if (INPUT_TAPPING == INTAP_MED){
             Relay1 = 1;
             Relay2 = 0;
        }
        else{
             Relay1 = 1;
             Relay2 = 1;
        }

        if (OUTPUT_TAPPING == OUTTAP_LOW){
           Relay3 = 1;
        }
        else{
             Relay3 = 0;
        }

        if (flagReg.normal == 1){
           Relay4 = 1;        // Main relay on; so output on
        }
        else{
             Relay4 = 0;        // Main relay off; so output off
        }
        flagReg.flgAC = 0;
     }
}

void main() {
     InitIO();
     ADC_Init();
     InitTimer2();
     InitDisplay();
     Calibrate();
     Delay();
     while(1){
         UpdateDisplay();
         StabilizeVoltage();
     }
}
 
Am I being really stupid and missing something obvious??

You are not being stupid. You are missing the point that OP does not know C. He is copying other peoples work and asking us to port the code for his compiler. I think Ian is just too nice (nothing wrong with that, but it does not help anyone in the long run).
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top