• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

reading an input

Status
Not open for further replies.

hobby_85

New Member
hey, im trying to brush up my skills by doing a couple of small exercises. I keep getting stuck on this one. Im trying to program my pic 16f688 such that it lights and LED when a switch is pressed. this is the code i have written.


#include <htc.h>

#define _XTAL_FREQ 4000000
#define SWITCH RC2
#define LED RC0

__CONFIG(MCLREN & UNPROTECT &WDTDIS);

void main()
{

TRISC = 0b00000100; //set rc2 as input and the rest as output

for(;;)
{
// PORTC = 0b00000000; //turn off everything. all low
if(RC2 == 1){
PORTC =0b00000001;
}
else{
PORTC =0b00000000;
}
}
}

The code compiles well, but when i put the chip on a breadboard and conect up everything according to this link (pg 22) http://www.electro-tech-online.com/custompdfs/2009/08/PIC_Base_C_1.pdf

the led just stays on forever. regardless of where the switch is connected.

Any suggestions?

Thanks heaps

im using a 16f688...if that helps.thanks!
 
Last edited:

be80be

Well-Known Member
You need to set portC right
Code:
CMCON0 = 7    //digital I/O
ANSEL = 0      //
PortC on that chip has ADC you have to turn it off or RC2 will read high
 
Last edited:

kpatz

New Member
You need to configure the PORTC pins as digital inputs, as the default on the 688 is analog (A/D or comparator).

Before you set TRISC, add these lines:

Code:
  CMCON0 = 7;   // Set RC<4,1:0> to digital I/O
  ANSEL = 0; //digital I/O
 

hobby_85

New Member
it works mate. thanks heaps.

so what does those two lines means? i have the datasheet in front of me here. rc0,rc1,rc2 and so on has AN5,AN4,AN6 next to it. So it means they are analog inputs and i need to swap em to digital, correct?.
 

hobby_85

New Member
and does this mean i have to do this for portA as well. i just glanced at the datasheet again. it says 'The ANSEL and CMCON0 registers must be initialized to configure an analog
channel as a digital input. Pins configured as analog inputs will read ‘0’.'...

so yeah, understand it. but dont quite know why CMCON0 = 7;

thanks
 

be80be

Well-Known Member
On that chip it turns them off on PORTA and PORTC
with those to settings
 
Last edited:

be80be

Well-Known Member
I was going to tell you but it wouldn't let me there net was down or something
any way H7 is 0111 if you look in the datasheet it tells you to turn off the comparator
to set bits 2-0 to 111 so you give it a CMCON0 = 7 that turns off the comparator by
setting 2-0 to 111
 

hobby_85

New Member
I was going to tell you but it wouldn't let me there net was down or something
any way H7 is 0111 if you look in the datasheet it tells you to turn off the comparator
to set bits 2-0 to 111 so you give it a CMCON0 = 7 that turns off the comparator by
setting 2-0 to 111
sorry man. could you tell me which page of the datasheet your looking at? im looking at page 33. Cant make out anything about CMCON0 =7.

Sorry about this.

Also, would you happen to know much about the PWM function of a PIC? Not the 16f688, but like the 16f690 version? Im actually trying to make a 40khz pulse and feed it into my ultrasonic transmitter.

thanks heaps.
 

be80be

Well-Known Member
It's on page 63
Code:
bit 7 C2OUT: Comparator 2 Output bit
When C2INV = 0:
1 = C2 VIN+ > C2 VIN-
0 = C2 VIN+ < C2 VINWhen
C2INV = 1:
1 = C2 VIN+ < C2 VIN-
0 = C2 VIN+ > C2 VINbit
6 C1OUT: Comparator 1 Output bit
When C1INV = 0:
1 = C1 VIN+ > C1 VIN-
0 = C1 VIN+ < C1 VINWhen
C1INV = 1:
1 = C1 VIN+ < C1 VIN-
0 = C1 VIN+ > C1 VINbit
5 C2INV: Comparator 2 Output Inversion bit
1 = C2 output inverted
0 = C2 output not inverted
bit 4 C1INV: Comparator 1 Output Inversion bit
1 = C1 Output inverted
0 = C1 Output not inverted
bit 3 CIS: Comparator Input Switch bit
When CM<2:0> = 010:
1 = C1IN+ connects to C1 VINC2IN+
connects to C2 VIN-
0 = C1IN- connects to C1 VINC2IN-
connects to C2 VINWhen
CM<2:0> = 001:
1 = C1IN+ connects to C1 VIN-
0 = C1IN- connects to C1 VINbit
2-0 CM<2:0>: Comparator Mode bits (See Figure 7-5)
000 = Comparators off. CxIN pins are configured as analog
001 = Three inputs multiplexed to two comparators
010 = Four inputs multiplexed to two comparators
011 = Two common reference comparators
100 = Two independent comparators
101 = One independent comparator
110 = Two common reference comparators with outputs
111 = Comparators off. CxIN pins are configured as digital I/O
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top