! bitwise operator, problem using this
mikea
Posts: 283
in the redled pub block i commented my problem. i want to toggle pins 8,9 off and on with the ! operator for efficiency rather than type it all out the long way. what additionally do i have to do besides put" !" in front of the" outa "? also in the lcd method below, i put "
here" so you could find it easier, i cant figure out how to play the note only one time, when the if statement is true .....currently it will constantly play the note as well as display message.in other words i want the message on always when appropriate but the note should only be a single time like a prompt to get attention. -mike
code:
{Object_Title_and_Purpose}
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
baud=19_200
TX_pin = 0
obj pst:"parallax serial terminal"
LCD : "FullDuplexSerial.spin"
pub main
pst.start(19200)
dira[28]:=0
repeat
pst.dec(ina[28]) 'display pin state in terminal window
waitcnt(clkfreq/20 +cnt) 'wait so i can read it
pst.clear 'clear # so they dont pile up across the screen
redled
lcd1
pub redled
dira[9]~~
dira[8]~~
dira[28]:=0
if ina[28]==1
outa[9]~~ 'i want to toggle pins 8,9 with the ! operator, !outa[9]~~
outa[8]~ 'when i put the bitwise operator in front it expects "end of the line" !outa[8]~ !outa[8]~
waitcnt(clkfreq/2+cnt) 'not sure how to proceed....
outa[9]~
else
outa[9]~
PUB lcd1
LCD.start(TX_PIN, TX_PIN, %1000, 19_200)
waitcnt(clkfreq / 100 + cnt) ' Pause for FullDuplexSerial.spin to initialize
LCD.tx(22) 'set diplay no cursor, no blink
LCD.tx(17) 'turn on back light
dira[28]:=0
if ina[28]==1
LCD.tx(212) 'tone length
here
LCD.tx(220) 'note
LCD.tx(12) 'clear screen
LCD.str(string("motion detected by girls room")) 'display message
waitcnt(clkfreq/2+cnt) 'slow down so screen doesnt pulse
else
LCD.tx(12) 'clear screen
LCD.str(string("all is cool.")) 'display message
waitcnt(clkfreq/2+cnt) 'slow down
here" so you could find it easier, i cant figure out how to play the note only one time, when the if statement is true .....currently it will constantly play the note as well as display message.in other words i want the message on always when appropriate but the note should only be a single time like a prompt to get attention. -mike
code:
{Object_Title_and_Purpose}
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
baud=19_200
TX_pin = 0
obj pst:"parallax serial terminal"
LCD : "FullDuplexSerial.spin"
pub main
pst.start(19200)
dira[28]:=0
repeat
pst.dec(ina[28]) 'display pin state in terminal window
waitcnt(clkfreq/20 +cnt) 'wait so i can read it
pst.clear 'clear # so they dont pile up across the screen
redled
lcd1
pub redled
dira[9]~~
dira[8]~~
dira[28]:=0
if ina[28]==1
outa[9]~~ 'i want to toggle pins 8,9 with the ! operator, !outa[9]~~
outa[8]~ 'when i put the bitwise operator in front it expects "end of the line" !outa[8]~ !outa[8]~
waitcnt(clkfreq/2+cnt) 'not sure how to proceed....
outa[9]~
else
outa[9]~
PUB lcd1
LCD.start(TX_PIN, TX_PIN, %1000, 19_200)
waitcnt(clkfreq / 100 + cnt) ' Pause for FullDuplexSerial.spin to initialize
LCD.tx(22) 'set diplay no cursor, no blink
LCD.tx(17) 'turn on back light
dira[28]:=0
if ina[28]==1
LCD.tx(212) 'tone length
here
LCD.tx(220) 'note
LCD.tx(12) 'clear screen
LCD.str(string("motion detected by girls room")) 'display message
waitcnt(clkfreq/2+cnt) 'slow down so screen doesnt pulse
else
LCD.tx(12) 'clear screen
LCD.str(string("all is cool.")) 'display message
waitcnt(clkfreq/2+cnt) 'slow down
Comments
The error message is correct. You can have OUTA[9]~ (to set the I/O pin to low) or you can have OUTA[9]~~ (to set the I/O pin to high) or you can have !OUTA[9] (to toggle the I/O pin's value), but you can't combine them like !OUTA[9]~ or !OUTA[9]~~. It doesn't make sense.
The ! operator will invert the bit(s).
The ~ operator sets the bit off.
So only use one at a time.
I'm not 100% clear on what you are trying to do? I loosely wrote this based on the fact that it sounds like you have a daughter that is sneaking out of the house or something? I don't have the bell to test with, but if I understood you correctly, you want 2 led's alternating back and forth and they will turn on when pin 28 == 0? As well as sound a chime and write something on the terminal alerting you to this? Am I correct in my interpretation? Below is my code, I had to tweak a couple of things to fit my setup. If I interpreted wrong, please let me know, but if I interpreted correctly and you have questions about my code, post here. Also, if you can elaborate a little more about what you are trying to do, that would help.
;o)
A cog to monitor the infrared sensors, a cog to control the LED's and a cog to do com data. Are you familiar with using cognew? If you had a layout of which pins were doing what, I could probably give example code.
turbosupra: i haven't used additional cogs yet, but would like to get into the swing of that.
pin 28 reads the state of the p.i.r. .....1 for high(motion sensed) , 0 for low
pins 8,9 flashed leds, one on while one off ...alternating during motion sense, or steady green when no motion pin 7
pin 0 is output to parallax lcd 27977-rt with built in piezospeaker, 1)displays a secure message if no motion sensed
2)motion detected by girls room (for now i only have one sensor, but will likely add more for different locations)
when motion is sensed it should alternately flash two leds , play only a couple notes and stop(possibly to wake me up), and display motion sense message.
when nothing sensed it would be cool to have a steady green led.
....turbosupra, thanks in advance i would like to see what that looks like with separate cogs...if i left something out let me know please.-mike
........could you show a short example of this, ive looked up the "or" and tried to include it for use but not doing something right to make it toggle pins...thanks.-mike
This selects I/O pins 8 and 9 as a 2 bit value with 9 as the MSB and 8 as the LSB. It then exclusive ors those bits with the constant %11 which toggles both of them. You could also do
OUTA ^= %11_0000_0000
This does the same thing, but exclusive ors the two bits (in the right bit positions) with the whole OUTA register.
one more question....if you added another "1" to the right of the "11" in place of a "0" so it was outa ^_1000_0000 would that be high for i/o pins 9,8,7?
thats binary that starts from the right at bit ,pin "0" and sets high or low in order up to 31? is that right? thanks- mike
outa[9..7] := %010
As long as the sensor detects movement you toggle
outa[9..8] ^= %11
When sensor says 'no more movement'
oua[9..7] := %001
Of course xor in this case works the same way as the bitwise not. It's speciality being a programable inverter is not needed in this case. But nevertheless it's usefull to know this detail.
Let me know if you have any questions about my code or why I did something the way that I did. You can uncomment the pst lines if you want to see what is going on, but you'll have to get rid of the pst.clear in the main method.
Wire length obviously changes resistance, but you can compensate for that with the proper hardware. Yes you can start a public or private method, the cog loads the method and if there is a repeat function in it, it will continue until stopped. I can be a method local to the parent or root spin file.