You are on page 1of 35

General Purpose Input/Output

Department of Electrical Engineering,


www.ee.ntou.edu.tw
National Taiwan Ocean University

3/21/2013

Richard Kuo
Assistant Professor
Outline
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
► Cortex-M0 MCU General Purpose Input/Output
4.NuMicro_GPIO.PPT

► Exercise : GPIO to scan 3x3 keypad (Smpl_7seg_Keypad)


► Exercise : GPIO controlled by 3x3 keypad (Smple_GPIO_Keypad)
► Exercise : GPIO to read Body Infrared Sensor
(Smpl_GPIO_BodyInfrared)
► Exercise : GPIO connecting to LCM16x2 (Smpl_GPIO_LCM16x2)
► Exercise : GPIO interface to 16x16 LED Matrix
(Smpl_GPIO_LED16x16)
Nu-LB-NUC140 Learning Board
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

7 segment LEDs

RGB LED GPA12,13,14


LEDs GPC12,13,14,15

3x3Keypad
Buzzer
GPB11
3x3 Keypad schematic
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Control Pins used for 3x3 Keypad
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Column control : GPA2, 1, 0
Raw control : GPA 3, 4, 5
► Key1 = GPA3 & GPA2
► Key2 = GPA3 & GPA1
► Key3 = GPA3 & GPA0
► Key4 = GPA4 & GPA2
► Key5 = GPA4 & GPA1
► Key6 = GPA4 & GPA0
► Key7 = GPA5 & GPA2
► Key8 = GPA5 & GPA1
► Key9 = GPA5 & GPA0
Smpl_7seg_Keypad
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Display Number = 5

Press middle key


Keypad Driver – Scankey.c
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
#include <stdio.h>
#include "Driver\DrvGPIO.h"
#include "ScanKey.h"

void delay(void)
{
int j;
for(j=0;j<1000;j++);
}

void OpenKeyPad(void)
{
uint8_t i;
/* Initial key pad */
for(i=0;i<6;i++)
DrvGPIO_Open(E_GPA, i, E_IO_QUASI);
}

void CloseKeyPad(void)
{
uint8_t i;

for(i=0;i<6;i++)
DrvGPIO_Close(E_GPA, i);
}
Keypad Driver – Scankey.c
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
uint8_t Scankey(void)
{
uint8_t act[4]={0x3b, 0x3d, 0x3e};
uint8_t i,temp,pin;

for(i=0;i<3;i++)
{
temp=act[i];
for(pin=0;pin<6;pin++)
{
if((temp&0x01)==0x01)
DrvGPIO_SetBit(E_GPA,pin);
else
DrvGPIO_ClrBit(E_GPA,pin);
temp>>=1;
}
delay();
if(DrvGPIO_GetBit(E_GPA,3)==0)
return(i+1);
if(DrvGPIO_GetBit(E_GPA,4)==0)
return(i+4);
if(DrvGPIO_GetBit(E_GPA,5)==0)
return(i+7);
}
return 0;
}
Smpl_7seg_Keypad
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int32_t main (void)
{
int8_t number;

UNLOCKREG();
DrvSYS_Open(48000000);
LOCKREG();

OpenKeyPad();

while(1)
{
number = Scankey(); // scan keypad to get a number (1~9)
show_seven_segment(0,number); // display number on 7-segment LEDs
DrvSYS_Delay(5000); // delay time for keeping 7-segment display
close_seven_segment(); // turn off 7-segment LEDs

}
}
Smpl_GPIO_Keypad
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Press key

Control GPIO outputs


GPIOs control 4-port Relay
Smpl_GPIO_Keypad
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Press key1 Press key2 Press key3 Press key4


Relay #1 off Relay #2 off Relay #3 off Relay #4 off

Vcc = 3.3V, GPIO output hi (base)  Relay Control pin = low (collector)
Smpl_GPIO_Keypad
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int32_t main (void) OpenKeyPad();
{ print_lcd(0,"Smpl_GPIO_Keypad");
char TEXT1[16] = "number: "; while(1)
int8_t i, number; {
number = Scankey();
UNLOCKREG();
sprintf(TEXT1+8,"%d", number);
SYSCLK->PWRCON.XTL12M_EN = 1;
print_lcd(1,TEXT1);
SYSCLK->CLKSEL0.HCLK_S = 0;
if (number!=0) DrvGPIO_SetBit(E_GPB,
LOCKREG();
number-1);
else
Initial_panel();
for (i=0; i<9; i++) DrvGPIO_ClrBit(E_GPB, i);
clr_all_panel();
}
for (i=0; i<9; i++) {
DrvGPIO_Open(E_GPB, i, E_IO_OUTPUT); }
DrvGPIO_ClrBit(E_GPB, i);
}
Smpl_GPIO_BodyInfrared
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Display Detected !

