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 support@ccsinfo.com

playing with key on PIC16F877A

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



Joined: 12 Jan 2013
Posts: 10

View user's profile Send private message AIM Address ICQ Number

playing with key on PIC16F877A
PostPosted: Wed Apr 03, 2013 10:40 pm     Reply with quote

hello everyone,

I am beginner in embedded system programming, still i have been learning it. The below code is when switch is pressed --> led ON.
My question is can I modify it to get when switch is pressed --> led OFF, such as reverse of this code, and initially all the led's are ON ?
Code:

#include <trial_1.h>
#define KEY_1 0x10
#define LED_1 0x01
#define KEY_2 0x20
#define LED_2 0x02
#define KEY_3 0x40
#define LED_3 0x04
#define SET_PIN 0x70
#define LED_OFF 0x0

#use delay(crystal=4000000)

void main ()
{
int8 key_input = LED_OFF;
port_b_pullups (true);
set_tris_b (SET_PIN) ; // 0111 0000 -> configure RB0, RB1, RB2 as output
                       //           -> configure RB4, RB5, RB6 as input
output_b (LED_OFF) ;   // Initialize all to off
 

//infinite loop
while (1)
  {
   key_input = SET_PIN & ~input_b();
     
   if ( key_input==KEY_1)        //  0x10  0001 0000 configure RB4 as input RB4
      output_b (LED_1) ; // 0x01  0000 0001 configure Rb0 as output
   else if( key_input==KEY_2 )   // 0x20 0010 0000 configure RB5 as input RB5
      output_b (LED_2) ;   //0x02  0000 0010 configure RB1 as output
   else if( key_input==KEY_3)   // 0x40 0100 0000 configure RB6 as input  RB6
      output_b (LED_3) ; //0x04  0000 0100 configure RB2 as output
   }
}
 

Thanks
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu Apr 04, 2013 6:52 am     Reply with quote

Hi,

I'd recommend that you use the 'Input' function to test your switch, and the 'Output_toggle' function to control your LED. You should be able to accomplish what you want in 5 or 6 lines of code, and it will be much simpler and easy to read!


Code:


#define SW_Input PIN_A0
#define LED PIN_A1

main()
{

Output_high(LED);

while(1)
{

if (input(SW_Input))
output_toggle(LED);

}



Obviously, change the pin defines to match your hardware, and change the 'Output_high' statement to 'Output_low' to reverse the startup behaviour of the LED. You'll also need to add the #include for your PIC, and the correct fuses.

John (the kinder and gentler version!)
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