Page 1 of 1

Output a byte

Posted: Tue Feb 19, 2013 8:24 pm
by mamcgu
It is discouraging to have to inquire about such a simple operation, but...
The job is to read and ADC and put the result on an LCD. I have gone over the manual multiple times and can't find a way to write a byte to 8 output pins.
Presumably after the ADC read you would have the result in string format in something like:

Code: Select all

dim lcdout$(4)
lcdout$=bin$(adcreading,4)
for i=1 to 4 step 1
character= lcdout(i)
' ?? here would go the byte move ??
next i
The question is how to get the 8 bit value from character to the 8 output pins. If there were an "and" instruction you could shift the bits one at a time, "and" out all but one, test the value and then "outd" a pin, for each pin. But that is not available. Is it really true that one can't move a byte to the output pins?
In PIC assembler you have
movf filereg1,w
movwf portc

Mark

Re: Output a byte

Posted: Wed Feb 20, 2013 12:18 am
by admin
One of the downsides of a 32-bit processor is that some byte oriented operations are a bit awkward; you have found one. On an LPC2136 the IO ports are 32-bits wide and IO can be awkward. Since bit-banging pins are a common operation I implemented bit IO and lived with some bulky code to do four bit IO for stepper drives and LCD's. Later there was a need for faster, wider and simultaneous IO and the PORT0, PORT1 and PORT instructions were added to the interpreter. See page 38 of the Language Reference for the details.

The bitwise and operator & and or operator + is described on page 23 of the Language Reference. To move a byte to the IO pins you should choose pins that are next go each other in the IO-port. The port status is read as P0=PORT0. Now AND P0 with a mask word to zero the bits not
used for the LCD, lets call it Mask0 and the data to output is New0 and the first used pin in Port0 is pinX.
P0 = PORT0 ' Read Port0
NewWord = ( New0 LSL pinX ) & Mask0 ' Shift the byte to the correct place in the 32-bit word and zero the other bits
P0 = P0 & ~Mask0 ' Zero the bits that are going to be updated
P0 = P0 + NewWord ' Or the IO bits into the 32-bit word
PORT0 = P0 ' Update the port

A few lines can be combined to make faster and more compact code but I have left them as separate lines for clarity. I have not tested this because I'm traveling but it should work or be close to correct.

Daniel

Re: Output a byte

Posted: Sun May 12, 2013 2:23 pm
by basic_mark
Mark,
Have you had any success with outputting bytes?
Or anyone else had success?
I have not been able to write any text that doesn't return a syntax error on the port command.

None of these work for me:
port0=x
port1=x
port0=x$
port(0)=x
port(1)=x
port(0)=x$


Can anyone post an actual line for the port command that doesn't return a syntax error, or tell me what I am doing wrong?
Should x be a integer --> 13 or a binary as string --> "1101"?

basic_mark

Re: Output a byte

Posted: Wed May 15, 2013 9:35 pm
by Andries Pretorius
I have not yet used the PORT functions, but reading through the language reference, I find it a bit obscure in terms of both syntax and usage. A few brief practical examples of the available options will help.

Can you assist Daniel?

Andries

Re: Output a byte

Posted: Tue May 21, 2013 5:05 pm
by basic_mark
Okay,

I have figured out the syntax for the PORT function. There is no "=" sign, just the variable or value. I'm really not sure why I didn't think of this before.

PORT0 x
PORT1 x


These examples above will satisfy the syntax. I'll return later with some behavior after more experimentation.

basic_mark