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.
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).

Agreed with you.

I've done quite an amount of porting from different compilers and it requires me to read the compiler manual - a lot of it.

And what's with the code which deals with floating point, especially in a PIC16F series? It's going to chew up a lot of memory and slow things down, regardless of compilers.
 
Agreed with you.

I've done quite an amount of porting from different compilers and it requires me to read the compiler manual - a lot of it.

And with small programs like that it is better to write the program from scratch. Just learn how the original code works and implement and test it step-by-step.
 
And with small programs like that it is better to write the program from scratch. Just learn how the original code works.

Yeah. The major part of microcontroller programming is to understand the registers in the peripherals too, and to understand its limitations. :)
 
Why would anyone want to learn C?? thats a ridiculous notion, there are more than enough people online that can program C in any compiler!! all you have to do is ask them! Ok sometimes you have to ask the NICELY, but on the whole you just got to ask. None of this learning stuff you talk about, actually the bit I dont understand was he had it working on two compilers! So he isnt learning he is switching compilers until he finds the magic one that codes for you (dont tell him which one it is ;))
 
Why would anyone want to learn C?? thats a ridiculous notion, there are more than enough people online that can program C in any compiler!! all you have to do is ask them! Ok sometimes you have to ask the NICELY, but on the whole you just got to ask. None of this learning stuff you talk about, actually the bit I dont understand was he had it working on two compilers! So he isnt learning he is switching compilers until he finds the magic one that codes for you (dont tell him which one it is ;))
He obviously doesn't want to learn C.... The changing of code from compiler to compiler is relatively simple... So it only took a minute..
 
Why would anyone want to learn C?? thats a ridiculous notion, there are more than enough people online that can program C in any compiler!! all you have to do is ask them! Ok sometimes you have to ask the NICELY, but on the whole you just got to ask. None of this learning stuff you talk about, actually the bit I dont understand was he had it working on two compilers! So he isnt learning he is switching compilers until he finds the magic one that codes for you (dont tell him which one it is ;))

He works on two compilers so that he can get away with the "lowest amount of space" possible in the produced *.hex file.

To be honest on any 8-bit microcontroller compilers, compiling floating point code results in a much huge output later. The coding part is more important than bothering about which compiler to use.

Plus, did he buy all of these? CCS compiler and MikroE provides very good trial versions. And that's a small piece of code he's writing anyway, so it will compile at all the trial editions. As far as I know, I have used a trial version of CCS and a licensed version of MikroC (PIC and dsPIC) and they worked pretty well.
 
If he is that concerned on space etc then why not ASM? Lets be straight here he didnt write a single line. But of course you could be right, At 13 1/2 I might just be developing cynicism! Its funny how nearly 3 years online does that to you lol
 
Lets not mock!!! If he truly understands C there won't be a problem.. However if he doesn't ( as I suspect ) the code I have modified for him will serve no purpose... If it doesn't work as expected, he won't be able to fix it... As it stands I don't know how the code will behave... I can't test it... I already think that the union I created hasn't got bit access.. This may need to be in a different part of the memory!!
 
Lets not mock!!! If he truly understands C there won't be a problem.. However if he doesn't ( as I suspect ) the code I have modified for him will serve no purpose... If it doesn't work as expected, he won't be able to fix it... As it stands I don't know how the code will behave... I can't test it... I already think that the union I created hasn't got bit access.. This may need to be in a different part of the memory!!

I like to help when I think the problem is a challenge. I learn from those kinds of problems. I see that mentality here in ETO all the time and it is great.. sometimes (many times) the members here are more interested about the problem than the original poster.. :)
 
Hello

My first post here. I need help NOW. I have exam tomorrow. What is this C everyone is talking about? Maybe C this or C that?
I C everything.

My kwestion is what is Compiler??? My Professor must have answer from me next day to start examining me.

Please hurry and give me answer now.
Dont waste time.
 
