View previous topic :: View next topic |
Author |
Message |
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
Testing Output |
Posted: Mon Jan 09, 2006 5:31 am |
|
|
Hi,
If I set an output high, How can I test the to see if the putput is high with C.
Any assistance will be greatly appriciated.
Andrew _________________ What has been learnt if you make the same mistake? |
|
|
Guest
|
|
Posted: Mon Jan 09, 2006 7:00 am |
|
|
Use input() function (see p119of October 05 manual - pdf file available on website)
e.g.
Code: | if( input(PIN_A0) )
printf("A0 is now high\r\n"); |
|
|
|
Ttelmah Guest
|
|
Posted: Mon Jan 09, 2006 7:47 am |
|
|
The only problem with this, is that if using standard_IO, the compiler will switch the pin to being an input, when this is executed. You will know it was 'high' when read, but it may well no longer be so. The answer is to directly read the input latch instead, or to use fixed/fast I/O.
So, if using standard_IO, on a 18F PIC:
Code: |
#byte INPUT_LATCH = 0xF80
#bit A0 = INPUT_LATCH.0
output_high (PIN_A0);
//This sets the pin as an output, and drives this high
while (A0==0) {
//waiting here for the pin to go high
//I'd suggest havin a counter, and a 'error' timeout
//If is not high in a few counts
}
//Here the pin is high
|
Best Wishes |
|
|
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Mon Jan 09, 2006 12:40 pm |
|
|
Thaks _________________ What has been learnt if you make the same mistake? |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
Re: Testing Output |
Posted: Mon Jan 09, 2006 7:42 pm |
|
|
Storic wrote: | Hi,
If I set an output high, How can I test the to see if the putput is high with C.
Any assistance will be greatly appriciated.
Andrew |
You think your confused now...
This depends on the load on the PIC's output pin. It is possible for you the set the ouput high, the PIC to drive the output high accordinging but if you "read" the (loaded) pin you will see it as low. Also you have to take into account the type of input. You might be driving a load which includes a CMOS level device but the PIC may have a TTL input stage. This means that what the PIC reads as an input does not agree with what the external device sees. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
Spare $0.02 ? |
Posted: Mon Jan 09, 2006 8:10 pm |
|
|
I'm simply curious, why would you need this feedback? The the time the line of C code output_high(PIN_A0); is executed, the pin is pretty much guaranteed to be high, unless the load connected to the pin is sinking more current then PIC can provide. Are you building, essentially, a 20mA fuse? |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Jan 11, 2006 9:25 am |
|
|
Quote: | I'm simply curious, why would you need this feedback? |
I've needed to output a partial word to a port and didn't want to change the status of any of the other pins on the same port. I would read the status of those pins and then include that data in the word when I sent it to the port.
Ronald |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
|
Posted: Wed Jan 11, 2006 7:39 pm |
|
|
rnielsen wrote: | Quote: | I'm simply curious, why would you need this feedback? |
I've needed to output a partial word to a port and didn't want to change the status of any of the other pins on the same port. I would read the status of those pins and then include that data in the word when I sent it to the port.
Ronald |
In that case you want to read the output latch (as opposed to the input latch), make your changes and write back out to the output latch. That assumes you are using a PIC (such as the PIC18F family) that has output latches. You can also use bit_set and bit_clear on the output latch.
If you are using a 16F series pic without output latches then you have a problem due to the pin load as previously discussed. To handle this you could implement your own port mirror byte. Whenever you want to write to the port you first write / modify the mirror byte the write the contents of the mirror byte to the port. This solution allows you to use bit_set and bit_clear on the mirror byte. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Sat Feb 04, 2006 6:28 am |
|
|
I used the code as below
Code: | #byte OP_LATCH = 0xF81 // PORT B; PORT A = 0xF80
#bit OUT1 = OP_LATCH.2
#bit OUT2 = OP_LATCH.3
#bit OUT3 = OP_LATCH.0
#bit OUT4 = OP_LATCH.5
|
and the test is
Code: | if (OUT1==0) E_Flag1 = 1; else E_Flag1 = 0;
if (OUT2==0) E_Flag2 = 1; else E_Flag2 = 0;
if (OUT3==0) E_Flag3 = 1; else E_Flag3 = 0;
if (OUT4==0) E_Flag4 = 1; else E_Flag4 = 0; |
thanks again (it took a while to understand the op_latch = 0xf80, I found the answer in the debug viewing the IO PORTS, this is where I found Port B = 0xF81.)
Andrew _________________ What has been learnt if you make the same mistake? |
|
|
|