Human Body Infrared Detector


Smpl_GPIO_BodyInfrared
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int main(void)
{
UNLOCKREG();
SYSCLK->PWRCON.XTL12M_EN = 1; //Enable 12Mhz crystal and set HCLK=12Mhz
SYSCLK->CLKSEL0.HCLK_S = 0;
LOCKREG();

DrvGPIO_Open(E_GPA, 0, E_IO_INPUT); // set GPA0 to input mode

Initial_panel();
clr_all_panel();

print_lcd(0, "Body Infrared ");


print_lcd(1, "GPA0 input......");

while(1) {
if (DrvGPIO_GetBit(E_GPA,0)==0) print_lcd(2, "Object Detected!");
else print_lcd(2, "No Detection! ");
}
}
LCD MODULE - PC1602
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Operating at 5V
LCM16x2 Pin Description
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
// LCD 16x2 Module
// pin1 Gnd (to Gnd)
// pin2 Vcc: (to +5V)
// pin3 Vo : brightness control (to Gnd)
// pin4 RS : 1=Data, 0=Instruction (to GPA0)
// pin5 RW : 1=Read, 0=Write (to GPA1)
// pin6 E : Chip Enable (to GPA2)
// pin7~14 : D0~D7 (to GPE0~7)
// pin15 A : backlight+ (to Vcc) no backlight, no coonection
// pin16 K : backlight- (to Gnd) no backlight, no connection

// DB[7:0] : Description
// 0000_0001: Clear Display
// 0000_001x: Return to Home (Display RAM address=0)
// 0000_01DS: Cursor move direction, Display Shift
// 0000_1DCB: Display/Cursor/Blinking on/off
// 0001_SRxx: Curosor move, shift R/L
// 01xx_xxxx: set CGRAM
// 1xxx_xxxx: set DDRAM
Smpl_GPIO_LCM16x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void LCD_data(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS_SET;
LCD_RW_CLR;
LCD_E_CLR;
DrvSYS_Delay(100);
LCD_E_SET;
}

void LCD_cmd(unsigned char wdata)


{
LCD_DATA=wdata;
LCD_RS_CLR;
LCD_RW_CLR;
LCD_E_CLR;
DrvSYS_Delay(100);
LCD_E_SET;
}
Smpl_GPIO_LCM16x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void Init_LCM16x2(void)
{
LCD_DATA=0;
DrvSYS_Delay(40000); // wait time >40ms after Vcc>4.5V
LCD_cmd(0x38); // 8-bit Interface
DrvSYS_Delay(1000);
LCD_cmd(0x38);
DrvSYS_Delay(37);
LCD_cmd(0x06); // Cursor Move to right, no shift
DrvSYS_Delay(37);
LCD_cmd(0x0C); // Display ON, Cursor Off, Blinking off
DrvSYS_Delay(37);
LCD_cmd(0x01); // Display Clear
DrvSYS_Delay(1520);
}
Smpl_GPIO_LCM16x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void display_xy(uint8_t x,uint8_t y) void display_string(uint8_t x,uint8_t y,unsigned
{ char *s)
if(y==1) x+=0x40; {
uint8_t i;
x+=0x80;
LCD_cmd(x); display_xy(x,y);
} i=0;
while((x+i)<16)
{
void display_char(uint8_t x, uint8_t y,
LCD_data(*s);
unsigned char dat)
s++;
{ i++;
display_xy(x,y); }
LCD_data(dat); }
}
Smpl_GPIO_LCM16x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
main()
{
unsigned char LcdBuf1[16]= "Welcome to NTOU ";
unsigned char LcdBuf2[16]= "Cortex-M0 MCU !!";

Init_LCM16x2();

display_string(0,0,LcdBuf1); // display line 1


display_string(0,1,LcdBuf2); // display line 2
}
Smpl_GPIO_LCM8x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Smpl_GPIO_LCM8x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
//
// NuMicro Nu-LB-NUC140
// Sample Code : Smpl_GPIO_LCM8x2
//
// LCD 8x2 Module
// pin1 Vss: (to Gnd)
// pin2 Vcc: to +5V (to +5V)
// pin3 Vee : brightness control (to Gnd)
// pin4 RS : 1=Data, 0=Instruction (to GPA0)
// pin5 RW : 1=Read, 0=Write (to GPA1)
// pin6 E : Chip Enable (to GPA2)
// pin7~14 : D0~D7 (to GPB0~7)
Smpl_GPIO_LCM8x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvSYS.h"

#define LCD_DATA GPIOB->DOUT // D0~7 pin = GPB0~7


