CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Really simple input/output LED c code

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ViserExcizer



Joined: 13 Mar 2011
Posts: 1

View user's profile Send private message

Really simple input/output LED c code
PostPosted: Sun Mar 13, 2011 3:38 am     Reply with quote

Hello, I need help, this is my first time coding in C

And i wrote the program below :

Code:

#include <16F877.h>
#use delay(clock=4000000,oscillator)

void main(){
   set_tris_a(0xb00011111);
   set_tris_b(0b11111111);
  while(true){

  //testing   output_b(0b0000001); 

      if(input(PIN_A0)==0){
      output_b(0b0000000);   
     
      }else if(input(PIN_A0)==1){

      output_b(0b0000001);
      }
   }   
   
}


The program is supposed to blink the LED connected to RB0 if
the switch to RA0 is pressed.

But It doesnt work. something is wrong with my syntax,
If i comment out the //testing output_b(0b0000001);
nothing happens and if i leave it in, the LED is just on, and never goes off when i press the switch to RA0



However the following program below works, because its a purely PIC output and no input is needed, which lead me to believe that there is something wrong with the code above. Please help. Thanks in advance.

Code:

#include <16F877.h>
#use delay(clock=4000000,oscillator)

void main(){
   set_tris_a(0xb00011111);
   set_tris_b(0b11111111);
  while(true){     

     output_b(0b11111111);
     delay_ms(1000);
     output_b(0b00000000);
     delay_ms(1000);     
   }   
   
}
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Sun Mar 13, 2011 5:36 am     Reply with quote

Try without the 'else if' and instead use 'else'

Code:
#include <16F877.h>
#use delay(clock=4000000,oscillator)

void main(){
   set_tris_a(0xb00011111);
   set_tris_b(0b11111111);
  while(true){

      if(input(PIN_A0)==0)
      {
         output_b(0b0000000);   
      }
      else
      {
         output_b(0b0000001);
      }
   }   
   
}

_________________
Vinnie Ryan
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Sun Mar 13, 2011 9:03 am     Reply with quote

PORTA powers up as analog inputs. You need to configure it for digital i/o.
This information is in the data sheet for the part.
_________________
David
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Mar 13, 2011 9:28 am     Reply with quote

By the way ..You don't need the set tris statements....the CCS compiler handles it. In the rare circumstance you need extremely fast I/O select FAST i/o and use set tris otherwise it is not needed.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 13, 2011 1:43 pm     Reply with quote

See this post for an example program and schematic:
http://www.ccsinfo.com/forum/viewtopic.php?t=43629&start=4


Also, the CCS compiler sets the TRIS for you. You don't need to set it.
And, please read the PIC data sheet on the correct setting of TRIS bits
to make pins into output pins. You will find a problem with the following:
Quote:
set_tris_b(0b11111111);
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Mon Mar 14, 2011 7:00 am     Reply with quote

Are you using a pullup resistor on the Port A pin, with the switch going to Gnd? If there's no pullup, you won't get reliable operation. Why not have your input going to Port B, and use its built-in pullups, and make Port A be the output?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group