Docstoc

1 Arduino POP-168 Hardware

Document Sample
1  Arduino POP-168 Hardware Powered By Docstoc
					                                    Micrcocontroller experiment in C with Arduino by POP-168 modulel1



 1 : Arduino POP-168 Hardware
       Arduino is an open-source electronics prototyping platform based on flexible,
easy-to-use hardware and software. Itís intended for artists, designers, hobbyists, and
anyone interested in creating interactive objects or environments.*

     Arduino can sense the environment by receiving input from a variety of sensors
and can affect its surroundings by controlling lights, motors, and other actuators. The
microcontroller on the board is programmed using the Arduino programming language
(based on Wiring) and the Arduino development environment (based on Processing).
Arduino projects can be stand-alone or they can communicate with software on running
on a computer. *

      The Arduino POP-168 was made from Innovative Experment Co., Ltd. (INEX-
www.inexglobal.com) Thailand. They make this module beasd-on the original Arduino-
mini module and modify the bootloader firmware to protect the failure bootloader
issue.

1.1 Arduino POP-168 kit series
      We have 3 versions; POP-Builder kit, POP-Builder Discovery kit and POP-Project kit

      POP-Builder Kit includes :

             l POP-168 module

             l POP-INTERFACE board

             l CX-4 Serial port cable

             l ZX-POT : Variable resistor board for Analog to Digital converter testing

             l ZX-LED : Single LED output board x 2

             l ZX-SWITCH : Switch inpout board x 2

             l ZX-SPEAKER : Piezo Speaker board x 1

             l Software and documentation CD-ROM

      The figure 1-1 shows all components of POP-Builder Kit




* Introduction paragraph is from Arduino website (www.arduino.cc)
 Micrcocontroller experiment in C with Arduino by POP-168 module
2 l




                                                               ZX-POTH ËÃ×Í ZX-POTV
                                                               Í‹ҧã´Í‹ҧ˹Öè§




Figure 1-1 POP-Builder Kit components




Figure 1-2 POP-Project kit componnets
                                      Micrcocontroller experiment in C with Arduino by POP-168 modulel3

      POP-Project kit includes :

             l POP-168 module

             l POP-INTERFACE board

             l CX-4 Serial port cable

             l Breadboard 390 points with wire jumpers

             l Experimenting components such as some resisitors, vairable resistors,
push-button switches, LED and piezo speaker

             l Software and documentation CD-ROM

      The figure 1-2 shows all components of POP-Project kit

      The latest version of POP kit is POP-Builder Discovery kit. It includes :

             l POP-168 module

             l POP-INTERFACE board

             l CX-4 Serial port cable

             l ZX-POT : Variable resistor board for Analog to Digital converter testing

             l ZX-LED : Single LED output board x 2

             l ZX-SWITCH : Switch inpout board x 2

             l ZX-SPEAKER : Piezo Speaker board x 1

             l ZX-DCM2 : 2-ch. DC motor driver

             l ZX-TILT : Tilt detection board

             l ZX-SOUND : Sound detection board

             l ZX-LDR : Light sensor board

             l GP2Y0A21 : 10-80cm. Infraredd distance sensor

             l DC motors with plastic blade

             l 48:1 BO2 DC motor gearboxes x 2

             l 4AA Batter holder

             l Software and documentation CD-ROM
 Micrcocontroller experiment in C with Arduino by POP-168 module
4 l




                                                   +5V   +5V

                                                                  LED2
                                    +5V +5V LED1                  Green
                                            Blue




                                                                           GND



                                                                                         +5V




                                                                                                            PD7

                                                                                                                      PD6

                                                                                                                            PD5

                                                                                                                                   PD4

                                                                                                                                          PD3

                                                                                                                                                PD2
                                                                                  RST



                                                                                                PB1

                                                                                                      PB0
                                 R1        R2 R3             R4
                                 4k7       10k 4k7           4k7                                                                                      PIN13

                    K1                                             PIN24
                   AVR
                In-System                                    R5                          +5V
               Programming
  +5V           Connector                                    R6
                                                                                                                                   CR1
                                                             R7                                                                   16MHz                                                       +5V
                                                                                                                                                +5V                                  C4
        GND                  MISO                                                                                                                                                   0.1 F
                                                                      R5-R8




                                                                                                          PD7
                                                                                                          PD6
                                                                                                                PD5
                                                                                        PB4
                                                                                              PB3
                                                                                              PB2
                                                                                                    PB1
                                                                                                    PB0
                                                             R8       150 x4
        GND                  SCK                                                  PB5                                   PB7
                                                                                                                                                                                     2         $
                                                                                 AVCC                                   PB6
        GND                  RST               +5V                                                                                                                                                  1
                                                                                  AN6                                   VCC                                                     4
                                                                                                                                                                    C6                   IC2             C5
                                                                                 AREF           IC1                     GND               C3                                         MAX3232
        GND                  PB2                                                 AGND     ATMega168-20AU                VCC              0.1 F                     0.1 F        5                   3   0.1 F
                                                C1                                           (TQFP32)
                                                                                  AN7                                   GND
                                               0.1 F
         Vcc                 MOSI                                                 AN0                                   PD4                                                    10                   7
                                                                                  AN1                                   PD3                                                     9                   8
                                                          C2
                                                         0.1 F                                                                                                R9              R10




                                                                                                    RST
                                                                                        AN2
                                                                                              AN3
                                                                                              AN4
                                                                                                    AN5

                                                                                                          PD0
                                                                                                          PD1
                                                                                                                PD2
                                                                                                                                                                                         15    6 C7
                                                                                                                                                              68              150
                                                                                                                                                                                                0.1 F
                                SW1
                                 BL
                             Bootloader
                             Mode switch




                                                             PIN1                                                                                     PIN12
                                                                     TX




                                                                                        GND
                                                                           RX




                                                                                                AN0

                                                                                                      AN1

                                                                                                            AN2

                                                                                                                      AN3

                                                                                                                            AN4

                                                                                                                                   AN5

                                                                                                                                          AN6

                                                                                                                                                AN7




                                                         +5Vdc

                                                                          RESET
                                                             GND
      Blue LED operation indicator
                                                                                                            Di 6/PWM (PD6)
                                                                                                            Di 5/PWM (PD5)

                                                                                                            Di 3/PWM (PD3)
                                                                                                            Di 9/PWM (PB1)




      Program mode : Turn-on
                                                                                                            VDD (+5Vdc)




      Run mode : Indicates the Di 13 port logic status
                                                                                                            Di 7 (PD7)


                                                                                                            Di 4 (PD4)

                                                                                                            Di 2 (PD2)
                                                                                                            Di 8 (PB0)
                                                                                                            RESET
                                                                                                            GND
                                                                                                            NC




                                 ISP connector
                                 pin assignment                                                                                                                                     Green LED
         GND                     Di 12 (PB4:MISO)                                                                                                              13
                                                                                                                                                                                    (power)
         GND                     Di 13 (PB5:SCK) Blue LED
                                                                                                                                                                    POP-168




         GND                     RESET                                                                                            ATMEGA168
         GND                     Di 10 (PB2:SS) PWM
        +VDD                     Di 11 (PB3:MOSI) PWM

                                                                                                            1                                            12
                                                                                                                       TxD
                                                                                                                       RxD



                                                                                                                An 1 (PC1)

                                                                                                                An 3 (PC3)
                                                                                                                      GND
                                                                                                                        NC




                                                                                                               An 7 (AN7)
                                                                                                                 An0 (PC0)

                                                                                                                 An2 (PC2)



                                                                                                               An 6 (AN6)
                                                                                                            An4/SDA (PC4)
                                                                                                            An5/SCL (PC5)




          BL switch for programming mode selection

                                                         2
      DB-9 female connector
                                                         3
      connect to computer's
          serial or COM port                             5




Figure 1-3 Schematic diagram, Pin assignment and simple connection of
POP-168 microcontroller module
                                                      Micrcocontroller experiment in C with Arduino by POP-168 modulel5



       K19
                                                                                  D1             IC1
     SERIAL                                                                                     78R05
                                                                                1N5819
   DOWNLOAD
                                                                                 SW1           IN         SHDN
                                                               BD1               ON
                                                 K1           2A 50V
                                              DC.input                                                    OUT                       +5V
                                               6-9Vdc                                                                        R1




                                                                                                    GND
                                                                      K2 +       C1                                   C3     1k
                                                                     Vin        220 F                   C2           0.1 F        LED1
                                                                   6-9Vdc -      25V                  1000 F          50V          ON
                                                                                                       6.3V


                                                                                         +5V                        +5V
                  +5V                         PIN1
                        R2                                              PIN24                              R17
   K3                   133                                                          C6                    133                     K18
                                                      TX                            0.1 F
  PC0                                                                                                                              PB1
 [An 0]                                               RX         GND                 50V                                          [Di 9]
                        R3                                                                                 R16
   K4                   133                                       RST                                      133                     K17
  PC1                                                                                                                              PB0
                                                      GND         +5V
 [An 1]                                                                                                                           [Di 8]
                        R4                                                                                 R15
                                                      AN0         PB1
   K5                   133                                                                                133                     K16
  PC2                                                 AN1         PB0                                                              PD7
 [An 2]                                                                                                                           [Di 7]
                        R5                            AN2         PD7                                      R14
   K6                   133                                                                                133                     K15
                                                      AN3         PD6
  PC3                                                                                                                              PD6
 [An 3]                                               AN4         PD5                                                             [Di 6]
                        R6                                                                                 R13
   K7                   133                           AN5         PD4                                      133                     K14
  PC4                                                                                                                              PD5
 [An 4]                                               AN6         PD3                                                             [Di 5]
                        R7                                                                                 R12
   K8                                                 AN7         PD2                                      133                     K13
                        133
  PC5                                                                                                                              PD4
 [An 5]                                       PIN12                     PIN13                                                     [Di 4]



                              R8        R9                                    R10        R11
                              133       133                                   133        133
           C4                                                                                                C5
          0.1 F                                                                                             0.1 F
           50V                                                                                               50V


                                                       SW2
                                                      RESET
                                 K9       K10                                    K11         K12
                                PC6       PC7                                    PD2         PD3
                               [An 6]    [An 7]                                 [Di 2]      [Di 3]



Figure 1-4 POP-INTERFACE board schematic diagram
 Micrcocontroller experiment in C with Arduino by POP-168 module