#define LCD_RS_HI DrvGPIO_SetBit(E_GPA,0) // RS pin = GPA0
#define LCD_RS_LO DrvGPIO_ClrBit(E_GPA,0)
#define LCD_RW_HI DrvGPIO_SetBit(E_GPA,1) // RW pin = GPA1
#define LCD_RW_LO DrvGPIO_ClrBit(E_GPA,1)
#define LCD_E_HI DrvGPIO_SetBit(E_GPA,2) // E pin = GPA2
#define LCD_E_LO DrvGPIO_ClrBit(E_GPA,2)
Smpl_GPIO_LCM8x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void Init_GPIO(void) void LCD_cmd(unsigned char wdata)
{ {
DrvGPIO_Open(E_GPA, 0, E_IO_OUTPUT); LCD_DATA=wdata;
DrvGPIO_Open(E_GPA, 1, E_IO_OUTPUT); LCD_RS_LO;
DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT); LCD_RW_LO;
DrvGPIO_ClrBit(E_GPA, 0); LCD_E_HI;
DrvGPIO_ClrBit(E_GPA, 1); DrvSYS_Delay(1000);
DrvGPIO_ClrBit(E_GPA, 2); LCD_E_LO;
} }

void LCD_data(unsigned char wdata)


{
LCD_DATA=wdata;
LCD_RS_HI;
LCD_RW_LO;
LCD_E_HI;
DrvSYS_Delay(1000);
LCD_E_LO;
}
Smpl_GPIO_LCM8x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void Init_LCM8x2(void) void display_char(uint8_t x, uint8_t y, unsigned char dat)
{ {
LCD_DATA=0; display_xy(x,y);
DrvSYS_Delay(37);// wait time >40ms after Vcc>4.5V LCD_data(dat);
LCD_cmd(0x38); }
DrvSYS_Delay(37); void display_string(uint8_t x,uint8_t y,unsigned char *s)
LCD_cmd(0x38); {
DrvSYS_Delay(37); uint8_t i;
LCD_cmd(0x38); display_xy(x,y);
DrvSYS_Delay(37); i=0;
LCD_cmd(0x0C); while((x+i)<8)
DrvSYS_Delay(37); {
LCD_cmd(0x01); LCD_data(*s);
DrvSYS_Delay(1520); s++;
LCD_cmd(0x06);
i++;
DrvSYS_Delay(37);
}
}
}
void display_xy(uint8_t x,uint8_t y)
{
if(y==1) x+=0x40;
x+=0x80;
LCD_cmd(x);
}
Smpl_GPIO_LCM8x2
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
main()
{
unsigned char LcdBuf1[8]= "Nuvoton ";
unsigned char LcdBuf2[8]= "CortexM0";
UNLOCKREG();
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); // Enable the 12MHz oscillator oscillation
DrvSYS_SelectHCLKSource(0); // HCLK clock source. 0: external 12MHz; 4:internal
22MHz RC oscillator
LOCKREG();
Init_GPIO();
Init_LCM8x2();
display_string(0,0,LcdBuf1);// 顯示第一行,第一列第 0 位置開始
display_string(0,1,LcdBuf2);// 顯示第二行,第二列第 0 位置開始
}
16x16 LED Matrix
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

► Interface Signals:
– VCC
– GND
– P24
– P23
– P22
– P21
16x16 LED Matrix schematic
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University

Sample Code : Smpl_GPIO_LED16x16


Smpl_GPIO_LED16x16
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
// LED Matrix 16x16
// pin description & connection
// P24 : CLK to GPA4
// P23 : AB to GPA3
// P22 : RCK to GPA2
// P21 : SRCK to GPA1
// P20 : SER to GPA0

#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvGPIO.h"

#define GPIO_port E_GPA


#define LED16x16_CLK 4 // 74HC164 clock
#define LED16x16_AB 3 // 74HC164 AB
#define LED16x16_RCK 2 // 74HC595 register clock
#define LED16x16_SRCK 1 // 74HC595 shift register clock
#define LED16x16_SER 0 // 74HC595 serial in
Smpl_GPIO_LED16x16
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void InitLED16x16(void)
{
DrvGPIO_InitFunction(E_FUNC_GPIO);

DrvGPIO_Open(GPIO_port, LED16x16_CLK, E_IO_OUTPUT);


DrvGPIO_Open(GPIO_port, LED16x16_AB, E_IO_OUTPUT);
DrvGPIO_Open(GPIO_port, LED16x16_RCK, E_IO_OUTPUT);
DrvGPIO_Open(GPIO_port, LED16x16_SRCK, E_IO_OUTPUT);
DrvGPIO_Open(GPIO_port, LED16x16_SER, E_IO_OUTPUT);
// set to default
DrvGPIO_ClrBit(GPIO_port, LED16x16_CLK);
DrvGPIO_ClrBit(GPIO_port, LED16x16_AB);
DrvGPIO_ClrBit(GPIO_port, LED16x16_RCK);
DrvGPIO_ClrBit(GPIO_port, LED16x16_SRCK);
DrvGPIO_ClrBit(GPIO_port, LED16x16_SER);
}
Smpl_GPIO_LED16x16
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void CLK(uint8_t level) {
if (level==0) DrvGPIO_ClrBit(GPIO_port, LED16x16_CLK);
else DrvGPIO_SetBit(GPIO_port, LED16x16_CLK); }

