View previous topic :: View next topic |
Author |
Message |
jonnylazer
Joined: 10 Feb 2008 Posts: 7
|
Reading DIP switches and Outputting ADC Result |
Posted: Thu Feb 21, 2008 3:47 am |
|
|
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
|
Dip Switches |
Posted: Thu Feb 21, 2008 4:24 am |
|
|
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
|
|
Posted: Thu Feb 21, 2008 5:26 am |
|
|
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
|
|
Posted: Fri Feb 22, 2008 5:42 am |
|
|
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 |
|
|
|