6 l




                                SW2
                               SERVO   C8     C9    C10
                               POWER 1000 F 1000 F 1000 F
        K4                            10V    10V    10V
              +Vm                                                             +Vm                                   +5V
      SERVO 4.8-7.2V                                                 LED1
      supply                                                         +Vm                                            PORT
                                                                     R1
                                                                     1k                                             GND
      K3                                                                                                                    +5V
              +Vin                                                                                        MCU port
   Battery    6-16V                               SW1
    input                                        POWER                                                                            J1
                                                                IN      IC1     OUT                                               P0
                                        +                             278R05                                +5V
     K2     +Vin                    ~              C1                                          C4
                                                 220 F               GND      C3              47 F                                J2
  DC input 7.5-16V                  ~                                       0.1 F
                                                  25V                                         16V                                 P1
                                                                             63V
                                        -
                                                              C2                                                                  J3
                                     BD1                    0.1 F                                                                 P2
                                    2A50V                    63V

                                                                                                      +5V                         J4
                                                                                                                                  P3
                                1                                                                    21
                                2                                                                             C7                  J5
                                3
                                                                                                            0.1 F                 P4
                                                                                                             63V
                                4                         POP168                                                                  J6
        K1
                               23                         Socket                                                                  P5

     SERIAL                                                                                                                       J7
   DOWNLOAD                    22                                                                                                 P6
     RS-232             SW2
  (RJ-11 female)       RESET        20 19 18 17 16 15 14 13 12 11 10 9           8    7   6   5                                   J8
                                                                                                                                  P7




                                                                                                                            +Vm


                                                                                                                                   P0
                                                                                                                                  +Vm
                                                                                                                                  GND
                                                                                                                                   P1
                                                                                                                                  +Vm
                                                                                                                                  GND
   BASIC Stamp port compatibleP15           P14 P13 P12 P11 P10 P9 P8 P7 P6 P5 P4 P3 P2 P1 P0                                      P2
       Arduino port compatible D9           D8 D7 D6 D5 D4 D3 D2 A7 A6 A5 A4 A3 A2 A1 A0                                          +Vm
                                                                                                                                  GND
                                                      POP168 PORT
                                                                                                                                   P3
                                                                                                                                  +Vm
                                                                                                      SERVO PORT                  GND
                                                                                                      P0 to P5 for POP168
                                                                                                                                   P4
                                                                                                                                  +Vm
                                                                                                                                  GND
                                                                                                                                   P5
                                                                                                                                  +Vm
                                                                                                                                  GND




Figure 1-5 The JX-POP168 schematic diagram
                                          Micrcocontroller experiment in C with Arduino by POP-168 modulel7


1.2 Software installation
       (1) Insert CD-ROM to CD drive of your computer.

       (2) Enter to Software à Arduino folder. Find the ArduinoSetup.exe and double-
click. The installation will be start .

1.3 Testing Arduino POP-168
       (1) Plug the POP-168 into socket on the carrier board (POP-INTERFACE or JX-POP168
board)

      (2) Open the Arduino software by click on the Start à All Programs à POP-168
Software Package à Arduino

       (3) The main window of Arduino software will appear.




       (4) Set hardware for the first times.

               (4.1) Set microcontroller type

                        Tools à Microcontroller (MCU) à Arduino Mini or POP-168
 Micrcocontroller experiment in C with Arduino by POP-168 module
8 l




                 (4.2) Set Serial Port

                           Tools à Serial Port à select ìCOMî port as POP-168 present




                          Arduino IDE supports serial or COM port from COM 1 to COM 9.
The standard serial port is COM1 or COM2. The higher COM port are created by Virtual
COM port driver of USB to Serial port converter. User must check at the control panel à
System à Device manager. See at the Port listing.

        (5) Open demo sketch : LED Blinking

                 File à Sketchbook à Examples à Digital à select ìBlinkî demo sketch.
                                                        Micrcocontroller experiment in C with Arduino by POP-168 modulel9

      (6) Prepare POP-168 run Bootloader mode

               - Power off POP-168

               - Hold BL switch , then Power on POP-168

               - Release BL switch

                   If Blue LED on POP-168 was lighted and not blink, then POP-168 had
entered Bootloader mode and ready to uploaded.



                                           
         ON




                                    PB1    PB0     PD7       PD6    PD5       PD4
                                    Di 9   Di 8    Di 7      Di 6   Di 5      Di 4




                                                                                                                              PD3
                                                                                                                           Di 3
                               ON
                                                                              Di 2 - ARDUINO Pinout




                                                                                                                              PD2
                                                                                                                           Di 2
                                                                         13
                                                                                                      PD2 - Real AVR Pin


       DC.INPUT
                      6-9Vdc




                                                     ATMEGA168




                                                                                                                                    Press RESET switch
                                                                                                                              AN7
                                                                                                                           An 7




                                                                   12


                    POP-INTERFACE BOARD                                                                                             following.
              Vin




                                                                                                                              AN6
                                                                                                                           An 6




                                    An 0   An 1    An 2      An 3   An 4      An 5
                                    2+    2+     2+        2+!    2+"       2+#
         SERIAL
         ON




                                    PB1    PB0               PD6    PD5       PD4
                               #    Blue LED is on
                                    Di 9   Di 8              Di 6   Di 5      Di 4
                                                                                                                              PD3
                                                                                                                           Di 3




                               ON
                                                                              Di 2 - ARDUINO Pinout




                                                                                                                              PD2
                                                                                                                           Di 2




                                                                         13
                                                                                                      PD2 - Real AVR Pin




       DC.INPUT
                      6-9Vdc




                                                     ATMEGA168

                                                                                                                                        !
                                                                                                                                       Release RESET
                                                                                                                              AN7
                                                                                                                           An 7




                                                                                                                                       switch
                                                                   12


                    POP-INTERFACE BOARD
              Vin




                                                                                                                              AN6
                                                                                                                           An 6




                                    An 0   An 1    An 2      An 3   An 4      An 5
                                    2+    2+     2+        2+!    2+"       2+#
         SERIAL




                                     " Release BL switch

Figure 1-7 Entering the POP-168 microcontroller module to Program mode
when use the POP-INTERFACE board.
  Micrcocontroller experiment in C with Arduino by POP-168 module
10 l




                                                                        +Vm 4.8-6V
          6-12Vdc
                                                                    for servo motor




                                          RS-232
                                Vin
                              6-12Vdc                                                         +Vm


                                                                                13
                                                                                                        +Vm
                                                                                           SERVO
                                                   POP-168
       POWER                                                                                                    Press BL switch
                                                        ATMEGA168
                                                                                               2#
                                                                                                               and hold.
                                                                                               2"
                                                                          12
                                                                                               2!
                                                                                                                Press RESET
                                                       RESET
                                                                    JX-POP168
                                                                 ATmega168 Matchbox            2
                                                                 Micro-Project board
                                          Microcontroller port ( P15-P0 )                      2
                                                                                                                switch and hold
                    ARDUINO   ,' ,& ,% ,$ ,# ," ,! ,   )% )$ )# )" )! )   ) )                                 together.
                        +5V 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0                    GND   +Vm 2
                                                                                                    2




                                                                        +Vm 4.8-6V
          6-12Vdc




                       #
                     Blue LED is on                         for servo motor

                                                                                              +Vm


                                                                                13
                                                                                                        +Vm
                                                                                           SERVO
       POWER                                       POP-168
                                                        ATMEGA168
                                                                                               2#

                                                                                               2"
                                                                          12
                                                                                               2!
                                                       RESET
                                                                    JX-POP168
                                                                                               2                Release RESET
                                                                 ATmega168 Matchbox
                                                                 Micro-Project board                          ! switch.
                                          Microcontroller port ( P15-P0 )                      2
                    ARDUINO   ,' ,& ,% ,$ ,# ," ,! ,   )% )$ )# )" )! )   ) )
                        +5V 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0                    GND   +Vm 2
                                                                                                    2




                               " Release BL switch.


Figure 1-8 Entering the POP-168 microcontroller module to Program mode
when use the JX-POP168 project board.
                                  Micrcocontroller experiment in C with Arduino by POP-168 modulel11

      (7) Compile sketch and Uploaded to POP-168. by clicking the Upload button
on Arduinoís toolbar.




      (8) Arduinoís console shown message if uploaded sketch finish.




      (9) Press RESET switch on the carrier board again. POP-168 is set to RUN mode.

             The LED at Di13 (Blue LED) on the POP-168 module blinks 1 second rate.
  Micrcocontroller experiment in C with Arduino by POP-168 module
12 l




1.4 How to use POP-INTERFACE board with POP-168
module
       The original Arduino hardware has 14 digital I/O pin ; Digital 0 to Digital 13 (Di 0
to Di 13) and 6 analog input ; Analog in0 to Analog in5 (An 0 to An 5). For POP-168
hardware has more 2 analog inputs; An6 and An 7.

      After fit the POP-168 module on the POP-INTERFACE board, all pins will be
converted to 3-pin JST connector for interfacing with INEXís sensor and peripheral boards.
Figure 1-9 shows the layout of POP-INTERFACE board after fitting the POP-168 module.

       Refer the figure 1-9, pin name in round rectagular is Arduino port name. The
regular port name such as PC0,PD1 etc. are ATmega168 port name.

     Addition, the POP-INTERFACE board provides female header connector which
connect to POP-168 port directly. You can plug the AWG#22 wire jumper to interface
this module to external devices or the breadboard following the figure 1-10
            ON




                                        PB1    PB0     PD7        PD6        PD5       PD4
                                        Di 9   Di 8     Di 7          Di 6   Di 5      Di 4

                                                                                                                                       PD3
                                                                                                                                    Di 3


                                   ON
                                                                                       Di 2 - ARDUINO Pinout




                                                                                                                                       PD2
                                                                                                                                    Di 2




                                                                                  13
                                                                                                               PD2 - Real AVR Pin




         DC.INPUT
                          6-9Vdc




                                                          ATMEGA168
                                                                                                                                       AN7
                                                                                                                                    An 7




                                                1                            12


                        POP-INTERFACE BOARD
                  Vin




                                                                                                                                       AN6
                                                                                                                                    An 6




                                        An 0   An 1    An 2       An 3       An 4      An 5
                                        2+    2+     2+         2+!        2+"       2+#
            SERIAL



Figure 1-9 The POP-INTERFACE board layout
                                                          Micrcocontroller experiment in C with Arduino by POP-168 modulel13




Figure 1-10 Show using the POP-INTERFACE board with breadboard



                                                     Blue LED on, the program is uploading
                                                       &
                                                                 " Press BL switch and hold.
               ! Turn-on the power switch                           % Release BL switch
                               ON




                                                          PB1    PB0    PD7       PD6        PD5       PD4
                                                          Di 9   Di 8   Di 7          Di 6   Di 5      Di 4

              
                                                                                                                                                       PD3
                                                                                                                                                    Di 3




  Apply the supply voltage                           ON
                                                                                                       Di 2 - ARDUINO Pinout




  6 to 9Vdc from adaptor
                                                                                                                                                       PD2
                                                                                                                                                    Di 2




                                                                                                  13
                                                                                                                               PD2 - Real AVR Pin




                             DC.INPUT

                                                                                                                                                              #
                                            6-9Vdc




                                                                          ATMEGA168




                                                                                                                                                             Press and hold
                                                                                                                                                             RESET switch
                                                                                                                                                       AN7
                                                                                                                                                    An 7




                                                                  1                          12


                                          POP-INTERFACE BOARD
                                    Vin




 Connect the CX-4 cable                                                                                                                                       $
                                                                                                                                                       AN6
                                                                                                                                                    An 6




 between POP-INTERFACE                                    An 0
                                                          2+
                                                                 An 1
                                                                 2+
                                                                        An 2
                                                                        2+
                                                                                  An 3
                                                                                  2+!
                                                                                             An 4
                                                                                             2+"
                                                                                                       An 5
                                                                                                       2+#                                                   Release RESET
 board and RS-232 serial       SERIAL                                                                                                                        switch
 port


                                                     CX-4 cable




