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.

Compiling c code for pic18f4550

Status
Not open for further replies.

Tolu

New Member
I am working on a temperature measurement project, I try to compile it but it gives the error "demo limit" which I understand is beyond the demo version and I will need a pro version. I need somebody who can help to compile the code into hex file.
 
Which compiler are you working with?
 
please find below the main code. The USBdsc.c attached is meant to be added to the project and then build.
//#include "USBdsc.c"
unsigned char i,j;
unsigned long Vin, Cint;
unsigned char op[12], Temperature[4], Read_buffer[4];
float Voltage,Celsius;
void System_init(void);
void Interrupt_Dis(void);
void Interrupt_En(void);
void Timer0_init(void);
void Remove_blank(void);
//--------------------------------------------------------
//
// Timer interrupt service routine
//
//--------------------------------------------------------
void interrupt(){
//HID_InterruptProc(); // Keep alive
TMR0L = 100; // Reload TMR
INTCON.TMR0IF = 0; // Re-enable TMR0 interrupts
}
//--------------------------------------------------------
// System_init(void)
// This function initializes the system.
// Pins used:
// Set inputs as analog, Ref=+5V
// Set A/D clock = Fosc/64, 8TAD
// Set PORT A as inputs
// Set PORT B as outputs
//--------------------------------------------------------
void System_init(void)
{
ADCON1 = 0x00;
ADCON2 = 0xA6;
TRISA = 0xFF;
TRISB = 0X00;
}
//--------------------------------------------------------
// void Interrupt_Dis(void)
//
// Set interrupt registers to power-on defaults
// Disable all interrupts
//
//--------------------------------------------------------
void Interrupt_Dis(void){
INTCON=0x00;
INTCON2=0xF5;
INTCON3=0xC0;
RCON.IPEN=0x00;
PIE1=0x00;
PIE2=0x00;
PIR1=0x00;
PIR2=0x00;
}
//--------------------------------------------------------
// void Interrupt_En(void)
//
// Set interrupt registers to power-on defaults
// Enable all interrupts
//
//--------------------------------------------------------
void Interrupt_En(void){
INTCON = 0xE0;
}
//--------------------------------------------------------
// void Timer0_init(void)
// Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
// and load TMR0L to 156 so that the time interval for timer
// interrupts at 48MHz is 256.156.0.083 = 3.3ms
//
// The timer is in 8-bit mode by default
//
//--------------------------------------------------------
void Timer0_init(void){
T0CON = 0x47; // Prescaler = 256
TMR0L = 100; // Timer count is 256-156 = 100
INTCON.TMR0IE = 1; // Enable T0IE
T0CON.TMR0ON = 1; // Turn Timer 0 ON
}
//--------------------------------------------------------
// Remove_blank(void)
// Remove leading blanks from op array.
// The function save first 5 character in temperature array.
//--------------------------------------------------------
void Remove_blank(void){
for(j=0; j<4; j++)Temperature[j]=' ';
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
Temperature[j]=op;
j++;
}
}
}
//--------------------------------------------------------
//
// Start of MAIN program
//
//--------------------------------------------------------
void main()
{
System_init();
Interrupt_Dis();
Timer0_init();
Interrupt_En();
//--------------------------------------------------------
// Enable USB port
//--------------------------------------------------------
Hid_Enable(&Read_buffer, &Temperature);
Delay_ms(1000);// Wait 1 second
Delay_ms(1000);// Wait 1 second
//--------------------------------------------------------
//
// Endless loop. Read Temperature from the A/D converter,
// convert into Celsius and send to the PC over the
// USB port every second
// forever loop
//
//--------------------------------------------------------
for(;;)
{
Vin = Adc_Read(0); // Read from channel 0 (AN0)
Voltage = (Vin * 5.0) / 1024.0; // In Voltage = Vin x 5/1024
Celsius = (Voltage * 100.0); // convert voltage to Temperature in Celsius
Cint = (int)Celsius; // As an integer number
PORTB = Cint;
LongToStr(Cint,op); // Convert to string in "op"
Remove_blank(); //To remove blanks characters from op and save it in temperature.
//--------------------------------------------------------
// Send Temperature (in array Temperature ) to the PC
//--------------------------------------------------------
Hid_Write(&Temperature,4); // Send to USB as 4 characters
Delay_ms(1000); // Wait 1 second
}
//--------------------------------------------------------
// Disable USB port
//--------------------------------------------------------
Hid_Disable();
}
 

