Download presentation
1
General Purpose Input/Output
3/21/2013 Richard Kuo Assistant Professor
2
Outline 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)
3
Nu-LB-NUC140 Learning Board
7 segment LEDs RGB LED GPA12,13,14 LEDs GPC12,13,14,15 3x3Keypad Buzzer GPB11
4
3x3 Keypad schematic
5
Control Pins used for 3x3 Keypad
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
6
Smpl_7seg_Keypad Display Number = 5 Press middle key
7
Keypad Driver – Scankey.c
#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) DrvGPIO_Close(E_GPA, i);
8
Keypad Driver – Scankey.c
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;
9
Smpl_7seg_Keypad int32_t main (void) { int8_t number; UNLOCKREG();
DrvSYS_Open( ); 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 }
10
Smpl_GPIO_Keypad Press key Control GPIO outputs
GPIOs control 4-port Relay
11
Smpl_GPIO_Keypad 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)
12
Smpl_GPIO_Keypad int32_t main (void) { char TEXT1[16] = "number: ";
int8_t i, number; UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN = 1; SYSCLK->CLKSEL0.HCLK_S = 0; LOCKREG(); Initial_panel(); clr_all_panel(); for (i=0; i<9; i++) { DrvGPIO_Open(E_GPB, i, E_IO_OUTPUT); DrvGPIO_ClrBit(E_GPB, i); } OpenKeyPad(); print_lcd(0,"Smpl_GPIO_Keypad"); while(1) { number = Scankey(); sprintf(TEXT1+8,"%d", number); print_lcd(1,TEXT1); if (number!=0) DrvGPIO_SetBit(E_GPB, number-1); else for (i=0; i<9; i++) DrvGPIO_ClrBit(E_GPB, i); }
13
Smpl_GPIO_BodyInfrared
Display Detected ! Human Body Infrared Detector
14
Smpl_GPIO_BodyInfrared
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! "); }
15
LCD MODULE - PC1602 Operating at 5V
16
LCM16x2 Pin Description // 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
17
Smpl_GPIO_LCM16x2 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_RS_CLR;
18
Smpl_GPIO_LCM16x2 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 LCD_cmd(0x0C); // Display ON, Cursor Off, Blinking off LCD_cmd(0x01); // Display Clear DrvSYS_Delay(1520); }
19
Smpl_GPIO_LCM16x2 void display_xy(uint8_t x,uint8_t y) { if(y==1) x+=0x40; x+=0x80; LCD_cmd(x); } void display_char(uint8_t x, uint8_t y, unsigned char dat) display_xy(x,y); LCD_data(dat); void display_string(uint8_t x,uint8_t y,unsigned char *s) { uint8_t i; display_xy(x,y); i=0; while((x+i)<16) LCD_data(*s); s++; i++; }
20
Smpl_GPIO_LCM16x2 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 }
21
Smpl_GPIO_LCM8x2
22
Smpl_GPIO_LCM8x2 // // 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)
23
Smpl_GPIO_LCM8x2 #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)
24
Smpl_GPIO_LCM8x2 void Init_GPIO(void) { DrvGPIO_Open(E_GPA, 0, E_IO_OUTPUT); DrvGPIO_Open(E_GPA, 1, E_IO_OUTPUT); DrvGPIO_Open(E_GPA, 2, E_IO_OUTPUT); DrvGPIO_ClrBit(E_GPA, 0); DrvGPIO_ClrBit(E_GPA, 1); DrvGPIO_ClrBit(E_GPA, 2); } void LCD_data(unsigned char wdata) LCD_DATA=wdata; LCD_RS_HI; LCD_RW_LO; LCD_E_HI; DrvSYS_Delay(1000); LCD_E_LO; void LCD_cmd(unsigned char wdata) { LCD_DATA=wdata; LCD_RS_LO; LCD_RW_LO; LCD_E_HI; DrvSYS_Delay(1000); LCD_E_LO; }
25
Smpl_GPIO_LCM8x2 void Init_LCM8x2(void) { LCD_DATA=0; DrvSYS_Delay(37);// wait time >40ms after Vcc>4.5V LCD_cmd(0x38); DrvSYS_Delay(37); LCD_cmd(0x0C); LCD_cmd(0x01); DrvSYS_Delay(1520); LCD_cmd(0x06); } void display_xy(uint8_t x,uint8_t y) if(y==1) x+=0x40; x+=0x80; LCD_cmd(x); void display_char(uint8_t x, uint8_t y, unsigned char dat) { display_xy(x,y); LCD_data(dat); } void display_string(uint8_t x,uint8_t y,unsigned char *s) uint8_t i; i=0; while((x+i)<8) LCD_data(*s); s++; i++;
26
Smpl_GPIO_LCM8x2 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位置開始 }
27
16x16 LED Matrix Interface Signals: VCC GND P24 P23 P22 P21
28
16x16 LED Matrix schematic Sample Code : Smpl_GPIO_LED16x16
29
Smpl_GPIO_LED16x16 // 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
30
Smpl_GPIO_LED16x16 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); }
31
Smpl_GPIO_LED16x16 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); }
32
Smpl_GPIO_LED16x16 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++) { SER(( (tmp>>j) & 0x01)); SRCK(1); } } RCK(1); CLK(1); AB(1);
33
Smpl_GPIO_LED16x16 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); }
34
General Disclaimer 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);
35
General Disclaimer Personality rights 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.