Last edited:
People will be back, post's have started to change that's a good sign. People are turning up with mad projects and asking question's because they have never seen a soldering iron before, thats how I started here. So I think we are seeing the new seeds being sown, it will take some time but I think things will grow back strong. 2 months ago I was sure this place was finished, we have been lucky it looks like it is going to regrow again!
Sometimes members having a pop at lazy people is a good thing, it makes others know it isnt a do your work for you site, its a we will do everything to HELP you site. Thats the one thing I like most about AAC, the members themselves take no nonsense, turn up with a do this for me or do that for me, and they get a shredding. Its all very polite but works, take a certain salad dressing that was banned here, he isnt doing so well over there. Mainly because the members are saying it as it is to him. The hard bit is knowing when people are going too far.
The most damaging thing I have seen here is also some of the things I like most, for example: A beginner comes along and just wants a simple answer to his problem, he has maybe built say a thermostat with a comparator as it's base. It dosnt work right, he asks a question. The choice is then sometimes to just give enough information to get it working or to go in depth about the problem. Before you know it, the experts are arguing and discussing really minute points, even the favorite conventional or electron flow, a beginner couldnt care less what way it flows what he wants to know is why it ISNT flowing.
I love those threads they get really deep into it, but they should be taken away from the original post. That way the beginner gets the simple answer he needed and is inclined to return, plus the interesting discussion still takes place so those of us that enjoy it can also join in.
Anyway I am typesetting and not experimenting!!
 
Hi Ian

Sorry for trying to put a bit of humour into things like ETO was. A fun place of learning.
And I now know that ETO does not need people like me anymore.

See you.
 
Last edited:
tvtech... The site does need people like you... but there is a subtle difference between humour and ignorance..

Some members do not see humour the same way you or I do.. Sometimes it comes across as being laughed at!! this discourages people from posting.... Just encourage posters and if they are past your help.. Just sit and watch...
 
Hi Ian

I have an active little mind. I have the unique ability to laugh at myself and all my shortcomings when I question others out there....

If I do or say something stupid I am the first to admit it. As much as a PRO as I consider myself, I like to play too. A little bit of a joke here or there always goes down well.

Nobody knows what goes on in my head (the Brain part) except me:).

Oh Oh...Ratch is summoning me. There is something he want's to debate.

See you later.

Regards,
tvtech
 
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();
     }
}
thank u sir , its work on Real Pic Simulator The display and led's fine..
but when mcu enter on delay mode adc sensing function not work .
PHP:
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;
}
this Delay mode we can use TMR1. like this , those in ccs c
Code:
/*****************************************************************************
Delay Defines
****************************************************************************/

#define STARTUP_SEC 15      // Change to 180 for read card
#define STARTUP_SECs 15

#define TIMER1_FREQUENCY (4000000 / 4)      // 1 clock tick = 1 instr. cycle = crystal frequency / 4


/*****************************************************************************
  Timer1 Interrupt, executed every 10 ms
****************************************************************************/
#INT_TIMER1
void TIMER1_isr(void)
{
   // Increment time_elasped to keep track of ms
   time_elasped++;
   // Increment seconds variable if 1000 ms have passed
 
   if (time_elasped == 100)  //high voltage cut led blink freqency @ 1000 ms
   {
        seconds++;
        BlinkLED1_flag=~BlinkLED1_flag;
        time_elasped = 0;
   }


   // If Quick Start switch is set between start up time, set QuickStart_flag
 
   if ((input(Delay_SWITCH)==0) && (seconds<STARTUP_SEC) )
      {
          QuickStart_flag = 1;
      }
    
   // On start up, blink LED1 for 3 mins
   //led1 blink mains normal ,led1 blink & Reads ADC
    // both are work in same time. not one by one.
    //if led1 blink duration complet then it no blink agen.
   //Has three mins passed?
 
    if (seconds > STARTUP_SEC)
    {
       BlinkMains_flag=1;
       //output_high(RL4);
    }

   //if(seconds=!TEMP_seconds)
   if(ErrorConditionExists_flag == 1)
   {
    if (TEMP_seconds > STARTUP_SECs)
    {
       BlinkMains_flag=1;
    }}
   //Reset Timer 1
   set_timer1(60545);
   clear_interrupt(INT_TIMER1); 
} 


/*****************************************************************************
  Function BlinkLED1
  Blink Low LED
****************************************************************************/
void BlinkLED1(void)
{
   if (BlinkLED1_flag==0)
   {
      output_high( LED_DELAY);
   }
   else
   {
   output_low( LED_DELAY);

   }
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top