Attachments

  • USBdsc.c
    7.9 KB · Views: 250
Hi Tolu, you can post the code in a code tag like this:

C:
//#include "USBdsc.c"
unsigned char i,j;
unsigned long Vin, Cint;
unsigned char op[12], Temperature[4], Read_buffer[4];
float Voltage,Celsius;
void System_init(void);
void Interrupt_Dis(void);
void Interrupt_En(void);
void Timer0_init(void);
void Remove_blank(void);
//--------------------------------------------------------
//
// Timer interrupt service routine
//
//--------------------------------------------------------
void interrupt(){
//HID_InterruptProc(); // Keep alive
TMR0L = 100; // Reload TMR
INTCON.TMR0IF = 0; // Re-enable TMR0 interrupts
}
//--------------------------------------------------------
// System_init(void)
// This function initializes the system.
// Pins used:
// Set inputs as analog, Ref=+5V
// Set A/D clock = Fosc/64, 8TAD
// Set PORT A as inputs
// Set PORT B as outputs
//--------------------------------------------------------
void System_init(void)
{
ADCON1 = 0x00;
ADCON2 = 0xA6;
TRISA = 0xFF;
TRISB = 0X00;
}
//--------------------------------------------------------
// void Interrupt_Dis(void)
//
// Set interrupt registers to power-on defaults
// Disable all interrupts
//
//--------------------------------------------------------
void Interrupt_Dis(void){
INTCON=0x00;
INTCON2=0xF5;
INTCON3=0xC0;
RCON.IPEN=0x00;
PIE1=0x00;
PIE2=0x00;
PIR1=0x00;
PIR2=0x00;
}
//--------------------------------------------------------
// void Interrupt_En(void)
//
// Set interrupt registers to power-on defaults
// Enable all interrupts
//
//--------------------------------------------------------
void Interrupt_En(void){
INTCON = 0xE0;
}
//--------------------------------------------------------
// void Timer0_init(void)
// Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
// and load TMR0L to 156 so that the time interval for timer
// interrupts at 48MHz is 256.156.0.083 = 3.3ms
//
// The timer is in 8-bit mode by default
//
//--------------------------------------------------------
void Timer0_init(void){
T0CON = 0x47; // Prescaler = 256
TMR0L = 100; // Timer count is 256-156 = 100
INTCON.TMR0IE = 1; // Enable T0IE
T0CON.TMR0ON = 1; // Turn Timer 0 ON
}
//--------------------------------------------------------
// Remove_blank(void)
// Remove leading blanks from op array.
// The function save first 5 character in temperature array.
//--------------------------------------------------------
void Remove_blank(void){
for(j=0; j<4; j++)Temperature[j]=' ';
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
Temperature[j]=op;
j++;
}
}
}
//--------------------------------------------------------
//
// Start of MAIN program
//
//--------------------------------------------------------
void main()
{
System_init();
Interrupt_Dis();
Timer0_init();
Interrupt_En();
//--------------------------------------------------------
// Enable USB port
//--------------------------------------------------------
Hid_Enable(&Read_buffer, &Temperature);
Delay_ms(1000);// Wait 1 second
Delay_ms(1000);// Wait 1 second
//--------------------------------------------------------
//
// Endless loop. Read Temperature from the A/D converter,
// convert into Celsius and send to the PC over the
// USB port every second
// forever loop
//
//--------------------------------------------------------
for(;
{
Vin = Adc_Read(0); // Read from channel 0 (AN0)
Voltage = (Vin * 5.0) / 1024.0; // In Voltage = Vin x 5/1024
Celsius = (Voltage * 100.0); // convert voltage to Temperature in Celsius
Cint = (int)Celsius; // As an integer number
PORTB = Cint;
LongToStr(Cint,op); // Convert to string in "op"
Remove_blank(); //To remove blanks characters from op and save it in temperature.
//--------------------------------------------------------
// Send Temperature (in array Temperature ) to the PC
//--------------------------------------------------------
Hid_Write(&Temperature,4); // Send to USB as 4 characters
Delay_ms(1000); // Wait 1 second
}
//--------------------------------------------------------
// Disable USB port
//--------------------------------------------------------
Hid_Disable();
}
 
please find below the main code. The USBdsc.c attached is meant to be added to the project and then build.
//#include "USBdsc.c"
unsigned char i,j;
unsigned long Vin, Cint;
unsigned char op[12], Temperature[4], Read_buffer[4];
float Voltage,Celsius;
void System_init(void);
void Interrupt_Dis(void);
void Interrupt_En(void);
void Timer0_init(void);
void Remove_blank(void);
//--------------------------------------------------------
//
// Timer interrupt service routine
//
//--------------------------------------------------------
void interrupt(){
//HID_InterruptProc(); // Keep alive
TMR0L = 100; // Reload TMR
INTCON.TMR0IF = 0; // Re-enable TMR0 interrupts
}
//--------------------------------------------------------
// System_init(void)
// This function initializes the system.
// Pins used:
// Set inputs as analog, Ref=+5V
// Set A/D clock = Fosc/64, 8TAD
// Set PORT A as inputs
// Set PORT B as outputs
//--------------------------------------------------------
void System_init(void)
{
ADCON1 = 0x00;
ADCON2 = 0xA6;
TRISA = 0xFF;
TRISB = 0X00;
}
//--------------------------------------------------------
// void Interrupt_Dis(void)
//
// Set interrupt registers to power-on defaults
// Disable all interrupts
//
//--------------------------------------------------------
void Interrupt_Dis(void){
INTCON=0x00;
INTCON2=0xF5;
INTCON3=0xC0;
RCON.IPEN=0x00;
PIE1=0x00;
PIE2=0x00;
PIR1=0x00;
PIR2=0x00;
}
//--------------------------------------------------------
// void Interrupt_En(void)
//
// Set interrupt registers to power-on defaults
// Enable all interrupts
//
//--------------------------------------------------------
void Interrupt_En(void){
INTCON = 0xE0;
}
//--------------------------------------------------------
// void Timer0_init(void)
// Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
// and load TMR0L to 156 so that the time interval for timer
// interrupts at 48MHz is 256.156.0.083 = 3.3ms
//
// The timer is in 8-bit mode by default
//
//--------------------------------------------------------
void Timer0_init(void){
T0CON = 0x47; // Prescaler = 256
TMR0L = 100; // Timer count is 256-156 = 100
INTCON.TMR0IE = 1; // Enable T0IE
T0CON.TMR0ON = 1; // Turn Timer 0 ON
}
//--------------------------------------------------------
// Remove_blank(void)
// Remove leading blanks from op array.
// The function save first 5 character in temperature array.
//--------------------------------------------------------
void Remove_blank(void){
for(j=0; j<4; j++)Temperature[j]=' ';
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
Temperature[j]=op;
j++;
}
}
}
//--------------------------------------------------------
//
// Start of MAIN program
//
//--------------------------------------------------------
void main()
{
System_init();
Interrupt_Dis();
Timer0_init();
Interrupt_En();
//--------------------------------------------------------
// Enable USB port
//--------------------------------------------------------
Hid_Enable(&Read_buffer, &Temperature);
Delay_ms(1000);// Wait 1 second
Delay_ms(1000);// Wait 1 second
//--------------------------------------------------------
//
// Endless loop. Read Temperature from the A/D converter,
// convert into Celsius and send to the PC over the
// USB port every second
// forever loop
//
//--------------------------------------------------------
for(;;)
{
Vin = Adc_Read(0); // Read from channel 0 (AN0)
Voltage = (Vin * 5.0) / 1024.0; // In Voltage = Vin x 5/1024
Celsius = (Voltage * 100.0); // convert voltage to Temperature in Celsius
Cint = (int)Celsius; // As an integer number
PORTB = Cint;
LongToStr(Cint,op); // Convert to string in "op"
Remove_blank(); //To remove blanks characters from op and save it in temperature.
//--------------------------------------------------------
// Send Temperature (in array Temperature ) to the PC
//--------------------------------------------------------
Hid_Write(&Temperature,4); // Send to USB as 4 characters
Delay_ms(1000); // Wait 1 second
}
//--------------------------------------------------------
// Disable USB port
//--------------------------------------------------------
Hid_Disable();
}
 
