MPLAB IDE code

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

MPLAB IDE code

Postby shinigami.alv » Fri Apr 20, 2012 1:50 pm

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);
}
shinigami.alv
Newbie
 
Posts: 12
Joined: Sat Mar 31, 2012 1:34 am

Re: MPLAB IDE code

Postby Idris » Fri Apr 20, 2012 3:43 pm

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);
}
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: MPLAB IDE code

Postby shinigami.alv » Fri Apr 20, 2012 8:21 pm

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.
shinigami.alv
Newbie
 
Posts: 12
Joined: Sat Mar 31, 2012 1:34 am

Re: MPLAB IDE code

Postby shinigami.alv » Sat Apr 21, 2012 3:11 am

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;
 
   }

 
}
shinigami.alv
Newbie
 
Posts: 12
Joined: Sat Mar 31, 2012 1:34 am

Re: MPLAB IDE code

Postby sich » Sat Apr 21, 2012 3:57 am

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.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 604
Joined: Tue Apr 21, 2009 2:15 pm

Re: MPLAB IDE code

Postby shinigami.alv » Sat Apr 21, 2012 4:29 am

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.)
shinigami.alv
Newbie
 
Posts: 12
Joined: Sat Mar 31, 2012 1:34 am

Re: MPLAB IDE code

Postby shinigami.alv » Sat Apr 21, 2012 4:52 pm

never mind, it's solved already. it turned out to be i did not put '__CONFIG (0x3F32);' . thanks for your help!
shinigami.alv
Newbie
 
Posts: 12
Joined: Sat Mar 31, 2012 1:34 am

Re: MPLAB IDE code

Postby sich » Sun Apr 22, 2012 5:39 pm

I missed that too :P Anyway, glad that the problem is solved.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 604
Joined: Tue Apr 21, 2009 2:15 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 0 guests

cron