![]() |
Forum Index : Microcontroller and PC projects : Bitwise AND/OR
Author | Message | ||||
thirsty Newbie ![]() Joined: 22/06/2016 Location: AustraliaPosts: 34 |
Hi Guys So I've got this statement: PORT(2,3,7,4,9,1)=&B10011001 What I'd like to do is conditionally change the first bit. I know I can AND/OR to change it but I'm not sure of the syntax. I was thinking: PORT(2,3,7,4,9,1) AND &B10000000 Would that work? |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
A little reference: [code] Truth tables: AND ========= | 0 | 1 --------- 0 | 0 | 0 --------- 1 | 0 | 1 OR ========= | 0 | 1 --------- 0 | 0 | 1 --------- 1 | 1 | 1 [/code] AND only gives a 1 when both values are 1 OR only gives a 0 when both values are 0 This is for a single bit. So each single bit is used when AND or OR numbers. If you want to make a single bit 1 then use OR with a bit 1 in that position. If you want to make a single bit 0 then use AND with a bit 0 in that position. Microblocks. Build with logic. |
||||
thirsty Newbie ![]() Joined: 22/06/2016 Location: AustraliaPosts: 34 |
Thanks for that. Do you know the syntax to do that in MMBasic? |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6269 |
I think you can read the state of pins that have been set as outputs. If so, currentstate = PORT(2,3,7,4,9,1) ' read the current settings
otherpins = currentstate AND &B01111111 toppin = currentstate AND &B10000000 ' value is 128 if set or zero if not set 'to set top pin leaving the other pins as they were PORT(2,3,7,4,9,1)=&B10000000+otherpins ' to reset top pin PORT(2,3,7,4,9,1)=&B00000000+otherpins ' or just PORT(2,3,7,4,9,1)=otherpins If that doesn't work, you will need to keep a record of the settings of the other pins. Jim VK7JH MMedit |
||||
robert.rozee Guru ![]() Joined: 31/12/2012 Location: New ZealandPosts: 2431 |
i feel i must be missing something. what is wrong with: pin(9) = 1
to set the top bit high,
pin(9) = 0
to clear the top bit, and,
pin(9) = not pin(9)
to flip the top bit?
(assuming the LAST pin specified in a PORT command is the topmost bit, just checked the manual and corrected post for this) cheers, rob :-) |
||||
thirsty Newbie ![]() Joined: 22/06/2016 Location: AustraliaPosts: 34 |
Actually Rob you are quite right - I think I was just trying to be a bit too clever :) and focusing on the need to change the pattern over the entire port range. The bit I'm trying to turn on/off is indeed on a particular pin and I can just turn it on or off by using the Pin command. Thanks again all. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |