PIC18F65J11 - Reading ports and giving an Integer Value

Thread Starter

Stackington21

Joined Oct 16, 2015
11
Hi all
Currently making a project that needs to read the inputs of 6 ports. I have 3 sensors and 3 push buttons that I need to return an integer value back to the main function. These need to be separate routines because the integers are totally separate, the only common ground is sharing the PORTC.

I thought of reading the entire port and then the appropriate HEX values given could in turn give the integer value but I assume there would be some way to read each individual port and if an active high is given, the integer value is returned to the main. Very confused with how each individual port can get read, i am still quite basic when it comes to C programming, any help will be tops!

Thanks
 

Dodgydave

Joined Jun 22, 2012
11,284
Normally when reading a port bit, you use the command BTFSS/C Port?, this tests if the bit on that port is high or low, Or MOVFW to read the whole port, if your reading an analogue value you need to use the A/D.
 

Thread Starter

Stackington21

Joined Oct 16, 2015
11
Ok so how does BTFSS/C work to read each port? Note i have already set all of the ports to be inputs. Originally i thought of using int values and if functions to pull those integers into the main function. Note the sensors are on ports RC3, RC4 and RC5 of the pic18

void sensor(void)
{
int a, b, c, d;
a = 0;
b = 1;
c = 2;
d = 3;

if(PORTC = 0x00)
{
int a;
}
if(PORTC = 0x08)
{
int b;
}
if(PORTC = 0x10)
{
int c;
}
if(PORTC = 0x20)
{
int d;
}
}
 

ErnieM

Joined Apr 24, 2011
8,377
You want to return 4 different integers holding a constant representing individual switches? That seems to be a long way around to get someplace next to you.

First off, you want to define these integers not in the function (where they are hidden) but in the main function where you need to access them. When calling your functn you pass a reference (or pointer) to these so the sub function can change the values of the main variables. Or, just make them global variables defined before the main function so any and all of your code can see and change them.

But why do any of that? PORTC already contains the data you are yanking apart for reasons not very apparent. Just test the port in Main() with a comparison to whatever button you seek.

Code:
    if (!PORTC) { do something for button A }
    ...
    if (PORTC & 0x20) { do something for button D }
However, you do not seem to be doing any button debouncing, so I expect you to have an issue with that sooner or later.
 

Picbuster

Joined Dec 2, 2013
1,047
The most easy way is at interrupt to :
collect the byte(s)
set a flag in interrupt .

in main you check for flag when set do what you have to do and clear flag.
Debounce: the flag is set and remains set until reset no nasty pulses from bouncing signals unless the handling time is shorter then wanted debouncing time. When handling time is to long you will miss an interrupt.
 

dannyf

Joined Sep 13, 2015
2,197
Assuming those are digital pins, here is what I would do.

#define sens1port. Portb //active high
#define sens1pin. (1<<2). //on pin2
#define sens1on() (sens1port & sens1pin) //high if sens1 is on

#define sens2port. (~portc). //active low
#define sens2pin. (1<<6). //on pin6
#define sens2on() (sens2port & sens2pin) //high if sens2 is on

So in your code you can write

If (sens1on()) do something...

Very easy to read. And if you change pun assignment or use reverse logic, change the corresponding defines and recompile.
 
Top