The problem might be that you use floating points (and longs) in your code. The floating point libraries are quite large and using long integers when they are not needed wastes memory and time too.

And, you do not even need floating point in your calculations.

C:
Voltage =(Vin *5.0)/1024.0;// In Voltage = Vin x 5/1024
Celsius =(Voltage *100.0);// convert voltage to Temperature in Celsius
Cint =(int)Celsius;// As an integer number

If you need only the temperature value, you can replace the above with:
C:
Cint = (Vin*500)/1024;

// or maybe even with this:
Cint = (Vin/2); // the error is practically zero
               // (because you are truncating the floating point result to integer anyway in your original code)

This gives you 1 Celsius resolution over the full 0 to 500 Celsius range.

If you only need to send the temperature values to PC, I would just send the raw ADC readings and do all the math at the PC end.
 
Last edited:
The problem might be that you use floating points (and longs) in your code. The floating point libraries are quite large and using long integers when they are not needed wastes memory and time too.

And, you do not even need floating point in your calculations.

C:
Voltage =(Vin *5.0)/1024.0;// In Voltage = Vin x 5/1024
Celsius =(Voltage *100.0);// convert voltage to Temperature in Celsius
Cint =(int)Celsius;// As an integer number

If you need only the temperature value, you can replace the above with:
C:
Cint = (Vin*500)/1024;
 