Figure 1-11 Shows the step of uploading the program into the POP-INTERFACE
board
  Micrcocontroller experiment in C with Arduino by POP-168 module
14 l




1.5 How to use JX-POP168 project board with
POP-168 module
1.5.1 Features of JX-POP168 Project board
       l Dual female socket 12 pin for support POP-168 microcontroller module
       l Prototype area 3.8" x 5" for making the circuit or small breadboard installation.
       l 2 of power supply terminals
        -       For POP-168 and any interfacing circuit ; use the external power suypply
such as DC adpator or battery 6 to12V 500mA (not include), on-board polarity protection
circuit. Include the power switch and LED indicator.
                 - For the servo motor ; support 4.8 to 6Vdc. Use 2-pin terminal block and
have the power swith with LED indicator.
       l On board +5Vdc regulator.
       l Reset switch.
     l Free pads for connecting all microcontroller port pins. Thewy are assigned to
compatible with BASIC Stamp port (P0 to P15) and Arduino microcontroller.
       l Provides in 3-pin JST connector for P0 to P7 (or A0 to A7).
     l 6 of Servo motor connector. They are connected with P0 to P5 of POP-168
module.
       l Supplied with CX-4 cable for connecting PCís serial port.

1.5.2 Using
       After fitting the POP-168 module on the JX-POP168 board, all port pin of this module
will connect to the female header. Using this project board has 2 selections foloiwign
thefigure 1-12.

            1. Attached the breadboard on the prototyping area. You can construct
the experiment interface circuit on this breadboard and conencto to Arduino POP-168
module by using AWG#22 wire jumpers.

                 2. Construct the interface circuit by soldering all components direct to
the soldering pad on the prototyping area.
                            Micrcocontroller experiment in C with Arduino by POP-168 modulel15




Figure 1-12 Show the using the JX-POP168 Project board with breadboard
(left) and soldering components direct to prototyping area (right)
  Micrcocontroller experiment in C with Arduino by POP-168 module
16 l
                                                                   Micrcocontroller experiment in C with Arduino by POP-168 modulel17



2 : Arduino POP-168 with I/O devices
interface example
2.1 Expanding output port by using 74HC595
shift register IC.

                                            DB-9
                                          (optional)
             5          3 2                      IC1
          K1
                                               POP-168
        RS-232                                                                                             +5V
                                                                                          +5V
                                                                                                  R9
                                                                                 SW2             4.7k
                                 1                                    24                                  16
                 Di 1/TxD                                                       RESET
                                     1




                                 2                                    23                                                  R1-R8
                 Di 0/RxD                                                     GND
                                 3                                    22                        10                         510
                                                                             RESET                   CLR              7
                  GND
                                 4                                    21   VDD/+5Vdc
                                                                                                                 Q7               LED8
                                 5                                    20                                            6
            An 0/Di 14/PC0                                                 Di 9/PWM/PB1         14               Q6               LED7
                                                  ATMEGA168




            An 1/Di 15/PC1
                                 6                                    19    Di 8/PB0
                                                                                                     DI             5
                                 7                                    18                                         Q5               LED6
            An 2/Di 16/PC2                                                  Di 7/PD7
                                                                                                12                  4
            An 3/Di 17/PC3
                                 8                                    17   Di 6/PWM/PD6              LATCH       Q4               LED5
                                 9                                    16                                            3
           An 4/Di 18/SDA/PC4                                              Di 5/PWM/PD5                          Q3               LED4
                                10                                    15                        11                  2
           An 5/Di 19/SCL/PC5                                               Di 4/PD4                 CLK                          LED3
                                11                                    14                                         Q2
                 An 6/AN6                                                  Di 3/PWM/PD3                             1
                 An 7/AN7
                                12                                    13    Di 2/PD2                             Q1               LED2
                                     12




                                                                                                13                  9
                                                                                                     OE          Q0               LED1
                                                              13




                                             POP-168
                                                                                                           8
                                                                                                                   IC2
                                                                                                                 74HC595




/***************************************************************
 * Code to using 74HC595 shift register to extend output port.
 * Credit : Carlyn Maw,Tom Igoe .
 * File    : 74HC595.pde
 * By      : Opas Sirikunchittavorn    15 Feb 2008
  ***************************************************************/
#define DATA_PIN    2 // Connect Di 2 to pin 14 of 74HC595
#define CLOCK_PIN 4 // Connect Di 4 to pin 11 of 74HC595
#define LATCH_PIN 6 // Connect Di 6 to pin 12 of 74HC595
void setup() {
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  pinMode(DATA_PIN, OUTPUT);
}
void hc595_out(unsigned char data){
    digitalWrite(LATCH_PIN, LOW);
    shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, data);
    digitalWrite(LATCH_PIN, HIGH);
}
void loop() {
  int j;
  for (j = 0x01; j <= 0x80; j<<=1)
  {
    hc595_out(j);
    delay(1000);
  }
}
  Micrcocontroller experiment in C with Arduino by POP-168 module
18 l




2.2 Expanding the digital I/O port by MCP23016
port expander IC
        The MCP23016 is 16-bit remote bidirectional I/O port expansion integrated circuit.
It requires I2C bus protocol for interfacing. You can config the expansion function of
each port pin to inout or output independent.

         MCP23016 pin assignment shows below.




2.2.1 Expanding 16 output ports

                                          DB-9
                                        (optional)
              5       3 2                     IC1                                                                +5V
                                           POP-168

  K1                                                                                                             20
                                                                                         +5V
RS-232                                                                                         RCLK              Vdd               R1-R8
                                                                         SW2                   3.9k                                 510
                                                                                                                              21            LED1
                Di 1/TxD
                               1                              24        RESET                         9               GP0.0
                                                                                                           CLK                22
                                   1




                               2                              23
                Di 0/RxD
                               3                              22
                                                                       GND
                                                                                               CCLK                   GP0.1                 LED2
                                                                      RESET                                                   23
                               4                              21                               33pF   8               GP0.2                 LED3
                   GND
                               5                              20
                                                                    VDD/+5Vdc
                                                                                                           Vss                24
             An 0/Di 14/PC0                                        Di 9/PWM/PB1                                       GP0.3                 LED4
                               6                              19                                      19                      25
                                             ATMEGA168




             An 1/Di 15/PC1                                          Di 8/PB0
                                                                                                           Vss                              LED5
                               7                              18                                                      GP0.4
             An 2/Di 16/PC2
                               8                              17
                                                                     Di 7/PD7                                                 26
             An 3/Di 17/PC3                                        Di 6/PWM/PD6                                       GP0.5                 LED6
                               9                              16                         +5V                                  27
           An 4/Di 18/SDA/PC4
                              10                              15
                                                                   Di 5/PWM/PD5                                       GP0.6                 LED7
           An 5/Di 19/SCL/PC5                                        Di 4/PD4                                                 28
                An 6/AN6
                              11                              14
                                                                   Di 3/PWM/PD3
                                                                                                                      GP0.7                 LED8
                              12                              13
               An 7/AN7                                              Di 2/PD2
                                                                                                             IC1
                                   12




                                                                                  R17          R18
                                                                                                           MCP23016
                                                         13




                                                                                  4.7k         4.7k
                                          POP-168
                                                                                                                                   R9-R16
                                                                                                      14                       1    510
                                                                                                         SCL          GP1.0                 LED9
                                                                                                      15                       2
                                                                                                         SDA          GP1.1                 LED1
                                                                                                                                            0
                                                                                                                               3            LED1
                                                                                                      18              GP1.2
                                                                                                           A2                  4            1
                                                                                                                                            LED1
                                                                                                      17              GP1.3                 2
                                                                                                           A1                 10            LED1
                                                                                                                      GP1.4                 3
                                                                                                      16                      11            LED1
                                                                                                           A0         GP1.5                 4
                                                                                                                              12            LED1
                                                                                                                      GP1.6                 5
                                                                                                      6                       13            LED1
                                                                                                           INT        GP1.7                 6
                                 Micrcocontroller experiment in C with Arduino by POP-168 modulel19

/*
 * Code for send data to MCP23016 (16 bit I2C I/O expander).
 * File : MCP23016.pde
 * By    : Opas Sirikunchittavorn   Apr 25th, 2008
 *
 * MCP23016       POP-168
 * SDA Pin 15 <> An 4 pin
 * SCL Pin 14 <> An 5 pin
 */

#include <Wire.h>
#define MCP23016_Addr     0x20

void setup()
{
  Wire.begin();
  delay(200);
  TWI_write_word(MCP23016_Addr,0x06,0x0000);            // all I/O output
  TWI_write_word(MCP23016_Addr,0x02,0x0000);            // all LOW
  delay(500);
}

void loop()
{
   TWI_write_word(MCP23016_Addr,0x00,0xaaaa);
  delay(250);
   TWI_write_word(MCP23016_Addr,0x00,0x5555);
  delay(250);
}

void TWI_write_word(unsigned char address,unsigned char reg,unsigned int data)
{
   Wire.beginTransmission(address);
   Wire.send(reg);
   Wire.send((unsigned char)data);
   Wire.send((unsigned char)(data>>8));
   Wire.endTransmission();
}
   Micrcocontroller experiment in C with Arduino by POP-168 module
20 l




2.2.2 Expanding input and output ports

                                           DB-9
                                         (optional)
               5         3 2                   IC1                                                                 +5V
                                            POP-168

  K1                                                                                                               20
                                                                                          +5V
