unable to read the indual ports

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

unable to read the indual ports

Post by saravana_3 »

in my following program I assign port A as the input and portB as the output, when I run the program I can control the outputs but I cannot scan my input ports, I dont know how to select the portA,0 when using the program the response only from the portA,4 but by pressing other ports of portA there is no response, pls tell me where am I making the mistake.


/* project name Micstation
date: 25.04.08
watch dog timer disabled
high speed ossilator enabled
low voltage programming disabled
*/
unsigned int OUTPUT_STATUS, COUNT1, COUNT2, COUNT3, COUNT4, COUNT5, COUNT6, COUNTER_STATUS;
unsigned int input;
char test_bit;
char oldinput;


void main() {
//STATUS = 0X03;
//PORTA = 0X05;
//PORTB = 0X06;
OUTPUT_STATUS = 0X21;
COUNT1 = 0X22;
COUNT2 = 0X23;
COUNTER_STATUS = 0X24;
COUNT3 = 0X25;
COUNT4 = 0X26;
COUNT5 = 0X27;
COUNT6 = 0X28;
test_bit = 0X29;
PORTA = 0X0;
TRISA = 0XFF;
PORTB = 0X0;
TRISB = 0X0;
PORTC = 0;
TRISC = 0;
PORTD = 0;
TRISD = 0;
OUTPUT_STATUS = 0;
input = PORTA;
oldinput = input;
while(1) {
//PORTC = 0xff;
//Delay_ms(1000);
asm {
BTFSS Status,3
nop
bsf portC,7 }

if (Button(&PORTA, 1, 1, 0)) {
test_bit = 1;
//PORTB = 0XD2;

}
else
PORTB = 0XD1;

if (test_bit && Button(&PORTA, 1, 1, 0)) {

PORTB = ~PORTB;
test_bit =0; }

}
}
saran

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: unable to read the indual ports

Post by Steve »

If you are using a chip with ADC inputs (e.g. the 16F88 or 16F877a), then you will need to turn these off within your code. The registers used depend on the chip you are using - have a look at the chip's datasheet.

Hurdy
Posts: 11
Joined: Fri Dec 09, 2005 3:03 am
Location: South Wales
Contact:

Re: unable to read the indual ports

Post by Hurdy »

I'm having the same problem. I've disabled the AD converter but still reading in nothing. I can only read in portA LA4.

Any ideas?

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: unable to read the indual ports

Post by Steve »

It's not always just the ADC that needs to be turned off. For example, the 16F628 needs to have the comparator module turned off before the PortA inputs work as digital inputs.

I can't really help any more until I know which chips you guys are using.

saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

Re: unable to read the indual ports

Post by saravana_3 »

hi I have solved the problem, I am using the pic16f877a by default the PORTA is as the analog input, to make it as the digital in change the starus in the ADCON1, for this chip I declare the ADCON1 (0-3) as 0111, then I can use the port A as the input
Saran
saran

Hurdy
Posts: 11
Joined: Fri Dec 09, 2005 3:03 am
Location: South Wales
Contact:

Re: unable to read the indual ports

Post by Hurdy »

ok brilliant thanks for the tips. I'm using an 18F4620 with Microchip C18.
When I have a few moments I'll look at my code again and try to enable PORTA as an input. I just assumed all ports were configured as digital in as standard. But there we, you learn something every day.

Cheers for the advice,

I'll post back my results when I get time.

Post Reply