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.

Errors compiling C code

Status
Not open for further replies.
My sincere apologies.... I had compiled it with XC8.... Not Hitech Lite!!! The code generated in the Hitech Lite version doesn't fit the code space.

I have the full version of XC8 so it compiles and takes up @ 2 thirds of the code space..

I will try to remove the float from the code to save some space.


Thought so!!! I have removed the floating point routines and it will now compile.
You may need to tweek the outputs as I may be 10 times out..

C:
/* Project name:
     Seven-segment display digital thermometer
 * Copyright:
     (c) Rajendra Bhatt, 2010.
      MCU:             PIC16F628A
     Oscillator:      XT, 4.0 MHz
*/
#include<htc.h>
#include "1wire.h"
#define _XTAL_FREQ 4000000	 // Xtal speed
#define abs(N) ( (N) >= 0 ? (N) : -(N) ) 
// Temperature digits
unsigned char i, DD0=0x3f, DD1=0x3f,DD2=0x00, CF_Flag=0xff, CF=0x3f, N_Flag;
// CF_Flag = 0: F, 1: C
// Variable to store temperature register value
int temp_value=0;
long temp_F;
 
//-------------- Function to Return mask for common cathode 7-seg. display
unsigned short mask(unsigned char  num) {
  switch (num) {
    case 0 : return 0x3F;
    case 1 : return 0x06;
    case 2 : return 0x5B;
    case 3 : return 0x4F;
    case 4 : return 0x66;
    case 5 : return 0x6D;
    case 6 : return 0x7D;
    case 7 : return 0x07;
    case 8 : return 0x7F;
    case 9 : return 0x6F;
    case 10 : return 0x40;  // Symbol '-'
    case 11 : return 0x39;   // Symbol C
    case 12 : return 0x71;   // Symbol F
    case 13 : return 0x00;  // Blank
  	} //case end
	return 0;
}
 