RS-232                                                                                          RCLK               Vdd               R1-R8
                                                                          SW2                   3.9k                            21    510
                   Di 1/TxD
                                1                              24        RESET                           9              GP0.0                LED1
                                                                                                             CLK                22
                                    1




                                2                              23
                 Di 0/RxD                                               GND
                                                                                                CCLK                    GP0.1                LED2
                                3                              22                                                               23
                                                                       RESET
                                4                              21                               33pF                    GP0.2                LED3
                    GND                                              VDD/+5Vdc                           8                      24
              An 0/Di 14/PC0
                                5                              20
                                                                    Di 9/PWM/PB1                             Vss        GP0.3                LED4
                                6                              19                                       19                      25
                                              ATMEGA168




              An 1/Di 15/PC1
                                7                              18
                                                                      Di 8/PB0
                                                                                                             Vss        GP0.4                LED5
              An 2/Di 16/PC2                                          Di 7/PD7                                                  26
              An 3/Di 17/PC3
                                8                              17
                                                                    Di 6/PWM/PD6                                        GP0.5                LED6
                                9                              16                         +5V                                   27
            An 4/Di 18/SDA/PC4                                      Di 5/PWM/PD5                                        GP0.6                LED7
                               10                              15
            An 5/Di 19/SCL/PC5                                        Di 4/PD4                                                  28
                               11                              14                                                       GP0.7                LED8
                 An 6/AN6                                           Di 3/PWM/PD3
                               12                              13
                An 7/AN7                                              Di 2/PD2
                                                                                                               IC1
                                    12




                                                                                   R17          R18
                                                                                                             MCP23016
                                                          13




                                           POP-168                                 4.7k         4.7k                                             +5V

                                                                                                        14                      1
                                                                                                           SCL          GP1.0                         S1
                                                                                                        15                      2
                                                                                                           SDA          GP1.1                         S2
                                                                                                                                3
                                                                                                        18              GP1.2                         S3
                                                                                                             A2                 4
                                                                                                        17              GP1.3                         S4
                                                                                                             A1                 10
                                                                                                                        GP1.4                         S5
                                                                                                        16                      11
                                                                                                             A0         GP1.5                         S6
                                                                                                                                12
                                                                                                                        GP1.6                         S7
                                                                                                        6                       13
                                                                                                             INT        GP1.7                         S8
                                                                                                                                             R9-R16
                                                                                                                                              4.7k




 /*
  * Code for send data to MCP23016 (16 bit I2C I/O expander).
  * Read input at GP1 and send to GP0.
  * File : MCP23016_io.pde
  * By    : Opas Sirikunchittavorn   25 April 2008
  *
  * MCP23016           POP-168
  * SDA Pin 15          Analog pin4
  * SCL Pin 14          Analog pin5
  */

 #include <Wire.h>
 #define MCP23016_Addr                                    0x20

 unsigned char gp0,gp1;                                                            // Global variable

 void setup(){
   Wire.begin();
   delay(200);
   TWI_write_word(MCP23016_Addr,0x06,0xFF00);                                                          // Port1 input, Port0 output
   TWI_write_word(MCP23016_Addr,0x02,0x0000);                                                          // Port1 HIGH, Port0 LOW
   delay(500);
 }
 void loop(){
   unsigned int data=0;
    data=TWI_read_word(MCP23016_Addr,2);
    TWI_write_byte(MCP23016_Addr,0x00,gp1);
   delay(100);
 }
                                   Micrcocontroller experiment in C with Arduino by POP-168 modulel21

 void TWI_write_byte(unsigned char address,unsigned char reg,unsigned char data){
    Wire.beginTransmission(address);
    Wire.send(reg);
    Wire.send((unsigned char)data);
    Wire.endTransmission();
 }
 void TWI_write_word(unsigned char address,unsigned char reg,unsigned int data){
    Wire.beginTransmission(address);
    Wire.send(reg);
   Wire.send((unsigned char)data);            //send gp0 (lower 8 bit first)
   Wire.send((unsigned char)(data>>8));         //send gp1 (upper 8 bit)
    Wire.endTransmission();
 }
 int TWI_read_word(int address,int numBytes){
   int result=0;
    Wire.requestFrom(address,numBytes);
    while(Wire.available()<2) {
   }
   gp0=Wire.receive();                         // 1st byte is gp0
   gp1=Wire.receive();                         // 2nd byte is gp1
   result=gp1;
    result=(gp1<<8)|gp0;
   return result;
 }



2.3 LCD module interface
      The kind of LCD module for this example is Chracter LCD module. It has 16
characters with 2 lines display. It compatible with Hitachi HD44780 LCD controller. LCD
module has 14 to 16 pins and can show the pin assignment as follows.


                                                  LED back light pin (available only
               1-GND




               5-R/W




               10-D3



               13-D6
               11-D4
               12-D5

               14-D7
               2-Vdd




                                                  LCD with Backlight type)
               9-D2
               8-D1
               7-D0
               4-RS




               15-A
               3-Vo




               16-K
               6-E
  Micrcocontroller experiment in C with Arduino by POP-168 module
22 l




2.3.1 4-bit LCD interface example



                                             DB-9
                                           (optional)
              5           3 2                    IC1
  K1                                           POP-168
RS-232

                                                                                                                     +5V

                                                                                               SW2
                  Di 1/TxD
                                  1                                              24           RESET
                                      1




                  Di 0/RxD
                                  2                                              23         GND
                                  3                                              22
                                                                                            RESET
                    GND
                                  4                                              21       VDD/+5Vdc
             An 0/Di 14/PC0
                                  5                                              20   Di 9/PWM/PB1
                                  6                                              19
                                                   ATMEGA168




             An 1/Di 15/PC1                                                                Di 8/PB0
             An 2/Di 16/PC2
                                  7                                              18        Di 7/PD7
             An 3/Di 17/PC3
                                  8                                              17   Di 6/PWM/PD6
            An 4/Di 18/SDA/PC4
                                  9                                              16   Di 5/PWM/PD5
            An 5/Di 19/SCL/PC5
                                 10                                              15        Di 4/PD4
                  An 6/AN6
                                 11                                              14   Di 3/PWM/PD3
                  An 7/AN7
                                 12                                              13        Di 2/PD2
                                      12




                                                                   13




                                              POP-168
                             +5V Can be connect Vo
                                 with GND if not
                                 connect with VR0


                     VR0
                                                      GND




                                                                                      R/W
                                                               Vdd




                     10k
                                                                                                                D2
                                                                                                                     D3




                                                                                                                                     D6
                                                                                                           D1
                                                                                                      D0




                                                                                                                           D4
                                                                                                                                D5


                                                                                                                                          D7
                                                                             RS
                                                                        Vo




                                                                                             E




                                                    1          2     3       4        5      6        7    8    9   10 11 12 13 14




 /********************************************************
  * Program : Test LCD display in “4 bits” mode.
  * By       : Opas Sirikunchittavorn    17 Feb 2008
  * File     : Text_LCD.pde
  * Target : POP-168 (Arduino ATmega168)
  *
  **************** Pin connection *********************
  * Name     LCD pin POP-168 pin
  * VO       3        GND    (Max contrast)
  * RS       4        9
  * R/W      5        GND    (write only)
  * EN       6        8
  * D0-D3    7-10     GND    (4 bit mode)
  * D4-D7    11-14     -
                      47
   ********************************************************/
 #undef int()
 #include <stdio.h>
                                 Micrcocontroller experiment in C with Arduino by POP-168 modulel23

#define   lcd_clear()            lcd_write_control(0x01)//             Clear Display
#define   lcd_cursor_home()      lcd_write_control(0x02)//             Set Cursor = 0
#define   lcd_display_on()       lcd_write_control(0x0E)//             LCD Display Enable
#define   lcd_display_off()      lcd_write_control(0x08)//             LCD Display Disable
#define   lcd_cursor_blink()     lcd_write_control(0x0F)//             Set Cursor = Blink
#define   lcd_cursor_on()        lcd_write_control(0x0E)//             Enable LCD Cursor
#define   lcd_cursor_off()       lcd_write_control(0x0C)//             Disable LCD Cursor
#define   lcd_cursor_left()      lcd_write_control(0x10)//             Shift Left Cursor
#define   lcd_cursor_right()     lcd_write_control(0x14)//             Shift Right Cursor
#define   lcd_display_sleft()    lcd_write_control(0x18)//             Shift Left Display
#define   lcd_display_sright()   lcd_write_control(0x1C)//             Shift Right Display

#define   RS    9                    // registry select LCD pin 4
#define   EN    8                    // enable LCD pin 6