// or maybe even with this:
Cint = (Vin/2); // the error is practically zero
               // (because you are truncating the floating point result to integer anyway in your original code)

This gives you 1 Celsius resolution over the full 0 to 500 Celsius range.

If you only need to send the temperature values to PC, I would just send the raw ADC readings and do all the math at the PC end.
 
Thanks, The code was written in MicroC pro for PIC v 5.8.0. Like I said all I needed was a fully compiled code into hex file because on compiling on my system it was giving "demo limit" error. I wouldn't mind anyone making a modification to it, compile it with pro full version to get the hex file that will still ensure the microcontroller perform the required task
 
Like I said all I needed was a fully compiled code into hex file because on compiling on my system it was giving "demo limit" error. I wouldn't mind anyone making a modification to it, compile it with pro full version to get the hex file that will still ensure the microcontroller perform the required task

I suggested a way you can (try) compile it yourself.. why don't you at least try it meanwhile you wait for somebody to compile the code for you (it might be a long wait).
 
Last edited:
Thanks I understand what you mean, and I guess it is going to work. I will let you know as soon as I tried it
 
Hi, I have been away for sometimes. I tried the code with little modification but it did

==============================================
............................................................................*/
//#include "USBdsc.c"
unsigned char i,j;
unsigned int Vin, Cint;
unsigned char op[12], Temperature[4], Read_buffer[4];
float Voltage,Celsius;
void System_init(void);
void Interrupt_Dis(void);
void Interrupt_En(void);
void Timer0_init(void);
void Remove_blank(void);

