View previous topic :: View next topic |
Author |
Message |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
get(portb) |
Posted: Sun Jun 02, 2013 1:16 pm |
|
|
Hello everyone,
Is there a method where i can get the current weight value of some port
for example
Code: | get_portB()
{
//code
return weight;
} |
note: PORTB is OUTPUT
Thanks in advance,
z3ngew
Last edited by z3ngew on Sun Jun 02, 2013 1:55 pm; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Sun Jun 02, 2013 1:24 pm |
|
|
weight?...
The port just has an 8bit value.
input_a()
input_b() etc.,
return this value for the corresponding port. |
|
![](templates/subSilver/images/spacer.gif) |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Sun Jun 02, 2013 1:46 pm |
|
|
I'm sorry but is it possible that you provide me a code snippet ? I cannot get it to work. Is there some directive that need to be declared ? I used it like this:
Code: |
int16 data;
data = input_B(); |
Thanks,
z3ngew |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Sun Jun 02, 2013 1:56 pm |
|
|
Depends on your chip.
In some cases the ports wake up configured for other things (generally not). So for instance on a PIC18F4550, if the PBADEN fuse is set, the whole port wakes configured for analog I/O, and you have to turn this off. There are similar things with things like some ECCP ports, etc..
This is always down to reading the chip data sheet, looking at the peripherals listed on the pins, and what their 'priority' is, then turning off any that override the normal I/O. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9173 Location: Greensville,Ontario
|
|
Posted: Sun Jun 02, 2013 1:57 pm |
|
|
Since Ports on a PIC are 8 bit wide,you need to declare the receiving variable as either int8 or unsigned int8 NOT int16!
int8 incoming_data;
incoming_data=port_b();
I usually say...
char incoming_data;
as 'char' is the same as 'unsigned int8' so data is from 000 to 255
This should work, if not, please post your entire program as there must be something else wrong...
hth
jay |
|
![](templates/subSilver/images/spacer.gif) |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Sun Jun 02, 2013 2:03 pm |
|
|
the data is NOT INPUT,
the TRIS register is 0 => output
I just want to know the current state value of the port??
thanks,
z3ngew |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Sun Jun 02, 2013 2:30 pm |
|
|
Talk about moving the goalposts.....
Code: |
#BYTE LATB=getenv("SFR:LATB")
//Then just read LATB as a variable
|
This will just read the output latch. |
|
![](templates/subSilver/images/spacer.gif) |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Sun Jun 02, 2013 2:35 pm |
|
|
This works great, Many thanks my friend. |
|
![](templates/subSilver/images/spacer.gif) |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Sun Jun 02, 2013 4:59 pm |
|
|
I'm sorry but this method seem to work in very strange way,
if i make the following:
Code: | #BYTE LATB=getenv("SFR:LATB")
int 8 data;
data = latb;
output_b(0);
printf(lcd_putc, "%d", data); |
Now the result is supposed to be there, but the result is a number 53
how com?
Thanks in advance,
z3ngew |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Mon Jun 03, 2013 1:45 am |
|
|
So?.
The number will be what was in the latch at the moment you read it. Dependant on what was previously written, how the chip happened to boot, etc. etc..
It'll be zero, if you read it after you write (unless pins that are controlled by other peripherals are involved). |
|
![](templates/subSilver/images/spacer.gif) |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Mon Jun 03, 2013 3:46 am |
|
|
this code works perfectly
Code: | #byte MCU_PORTA = getenv("SFR:PORTA") |
Thanks for your support,
z3ngew |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19365
|
|
Posted: Mon Jun 03, 2013 4:02 am |
|
|
Er.
You seem to be misunderstanding things wildly.
The register 'PORTB', is the physical port.
The register 'LATB', is the output latch
If as you said the TRIS is set to 0, then they will only differ if the pins are overloaded.
If you write to the output latch, and the TRIS is zero, then the value you write will be sent to the port drivers. If you read the latch, this will show what is being sent to the drivers. If the value from the port differs from the latch, then something is preventing the pins going to the levels specified in the latch, and the chip is being overdriven.
PORTB, is what input_b() returns.
I have a sneaking suspicion you are trying to control TRIS yourself, and have not selected FAST_IO.
#use fast_io(b)
Then you can control TRIS and the compiler will not override your settings, and use input_b to read the port. |
|
![](templates/subSilver/images/spacer.gif) |
|