Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I am trying to write code that will set bit0 on PORTA in the PIC18F452 high if any of the conditions in the IF statements has been met. I am using the MPLAB ICD2 debugger and the Microchip C18 Toolsuite. The code is as follows:

#include <p18f452.h>

void main (void)
{
    // array containing vital sign values 
    unsigned char pulseR = 0x32;  // syntax error 
    unsigned char sysP = 0x64;
    unsigned char diasP = 0x26;

    // acceptable limits for vital signs according to relavant heath officials
    unsigned char pulseR_ULimit = 0xA0;
    unsigned char pulseR_LLimit = 0x28;

    unsigned char diastolic_ULimit = 0x5A;
    unsigned char diastolic_LLimit = 0x3C;

    unsigned char systolic_ULimit = 0x8C;
    unsigned char systolic_LLimit = 0x5A;

    TRISAbits.TRISA0 = 0;

    //Comparing obtained values with acceptable limits
    if (sysP < systolic_LLimit || sysP > systolic_ULimit)
    {
        PORTAbits.RA0 = 1;
    }
    else if (diasP < diastolic_LLimit || diasP > diastolic_ULimit ) 
    {
        PORTAbits.RA0 = 1 ;    
    }
    else if (pulseR < pulseR_LLimit || pulseR > pulseR_ULimit ) 
    {
        PORTAbits.RA0 = 1;
    }
    else                 
        PORTAbits.RA0 = 0;
}

Added:

The Build output is as follows:

 Debug build of project `C:\Users\Owner\Desktop\School Files\ECNG 3006 2011\Labs\Lab 3\Softare-Hardware Only\Lab3_3.mcp' started.
Preprocessor symbol `__DEBUG' is defined.
Sat Nov 19 13:56:03 2011
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Users\Owner\Desktop\School Files\ECNG 3006 2011\Labs\Lab 3\Softare-Hardware Only\Lab3_3.mcs".
Clean: Done.
Couldn't locate build tool.  Check tool locations.
----------------------------------------------------------------------
Debug build of project `C:\Users\Owner\Desktop\School Files\ECNG 3006 2011\Labs\Lab 3\Softare-Hardware Only\Lab3_3.mcp' failed.
Preprocessor symbol `__DEBUG' is defined.
Sat Nov 19 13:56:05 2011
----------------------------------------------------------------------
BUILD FAILED

Can somebody please help me troubleshoot this code?

share|improve this question
@D Brown - copy and paste the entire output. That is not the actual error message. It will probably on the line underneath "Executing C:\...etc" – Oli Glaser Nov 18 '11 at 20:05
@Oli Glaser - See edits to questions above. – D Brown Nov 19 '11 at 18:04
@D Brown - see edit to answer. Let me know how it goes. If it doesn't work can you replace the above error with the new one. – Oli Glaser Nov 19 '11 at 18:55
@Oli Glaser - I did what you suggested in the edits, but I'm still having the same problem... – D Brown Nov 21 '11 at 1:40
@D Brown - Check that Project -> Select Language Toolsuite is set correctly to C18. If it is, then the executables are probably still not set correctly, make sure all 4 shown in the below picture are set to the right path. – Oli Glaser Nov 21 '11 at 5:50

2 Answers

For setting an output you should use LATAbits.RA0 = x instead of PORTAbits.RA0 = x.

Also, try adding some brackets. Try this:

#include <p18f452.h>

void main (void)
{
    // array containing vital sign values 
    unsigned char pulseR = 0x32;  // syntax error 
    unsigned char sysP = 0x64;
    unsigned char diasP = 0x26;

    // acceptable limits for vital signs according to relavant heath officials
    unsigned char pulseR_ULimit = 0xA0;
    unsigned char pulseR_LLimit = 0x28;

    unsigned char diastolic_ULimit = 0x5A;
    unsigned char diastolic_LLimit = 0x3C;

    unsigned char systolic_ULimit = 0x8C;
    unsigned char systolic_LLimit = 0x5A;

    TRISAbits.TRISA0 = 0;

    //Comparing obtained values with acceptable limits
    if ((sysP < systolic_LLimit)||(sysP > systolic_ULimit))
    {
        LATAbits.RA0 = 1;
    }
    else if ((diasP < diastolic_LLimit)||(diasP > diastolic_ULimit)) 
    {
        LATAbits.RA0 = 1 ;    
    }
    else if ((pulseR < pulseR_LLimit)||(pulseR > pulseR_ULimit)) 
    {
        LATAbits.RA0 = 1;
    }
    else 
    {                
        LATAbits.RA0 = 0;
    }
}
share|improve this answer

Put the TRISAbits.RA0 = 0; after the declaration of your variables (they need to be declared at the start of routines)
So just after unsigned char systolic_LLimit = 0x5A;
It should compile fine then.

EDIT - check your Project build options (Project->Build Options->Project) are set up for the correct include directory. Here is a clip of my settings for the latest C18 compiler (you may have to root around on your C drive for yours if installed in a different place or using a previous version)

Build Options

EDIT 2 - yes, the header file you are using is the problem. If you look at the structure for TRISA, you can see the bits are named TRISAx:

 extern volatile near unsigned char       TRISA; 
extern volatile near struct { 
  unsigned TRISA0:1; 
  unsigned TRISA1:1; 
  unsigned TRISA2:1; 
  unsigned TRISA3:1; 
  unsigned TRISA4:1; 
  unsigned TRISA5:1; 
  unsigned TRISA6:1; 
} TRISAbits; 

So if you change it to TRISAbits.TRISA0 = 0; it should work. Similarly you may have to change other things to comply with the header file.
Or don't change anything and just start using microchips latest header file (it should be in the compiler directory named \h (see above picture)

EDIT 3 - "can't find build tool"
This means MPLAB can't find the compiler executable. You need to go to "Project -> Set Language Tool Locations" and set the C18 Toolsuite executables correctly.
See this thread for some discussion of the issue. This is a snip of how it look on my laptop (note the path/file may be different on your computer depending on what version you are using):

C18 Location

share|improve this answer
So I have tried your advice, but now I get an error that says: "unknown member 'RA0' in '__tag_24'" at the same line TRISAbits.RA0 = 0;, even though I have included the relevant header file. Could you help me troubleshoot this? – D Brown Nov 18 '11 at 18:24
I just tried it here and it compiles fine. Have you got the 0 in RA0 correct? (i.e. a numeric zero) If that's okay then it may be something up with the header file. Try using #include <p18cxxx.h> instead. What version of the compiler are you using? – Oli Glaser Nov 18 '11 at 18:29
Yes, it is RA0, but I am still getting the same error unfortunately. I got the p18f452 header file from this link (read.pudn.com/downloads155/sourcecode/embed/685984/h/…), could the header file that I am using be the problem. – D Brown Nov 18 '11 at 18:35
Ah right, that's the issue - see edit. – Oli Glaser Nov 18 '11 at 18:43
I have tried your second change and I get the follwing error: "Debug build of project C:\Users\Owner\Desktop\School Files\ECNG 3006 2011\Labs\Lab 3\Softare-Hardware Only\Lab3_3.mcp' failed. Preprocessor symbol __DEBUG' is defined." Why would it fail? – D Brown Nov 18 '11 at 18:54
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.