Im new to this, gotta simple problem:
tenthumbs
Posts: 4
I am new to microcontrollers. I have a BS2p40. I can't seem to get any of the I/O pins to work properly. Here is a program stub:
' {$STAMP BS2p}
' {$PBASIC 2.5}
ex· VAR Bit
MAINIO
DIRS=%0000000011111111
INH=1
ex=IN15
DEBUG ex
This piece of code loads fine, but comes back as a zero, and the pin reads low with my meter. In fact, none of the pins read high. Am i just making a simple error here?
Thanks
' {$STAMP BS2p}
' {$PBASIC 2.5}
ex· VAR Bit
MAINIO
DIRS=%0000000011111111
INH=1
ex=IN15
DEBUG ex
This piece of code loads fine, but comes back as a zero, and the pin reads low with my meter. In fact, none of the pins read high. Am i just making a simple error here?
Thanks
Comments
INH (like all the input pins names) is functionally a read-only value and you really shouldn't
use it on the left hand side of an assignment.
The DIRS statement you've written sets pins 8-15 as inputs and pins 0-7 as outputs which
by default are low logic levels (near 0V). If you want to set the output pins as a group, use
"OUTL = 1" to set pin 0 to high and pins 1-7 to low.
What do you have connected to pin 15?
ex· var· bit
mainio
dirs=%0000000011111111
OUTH=1
ex=IN1
debug bin ex
You're setting the upper half of the output register to 7 zeros and a one (pins 15-8),
but you're setting the direction of those bits to input mode. You're trying to sample
I/O pin 1's state and display it, but you've set the direction of I/O pins 7-0 to output.
You can either change the value assigned to dirs to %1111111100000000 or you
can change the OUTH to OUTL and the IN1 to IN9 (or whatever) and leave the dirs
assignment the same.
DIRS is 15 bits of memory space, same as 2 Bytes of memory same as one WORD of memory
DIRH AND DIRL are two more names that share the same memory address only one byte at a time
OUTS,OUTB,OUTC,OUTD are four more names that point to the same memory only 4 bits at a time
bit0 through bit15 are sixteen names that point to the same memory only one bit at a time
to set a pin HIGH you need to set the data direction bit in DIRS or DIRH or DIRL or any of the above names that point to the bit you want to set.
Setting a data direction register bit to 1 sets one of the pins to OUTPUT
There are two things the pin can be at this point, HIGH or LOW
There is a second register called the OUTS. You set the same bit position to a 1 in this register to make the pin HIGH or set it to 0 to make it low.
The default condition is LOW or 0
When setting bits it is easier to use the binary form of the number using the % sign. You used it for the DIRS register, use the same for the OUTS register and you will be able to read your code better.
To set pin 0 and 15 to output HIGH you could use
DIRS= %1000000000000001
OUTS=%1000000000000001
or
DIR15= 1
OUT15=1
DIR1=1
OUT1=1
or
OUTA=%0001
DIRA= %0001
OUTD=%1000
DIRD= %1000
or
OUTL=%00000001
DIRL= %00000001
OUTH=%10000000
DIRH= %10000000
remember in all of the cases above except for the DIR1,DIR15,OUT1,OUT15 statements will set the other bits to 0
That is why there are the different forms, or names to refer to a group of bits or just one bit.
You can also use the Binary operators OR "|" and AND "&" to set the bits or mask out (clear a bit) a single bit from a register.
for example. To set bit 15 but not change any of the other bits in DIRS you can use
DIRS = DIRS | %1000000000000000 only bit 15 is set.
to clear bit 15 to 0 you can mask the bit using the following binary operator AND
DIRS = DIRS AND %0111111111111111 this masks out bit 15 ad allows all other 1 bits to stay the same.
Now you can start twiddling bits.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Post Edited (metron9) : 4/24/2007 6:40:15 AM GMT