You are here: Home FORUM Support DIO for Pin Input

nqBASIC

  :: Support
Welcome Guest   [Register]  [Login]
 Subject :DIO for Pin Input.. 28-04-2010 15:01:34 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA

I'm a newbie. I'm trying to build simple code for the 32 pin module on a school board to turn on LED D2 while SW2 is pressed; turn the LED off when SW2 is released. Simple, right? Not for me. I can't seem to understand the DIO object syntax for setting PAD06 as an input, or how to use it in a conditional statement.  I've tried lots of syntax and I still get errors. What's wrong with my code?

/* File: 1 Button & LED
Hardware:   NanoCore32pin module, Docking Module
Descript:   Pressing SW2 at PAD06 lights LED D2 at PT0
*/
/*These constants are pre-defined in STDLIB.NQB
Const FOREVER = 1   
Const OFF = 0
Const ON = 1
*/

//Objects
dim SW2 as new DIO(PAD06)  //define pushbutton switch SW2 at PAD06
dim LED_D2 as new DIO(PT0)  //define DIO object as LED D2 at PT0-pin

main
SW2.PIN_Dir(PAD06,DIR_INPUT) //setup PAD06 as input from SW2, pulldown when pressed
SW2.PIN_In(PAD06)
 while (FOREVER) 
  if SW2       //SW2 pressed
   LED_D2.PIN_Out (PT0,ON)  //Turn LED D2 ON
  else
   LED_D2.PIN_Out (PT0,OFF) //Turn LED D2 OFF
  end if
 end while
end main

Thanks

 

IP Logged
 Subject :Re:DIO for Pin Input.. 28-04-2010 16:58:18 
carl
Joined: 27-05-2009 10:51:54
Posts: 17
Location: Toronto

Hi AJ,

Although they don't cover everything, it will really help to study the example projects provided with the compiler.

In the fornc12dxc32 project there's a section of code very similar to yours:

	if (switch == ON) 
D.PIN_Out (LED, ON) //Turn LED ON
S.SER_Put_string ("On")
else
D.PIN_Out (LED, OFF) //Turn LED OFF
S.SER_Put_string ("Off")
end if
The examples can also be found online, in the Support Library: http://www.technologicalarts.ca/support/docs/nqBASIC/ProjectExamples/

 

Carl
IP Logged
Last Edited On: 28-04-2010 17:17:19 By carl for the Reason
 Subject :Re:DIO for Pin Input.. 29-04-2010 16:38:05 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA
Yes, I looked over the example code but did not see an explicit example of setting a DIO pin direction to input. AJ
IP Logged
 Subject :Re:DIO for Pin Input.. 30-04-2010 08:32:21 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA

Sorry, my first post indicated I was using a school board. I'm using a docking module having two pushbutton switches (SW2, SW3) and two LEDs (D2, D3). I'm trying to turn on LED D2 at PT0 by pressing SW2 at PAD06. How do I setup the PAD06 pin for an input?

Here's the code snipet:

dim SW2 as new DIO(PAD06)  //define pushbutton switch SW2 at PAD06
dim LED_D2 as new DIO(PT0)  //define DIO object as LED D2 at PT0-pin

main
SW2.PIN_Dir(PAD06,DIR_INPUT) //setup PAD06 as input from SW2, pulldown when pressed
SW2.PIN_In(PAD06,SW2)   //setup PAD06 as input from SW2, hi when released, lo when pressed

while(FOREVER) 
  if (SW2 == 0)     //SW2 pressed = PAD06 is low
   LED_D2.PIN_Out (PT0,ON)  //Turn LED D2 ON
 else
   LED_D2.PIN_Out (PT0,OFF) //Turn LED D2 OFF
  end if
 end while

These first two lines in main seem to do the same thing, I get no compile errors, but no LED lights. How does one properly set PAD06 for input?

IP Logged
 Subject :Re:DIO for Pin Input.. 03-05-2010 07:00:24 
carl
Joined: 27-05-2009 10:51:54
Posts: 17
Location: Toronto
Here is an example, from our upcoming project book, which should clarify how to use DIO as input:

/* This example uses a Pushbutton switch to turn On one LED*/
Const OFF = 0
Const ON = 1
Const FOREVER = 1
Dim myLED1 as new DIO(PT2)
Dim mySW1 as new DIO(PT3)
main
mySW1.PIN_Dir(PT3,DIR_INPUT) /* set PT3 as an input */
while (FOREVER)
mySW1.PIN_In(PT3,mySW1) /* Read the value of the port */
if(mySW1 == 1)
myLED1.PIN_Out(PT2,ON)
end if
mySW1.PIN_In(PT3,mySW1) /* Read the value of the port */
if(mySW1 == 0)
myLED1.PIN_Out(PT2,OFF)
end if
end while
end main

 

IP Logged
 Subject :Re:DIO for Pin Input.. 03-05-2010 14:22:32 
ajbeal
Joined: 01-11-2009 11:18:04
Posts: 20
Location: Knoxville, TN USA

Carl, thanks for your reply. This now makes sense how to read the port pin and where to place the read (PIN_In) statement in the code. That's what was missing. I used your code with my ports and identifiers and here's my revised code for the docking module and 32pin module.

Const FOREVER = 1   
Const OFF = 0
Const ON = 1

//Objects
dim SW2 as new DIO(PAD06)  //define pushbutton switch SW2 at PAD06
dim LED2 as new DIO(PT0)  //define DIO object as LED D2 at PT0

main
SW2.PIN_Dir(PAD06,DIR_INPUT) //setup PAD06 as input from SW2
 while (FOREVER)
  SW2.PIN_In(PAD06,SW2)  //read PAD06 as input from SW2
  if (SW2 == 0)    //SW2 is lo when pressed
   LED2.PIN_Out(PT0,ON) //Turn LED D2 ON
  end if
  SW2.PIN_In(PAD06,SW2)  //read PAD06 as input from SW2
  if (SW2 == 1)    //SW2 is hi when released
   LED2.PIN_Out(PT0,OFF) //Turn LED D2 OFF
  end if
 end while 
end main

It still doesn't work. I tested the PAD06 pin with a logic probe and verified SW2 pulls PAD06 lo when pressed; hi when released. I tested PT0 with other code to turn on the LED and it works. I'll keep trying. Regards

IP Logged
 Subject :Re:DIO for Pin Input.. 03-06-2010 11:52:52 
carl
Joined: 27-05-2009 10:51:54
Posts: 17
Location: Toronto

Hi AJ,

I finally had a chance to investigate this and confirmed my suspicions.  There is a bug in nqBASIC involving PTAD pins used as inputs.  Unlike other ports, there is a separate input register for this port (i.e. not the same one as used for outputs on PTAD).  However, the compiler is reading the PTAD output register instead of the input register.  This will be fixed in the next revision.

To get your program to work, I suggest you jumper the PTAD6 and PTAD7 lines to other input pins.  Aside from that, your code is correct.

Carl

IP Logged
Page # 


Powered by ccBoard