//--------------------------------------------------------
//
// Timer interrupt service routine
//
//--------------------------------------------------------
// Timer interrupt service routine
void interrupt()
{
HID_InterruptProc(); // Keep alive
TMR0L = 100; // Reload TMR0L
INTCON.TMR0IF = 0; // Re-enable TMR0 interrupts
}
//--------------------------------------------------------
// System_init(void)
// This function initializes the system.
// Pins used:
// Set inputs as analog, Ref=+5V
// Set A/D clock = Fosc/64, 8TAD
// Set PORT A as inputs
// Set PORT C as outputs
//--------------------------------------------------------
void System_init(void)
{
ADCON1 = 0x00;
ADCON2 = 0xA6;
TRISA = 0xFF;
TRISC = 0X00;
}
//--------------------------------------------------------
// void Interrupt_Dis(void)
//
// Set interrupt registers to power-on defaults
// Disable all interrupts
//
//--------------------------------------------------------
void Interrupt_Dis(void){
INTCON=0x00;
INTCON2=0xF5;
INTCON3=0xC0;
RCON.IPEN=0x00;
PIE1=0x00;
PIE2=0x00;
PIR1=0x00;
PIR2=0x00;
}
//--------------------------------------------------------
// void Interrupt_En(void)
//
// Set interrupt registers to power-on defaults
// Enable all interrupts
//
//--------------------------------------------------------
void Interrupt_En(void){
INTCON = 0xE0;
}
//--------------------------------------------------------
// void Timer0_init(void)
// Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
// and load TMR0L to 156 so that the time interval for timer
// interrupts at 48MHz is 256.156.0.083 = 3.3ms
//
// The timer is in 8-bit mode by default
//
//--------------------------------------------------------
void Timer0_init(void){
T0CON = 0x47; // Prescaler = 256
TMR0L = 100; // Timer count is 256-156 = 100
INTCON.TMR0IE = 1; // Enable T0IE
T0CON.TMR0ON = 1; // Turn Timer 0 ON
}
//--------------------------------------------------------
// Remove_blank(void)
// Remove leading blanks from op array.
// The function save first 5 character in temperature array.
//--------------------------------------------------------
void Remove_blank(void){
for(j=0; j<4; j++)Temperature[j]=' ';
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
Temperature[j]=op;
j++;
}
}
}
//--------------------------------------------------------
//
// Start of MAIN program
//
//--------------------------------------------------------
void main()
{
System_init();
Interrupt_Dis();
Timer0_init();
Interrupt_En();
//--------------------------------------------------------
// Enable USB port
//--------------------------------------------------------
Hid_Enable(&Read_buffer, &Temperature);
Delay_ms(1000);// Wait 1 second
Delay_ms(1000);// Wait 1 second
//--------------------------------------------------------
//
// Endless loop. Read Temperature from the A/D converter,
// convert into Celsius and send to the PC over the
// USB port every second
// forever loop
//
//--------------------------------------------------------
for(;;)
{
Vin = Adc_Read(0); // Read from channel 0 (AN0)
Cint = (Vin/2); // convert voltage to Temperature
Cint = (int)Celsius; // As an integer number
PORTC = Cint;
LongToStr(Cint,op); // Convert to string in "op"
Remove_blank(); //To remove blanks characters from op and save it in temperature.
//--------------------------------------------------------
// Send Temperature (in array Temperature ) to the PC
//--------------------------------------------------------
Hid_Write(&Temperature,4); // Send to USB as 4 characters
Delay_ms(1000); // Wait 1 second
}
//--------------------------------------------------------
// Disable USB port
//--------------------------------------------------------
Hid_Disable();
}
n't work. I am posting the code again if there is anyone who can please help look at it and see what may be wrong.
Thanks
find the code below
 
Hi, I have been away for sometimes. I tried the code with little modification but it did

==============================================
............................................................................*/
//#include "USBdsc.c"
unsigned char i,j;
unsigned int Vin, Cint;
unsigned char op[12], Temperature[4], Read_buffer[4];
float Voltage,Celsius;
void System_init(void);
void Interrupt_Dis(void);
void Interrupt_En(void);
void Timer0_init(void);
void Remove_blank(void);