void display_temp(unsigned char  DD0, unsigned char  DD1, unsigned char  DD2, unsigned char  CF)    {
    for (i = 0; i<=100; i++) {
      
      RA0 = 1;          // Select Ones Digit
      RA1 = 0;
      RA2 = 0;
      RA3 = 0;
	  PORTB = DD0;
      __delay_ms(8);
      RA0 = 0;
      RA1 = 1;        // Select Tens Digit
      RA2 = 0;
      RA3 = 0;
	  PORTB = DD1;
      __delay_ms(8);
      RA0 = 0;
      RA1 = 0;
      RA2 = 1;        // Select +/- Digit
      RA3 = 0;
	  PORTB = DD2;
      __delay_ms(8);

      RA0 = 0;
      RA1 = 0;
      RA2 = 0 ;
      RA3 = 1;  
	  PORTB = CF;      // Select CF Digit
      __delay_ms(8);
        }
     return;
}
 
 
void main() {
  CMCON  |= 7;      // Disable Comparators
  TRISB = 0x00;    // Set PORTB direction to be output
  PORTB = 0x00;    // Turn OFF LEDs on PORTB
  TRISA0 = 0;  // RA.0 to RA3 Output
  TRISA1 = 0;
  TRISA2 = 0;
  TRISA3 = 0;
 
 
    //--- main loop
  do {
 
    N_Flag = 0;  // Reset Temp Flag
    //--- perform temperature reading
    OW_reset_pulse();      // Onewire reset signal
    OW_write_byte(0xCC);   // Issue command SKIP_ROM
    OW_write_byte(0x44);   // Issue command CONVERT_T
    display_temp(DD0, DD1, DD2,CF)   ;
    OW_reset_pulse();
    OW_write_byte(0xCC);    // Issue command SKIP_ROM
    OW_write_byte(0xBE);    // Issue command READ_SCRATCHPAD
 
    // Next Read Temperature
    // Read Byte 0 from Scratchpad
    temp_value =  OW_read_byte();
    // Then read Byte 1 from Scratchpad and shift 8 bit left and add the Byte 0
    temp_value = (OW_read_byte() << 8) + temp_value;
 
    if (temp_value & 0x8000) {
     temp_value = ~temp_value + 1;
     N_Flag = 1;   // Temp is -ive
     }
    if (temp_value & 0x0001) temp_value += 1;   // 0.5 round to 1
    temp_value = temp_value >> 1 ;
    if (CF_Flag == 0) {
     if (N_Flag ==1) {
      temp_F = (32-9*temp_value/5)*10 + 6;
      if (temp_F < 0){
         N_Flag=1;
         temp_value = abs(temp_value);
         }
         else N_Flag = 0;
      }
    else temp_F = (9*temp_value/5+32)*10 + 6; //If decimal is greater or equal
                                             // to 0.5, add 0.5
    temp_value = (int)(temp_F/10);
    CF = 12;
    }
 
    if (CF_Flag == 0xff) CF = 11;
 
    DD0 = temp_value%10;  // Extract Ones Digit
    DD0 = mask(DD0);
    DD1 = (temp_value/10)%10; // Extract Tens Digit
    DD1 = mask(DD1);
    DD2 =  temp_value/100; // Extract Hundred digit
    CF = mask(CF);
    if (N_Flag == 1) DD2=10;
    else if (DD2 == 0) DD2 = 13 ;
    DD2 = mask(DD2) ;
 
      PORTB=0x00;
      CF_Flag =~CF_Flag;
 
    } while (1);
 
Last edited:
Why would you do a crappy switch-case structure instead of a simple look-up table? Swtich-case is the worst concept in C-language. Then there are 3 nested if-statements.. horrible for embedded systems. And many Ifs without curly brackets.. scary. The code is very much error-prone.
 
Last edited:
Why would you do a crappy switch-case structure instead of a simple look-up table? Swtich-case is the worst concept in C-language. Then there are 3 nested if-statements.. horrible for embedded systems. And many Ifs without curly brackets.. scary. The code is very much error-prone.

He isn't.... It was code copied from a working MikroC project.... I just helped him to convert it to Hitech!!
 
He isn't.... It was code copied from a working MikroC project.... I just helped him to convert it to Hitech!!

The code you copied is not a good code. Good thing he isn't using it.
 
Thanks for your help Ian. I'll probably get back to that this weekend. BTW, could you explain what you mean by "You may need to tweek the outputs as I may be 10 times out.."?
 
Forget that!!! I have since tested the output.... They read ok.... If you look at the floating point calculations, you'll see its easy to be 10 or 100 times out when converting to fixed point maths..
 
Just got a chance to try again. I get 2 errors now which seem to be related to the last line of code,

Code:
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 "C:\PK2 Lessons\LPC Demo Board\Thermometer\thermometer.c" -q --chip=16F628A -P --runtime=default --opt=default,+asm,-debug,-speed,+space,9 --warn=0 -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s" 
Error   [195] C:\PK2 Lessons\LPC Demo Board\Thermometer\thermometer.c; 139.0 expression syntax
Error   [300] C:\PK2 Lessons\LPC Demo Board\Thermometer\thermometer.c; 139.0 unexpected end of file
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 "C:\PK2 Lessons\LPC Demo Board\Thermometer\1wire.c" -q --chip=16F628A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
 
If you are referring to the code Ian posted, it is missing one curly bracket } at the end.
 
Thanks everyone, especially Ian. I put the last } in and it compiled.

Great! sorry (Ian) about some of my comments. I'm sometimes a little too "fanatic" about the C language. First thing to care about (when writing embedded code) is to make it work (in a way that makes sense and is logical).. then you can "tidy up" or optimize if necessary.
 
Last edited:
Thanks everyone, especially Ian. I put the last } in and it compiled.

Sorry about that!!! Copy and paste issue.... Once you get a bit more familiar, you'll spot these errors yourself.

Mr T!!! You sometimes forget people are just starting out.... Good C programming will eventually come their way but as for now... working code is good enough
 
As I understand the DS1820 has a serial number or something to identify it on the data line so you can have multiple sensors on the same line. If so, how would the PIC know the difference between them? Do we have to write the number into the code somewhere? The reason I ask is after compiling, programming, and building this nothing happens, no lights on the displays or anything.
 
It was working on ISIS very well.... Have you a pullup resistor on the data pin of the ds1820?

I didn't at first, that blog didn't make mention of one. After looking at the DS1820 in other circuits I started to wonder so I tried one and got the same result. I wonder if it matters that I'm using a DS18S20 instead. Digi-Key didn't carry the DS1820. I'm going to try reloading the PIC, maybe I messed up there.

All I have to do is open the workspace, select Project>Build, wait for a successful build, then select Programmer>Program, correct?
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top