void setup (void) {
  for (int i = 4;i < 10;i++){
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  LcdInitialize();                   // Initialize LCD
  lcd_cursor_off();                  // hide underline cursor
}
void loop(){
  lcd_clear();
  goto_cursor(0x00);

    lcd_print(“ INEX POP-168”);
    goto_cursor(0x40);
    lcd_print(“ Arduino Family “);
    delay(2000);

    lcd_clear();
    goto_cursor(0x00);
    lcd_print(“ ATmega168 MCU”);
    goto_cursor(0x40);
    lcd_print(“By INEX Co.,Ltd.”);
    delay(2000);
}

void goto_cursor(unsigned char i){
  i |= 0x80;
  lcd_write_control(i);
}

void lcd_print(char *str){
  int i;
  for(i=0;i<16 && str[i]!=0;i++)
    lcd_write_ascii(str[i]);
}

void LcdInitialize() {
  delay(200);
  LcdSendBits(0x02);                         // 0x2
  lcd_write_control(0x33);                   // 4 bit mode initialize command
  lcd_write_control(0x32);                   // 4 bit mode initialize command

    lcd_write_control(0x28);                 // 4 bit mode command
  Micrcocontroller experiment in C with Arduino by POP-168 module
24 l



       lcd_write_control(B00001110);                 // 0x0E Display on
       delayMicroseconds(50);                        // specification says > 39us

     lcd_write_control(B00000001);                   // 0x01 clear display
     delay(2);                                       // specification says > 1.53ms

     lcd_write_control(B00000110);                   // 0x06 move cursor to right.
     delay(2); //
 }


 void LcdSendBits(int value){
   for (int i = 0;i <= 3;i++){
     digitalWrite(i + 4,value & 01); // set bit value
     value >>= 1; // shift bits
   }
   digitalWrite(EN,HIGH);
   delayMicroseconds(5);               // pause 1.4 ?s according to datasheet
   digitalWrite(EN,LOW);               // toggle enable line transfer bits
   delayMicroseconds(5);               // pause 1.4 ?s according to datasheet
 }

 void lcd_write_byte(unsigned char value){
   LcdSendBits(value >> 4);            // msw
   LcdSendBits(value);              // lsw
 }

 void lcd_write_control(unsigned char value){
   digitalWrite(RS,LOW);
   lcd_write_byte(value);
   delay(2);
 }

 void lcd_write_ascii(unsigned char c){
   digitalWrite(RS,HIGH);
   lcd_write_byte(c);
 }

         After running program, LCD will shows :

            INEX       POP-168
            Arduino          Family
         2 seconds later, the screen will change to :

            ATmega168            MCU
            By     INEX       Co.,Ltd.
         and back to firt screen within 2 seconds.
                              Micrcocontroller experiment in C with Arduino by POP-168 modulel25



                            (1) Prepare a 14-pin IDC male single header
                            by cutting the full 40-pin to 14-pin


                            LCD module

                            (2) Plug the LCD module on the IDC header


                            LCD module

                            (3) Solder LCD module with 14-pin header




  (a) Interface with POP-INTERFACE         (b) Interface with JX-POP168 board

Figure 2-1 Preparing the LCD module to interface with POP-INTERFACE board
(a) and JX-POP168 project board in (b)
   Micrcocontroller experiment in C with Arduino by POP-168 module
26 l




2.3.2 A/D monitoring on LCD module example



                                                                         DB-9
                                                                       (optional)
                                           5           3 2                   IC1
   K1                                                                      POP-168
 RS-232

                                                                                                                                                 +5V

                                                                                                                           SW2
                                               Di 1/TxD
                                                              1                                              24           RESET
                                                                  1




                                               Di 0/RxD
                                                              2                                              23
                                                                                                                        GND
       +5V                                                    3                                              22
                                                                                                                        RESET
                                                 GND
                                                              4                                              21       VDD/+5Vdc
                                           An 0/Di 14/PC0
                                                              5                                              20   Di 9/PWM/PB1
                                                              6                                              19
                                                                               ATMEGA168




                                           An 1/Di 15/PC1                                                              Di 8/PB0
                                                              7                                              18
 VR1                                       An 2/Di 16/PC2                                                              Di 7/PD7
                                                              8                                              17
 10k                                       An 3/Di 17/PC3                                                         Di 6/PWM/PD6
                                       An 4/Di 18/SDA/PC4
                                                              9                                              16   Di 5/PWM/PD5
                                       An 5/Di 19/SCL/PC5
                                                             10                                              15        Di 4/PD4
                                               An 6/AN6
                                                             11                                              14   Di 3/PWM/PD3
                                               An 7/AN7
                                                             12                                              13        Di 2/PD2
                                                                  12
    use POP-INTERFACE
    Connect this port if




                                                                                               13




                                                                          POP-168
                                                          +5V Can be connect Vo
                                                              with GND if not
                                                              connect with VR0


                                                  VR0
                                                                                  GND




                                                                                                                  R/W
                                                                                           Vdd




                                                  10k
                                                                                                                                            D2
                                                                                                                                                 D3




                                                                                                                                                                 D6
                                                                                                                                       D1
                                                                                                                                  D0




                                                                                                                                                       D4
                                                                                                                                                            D5


                                                                                                                                                                      D7
                                                                                                         RS
                                                                                                    Vo




                                                                                                                         E




                                                                                1          2     3       4        5      6        7    8    9   10 11 12 13 14
                           POTENTIOMETER
A




 /********************************************************
  * Program : Read input from ADC ch0 and display to LCD.
  *             LCD display driven in “4 bits” mode.
  * By       : Opas Sirikunchittavorn       17 Feb 2008
  * File     : Text_LCD_ADC.pde
  * Target : POP-168 (Arduino ATmega168)
  *
  ******************* Pin connection ******************
  * Name     LCD pin POP-168 pin
  * VO            3        GND     (Max contrast)
  * RS            4        9
  * R/W      5          GND     (write only)
  * EN            6        8
  * D0-D3    7-10   GND      (4 bit mode)
  * D4-D7    11-14       -
                        47
   ********************************************************/
 #define lcd_clear()               lcd_write_control(0x01)// Clear Display
 #define lcd_cursor_home()         lcd_write_control(0x02)// Set Cursor = 0
 #define lcd_display_on()          lcd_write_control(0x0E)// LCD Display Enable
                                  Micrcocontroller experiment in C with Arduino by POP-168 modulel27

#define   lcd_display_off()       lcd_write_control(0x08)// LCD Display Disable
#define   lcd_cursor_blink()      lcd_write_control(0x0F)// Set Cursor = Blink
#define   lcd_cursor_on()         lcd_write_control(0x0E)// Enable LCD Cursor
#define   lcd_cursor_off()        lcd_write_control(0x0C)// Disable LCD Cursor
#define   lcd_cursor_left()       lcd_write_control(0x10)// Shift Left Cursor
#define   lcd_cursor_right()      lcd_write_control(0x14)// Shift Right Cursor
#define   lcd_display_sleft()     lcd_write_control(0x18)// Shift Left Display
#define    lcd_display_sright()   lcd_write_control(0x1C)// Shift Right Display
#define   RS     9                     // registry select LCD pin 4
#define   EN     8                     // enable LCD pin 6
#define   ANALOG_PIN 0

void setup (void)
{
  for (int i = 4;i < 10;i++) {
    pinMode(i,OUTPUT);
    digitalWrite(i,LOW);
  }
  LcdInitialize();                       // Initialize LCD
  lcd_cursor_off();                      // hide underline cursor
}

void loop()
{
  int analog_value,temp;
  char str[5],lcd_text[17];
  lcd_clear();
  goto_cursor(0x00);
  lcd_print(“Analog input An0”);
    analog_value = analogRead(ANALOG_PIN); // Read analog input pin An 0.
  temp = analog_value*5.0/1023*1000;     // convert to volt and scale *1000
  lcd_text[4] = (temp%10) + ‘0’;
    temp /=10;
  lcd_text[3] = (temp%10) + ‘0’;
  temp /=10;
  lcd_text[2] = (temp%10) + ‘0’;
  lcd_text[1]=’.’;
  temp /=10;
  lcd_text[0] = (temp) + ‘0’;
  lcd_text[5]=’\0';

    goto_cursor(0x40);
    lcd_print(“ = “);
    lcd_print(lcd_text);
    lcd_print(“ Volt.”);
    delay(100);
}

void goto_cursor(unsigned char i)
{
  i |= 0x80;
  lcd_write_control(i);
}
void lcd_print(char *str)
{
  int i;
  for(i=0;i<16 && str[i]!=0;i++)
    lcd_write_ascii(str[i]);
}
  Micrcocontroller experiment in C with Arduino by POP-168 module
28 l



 void LcdInitialize()
 {
   delay(200);                                   // specification says > 30ms after power on
   LcdSendBits(0x02);                            // 0x2

       lcd_write_control(0x33);                  // 4 bit mode initialize command

       lcd_write_control(0x32);                      // 4 bit mode initialize command

       lcd_write_control(0x28);                      // 4 bit mode command

       lcd_write_control(B00001110);                 // 0x0E Display on
       delayMicroseconds(50);                        // specification says > 39us

     lcd_write_control(B00000001);                   // 0x01 clear display
     delay(2);                                       // specification says > 1.53ms

     lcd_write_control(B00000110);                   // 0x06 move cursor to right.
     delay(2);                                       //
 }

 void LcdSendBits(int value)
 {
    for (int i = 0;i <= 3;i++)
    {
      digitalWrite(i + 4,value & 01); // set bit value
      value >>= 1; // shift bits
    }
    digitalWrite(EN,HIGH);
    delayMicroseconds(5);           // pause 1.4 ?s according to datasheet
    digitalWrite(EN,LOW);           // toggle enable line transfer bits
    delayMicroseconds(5);           // pause 1.4 ?s according to datasheet
 }

 void lcd_write_byte(unsigned char value)
 {
   LcdSendBits(value >> 4);         // msw
   LcdSendBits(value);              // lsw
 }

 void lcd_write_control(unsigned char value)
 {
   digitalWrite(RS,LOW);
   lcd_write_byte(value);
   delay(2);
 }

 void lcd_write_ascii(unsigned char c)
 {
   digitalWrite(RS,HIGH);
   lcd_write_byte(c);
 }
                                                  Micrcocontroller experiment in C with Arduino by POP-168 modulel29


2.4 SLCD16X2 interface example
2.4.1 SLCD16x2 information
2.4.1.1 Features
       l Serial Input RS-232 or Invert/Non-invert TTL/CMOS logic level.

       l 1/8 or 1/16 Duty can be selected by jumper.

      l Scott Edwardsís LCD Serial BackpackTM command compatible addition with
Extended Command that make LCD control easier.

       l Easy to interface with the microcontroller

       l Operation with +5 to 12 Vdc supply

2.4.1.2 Setting up
      In the figure 2-2, it shows the detail of SLCD16x2 backside. User will see 4 jumpers
to configuration as follows

                  (1) Mode command jumper : Select the command modes. SLCD16x2
has 2 modes. One is Standard command (ST). This mode compatible with Scott Edwardsís
LCD Serial Backpack TM . Another mode is Extended mode command (EX). For
MicroCamp activities select Standard command mode (ST).




                                                                                              Brightness
                                                                                              adjustment

                                                                                                     Ground
                                                                                                     Data input

                                                                                                     +5V supply


     Command mode         Line display                     Baudrate             Interface signal
                                                                                 Invert logic or RS-232
       Extended (EX)       8-digit per line (8)           9600 bps (96)
                                                                                 (IN)



                          16-digit per line
       Standard (ST)                                      2400 bps (24)          Direct logic (DI)
                          (16)




Figure 2-2 Details of SLCD16x2’s jumper selections
  Micrcocontroller experiment in C with Arduino by POP-168 module
30 l




            (2) Lines jumper : Select the line displays ; 1/8 and 1/16 Duty. 1/8 Duty
means displaying 8 digit per line. 1/16 Duty means displaying 16 digit per line or more.
Normally set to 1/16.

                (3) Baudrate select jumper : 2 selections as 2400 and 9600 bps (bit per
second) with 8N1 data format (8-bit data, no parity bit and 1 stop bit)

              (4) Interface signal jumper : 2 selections as Invert logic TTL/CMOS level or
RS-232 (IN) and Direct logic TTL/CMOS level (DI).

       SLCD16x2 provides a brightness adjustment with variable resistor at BRIGHTNESS
position.

     Interfacing connector has 3 pins : +5V Supply voltage (+), Serial data input (S)
and Ground (G).

2.4.1.3 Interfacing SLCD16x2 with all POP-168 carrier boards
       The JST3AA-8 cable is required for connecting between SLCD with MicroCamp
controller board. This cable wire assignment can show below.

2mm. pitch                                                                2mm. pitch
 GND
                    GND
  S                              S/Data
 +5V
                                               +5V

       The JST3AA-8 cable has the both end as 2mm. housing. After conecting, set all
jumpers following the figure below.




                ï Select command mode to Extension mode (EX).

                ï Select the lines display to 16-digit per line (16).

                ï Select baudrate to 9600 bps (96).

                ï Select the interface signal to Direct (DI).
                                         Micrcocontroller experiment in C with Arduino by POP-168 modulel31

2.4.1.4 Data and Command sending in Extended mode
      Once the SLCD16x2 is properly connected and configured, data and command
can be sent serially. For data sending, you can send any message such as ìHelloî via
serial I/O directly, ìHelloî message will be shown on your LCD.

      For command sending, you can send instruction set to SLCD16x2 (see table 2-1)


    Command
                Operation
     (ASCii)
       128      Set the first position
       129      Clear screen
       130      Set cursor position to upper left
       131      Cursor not shift after data entry
       132      Shift cursor after data entry
       133      Shift cursor back after data entry
       134      Display on
       135      Display off
       136      Display on and Cursor on
       137      Display on and Cursor blink
       138      Shitf cursor to left
       139      Shitf cursor to right
       140      Shift new character to left
       141      Shift new character to right
       142      Write data to CGRAM0
       143      Write data to CGRAM1
       144      Write data to CGRAM2
       145      Write data to CGRAM3
       146      Write data to CGRAM4
       147      Write data to CGRAM5
       148      Write data to CGRAM6
       149      Write data to CGRAM7
       150      Access DDRAM at 0x00
       151      Access DDRAM at 0x10
       152      Access DDRAM at 0x14
       153      Access DDRAM at 0x20
       154      Access DDRAM at 0x40                          Table 2-1 Instruction set of
       155      Access DDRAM at 0x50                          SLCD16x2 module in Extended
       156      Access DDRAM at 0x54                          mode
  Micrcocontroller experiment in C with Arduino by POP-168 module
32 l




2.4.1.5 Programming procedure
       (1) Inlude SoftwareSerial.h library with #include

       (2) Set the interface port with #define. For example :
                #define rxPin 3
                #define txPin 2

                SoftwareSerial mySerial =                 SoftwareSerial(rxPin, txPin);

    (3) In setup(), set the port operation to logic HIGH, delay, set the baudrate with
command mySerial.begin(9600); and define the instruction set. See the example below.
                digitalWrite(txPin, HIGH);
                // set txPin high (as recommended in forum)
                delay(1000);          //***
                // define pin modes for tx, rx pins:
                pinMode(rxPin, INPUT);
                pinMode(txPin, OUTPUT);
                // set the data rate for the serial ports
                mySerial.begin(9600);
                // say something
                mySerial.print(128,BYTE);
                mySerial.print(129,BYTE);
                mySerial.print(0xFE,BYTE);
                mySerial.print(0x38,BYTE);                    // 2 line, 5x7 mode
                mySerial.print(0xFE,BYTE);                    // Send command
                mySerial.print(0xC0,BYTE);                    // Show on line 2
                delay(2000);

2.4.1.6 Example command for controlling SLCD16x2
       (1) Intial SLCD16x2

           mySerial.print(128,BYTE);                    // Init LCD

       (2) Clear screen command
           mySerial.print(129,BYTE);                    // clear screen

       (3) Move cursor to upper left position
           mySerial.print(130,BYTE);

       (4) Move cursor to lower line left postion

           mySerial.print(154,BYTE);                     // Move cursor to line2

       (5) Write the message with ì î .

           mySerial.print(“Hello”);                  // Show “HELLO”
                                                                                                 Micrcocontroller experiment in C with Arduino by POP-168 modulel33

2.4.1.7 ADC monitoring with SLCD16x2 example



                                                                               DB-9
                                                                             (optional)
                                                 5           3 2                   IC1
        K1                                                                       POP-168
      RS-232

                                                                                                                             +5V

                                                                                                                    SW2
                                                     Di 1/TxD
                                                                    1                                    24        RESET
                                                                        1




                                                     Di 0/RxD
                                                                    2                                    23       GND
             +5V                                                    3                                    22
                                                                                                                 RESET
                                                       GND
                                                                    4                                    21   VDD/+5Vdc
                                                 An 0/Di 14/PC0
                                                                    5                                    20   Di 9/PWM/PB1
                                                                    6                                    19
                                                                                     ATMEGA168




                                                 An 1/Di 15/PC1                                                 Di 8/PB0
                                                                    7                                    18
      VR1                                        An 2/Di 16/PC2                                                 Di 7/PD7
                                                                    8                                    17
      10k                                        An 3/Di 17/PC3                                               Di 6/PWM/PD6
                                            An 4/Di 18/SDA/PC4
                                                                    9                                    16   Di 5/PWM/PD5
                                             An 5/Di 19/SCL/PC5
                                                                   10                                    15     Di 4/PD4
                                                     An 6/AN6
                                                                   11                                    14   Di 3/PWM/PD3
                                                     An 7/AN7
                                                                   12                                    13     Di 2/PD2
                                                                        12
          use POP-INTERFACE
          Connect this port if




                                                                                                    13




                                                                                POP-168                                      Connect from Di 2 of
                                                                                                                             POP-INTERFACE board
                                 POTENTIOMETER
      A




                                                                  DSP1
                                                                  SLCD16x2




/********************************************************************
* Program : Read input from ADC ch0 and display to SLCD16x2 in Extended mode
* By       : Opas Sirikunchittavorn    5 Mar 2008
* File     : SLCD16x2_ADC.pde
* Target : POP-168 (Arduino ATmega168)
*******************************************************************/
#include <SoftwareSerial.h>
#define rxPin 3            // Set Di 3 as rxPin
#define txPin 2            // Set Di 2 as txPin
#define ANALOG_PIN 0
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
void setup() {
   digitalWrite(txPin, HIGH);    // set txPin high (as recommended in forum)
   delay(1000); //***
   // define pin modes for tx, rx pins:
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
  Micrcocontroller experiment in C with Arduino by POP-168 module
34 l



       // set the data rate for the serial ports
       mySerial.begin(9600);
       // say something
       mySerial.print(128,BYTE);
       mySerial.print(129,BYTE);
       mySerial.print(0xFE,BYTE);
       mySerial.print(0x38,BYTE);      // 2 line display, 5x7 mode
       mySerial.print(0xFE,BYTE);      // Send command
       mySerial.print(0xC0,BYTE);      // Show on line 2
       delay(2000);
 }

 void loop()
 {
    int analog_value,temp;
    char str[5],lcd_text[17];
    mySerial.print(130,BYTE);     // 1234567890123456
    mySerial.print(“Analog input An0”);

       analog_value = analogRead(ANALOG_PIN);                     // Read analog i/p at An 0.
       temp = analog_value*5.0/1023*1000;                         // ? volt and scale *1000
       lcd_text[4] = (temp%10) + ‘0’;

       temp /=10;
       lcd_text[3] = (temp%10) + ‘0’;
       temp /=10;
       lcd_text[2] = (temp%10) + ‘0’;
       lcd_text[1]=’.’;
       temp /=10;
       lcd_text[0] = (temp) + ‘0’;
       lcd_text[5]=’\0';

     mySerial.print(154,BYTE);                                        // Move cursor to line2
     mySerial.print(“ = “);
     mySerial.print(lcd_text);
     mySerial.print(“ Volt. “);
     delay(500);
 }
                                                  Micrcocontroller experiment in C with Arduino by POP-168 modulel35


2.5 Sound output example


                                             DB-9
                                           (optional)                                                   SPEAKER     O

             5          3 2                      IC1
           K1                                  POP-168
         RS-232
                                                                                               +5V
                                                                                                               +S
                                                                               SW2
                    Di 1/TxD
                                  1                                 24        RESET
                                      1




                   Di 0/RxD
                                  2                                 23      GND
                                  3                                 22
                                                                            RESET
                     GND
                                  4                                 21   VDD/+5Vdc
                   An 0/PC0
                                  5                                 20   Di 9/PWM/PB1
                                  6                                 19
                                                   ATMEGA168




                   An 1/PC1
                                  7                                 18
                                                                           Di 8/PB0     Connect this port if
                   An 2/PC2
                                  8                                 17
                                                                           Di 7/PD7
                                                                                        use POP-INTERFACE
                   An 3/PC3                                              Di 6/PWM/PD6
                  An 4/SDA/PC4
                                  9                                 16   Di 5/PWM/PD5
                  An 5/SCL/PC5
                                 10                                 15     Di 4/PD4
                   An 6/AN6
                                 11                                 14   Di 3/PWM/PD3
                   An 7/AN7
                                 12                                 13     Di 2/PD2
                                      12




                                                                                                 C1        SP1
                                                                                                10 F      Piezo
                                                               13




                                              POP-168




/***********************************************************
  * Program drive piezo speaker to sound at 1kHz 0.5 sec
      and off 0.5 sec.
  * File : Sound_1kHz.pde
  * By     : Opas Sirikunchittavorn   23 Feb 2008
  *
  ***********************************************************/
#define PIEZO_PIN 3
void setup() {
    pinMode(PIEZO_PIN,OUTPUT);        // Piezo speaker at pin 3
}
void loop() {
    int i;
    for(i=0;i<500;i++) {
       digitalWrite(PIEZO_PIN,HIGH);
       delayMicroseconds(500);
       digitalWrite(PIEZO_PIN,LOW);
       delayMicroseconds(500);
    }
    delay(500);                      // off buzzer 0.5 second.
}
  Micrcocontroller experiment in C with Arduino by POP-168 module
36 l
                                      Micrcocontroller experiment in C with Arduino by POP-168 modulel37



3 : Arduino POP-168 with
sensor interface example
3.1 Interface with ZX-SOUND
3.1.1 ZX-SOUND features
      l Detect sound pressure with Condenser Microphone.

      l Output voltage 0 to +5V varies from sound pressure level that microphone
detected. If pressure more, the output voltage goes high and vice versa.

      The ZX-SOUD schematic diagram is shown below.


                                                                   R5
                                            C1              R6    68k               R7
                    R1                   470uF/16V          1k                     12R
                              C2                                  6           8
                    22k   0.1uF/50V                                   +
                                           3                                        7
                                               +                          IC1/2
                                                            1     5
                          MIC1         R2 2         IC1/1             -       4    C3
                                      100k      -                                 22uF

                                                                                          +  S
                                                   R4
                                               R3 100k      IC1 : TLC272
                                               1k
  Micrcocontroller experiment in C with Arduino by POP-168 module
38 l




3.1.2 Sound controls LED example
      This exmaple demonstrates thecontrolling LED by reading analog signal from
sound detection of ZX-SOUND. LED will light up if sound level is high and off if no sounbd
detection.




                                                               DB-9
                                                             (optional)
                                5         3 2                      IC1
              K1                                                 POP-168
            RS-232

                                                                                                                  +5V
                                                                                                                                 ZX-LED
                                                                                                  SW2
                                      Di 1/TxD
                                                                                      "         RESET                                +S
                                                        1




                                      Di 0/RxD
                                                                                       !        GND
                                                    !
                                                                                               RESET
                                        GND
                                                    "                                      VDD/+5Vdc
                                     An 0/PC0
                                                    #                                      Di 9/PWM/PB1   Connect this port if use
                                                    $                                  '
                                                                     ATMEGA168




                                     An 1/PC1                                                 Di 8/PB0     POP-INTERFACE board
 Connect from                                       %                                  &
                                     An 2/PC2                                                 Di 7/PD7
 POP-INTERFACE board                 An 3/PC3
                                                    &                                  %   Di 6/PWM/PD6
                                    An 4/SDA/PC4
                                                    '                                  $   Di 5/PWM/PD5
                                                                                     #
                                    An 5/SCL/PC5                                              Di 4/PD4         R1 LED1
                                     An 6/AN6
                                                                                     "   Di 3/PWM/PD3       510
                            )                                                         !
                                     An 7/AN7                                                 Di 2/PD2
                                                        12




                       +S
                                                                                 13




                                                                POP-168
                ZX-SOUND




 /***************************************************************
  * Program for read input from sound module,
  * and used sound level to control brightness of LED at Di 5
  * File    : AudioIn.pde
  * By      : Opas Sirikunchittavorn    23 Feb 2008
   ***************************************************************/
 #define LED_PIN       5    // LED for PWM output.
 #define ANALOG_PIN    1    // Analog input at pin An 1.
 int analogValue = 0;       // Variable for store analog value

 void setup()
 {
   pinMode(LED_PIN, OUTPUT);
 }

 void loop()
 {
   analogValue = analogRead(ANALOG_PIN);                                         //   Read analog input at An 0.
   analogValue = analogValue/4;                                                  //   Scale value to 0-255
   analogWrite(LED_PIN, analogValue);                                            //   Adjust brightness of LED
   delay(100);                                                                   //   Delay 0.1 second.
 }
                                             Micrcocontroller experiment in C with Arduino by POP-168 modulel39


