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

Reading DIP switches and Outputting ADC Result

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



Joined: 10 Feb 2008
Posts: 7

View user's profile Send private message

Reading DIP switches and Outputting ADC Result
PostPosted: Thu Feb 21, 2008 3:47 am     Reply with quote

Hi people,
just wondered if any one could help.....my program reads 4 ADC channels on my PIC (16f916) and stores the results in varables X1,X2,Y1 and Y2..

What I want to do is read two dip switches connected to port C (bits 0 and 1) and depending on the value display the relevant ADC result on Port B

i.e. if switches read...

00: then display X1 on port B
01: then display X2 on port B
10: then display Y1 on port B
11: then display Y2 on port B

any ideas would be welcome

thanks
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

Dip Switches
PostPosted: Thu Feb 21, 2008 4:24 am     Reply with quote

Code:

if (dip_0==1)
{
   if (dip_1==1)
   {
      //two dips ON aka binary 3
   }
   
   else
   {
      //Dip_1=ON Dip_0=Off aka binary 1
   }
}

else
{
   if (dip_1==1)
   {
      //Dip_0=OFF Dip_1=ON aka binary 2
   }
   
   else
   {
      //no dips ON aka binary 0
   }
}


Dip_0 LSB, Dip_1 MSB

You could also combine the bits into a int and use a switch(no pun intended) statement, but think this will suffice.

Regards
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Feb 21, 2008 5:26 am     Reply with quote

Code:

switch(input_c() & 0x03) {
case 0x00: // no dips
  printf("...");
  break;
case 0x01: // dip1 only
  printf("...");
  break;
case 0x02: // dip2 only
  printf("...");
  break;
case 0x03: // both
  printf("...");
  break;
}
jonnylazer



Joined: 10 Feb 2008
Posts: 7

View user's profile Send private message

PostPosted: Fri Feb 22, 2008 5:42 am     Reply with quote

HI

I have simplified things because I think the problem is a basic one..

here is some code I have written... what it attempts to do is check what value is on PIN_A0, if its a 1 then output 0xFF to port B and if its a 0 then output 0x00 to port B .

.....................................................................................

#INCLUDE <16f916.h>
#DEFINE DEVICEOSC 8000000
#fuses HS,NOWDT,PUT,NOPROTECT,BROWNOUT,
#use delay(clock=DEVICEOSC)
#DEFINE IN PIN_A0

void LEDS_ON(void){
output_b(0xFF);}


void LEDS_OFF(void){
output_b(0x00);}


void main(void) {



while(true)
{
if (input(IN))
LEDS_ON ();

else
LEDS_OFF ();

}

}

.............................................................................

can anybody see whats wrong here???

thanks for reading
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