programming IO pins

Questions about the BASICtools and MakeItC
Post Reply
YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

programming IO pins

Post by YahooArchive »

I cannot get an input to read into code. I have a SuperPro Plus board and have
tried several methods. I have read the NXP datasheet which does not give any
examples. I have also searched the forum.

Here is the code I have tried:

LPC_GPIO1->DIR &= ~(1<<28); // p1.28 set as an input
if ((i&2)=(P1(28))) // this does not work
if ((i&2)=(FIO1P28)) // this does not work

I am just trying to read a pin. It would also be helpful if everyone put
comments next to there lines of code so inexperienced people like myself can
follow along easier.



YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

> I cannot get an input to read into code. I have a SuperPro Plus board and
have tried several methods. I have read the NXP datasheet which does not give
any examples. I have also searched the forum.

LPC_GPIO1->DIR &= ~(1<<28); // p1.28 set as an input
i = LPC_GPIO1->PIN & (1<<28); // reads p1.28

> Here is the code I have tried:
>
> LPC_GPIO1->DIR &= ~(1<<28); // p1.28 set as an input

the first is OK

> if ((i&2)=(P1(28))) // this does not work

(i&2)=(P1(28) (i&2) is not a legal lvalue in C (target of a save)
P1(28) is the BASIC construct, not C

> if ((i&2)=(FIO1P28)) // this does not work

FIO1P28 is not defined anywhere, as the C compiler complained about.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

i am trying to get my armmite to read a pin status(button) and act when it is
pushed. i can get the program to do what i want 1 cycle at a time but i want it
to read continuously and act when pushed. here is the code i have used. i AM
that much of an amature but i am learning. thanks for any help.

DIM A$(10)

BAUD1 =115200 ' set baud rate and enable channel
x=IO(14) ' read pin 14

IF x =-1 then GOSUB PRINTSTR1

A$ = "Hello World"

' Send a string of characters serially out UART0
PRINTSTR1:
I=0
WHILE A$(I)
TXD1 ( A$(I) )
I=I+1
LOOP

RETURN

thanks bruce for the help on the lcd got it working now

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

> i am trying to get my armmite to read a pin status(button) and act when it is
pushed. i can get the program to do what i want 1 cycle at a time but i want it
to read continuously and act when pushed. here is the code i have used.

A couple things you need to know, push-buttons are not ideal devices, when you
push it to make connection you will get switch bounce, which you can google for
a more complete description, but it means that it will make and disconnect
contact many times until it settles into the connected (usually on) state.
Going the other way it may do it as well but most of the time it will disconnect
cleanly.

So you need to put a loop around your code with timing.

the following will call your PRINTSTR1 routine anytime the button changes state.

lastx=IO(14)

while 1
x = IO(14)
if x = lastx then
else
wait(100) ' you may have to adjust this time
x = IO(14)
if x <> lastx then
GOSUB PRINTSTR1
lastx = x
endif
endif
loop

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

Thanks for the help but I am still having problems. So when I run the following
code.
printf("%d ", (LPC_GPIO1->PIN & (1<<28)));

I get
" 268435456"

How do I get a "1" or "0"

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

Your result is correct. In binary "268435456" is:

0001 0000 0000 0000 0000 0000 0000 0000

i.e. bit 28 is set

If you want to display the result as 1 (or zero for the alternative result) you
will need shift it right by 28 places before printing it.

Regards,
Chris Burrows
http://www.astrobe.com

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

I am using the Coridium SuperPro Pro+ and am trying to write to GPIO pin P0.26.
The pin is not labeled on the datasheet. It is supposed to be purposed for
P0.26/RXD3/AD03/AOUT. I know this has something to do with the PINSEL1 but I do
not see an example. I just want it to be an output.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

I am using C not BASIC by the way.

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

In both BASIC and C P0(25) thru P0(26) are configured as AD(2) thru AD(3)

As you say that is controlled by 2 bit values in the PINSEL1 register. Those 2
bits select the function of those pins. Our initialize tin code set bits 18:19
and 29:21 to the value 01' which selects the AD function.

To change either of those back, the Table 80 in the NXP User Manual shows the
value of 00 for the GPIO function

To set those to zero you can use

BASIC

PINSEL1 and= &Hffcfffff ' return P0(26) to GPIO

C

PINSEL1 &= 0xffcfffff; // same operation

YahooArchive
Posts: 1462
Joined: Fri Oct 19, 2012 5:11 am

Re: programming IO pins

Post by YahooArchive »

>from help line
>My problem is simple. I\'m using Armite 7 with MakeitC.
>I can\'t get correct result from IN instruction.
>INPUT(5);
>a=IN(5);
>when I print integer \'a\', it shows 0 or -1, but never 1, even though pin 5
has 3.3 V from the board.

>Do the pins need pull up resistors? But OUTPUT, HIGH, and LOW work for all
pins.

Actually 0 and -1 are the right answer (the current C manual is wrong, and will
be corrected) it should read like the BASIC manual--

When reading from IN (expression), -1 or 0 will be returned corresponding to the
voltage level on the pin numbered expression. Why -1 and 0? The main reason is
that operations of operators like NOT are assumed to be bitwise until there is a
Boolean operation in the expression, and NOT 0 is equal to -1.

In C IN(x) is defined by a macro in coridium_pcb.h

#define IN(pin) ((LPC_GPIO0->PIN & (1<

Post Reply