3.2 GP2Y0A21 distance sensor interface example
       GP2Y0A21 can measure the distance in range 10 to 80 cm. The output is DC
voltage 0.4 to 2.4V.

       The formula to convert the digtial value from A/D converter of POP-168 module
to distance value of GP2Y0A21 is :

                           $%&%
                       4          "
                           8 !

              Thus, R is Distance in cm.

                       V is the digtial value from A/D converter of POP-168 module

       This example demonstrates the reading distance dtection value from GP2Y0A21
and display on the Serial monitor of Arduino IDE.

       After upload the code, must open the Serial monitor.




                                                                     DB-9
                                                                   (optional)
                                      5         3 2                       IC1
                   K1                                                   POP-168
                 RS-232

                                                                                                                +5V

                                                                                                       SW2
                                            Di 1/TxD
                                                          1                                 24        RESET
                                                              1




                                            Di 0/RxD
                                                          2                                 23       GND
                                                          3                                 22
                                                                                                    RESET
                                              GND
                                                          4                                 21   VDD/+5Vdc
                                           An 0/PC0
                                                          5                                 20   Di 9/PWM/PB1
                                                          6                                 19
                                                                           ATMEGA168




                                           An 1/PC1                                                Di 8/PB0
                                                          7                                 18
             Connect from                  An 2/PC2
                                                          8                                 17
                                                                                                   Di 7/PD7
                                           An 3/PC3                                              Di 6/PWM/PD6
             POP-INTERFACE                An 4/SDA/PC4
                                                          9                                 16   Di 5/PWM/PD5
                                                         10                                 15
             connector                    An 5/SCL/PC5
                                                         11                                 14
                                                                                                   Di 4/PD4
                                           An 6/AN6                                              Di 3/PWM/PD3
                                           An 7/AN7
                                                         12                                 13     Di 2/PD2
                                                              12




                                                                                       13




                                                                      POP-168
  Micrcocontroller experiment in C with Arduino by POP-168 module
