Page 1 of 1

MPLAB IDE code

PostPosted: Fri Apr 20, 2012 1:50 pm
by shinigami.alv
I'm using MPLAB IDE to compile but having problems with some of the code here. MPLAB does not recognize some of the codes i wrote. It cannot identify pin_C2 etc. How to define single pin in MPLAB? i previously used C compiler IDE and it worked fine. below is part of my code:

if (input(pin_C2))
{
no=1;
output_high(pin_D2);
while(input(pin_C2));
delay_ms(2000);
output_low (pin_D2);
send_packet(no);
}

Re: MPLAB IDE code

PostPosted: Fri Apr 20, 2012 3:43 pm
by Idris
shinigami.alv WROTE:I'm using MPLAB IDE to compile but having problems with some of the code here. MPLAB does not recognize some of the codes i wrote. It cannot identify pin_C2 etc. How to define single pin in MPLAB? i previously used C compiler IDE and it worked fine. below is part of my code:

if (input(pin_C2))
{
no=1;
output_high(pin_D2);
while(input(pin_C2));
delay_ms(2000);
output_low (pin_D2);
send_packet(no);
}


From the code you wrote, its more to CCS compiler. For MPLAB + Hi Tech C it should be something like this.
if(RC2==1)
{
no = 1;
RD2 = 1;
while(RC2==1);
__delay_ms(2000);
RD2 = 0;
send_packet(no);
}

Re: MPLAB IDE code

PostPosted: Fri Apr 20, 2012 8:21 pm
by shinigami.alv
thanks for the reply. now error 499 occurred which is caused by the delay code (undefined symbol: _delay_ms(basic.obj) ). searched online and stated that i need to include (#include <plib.h>) and in the project build options, on the linker tab check the "Link in Peripheral Library" box in MPLABĀ® IDE. however, i cannot check the box.

Re: MPLAB IDE code

PostPosted: Sat Apr 21, 2012 3:11 am
by shinigami.alv
well, i kinda solved the problem and can now build successfully. but now my hardware is not working. my program supposes to light up LED for 2 secs after releasing a button. sorry for the questions as i just recently changed to MPLAB. below is my code:

CODE: SELECT_ALL_CODE
#include <pic.h>
#define _XTAL_FREQ 20000000

void main(void)
{             
 
  TRISD = 0b000000;
  TRISC = 0b111111;;

   while(1)
  {
 
  if (RC2==1)
  {
 RD2==1;
 while (RC2==1);
 __delay_ms(2000);
 RD2==0;
 }
 
 else if (RC3==1)
  {
 RD3==1;
 while(RC3==1);
 __delay_ms(2000);
 RD3==0;
 }
 
 else RD==1;
 
   }

 
}

Re: MPLAB IDE code

PostPosted: Sat Apr 21, 2012 3:57 am
by sich
Firstly, you're using the wrong operator.

= is the assignment operator. RD2 = 1; will set the RD2 equal to the value 1.
== is the equality operator. This operator returns true if both values are equal, and returns false if they are not.

Secondly, you have to make sure your hardware is active-High or active-Low. When the switch is pressed, does it produces logic Low '0' to the input pin? Or does it gives High '1' instead? That determines you should compare '0' or '1' in your if and while statements.

Thirdly, RD at the last line should give you an error.

Re: MPLAB IDE code

PostPosted: Sat Apr 21, 2012 4:29 am
by shinigami.alv
okay, thanks for pointing out my silly mistake on the operator stuff. ya, my 16F877A is active-low, but i use an inverter to make it active high so no problem on that. Btw, how to set port D high? can 'PORTD = 255' do the trick? coz i tried that and program still not working. (changed the equality and comparator operator too.)

Re: MPLAB IDE code

PostPosted: Sat Apr 21, 2012 4:52 pm
by shinigami.alv
never mind, it's solved already. it turned out to be i did not put '__CONFIG (0x3F32);' . thanks for your help!

Re: MPLAB IDE code

PostPosted: Sun Apr 22, 2012 5:39 pm
by sich
I missed that too :P Anyway, glad that the problem is solved.