Re-compiling C code

Status
Not open for further replies.
I have a question about C code and making changes. Lets say I find a project on the internet where the author has furnished the program in C along with the resulting hex file. I then build the project and program the microcontroller via the hex file and the circuit works as described. Now lets say I want to make some changes to how the circuit works. My question is when I make changes to the c code is it required to re-compile it using the same compiler the author did or can I use any compiler to generate a new hex file?
 
Yes mostly.... You may have to tweak the code slightly from compiler to compiler.
The trouble is with device specific definitions... These differ from compiler to compiler...

Can you give me a hint on which compilers specifically you are talking about?
 
It looks like the author originally used something called CCS C compiler and I'm trying to use MPLAB 8.91 with HI-TECH ANSI C.
 
I thought in may have been MikroC ... If you need any help... Just post the code and I'll help putting it into Hi-tech ( xc8)
 
That would be awesome of you!

Code:
#include <16F676.h>
#device adc=10
#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,NOMCLR
#use delay (clock=4000000) // 4MHz clock
#rom 0x3ff={0x3444}
#byte PORTA = 0x05
#byte PORTC = 0x07
#byte TRISA = 0x85
#byte TRISC = 0x87
#define SPORTA PORTA
#define SPORTC PORTC
#define TICKS_BETWEEN_INTERRUPTS 5000 //5000
#define INTERRUPT_OVERHEAD 35
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
const char SegCode[11] = {0x40,0x57,0x22,0x06,0x15,0x0C,0x08,0x56,0x00,0x04,0xFF};
// 0 1 2 3 4 5 6 7 8 9
const char Column[3] = {0x02,0x01,0x04};
static char Segment[3] = {0x7f,0x7f,0x7f};
static unsigned char ColCount=0x00;
void CPU_SETUP(void);
void Display(void);
void HTO7S(unsigned int32 Num);
byte i;
unsigned int32 result;
#INT_TIMER1
void Timer1(void)
{
set_timer1(TMR1RESET);
Display();
}
void main()
{
unsigned char i;
CPU_SETUP();
while(true)
{
result=0;
for (i=0;i<20;i++)
{
set_adc_channel(3);
delay_ms(1);
result=result+read_adc();
}
//result = 0x3fe;
HTO7S(result/20);
delay_ms(200);
}
}
void CPU_SETUP()
{
setup_comparator(NC_NC_NC_NC); // not use comparator module
setup_adc_ports( sAN3 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64);
TRISA=0b00011000;
PORTA=0x27;
TRISC=0b00000000;
PORTC=0x37;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(TMR1RESET);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
}
//-------------------------------------
// Display routine
//-------------------------------------
void Display()
{
PORTA = 0b00100111; // off all digits column and Segment G
PORTC = 0b00111111; // off segment a-f
delay_cycles(2);
if (ColCount>=3)
ColCount=0;
SPORTC = Segment[ColCount];
SPORTA = ((Segment[ColCount] & 0b01000000)>>1) | (Column[ColCount]^0x07);
ColCount++;
}
//--------------------------------------
// Convet HEX 2 byte to 7-Segment code
//--------------------------------------
void HTO7S(unsigned int32 Num)
{
unsigned int32 res;
Segment[0]=SegCode[30*Num/10230];
if (Segment[0]==0x40)
Segment[0]=0xFF;
res = 30*Num%10230;
Segment[1]=SegCode[10*res/10230];
res=10*res%10230;
Segment[2]=SegCode[10*res/10230];
}

The first thing I ran into is that I didn't have the include file for the processor so I downloaded one from the internet. I don't know if that was the right thing to do but it got me over one error. After that a long list of other errors popped up which I can post if you want me to.
 
Last edited:
I have it in XC8 (Hi-tech)

C:
#include <XC.h>

#pragma config BOREN = OFF, CPD = OFF, FOSC = INTRCIO, MCLRE = OFF, WDTE = OFF, CP = OFF, PWRTE = OFF
#define _XTAL_FREQ 4000000 // 4MHz clock

#define SPORTA PORTA
#define SPORTC PORTC
#define TICKS_BETWEEN_INTERRUPTS 5000 //5000
#define INTERRUPT_OVERHEAD 35
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
const char SegCode[11] = {0x40,0x57,0x22,0x06,0x15,0x0C,0x08,0x56,0x00,0x04,0xFF};
// 0 1 2 3 4 5 6 7 8 9
const char Column[3] = {0x02,0x01,0x04};
static char Segment[3] = {0x7f,0x7f,0x7f};
static unsigned char ColCount=0x00;
void CPU_SETUP(void);
int read_adc(void);
void Display(void);
void HTO7S(unsigned long Num);
unsigned long result;

void interrupt  Timer1(void)
    {
    TMR1L = (unsigned char) (TMR1RESET & 0xFF);
    TMR1H = (unsigned char) (TMR1RESET >> 8);
    Display();
    TMR1IF = 0;
    }

void main()
    {
    unsigned char i;
    CPU_SETUP();
    while(1)
        {
        result=0;
        for (i=0;i<20;i++)
            {
            ADCON0 = 0x8D;
            __delay_ms(1);
            result=result+read_adc();
            }
        //result = 0x3fe;
        HTO7S(result/20);
        __delay_ms(200);
        }
    }

int read_adc()
    {
    unsigned int res;
    GO = 1;
    while(GO);
    res = (int)ADRESH << 8;
    res+= ADRESL;
    return res;   
    }

void CPU_SETUP()
    {
    CMCON = 7;                 // not use comparator module
    ANSEL = 0x8;
    ADCON1 = 0x50;
    TRISA=0b00011000;
    PORTA=0x27;
    TRISC=0b00000000;
    PORTC=0x37;
    T1CON = 1;
    TMR1L = (unsigned char) (TMR1RESET & 0xFF);
    TMR1H = (unsigned char) (TMR1RESET >> 8);
    TMR1IE = 1;
    PEIE = 1;
    GIE = 1;
    }

//-------------------------------------
// Display routine
//-------------------------------------
void Display()
    {
    PORTA = 0b00100111; // off all digits column and Segment G
    PORTC = 0b00111111; // off segment a-f
    NOP();
    NOP();
    if (ColCount>=3)
        ColCount=0;
    SPORTC = Segment[ColCount];
    SPORTA = ((Segment[ColCount] & 0b01000000)>>1) | (Column[ColCount]^0x07);
    ColCount++;
    }

//--------------------------------------
// Convet HEX 2 byte to 7-Segment code
//--------------------------------------
void HTO7S(unsigned long Num)
    {
    unsigned long res;
    Segment[0]=SegCode[30*Num/10230];
    if (Segment[0]==0x40)
        Segment[0]=0xFF;
    res = 30*Num%10230;
    Segment[1]=SegCode[10*res/10230];
    res=10*res%10230;
    Segment[2]=SegCode[10*res/10230];
    }

Works....
 
What version of MPLAB are you using? I couldn't get it to work with MPLAB 8.91 / HI-TECH ANSI C so I downloaded MPLAB X IDE 2.00 and XC8. It seems to have trouble with the delay commands...
See image.
 
Ah!!! There is a problem.... I have the Pro version of XC8... The pic16f676 has a tiny memory model... The code I compile fits... Where the "Lite" version isn't optimised...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…