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.

Restarting pic C programming help

Status
Not open for further replies.

Kryten

New Member
Hi
Im having trouble finding out what im doing wrong with my code. Im using MPlab with HiTech compiler

Code:
#include <htc.h>

 void main (void)
{
  
 while (1){
 #if (RA0 == 1 && RA1 ==0)
	{
	RA2 = 1;
	_delay_ms(5);
	RA2 = 0;
	}
#elif (RA0 == 0 && RA1 == 1)
	{
	RA2 = 1;
	_delay_ms(5);
	RA2 = 0;
	_delay_ms(500);
	}
#else 
	{
	RA2=0;
	}
}
}

I get this error :

Code:
Error   [100] ; . unterminated #if[n][def] block from line 28

Can someone pleas tell me what im doing wrong?
 

Pommie

Well-Known Member
Most Helpful Member
You shouldn't be using # as this is a compiler directive. Just use if, else if and else.

Mike.
 
Last edited:

Kryten

New Member
When i change my code to this :
Code:
#include <htc.h>

 void main (void)
{
  
 while (1){
 if (RA0 == 1 && RA1 ==0) 	// if input pin 0 is high and pin 1 low then
	{						// turn pin 2 on delay for 5 ms and turn off agin
	RA2 = 1;
	_delay.ms(5);
	RA2 = 0;
	}
else if (RA0 == 0 && RA1 == 1) // if input pin 1 is high and pin 0 low then
	{						   // turn on pin 2 for 5 ms and off for 500 ms
	RA2 = 1;				   // loop until pin 1 is low
	_delay_ms(5);
	RA2 = 0;
	_delay_ms(500);
	}
else 
	{
	RA2 = 0;
	}
}
}
I get a lot more errors.
Code:
Error   [192] \resp-trig.c; 22.5 undefined identifier "RA0"
Error   [192] \resp-trig.c; 22.17 undefined identifier "RA1"
Error   [192] \resp-trig.c; 24.1 undefined identifier "RA2"
Error   [196] \resp-trig.c; 25.11 struct/union required
Error   [192] \resp-trig.c; 30.1 undefined identifier "RA2"
Error   [196] \resp-trig.c; 31.11 struct/union required
Error   [196] \resp-trig.c; 33.11 struct/union required
Error   [192] \resp-trig.c; 37.1 undefined identifier "RA2"

Im using the 12f629 but cant find a proper .h file for it.. Could that be the reason why my pins wont be accepted?
 
Last edited:

Kryten

New Member
I might not have been good at doing this at all but I think I need some sort of port declaration, I mean I have to say witch pin is input and output, and set the states as all low or something.

Like I said in the top Im starting back up with C programming Its been almost tow years since I last entered a compiler so I want to get back in there again.
 

Pommie

Well-Known Member
Most Helpful Member
On the 12F629 Microchip renamed portA as GPIO and so you use GPIO3 etc as the pin definitions.

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
Just noticed a few errors in your code. So, here is a version that compiles,
Code:
#include <htc.h>
__CONFIG (UNPROTECT & MCLRDIS & PWRTDIS & WDTDIS & INTIO & BOREN);

#define _XTAL_FREQ 4000000

void main (void)
{
    GPIO    = 0x00;
    CMCON    = 0x07;
    TRISIO    = 0b11111011;            // GP2 is output  
    while (1){
        if (GPIO0 == 1 && GPIO1 ==0)     // if input pin 0 is high and pin 1 low then
        {                            // turn pin 2 on delay for 5 ms and turn off agin
            GPIO2 = 1;
            __delay_ms(5);
            GPIO2 = 0;
        }
        else if (GPIO0 == 0 && GPIO1 == 1)  // if input pin 1 is high and pin 0 low then
        {                                    // turn on pin 2 for 5 ms and off for 500 ms
            GPIO2 = 1;                        // loop until pin 1 is low
            __delay_ms(5);
            GPIO2 = 0;
            __delay_ms(125);
            __delay_ms(125);
            __delay_ms(125);
            __delay_ms(125);
        }
        else 
        {
            GPIO2 = 0;
        }
    }
}

Mike.
 

Kryten

New Member
Ok so my first try (not posted here) was kinda correct. I used GPIO.1

And i see that the delay is max 125 ms or is that just a way to make shure it wont do anything else that intened?

Thanks Mike for the help .

Edit.. I almost forgot that im not going to use xtal, just internal clock (the delay time is not critical)

Im going to look in the Data sheet for the pic to get it working as I want
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
The config line select the internal clock.
Code:
__CONFIG (UNPROTECT & MCLRDIS & PWRTDIS & WDTDIS & [COLOR="red"]INTIO[/COLOR] & BOREN);

The delay is converted into clock cycles and 500mS is too big a number. The cycles/2 needs to fit in 16 bits.

Mike.
 

Kryten

New Member
Reason I asked is that i see the line
Code:
#define _XTAL_FREQ 4000000
and I was not thinking about using a xtal.
Will it work without xtal connected?

Do i get it right when the definition is just to let the uC know the speed?
 

Kryten

New Member
Just to be sure i have gotten the hang of it now..

The pull down / up resistor should be some where in between 1 and 10 k right?
 

Kryten

New Member
Ok thanks for the info Bill. I was planning to use 5v as 1 but i guess i can change it.

Thats why i asked but also for future projects where I might have need for pullup or pulldown res.
 

Kryten

New Member
EDIT

It seems that the programmer i used neede to have config bits/word programmed separately to the uC ??!?!
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top