View previous topic :: View next topic |
Author |
Message |
Futterama
Joined: 17 Oct 2005 Posts: 98
|
Setting 2 pins high or low simultaneously |
Posted: Tue Dec 15, 2009 2:39 pm |
|
|
Hi forum,
I'm using the PIC10F222 and would like to set 2 pins high in the same instruction and also set the same 2 pins low in the same instruction.
I guess I need to read the GPIO register and then use the bitwise AND and OR operators and then write the modified values back to the GPIO register.
The problem is that I have completely forgotten how to use bitwise operators.
I got this code:
Code: |
GPIO |= 0b00000110; // Set pins GP1 and GP2 high
GPIO &= 0b11111001; // Set pins GP1 and GP2 low
|
But I'm not sure this is correct. As the comments suggest, I'm trying to set GP1 and GP2 high at the same time (in the same instruction) and also set them low in the same instruction.
Please advise.
Thanks. |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Tue Dec 15, 2009 4:24 pm |
|
|
Hi,
I found the possibility to test the above code, and it seems to work, but still, I'm not sure the outputs change simultaneously, I don't have the equipment to check.
Anyone? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Dec 16, 2009 11:15 am |
|
|
Yes, that should work. But the easy way to find out without a scope is to look at the assembly code in the .lst file. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Wed Dec 16, 2009 1:20 pm |
|
|
SherpaDoug wrote: | But the easy way to find out without a scope is to look at the assembly code in the .lst file. |
I haven't learned the assembly language, so that's not "easy" for me, but I did it anyway, and it seemed to only use 2 instructions:
Code: | .................... GPIO |= 0b00000110; // Set pins GP1 and GP2 high
0107: MOVLW 06
0108: IORWF 06,F |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Wed Dec 16, 2009 1:36 pm |
|
|
if you output a byte to a port
all pins in that port will set simultaneously...ethier low or high or combinations.
set port B = 0xFF , all pins go high,
set port B = 0x00 , all pins go low...
set port B = 0x0F , higher half goes low, lower half goes high
and so on...
(this is not actual code... im sure you get what i mean)
hope i helped...if you still needed help at this point...(alot of smart people around here)
gabriel _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Wed Dec 16, 2009 1:37 pm |
|
|
With logical OR or AND you are "masking" the port so if you turn it all on or all off it will be simultaneously for whole port. Or in your case for two I/O pins.
Those two instructions in your case d the following thing. First one loads 06 (or 0b00000110) to the W register and the second one executes logical OR on register at location 06 (that is GPIO) and result is saved to the register it self.
Hope the explanation helps. |
|
|
Futterama
Joined: 17 Oct 2005 Posts: 98
|
|
Posted: Wed Dec 16, 2009 1:38 pm |
|
|
Thanks for your replies, but I got it working OK now
bungee, I thought it worked that way, you confirmed it for me, thanks. |
|
|
|