//--------------------------------------------------------
//
// Timer interrupt service routine
//
//--------------------------------------------------------
// Timer interrupt service routine
void interrupt()
{
HID_InterruptProc(); // Keep alive
TMR0L = 100; // Reload TMR0L
INTCON.TMR0IF = 0; // Re-enable TMR0 interrupts
}
//--------------------------------------------------------
// System_init(void)
// This function initializes the system.
// Pins used:
// Set inputs as analog, Ref=+5V
// Set A/D clock = Fosc/64, 8TAD
// Set PORT A as inputs
// Set PORT C as outputs
//--------------------------------------------------------
void System_init(void)
{
ADCON1 = 0x00;
ADCON2 = 0xA6;
TRISA = 0xFF;
TRISC = 0X00;
}
//--------------------------------------------------------
// void Interrupt_Dis(void)
//
// Set interrupt registers to power-on defaults
// Disable all interrupts
//
//--------------------------------------------------------
void Interrupt_Dis(void){
INTCON=0x00;
INTCON2=0xF5;
INTCON3=0xC0;
RCON.IPEN=0x00;
PIE1=0x00;
PIE2=0x00;
PIR1=0x00;
PIR2=0x00;
}
//--------------------------------------------------------
// void Interrupt_En(void)
//
// Set interrupt registers to power-on defaults
// Enable all interrupts
//
//--------------------------------------------------------
void Interrupt_En(void){
INTCON = 0xE0;
}
//--------------------------------------------------------
// void Timer0_init(void)
// Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
// and load TMR0L to 156 so that the time interval for timer
// interrupts at 48MHz is 256.156.0.083 = 3.3ms
//
// The timer is in 8-bit mode by default
//
//--------------------------------------------------------
void Timer0_init(void){
T0CON = 0x47; // Prescaler = 256
TMR0L = 100; // Timer count is 256-156 = 100
INTCON.TMR0IE = 1; // Enable T0IE
T0CON.TMR0ON = 1; // Turn Timer 0 ON
}
//--------------------------------------------------------
// Remove_blank(void)
// Remove leading blanks from op array.
// The function save first 5 character in temperature array.
//--------------------------------------------------------
void Remove_blank(void){
for(j=0; j<4; j++)Temperature[j]=' ';
j=0;
for(i=0;i<=11;i++)
{
if(op != ' ') // If a blank
{
Temperature[j]=op;
j++;
}
}
}
//--------------------------------------------------------
//
// Start of MAIN program
//
//--------------------------------------------------------
void main()
{
System_init();
Interrupt_Dis();
Timer0_init();
Interrupt_En();
//--------------------------------------------------------
// Enable USB port
//--------------------------------------------------------
Hid_Enable(&Read_buffer, &Temperature);
Delay_ms(1000);// Wait 1 second
Delay_ms(1000);// Wait 1 second
//--------------------------------------------------------
//
// Endless loop. Read Temperature from the A/D converter,
// convert into Celsius and send to the PC over the
// USB port every second
// forever loop
//
//--------------------------------------------------------
for(;;)
{
Vin = Adc_Read(0); // Read from channel 0 (AN0)
Cint = (Vin/2); // convert voltage to Temperature
Cint = (int)Celsius; // As an integer number
PORTC = Cint;
LongToStr(Cint,op); // Convert to string in "op"
Remove_blank(); //To remove blanks characters from op and save it in temperature.
//--------------------------------------------------------
// Send Temperature (in array Temperature ) to the PC
//--------------------------------------------------------
Hid_Write(&Temperature,4); // Send to USB as 4 characters
Delay_ms(1000); // Wait 1 second
}
//--------------------------------------------------------
// Disable USB port
//--------------------------------------------------------
Hid_Disable();
}
n't work. I am posting the code again if there is anyone who can please help look at it and see what may be wrong.
Thanks
find the code below
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top