40 l



 /******************************************************************
  * Code read input from GP2Y0A21 (Infrared Distance Ranger Module)
  * Display to Serial monitor
  * File : GP2Y0A21.pde
  * By    : Chaiwat Limpornchitwilai   May 28th, 2008
   ******************************************************************/
 #define ANALOG_PIN 6     // Analog input at pin An 6.

 int distance;
 int analogValue;                     // variable to store analog value

 void setup() {
   Serial.begin(9600);
 }
 void loop() {
   analogValue = analogRead(ANALOG_PIN);                                  // Read analog input at An 0.
   distance = (6787/(analogValue-3))-4;                                   // convert to distance
   if (distance >= 10 && distance <=80) {
     Serial.print(“Distance : “);
     Serial.print(distance);                                              // print output
     Serial.println(“ cm”);
     delay(200);
   }
   else if (distance < 10) {
     Serial.println(“Under Range”);
     delay(200);
   }
   else {
     Serial.println(“Over Range”);
     delay(200);
   }
 }




3.3 Interace with ZX-TILT
        The ZX-TILT use the tilt sensor GP1S036HEZ from Sharp Microelectronics. Output is 2-
bit for tilt angle 4 ranges 0, 90, 180 and 270 degree. The schematic of this sensor is
shown below.



                                                                         LED B                LED A
                                      1    5                             GREEN                RED
                                               GP1S036HEZ
                                               4                                                      OUTPUT-A
                                                              10k   1k                 10k   1k
                                      2    3                                                                     S
                                                                                                                 +

                                      1k

                                                                                                                 S
                                                                                                                 +
                                                                                                   OUTPUT-B
                                                                                              0.1uF
                                                      Q1                       Q2
                                                    KRC102                   KRC102
                                                   (DTC114)                 (DTC114)
                         Micrcocontroller experiment in C with Arduino by POP-168 modulel41


3.3.1 Tilt direction detection versus Output Logic

                           0 degree Direction

                           A Condition is false = LED A off = OUTA ì1î

                           B Condition is false = LED B off = OUTB ì1î




                           90 degree Direction

                           A Condition is true = LED A on = OUTA ì0î

                           B Condition is false = LED B off = OUTB ì1î




                           180 degree Direction

                           A Condition is true = LED A on = OUTA ì0î

                           B Condition is true = LED B on = OUTB ì0î




                           270 degree Direction

                           A Condition is false = LED A off = OUTA ì1î

                           B Condition is true = LED B on = OUTB ì0î
  Micrcocontroller experiment in C with Arduino by POP-168 module
42 l




         This example demonstrates the reading tilt dtection from ZX-TILT and display on
the Serial monitor of Arduino IDE.

         After upload the code, must open the Serial monitor.




                                                  DB-9
                                                (optional)
                   5        3 2                       IC1




                                                                                                                    ZX-TILT
  K1                                                POP-168
RS-232

                                                                                                     +5V                  A   B

                                                                                    SW2
                        Di 1/TxD
                                                                        "         RESET                                  A   B
                                           1




                        Di 0/RxD
                                                                         !       GND
                                       !
                                                                                RESET
                          GND
                                       "                                     VDD/+5Vdc
                        An 0/PC0
                                       #                                  
                                                                                             µ‹Í¨Ò¡¨Ø´µ‹Í¢Í§ºÍÏ´
                                                                              Di 9/PWM/PB1
                                                        ATMEGA168




                        An 1/PC1
                                       $                                 '    Di 8/PB0
                                       %                                 &
                        An 2/PC2
                                       &                                 %
                                                                               Di 7/PD7         POP-INTERFACE
                        An 3/PC3                                              Di 6/PWM/PD6
                       An 4/SDA/PC4
                                       '                                 $   Di 5/PWM/PD5
                       An 5/SCL/PC5
                                                                       #    Di 4/PD4
                        An 6/AN6
                                                                       "   Di 3/PWM/PD3
                        An 7/AN7
                                                                        !    Di 2/PD2
                                           12




                                                                    13




                                                   POP-168



 /**********************************************************
  * Code for read input of ZX-TILT and display to RS232.
  * File    : ZX-TILT.pde     (Size after compile 2584 bytes)
  * Target : POP-168 (Arduino ATmega168)
  * By      : Opas Sirikunchittavorn     (9 Mar 2008)
   **********************************************************/
 #define IN_A 6                       // Input A of ZX-TILT
 #define IN_B 4                       // Input B of ZX-TILT

 int a,b;
 void setup() {
   pinMode(IN_A,INPUT);
   pinMode(IN_B,INPUT);
   Serial.begin(9600);
 }
 void loop() {
   a = digitalRead(IN_A);
   b = digitalRead(IN_B);

     if((a == HIGH) && (b == HIGH))
       Serial.println(“0 degree”);
     else if ((a == HIGH) && (b == LOW))
       Serial.println(“90 degree”);
     else if ((a == LOW) && (b ==LOW))
       Serial.println(“180 degree”);
     else
       Serial.println(“270 degree”);
     delay(100);                       // wait 100 milisecond
 }
                                        Micrcocontroller experiment in C with Arduino by POP-168 modulel43



 4 : Arduino POP-168 with
 motor control example
4.1 The ZX-DCM2 motor driver board features
       l Drive DC motor 2 channels with bi-color LED indicator.

       l L293D H-Bridge driver Max. load current 600mA.

       l Fully 3-Control signal wires

      l Suitable supply voltage +4.8 to 30Vdc. Separate control voltage (+5V) and
Motor supply voltage (+V)

       l INEX Standard 3-pin JST connector. Confort to use with INEX controller boards.

       The heart of DC motor driver circuit is L293D H-Bridge driver (may be use SN754410
replacement). In the Table 1 shows all signals to control the DC motor driver circuit.

       Motor driver format has 4 types as

              (1) Clockwise motor driving

              (2) Anti-clockwies motor driving

              (3) Motorís shaft is free

              (4) Motorís shaft is locked or Brake


  12EN/34EN pin      1A/3A pin          2A/4A pin                    Motor operation
         0                  X                X                            Shaft free
         1                  0                0                     Shaft locked or Brake
         1                  0                1                       Clockwise turning
         1                  1                0                    anti-clockwise turning
         1                  1                1                     Shaft locked or Brake

 X means logic "0" or "1"

       L293D outputs connect to DC motor and provides LED status for motor supply
voltage. If supply in DIRECT, LED will lights in Green. The opposite, if red LED lights, it
means supply voltage is INVERT. Users can use the different color for defining direction.
  Micrcocontroller experiment in C with Arduino by POP-168 module
44 l