void AB(uint8_t level) {


if (level==0) DrvGPIO_ClrBit(GPIO_port, LED16x16_AB);
else DrvGPIO_SetBit(GPIO_port, LED16x16_AB); }

void RCK(uint8_t level) {


if (level==0) DrvGPIO_ClrBit(GPIO_port, LED16x16_RCK);
else DrvGPIO_SetBit(GPIO_port, LED16x16_RCK); }

void SRCK(uint8_t level) {


if (level==0) DrvGPIO_ClrBit(GPIO_port, LED16x16_SRCK);
else DrvGPIO_SetBit(GPIO_port, LED16x16_SRCK); }

void SER(uint8_t level) {


if (level==0) DrvGPIO_ClrBit(GPIO_port, LED16x16_SER);
else DrvGPIO_SetBit(GPIO_port, LED16x16_SER); }
Smpl_GPIO_LED16x16
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
void display() {
uint8_t i,ia,j,tmp;
AB(0);
for(i=0;i<16;i++) {
CLK(0);
RCK(0);
SRCK(0);
for(ia=2; ia>0; ia--) {
tmp = ~ data[(i*2)+ia-1]; // even-byte will shift to Right, odd-byte at Left
for(j=0; j<8; j++) {
SRCK(0);
SER(( (tmp>>j) & 0x01));
SRCK(1); }
}
RCK(1);
CLK(1);
AB(1);
}
SRCK(0);
RCK(0);
CLK(0);
}
Smpl_GPIO_LED16x16
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
int32_t main (void)
{
uint8_t k;
UNLOCKREG();
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //Enable the 12MHz oscillator oscillation
DrvSYS_Delay(5000); //Waiting for 12M Xtal stable

DrvSYS_SelectHCLKSource(0); // HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator


LOCKREG();

DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0);

InitLED16x16();
while(1)
{
for(k=0;k<32;k++) data[k]=greenman1[k];
display();
Delay(50000);
}
}
General Disclaimer
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
The Lecture is strictly used for educational purpose.

MAKES NO GUARANTEE OF VALIDITY


►The lecture cannot guarantee the validity of the information found here. The lecture may recently have been
changed, vandalized or altered by someone whose opinion does not correspond with the state of knowledge in the relevant
fields. Note that most other encyclopedias and reference works also have similar disclaimers.
No formal peer review
►The lecture is not uniformly peer reviewed; while readers may correct errors or engage in casual peer review, they have
no legal duty to do so and thus all information read here is without any implied warranty of fitness for any purpose or use
whatsoever. Even articles that have been vetted by informal peer review or featured article processes may later have been
edited inappropriately, just before you view them.
No contract; limited license
►Please make sure that you understand that the information provided here is being provided freely, and that no kind of
agreement or contract is created between you and the owners or users of this site, the owners of the servers upon which it is
housed, the individual Wikipedia contributors, any project administrators, sysops or anyone else who is in any way
connected with this project or sister projects subject to your claims against them directly. You are being granted a limited
license to copy anything from this site; it does not create or imply any contractual or extracontractual liability on the part of
Wikipedia or any of its agents, members, organizers or other users.
►There is no agreement or understanding between you and the content provider regarding your use or modification of
this information beyond the Creative Commons Attribution-Sharealike 3.0 Unported License (CC-BY-SA) and the GNU
Free Documentation License (GFDL);
General Disclaimer
Department of Electrical Engineering, www.ee.ntou.edu.tw
National Taiwan Ocean University
Trademarks
►Any of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, used or cited in
the lectures are the property of their respective owners. Their use here does not imply that you may use them for any
purpose other than for the same or a similar informational use as contemplated by the original authors under the CC-BY-
SA and GFDL licensing schemes. Unless otherwise stated , we are neither endorsed by nor affiliated with any of the
holders of any such rights and as such we cannot grant any rights to use any otherwise protected materials. Your use of any
such or similar incorporeal property is at your own risk.
Personality rights
►The lecture may portray an identifiable person who is alive or deceased recently. The use of images of living or recently
deceased individuals is, in some jurisdictions, restricted by laws pertaining to personality rights, independent from their
copyright status. Before using these types of content, please ensure that you have the right to use it under the laws which
apply in the circumstances of your intended use. You are solely responsible for ensuring that you do not infringe someone
else's personality rights.

You might also like