4.2 Hardware connection
4.2.1 with POP-INTERFACE board



                                           DB-9                                            +5V
                                         (optional)                                                    R2                                        R3
                                                                                                       1k                                       2.2k                                   +
           5          3 2                      IC1
                                                                                                     LED1                                       LED2                                   -
         K1                                  POP-168                                                 +5V                                         Vm
       RS-232                                                                                                                       16   8                                         +Vm
                                                                                                                                     Vcc Vm                                       Motor
                                                                                               +5V                                                                C3      C4
                                                                                                                                                                470/16V 470/16V   supply
                                                                                  SW2
                  Di 1/TxD
                                1                                 24             RESET
                                    1




                  Di 0/RxD
                                2                                 23          GND
                                3                                 22
                                4                                 21
                                                                             RESET                                                                         11
                    GND                                                  VDD/+5Vdc                                                                3Y
                  An 0/PC0
                                5                                 20   Di 9/PWM/PB1
                                                                                                                          9
                                                                                                                               2E     IC2                         R1
                                6                                 19
                                                 ATMEGA168




                  An 1/PC1
                                7                                 18
                                                                           Di 8/PB0
                                                                                                                          10         L293D                        1k
                  An 2/PC2                                                 Di 7/PD7                                            2A                                        C2            +
                                8                                 17
                  An 3/PC3                                             Di 6/PWM/PD6                                                                                     0.1 F          -
                An 4/SDA/PC4
                                9                                 16   Di 5/PWM/PD5
                                                                                                                          15 2B
                An 5/SCL/PC5
                               10                                 15       Di 4/PD4
                               11                                 14                                                                                                              MOTOR
                 An 6/AN6                                              Di 3/PWM/PD3                                                                        14
                               12                                 13                                                                               4Y                             OUTPUT
                 An 7/AN7                                                  Di 2/PD2
                                    12




                                                                                                                                                            LED2
                                                             13




                                            POP-168                                                                             G G G G
                                                                                                                               4 5 12 13




                                              Connect from
                                              POP-INTERFACE port
                                                                                                                                                           +




                                              connector


                                                                                                                      +
                                                                         +VM




                                                                                                                                            +5V




                                                                                                                                                                   Battery 4.8/6V
                                                                                                     +VM                                                                AA x4
                                                                       PWM2 DIR2A DIR2B




                                                                                                                                        DIR1B DIR1A PWM1




                                                                                          G                               +5
                                                                        2E




                                                                                                                                                     1E




                                                                                          2E                              1E
                                                                                                     L293D/SN754410




                                                                                          2A                              1A
                                                                                          2B                              1B




                                                                         ZX-DCM2
                                                                                          OUT2                        OUT1


                                                                                                 +                    +



                                                                        DC MOTOR
                                                                          6-12V
                                                                                                                 If using the ZX-DCM2 board
                                                                                           Micrcocontroller experiment in C with Arduino by POP-168 modulel45


4.2.2 with JX-POP168 project board


                                                                     +Vm 4.8-6V
   6-12Vdc




                                      RS-232                     for servo motor

                            Vin                                                                +V
                          6-12Vdc                                                              m

                                                                             13
                                                                                                        +V
                                                                                                        m
                                                                                        SERVO
POWER                                          POP-168
                                                     ATMEGA168
                                                                                            P5

                                                                                              P4
                                                                       12
                                                                                              P3
                                                    RESET
                                                                 JX-POP168
                                                            ATmega168 Matchbox                P2
                                                            Micro-Project board
                                      Microcontroller port ( P15-P0)                          P1




                                                                                                                                                                                        +
               ARDUINO    ,' ,& ,% ,$ ,# ," ,! ,    )% )$ )# )" )! )   ) )
                   +5V 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0                      GND   +Vm   P0
                                                                                                    P

                                                                                                                                                          +

                                                                                                               +VM




                                                                                                                                                                         +5V
                                                                                                                                         +VM
                                                                                                             PWM2 DIR2A DIR2B




                                                                                                                                                                     DIR1B DIR1A PWM1
                                                                                                                                G                               +5
                                                                                                              2E




                                                                                                                                                                                  1E
                                                                                                                                2E                              1E




                                                                                                                                         L293D/SN754410
                                                                                                                                2A                              1A
                                                                                                                                2B                              1B




                                                                                                               ZX-DCM2
                                                                                                                                OUT2                          OUT1


                                                                                                                                     +                    +
      -2%+     -2$+     -2#+         -2"+     -2!+        -2 +        -2+      -2+


                                                                                                              DC MOTOR
                                                                                                                6-12V
  Micrcocontroller experiment in C with Arduino by POP-168 module
46 l




4.2.3 The example code
 /*************************************************************************
  * Code for control DC motor by use L239D.
  * Forward 100% 10s, 50% 10s, stop 1s, reverse 100% 10s, 50% 10s, stop 1s.
  * File : L293D.pde
  * Target: POP-168 (Arduino ATmega168)
  * By      : Opas Sirikunchittavorn     (23 Apr 2008)
  *************************************************************************/
 #define PWM2    9             // Di 9 cinnect to PWM2
 #define DIR2A   7             // Di 7 cinnect to DIR2A
 #define DIR2B   6             // Di 6 cinnect to DIR2B

 void setup() {
   pinMode(DIR2A,OUTPUT);
   pinMode(DIR2B,OUTPUT);
 }
 void loop() {
   digitalWrite(DIR2A,LOW);                      // forward 100%
   digitalWrite(DIR2B,HIGH);
   analogWrite(PWM2,255);
   delay(10000);
      analogWrite(PWM2,127);                // forward 50%
     delay(10000);
     digitalWrite(PWM2,LOW);               // stop motor
     digitalWrite(DIR2A,LOW);
     digitalWrite(DIR2B,LOW);
     delay(1000);
      digitalWrite(DIR2A,HIGH); // reverse 100%
     digitalWrite(DIR2B,LOW);
     analogWrite(PWM2,255);
     delay(10000);
      analogWrite(PWM2,127);                // reverse 50%
     delay(10000);
      digitalWrite(PWM2,LOW);               // stop motor
     digitalWrite(DIR2A,LOW);
     digitalWrite(DIR2B,LOW);
     delay(1000);
 }
       Program operation start with drive the DC motor to forward direction with 100%
or maximum speed 10 seconds. After that, decrease speed to 50% by 10 seconds and
stop 1 second. Next, reverse direction and drive with 100% speed by 10 seconds,
decrease to 50% by 10 seconds and stop 1 second. It will run again.
                                                                                              Micrcocontroller experiment in C with Arduino by POP-168 modulel47


4.3 Control speed and direction of DC motor
driver with external device example
       This example demonstrates the DC motor control by using Variable resistor for
controlling the speed and Switch for changing the direction.


                                                                DB-9                                            +5V
                                                              (optional)                                                    R2                                          R3
                                                                                                                            1k                                         2.2k                                     +
                                 5         3 2                       IC1
                                                                                                                          LED1                                         LED2                                     -
                   K1                                              POP-168                                                +5V                                           Vm
                 RS-232                                                                                                                                    16   8                                           +Vm
                                                                                                                                                            Vcc Vm                       C3      C4        Motor
                                                                                                                    +5V
                                                                                                                                                                                       470/16V 470/16V     supply
                                                                                                       SW2
                                       Di 1/TxD
                                                                                      "              RESET
                                                         1




                                       Di 0/RxD
                                                                                       !           GND
                                                     !
                                                     "                                  
                                                                                                  RESET                                                                           11
                                          GND                                                 VDD/+5Vdc                                                                  3Y
                                       An 0/PC0
                                                     #                                     Di 9/PWM/PB1
                                                                                                                                                 9
                                                                                                                                                      2E     IC2                         R1
                                                     $                                 '
                                                                      ATMEGA168




                                       An 1/PC1
                                                     %                                 &
                                                                                                Di 8/PB0
                                                                                                                                                 10         L293D                        1k
                                       An 2/PC2                                                 Di 7/PD7                                              2A                                         C2             +
  +5V                                                &                                 %
                                       An 3/PC3                                             Di 6/PWM/PD6                                                                                       0.1 F            -
                                     An 4/SDA/PC4
                                                     '                                 $   Di 5/PWM/PD5
                                                                                                                                                 15 2B
        VR1                          An 5/SCL/PC5
                                                                                     #       Di 4/PD4
                                                                                     "                                                                                                                MOTOR
        10k                           An 6/AN6                                              Di 3/PWM/PD3                                                                          14
                                                                                      !                                                                                 4Y                             OUTPUT
                                      An 7/AN7                                                  Di 2/PD2
                                                         12




                                                                                                                                                                                   LED2
                                 R2
                                                                                  13




                                 10k
                                                  +5V            POP-168                                                                               G G G G
                                                                                                                                                      4 5 12 13
POP-INTERFACE




                                     S1
port connector
Connect from




                                                                    Connect from
                                                                     POP-INTERFACE port
                                                                                                                                                                                  +




                                                                    connector


                                                                                                                                           +
                                                                                              +VM




                                     LOW            HIGH
                                                                                                                                                                   +5V
                 POTENTIOMETER




                                                                                                                                                                                          Battery 4.8/6V
                                                                                                                          +VM                                                                  AA x4
                                                                                            PWM2 DIR2A DIR2B




                                                                                                                                                               DIR1B DIR1A PWM1




                                                                                                               G                                 +5
                                                                                             2E




                                                                                                                                                                            1E




                                                                                                               2E                                1E
                                                                                                                          L293D/SN754410




                                                                                                               2A                                1A
                                 D




                                                                                                               2B                                1B
  A




                                                                                              ZX-DCM2
                                                                                                               OUT2                            OUT1


                                                                                                                      +                    +



                                                                                             DC MOTOR
                                                                                                                                  The connection diagram if
                                                                                               6-12V                              using ZX-DCM2 board
  Micrcocontroller experiment in C with Arduino by POP-168 module
48 l



 /*********************************************************************
  * Code for control DC motor by use L239D.
  * Use voltage from An 2 for control speed.
  * File    : L293D_ADC.pde     (Size after compile 1598 bytes)
  * Target : POP-168 (Arduino ATmega168)
  * By      : Opas Sirikunchittavorn     (23 Apr 2008)
   **********************************************************************/
 #define PWM2     9            // Di 9 cinnect to PWM2
 #define DIR2A    7            // Di 7 cinnect to DIR2A
 #define DIR2B    6            // Di 6 cinnect to DIR2B
 #define BUTTON 17              // Di 17/An 3 connect to push button
 #define ADC      7            // An 7 connect to variale resistor

 int   volt;
 int   direction=0;                         // 0 = forward,
 int   reading;                             // the current reading from the input pin
 int   previous = HIGH;                     // the previous reading from the input pin

 void setup() {
   pinMode(DIR2A,OUTPUT);
   pinMode(DIR2B,OUTPUT);
   pinMode(BUTTON,INPUT);
 }

 void loop() {
   reading = digitalRead(BUTTON);

     // if we just pressed the button (i.e. the input went from HIGH to LOW.
     if (reading == LOW && previous == HIGH)
     {
       delay(10);                        // wait for decounce
       if(digitalRead(BUTTON) == LOW)    // if the input remain LOW
       {
         digitalWrite(DIR2A,LOW);        // stop motor
         digitalWrite(DIR2B,LOW);
         digitalWrite(PWM2,LOW);
         delay(500);                     // .5 second
         direction = !direction;         // invert direction of motor
       }
     }
     previous = reading;

     volt=analogRead(ADC);
     volt=volt/4;
     if(direction == 0) {
       digitalWrite(DIR2A,LOW);                           // forward
       digitalWrite(DIR2B,HIGH);
       analogWrite(PWM2,volt);
     }
     else {
       digitalWrite(DIR2A,HIGH);                          // reverse
       digitalWrite(DIR2B,LOW);
       analogWrite(PWM2,volt);
     }
     delay(100);
 }

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:200
posted:8/14/2011
language:English
pages:48