Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/25/2017 Richard Kuo Assistant Professor

Similar presentations


Presentation on theme: "3/25/2017 Richard Kuo Assistant Professor"— Presentation transcript:

1 3/25/2017 Richard Kuo Assistant Professor
NuMicro GPIO 3/25/2017 Richard Kuo Assistant Professor

2 Outline 3.NuMicro_GPIO.ppt Project : Two-wheel robot car
GPIO Interface Introduction Lab. LED control (GPIO_LED) Lab. Sensor input (GPIO_PIR) Lab. Buzzer (GPIO_Buzzer) Lab. 7seg & Keypad (only for Nu-LB-NUC140) Lab. GPIO Interrupt (GPIO_IRQ) Lab. External Interrupt (GPIO_EXTINT) Lab. Step Motor control (GPIO_Stepmotor) Lab. DC Motor control (GPIO_L298N) Project : Two-wheel robot car Project : LED Cube 3x3x3 Project: Electronic Safe

3 Peripherals To use GPIO to access the following components/modules
LEDs Buzzer IR PIR Relays Step Motor Keypad

4 GPIO I/O Modes : Input Input only with high impedance
Set GPIOx_PMD (PMDn[1:0]) to 00b the GPIOx port [n] pin is in Input mode and the I/O pin is in tri-state (high impedance) without output drive capability GPIO_SetMode(PC, BIT12, GPIO_MODE_INPUT);

5 GPIO I/O Modes - Output Push-Pull Output
GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT);

6 GPIO I/O Modes – Open_Drain
GPIO_SetMode(PC, BIT12, GPIO_MODE_OPEN_DRAIN);

7 GPIO I/O Modes Quasi bi-direction
GPIO_SetMode(PC, BIT12, GPIO_MODE_QUASI);

8 StdDriver/GPIO_OutputInput
GPIO set mode : GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT); I/O groups : NUC140 = PA, PB, PC, PD, PE NANO102 = PA, PB, PC, PD, PE, PF M051 = P0, P1, P2, P3, P4, P5 Mini58 = P0, P1, P2, P3, P4, P5 port no = BIT0~7 / 15 I/O modes GPIO_MODE_INPUT : input only mode GPIO_MODE_OUTPUT : push-pull output mode GPIO_MODE_OPEN_DRAIN : open-drain output mode GPIO_MODE_QUASI : quai bi-direction mode Output value : PC12 = 0; Read Input : if (PC12 !=0) { }

9 LED circuit

10 GPIO pin to drive LED PC12 Each IO pad drive/sink ~25mA
GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT); Each IO pad drive/sink ~25mA Entire Chip drive/sink ~200mA

11 GPIO_LED Initialize Clocks & Pins Output Mode int main(void) {
SYS_Init(); GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT); while(1) { PC12 =0; // turn on LED CLK_SysTickDelay(100000); // Delay PC12 =1; // turn off LED } Initialize Clocks & Pins Output Mode

12 PIR (Passive InfraRed) sensor

13 Reflective IR Sensor : TCRT5000
Application Circuit Picture 220 Vcc TCRT5000 GPIO input PA0 R1 R2 10K Gnd IR Diode keep emitting Infrared light IR Transistor receive the reflective Infrared light Top View

14 GPIO_PIR display Detection message Passive Infrared Detector
detecting human body

15 GPIO_PIR int main(void) { SYS_Init(); GPIO_SetMode(PA, BIT0, GPIO_MODE_INPUT); while(1) { if (PA0==1) printf("PIR detected!\n"); else printf("PIR no detection!\n"); } Input Mode

16 Buzzer schematic (Nu-LB-NUC140)
0 ohm R1 is shorted

17 GPIO_Buzzer void Init_Buzzer(void) { GPIO_SetMode(PB, BIT11, GPIO_PMD_OUTPUT); PB11=1; // turn off Buzzer } void Buzz(int number) int i; for (i=0; i<number; i++) { PB11 =0; // turn on Buzzer CLK_SysTickDelay(100000); // Delay PB11 =1; // turn off Buzzer

18 7-segment LED

19 7-segment LED – component diagram

20 7-segment LED – application circuit

21 Exercise : 7-segment LED
PE0 PE1 PE2 PE3 PE4 PE5 PE6 PE7 PC4 Nu-LB-NUC140

22 Seven_Segment.c #include <stdio.h> #include "NUC100Series.h"
#include "GPIO.h" #include "SYS.h" #include "Seven_Segment.h" #define SEG_N0 0x82 #define SEG_N1 0xEE #define SEG_N2 0x07 #define SEG_N3 0x46 #define SEG_N4 0x6A #define SEG_N5 0x52 #define SEG_N6 0x12 #define SEG_N7 0xE6 #define SEG_N8 0x02 #define SEG_N9 0x62 #define SEG_N10 0x22 #define SEG_N11 0x1A #define SEG_N12 0x93 #define SEG_N13 0x0E #define SEG_N14 0x13 #define SEG_N15 0x33 unsigned char SEG_BUF[16]={SEG_N0, SEG_N1, SEG_N2, SEG_N3, SEG_N4, SEG_N5, SEG_N6, SEG_N7, SEG_N8, SEG_N9, SEG_N10, SEG_N11, SEG_N12, SEG_N13, SEG_N14, SEG_N15}; void OpenSevenSegment(void) { GPIO_SetMode(PC, BIT4, GPIO_PMD_OUTPUT); GPIO_SetMode(PC, BIT5, GPIO_PMD_OUTPUT); GPIO_SetMode(PC, BIT6, GPIO_PMD_OUTPUT); GPIO_SetMode(PC, BIT7, GPIO_PMD_OUTPUT); PC4=0; PC5=0; PC6=0; PC7=0; GPIO_SetMode(PE, BIT0, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT1, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT2, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT3, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT4, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT5, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT6, GPIO_PMD_OUTPUT); GPIO_SetMode(PE, BIT7, GPIO_PMD_OUTPUT); PE0=0; PE1=0; PE2=0; PE3=0; PE4=0; PE5=0; PE6=0; PE7=0; }

23 Seven_Segment.c switch(i) {
void ShowSevenSegment(unsigned char no, unsigned char number) { unsigned char temp,i; temp=SEG_BUF[number]; for(i=0;i<8;i++) { if((temp&0x01)==0x01) switch(i) { case 0: PE0=1; break; case 1: PE1=1; break; case 2: PE2=1; break; case 3: PE3=1; break; case 4: PE4=1; break; case 5: PE5=1; break; case 6: PE6=1; break; case 7: PE7=1; break; } else switch(i) { case 0: PE0=0; break; case 1: PE1=0; break; case 2: PE2=0; break; case 3: PE3=0; break; case 4: PE4=0; break; case 5: PE5=0; break; case 6: PE6=0; break; case 7: PE7=0; break; } temp=temp>>1; switch(no) { case 0: PC4=1; break; case 1: PC5=1; break; case 2: PC6=1; break; case 3: PC7=1; break;

24 Seven_Segment.c void CloseSevenSegment(void) { PC4=0; PC5=0; PC6=0;
}

25 4x4 Keypad

26 3x3 Keypad on Learning board
1 2 3 4 5 6 7 8 9

27 3x3 Keypad schematic (Nu-LB-NUC140)

28 Keypad GPIO setting GPIO pin mode : Input
GPIO_SetMode(PA, BIT0, GPIO_MODE_QUASI); Key press will connect output pin to input pin Quasi output 1 : pin is floating Quasi output 0 : pin drive to Low Keypad scanning Set input pins PA3,4,5 to 1 Set output pins to 0 one at a time PA0,1,2 PA3=1; PA4=1; PA5=1; PA0=1; PA1=1; PA2=0; if (PA3==0) return 1; if (PA4==0) return 4; if (PA5==0) return 7;

29 Scankey.c void OpenKeyPad(void) {
GPIO_SetMode(PA, BIT0, GPIO_MODE_QUASI); GPIO_SetMode(PA, BIT1, GPIO_MODE_QUASI); GPIO_SetMode(PA, BIT2, GPIO_MODE_QUASI); GPIO_SetMode(PA, BIT3, GPIO_MODE_QUASI); GPIO_SetMode(PA, BIT4, GPIO_MODE_QUASI); GPIO_SetMode(PA, BIT5, GPIO_MODE_QUASI); } uint8_t ScanKey(void) { PA0=1; PA1=1; PA2=0; PA3=1; PA4=1; PA5=1; if (PA3==0) return 1; if (PA4==0) return 4; if (PA5==0) return 7; PA0=1; PA1=0; PA2=1; PA3=1; PA4=1; PA5=1; if (PA3==0) return 2; if (PA4==0) return 5; if (PA5==0) return 8; PA0=0; PA1=1; PA2=1; PA3=1; PA4=1; PA5=1; if (PA3==0) return 3; if (PA4==0) return 6; if (PA5==0) return 9; return 0; }

30 GPIO_7seg_Keypad Scankey.c : function calls for scanning keypad 3x3
7-segment display number = 5 Press middle key of 3x3 keypad Scankey.c : function calls for scanning keypad 3x3

31 GPIO_Keypad Press key Vcc/Gnd GPIOs control 4-port Relay

32 GPIO_Keypad // PA0,1,2,3,4,5 connected to 3x3 Keypad int main(void)
// PC12,13,14,15 connected to LEDs (or Relays) #include <stdio.h> #include "NUC100Series.h" #include "MCU_init.h" #include "SYS_init.h" #include "Scankey.h“ void Init_GPIO(void) { GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT); GPIO_SetMode(PC, BIT13, GPIO_MODE_OUTPUT); GPIO_SetMode(PC, BIT14, GPIO_MODE_OUTPUT); GPIO_SetMode(PC, BIT15, GPIO_MODE_OUTPUT); PC12=1; PC13=1; PC14=1; PC15=1; } int main(void) { uint32_t i =0; SYS_Init(); OpenKeyPad(); Init_GPIO(); while(1) { i=ScanKey(); switch(i) { case 1 : PC12=0; break; case 2 : PC13=0; break; case 3 : PC14=0; break; case 4 : PC15=0; break; default: PC12=1; PC13=1; PC14=1; PC15=1; break; }

33 GPIO Interrupt pin circuit
Pullup resistor MCU

34 GPIO Interrupt pin setting
GPIO pin mode : Input GPIO_SetMode(PB, BIT12, GPIO_MODE_INPUT); GPIO pin chip internal Pull-up GPIO_ENABLE_PULL_UP(PB, BIT12); GPIO interrupt trigger type : RISING/FALLING/BOTH_EDGE/HIGH/LOW GPIO_EnableInt(PB, BIT12, GPIO_INT_FALLING); GPIO Interrupts : two groups = ABC or DEF NVIC_EnableIRQ(GPABC_IRQn); // ABC group share 1 interrupt to CPU NVIC_EnableIRQ(GPDEF_IRQn); // DEF group share 1 interrupt to CPU GPIO pin debouncing Debounce Clock Source = LIRC (10KHz) or HCLK GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC, GPIO_DBCLKSEL_64); GPIO_ENABLE_DEBOUNCE(PB, BIT12);

35 Initialize GPIO interrupt pins
// KEY1 to PB12, KEY2 to PB13, KEY3 to PB14 void Init_KEY(void) { GPIO_SetMode(PB, (BIT12 | BIT13 | BIT14), GPIO_MODE_INPUT); GPIO_ENABLE_PULL_UP(PB, (BIT12 | BIT13 | BIT14)); GPIO_EnableInt(PB, 12, GPIO_INT_FALLING); GPIO_EnableInt(PB, 13, GPIO_INT_FALLING); GPIO_EnableInt(PB, 14, GPIO_INT_FALLING); NVIC_EnableIRQ(GPAB_IRQn); GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC, GPIO_DBCLKSEL_64); GPIO_ENABLE_DEBOUNCE(PB, (BIT12 | BIT13 | BIT14)); } GPAB_IRQn for NUC100 series GPABC_IRQn for Nano100 series

36 GPIO_IRQ int32_t main() { SYS_Init(); Init_KEY();
printf("Testing KEY1/2/3 IRQ generation:\n"); while(1) { if (KEY1_Flag) { printf("KEY1/PB12 Interrupt!\n"); KEY1_Flag=0; } if (KEY2_Flag) { printf("KEY2/PB13 Interrupt!\n"); KEY2_Flag=0; } if (KEY3_Flag) { printf("KEY3/PB14 Interrupt!\n"); KEY3_Flag=0; } }

37 GP01_IRQHandler (Mini58Series)
void GPIO01_IRQHandler(void) { /* To check if P1.5 interrupt occurred */ if (P1->INTSRC & BIT5) { P1->INTSRC = BIT5; printf("P1.5 INT occurred. \n"); } else { /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */ P0->INTSRC = P0->INTSRC; P1->INTSRC = P1->INTSRC; printf("Un-expected interrupts. \n"); }

38 External Interrupt Pins (INT0 & INT1)
Two Pins dedicated for External Interrupt (higher interrupt priority then GPIO interrupt)

39 GPIO_EXTINT void EINT0_IRQHandler(void) {
GPIO_CLR_INT_FLAG(PB, BIT14); // Clear GPIO interrupt flag printf("PB14 EINT0 occurred.\n"); } void EINT1_IRQHandler(void) GPIO_CLR_INT_FLAG(PB, BIT15); // Clear GPIO interrupt flag printf("PB15 EINT1 occurred.\n");

40 GPIO_EXTINT void Init_EXTINT(void) {
// Configure EINT0 pin and enable interrupt by falling edge trigger GPIO_SetMode(PB, BIT14, GPIO_MODE_INPUT); GPIO_EnableEINT0(PB, 14, GPIO_INT_FALLING); NVIC_EnableIRQ(EINT0_IRQn); // Configure EINT1 pin and enable interrupt by rising and falling edge trigger GPIO_SetMode(PB, BIT15, GPIO_MODE_INPUT); GPIO_EnableEINT1(PB, 15, GPIO_INT_RISING); // RISING, FALLING, BOTH_EDGE, HIGH, LOW NVIC_EnableIRQ(EINT1_IRQn); // Enable interrupt de-bounce function and select de-bounce sampling cycle time GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC, GPIO_DBCLKSEL_64); GPIO_ENABLE_DEBOUNCE(PB, BIT14); GPIO_ENABLE_DEBOUNCE(PB, BIT15); }

41 GPIO_EXTINT int32_t main() { SYS_Init(); Init_EXTINT(); while(1); } Note: Open Debug Session to run in Keil MDK to see printf message !

42 Stepping Motor Operation principle of stepper motor

43 Step Motor Driving Methods
Four phase Step Motor : 4 phases are A, /A, B, /B Diving Method as left Wave Drive: single phase full step Full Step Drive : two phase full step Half Stepping : one/two phase half step Microstepping

44 5V Step Motor 28YBJ-48 步進電機 28YBJ-48 ULN2003 驅動IC

45 GPIO_Stepmotor 5V Step Motor Driver IC : TI ULN2003A Connections
Motor A+ connected to INA of ULN2003A, controlled by NUC140 GPA3 Motor A- connected to INB of ULN2003A, controlled by NUC140 GPA2 Motor B+ connected to INC of ULN2003A, controlled by NUC140 GPA1 Motor B- connected to IND of ULN2003A, controlled by NUC140 GPA0 unsigned char CCW[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //counter-clockwise sequence unsigned char CW[8] ={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08}; //clockwise sequence

46 Stepping Control Port Step InA 1 InB InC InD Port Step InA 1 InB InC
Clockwise : 0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08 Counter clockwise : 0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09 Port Step InA 1 InB InC InD Port Step InA 1 InB InC InD

47 GPIO_Stepmotor

48 System Diagram : Two-Wheel DC Motors
微控制器 直流馬達 MCU 直流馬達 GPIOs 全橋直流馬達 驅動板

49 DC Motor Driver : L298N 直流馬達驅動板

50 H-Bridge Motor Driver Circuit
Two GPIO pins output to control current direction thru a DC motor IN1=1 & IN2=0 IN1 IN2 IN1=1 & IN2=0 IN1=0 & IN2=1 M Vcc IN1=0 & IN2=1

51 GPIO_L298N #include <stdio.h> #include "NUC100Series.h"
#include "MCU_init.h" #include "SYS_init.h" void Init_GPIO(void) { GPIO_SetMode(PA, BIT0, GPIO_MODE_OUTPUT); GPIO_SetMode(PA, BIT1, GPIO_MODE_OUTPUT); GPIO_SetMode(PA, BIT2, GPIO_MODE_OUTPUT); GPIO_SetMode(PA, BIT3, GPIO_MODE_OUTPUT); PA0=0; PA1=0; PA2=0; PA3=0; } void Forward() { PA0=1; PA1=0; PA2=1; PA3=0; } void Backward() { PA0=0; PA1=1; PA2=0; PA3=1; } void Right() { PA0=1; PA1=0; PA2=0; PA3=1; } void Left() { PA0=0; PA1=1; PA2=1; PA3=0; } void Stop() { PA0=0; PA1=0; PA2=0; PA3=0; } int main(void) { SYS_Init(); Init_GPIO(); Forward(); CLK_SysTickDelay(500000); // Delay Backward(); Left(); Right(); CLK_SysTickDelay(500000); //Delay Stop(); }

52 Project : Two-Wheel Robotic Car

53 Project : LED Cube 3x3x3 Ex.Video :

54 LED Cube 3x3x3 schematic 1 1 1 2 2 2 3 3 3 PD0 PD1 PD2 PD3 PD4 PD5 PD6
PA0 PA1 PA2

55 Project : LED Cube 8x8x8 Ex. Video:

56 Project : Electronic Safe
Keypad input passcode, GPIO output to control magnet valve Reference : Samsung Door Lock

57 Electronic Safe : Interface Circuit
Vcc +6V (1.5V AA x4) Gnd Ground GPB15 GPA11 2N2222

58 Electronic Safe : Interface Circuit
GPA12 +6V GPA13 GPA14 GPA15 2N2222 Gnd

59 Important Notice ! This educational material are neither intended nor warranted for usage in systems or equipment, any malfunction or failure of which may cause loss of human life, bodily injury or severe property damage. Such applications are deemed, “Insecure Usage”. Insecure usage includes, but is not limited to: equipment for surgical implementation, atomic energy control instruments, airplane or spaceship instruments, the control or operation of dynamic, brake or safety systems designed for vehicular use, traffic signal instruments, all types of safety devices, and other applications intended to support or sustain life. All Insecure Usage shall be made at user’s own risk, and in the event that third parties lay claims to the author as a result of customer’s Insecure Usage, the user shall indemnify the damages and liabilities thus incurred by using this material. Please note that all lecture and sample codes are subject to change without notice. All the trademarks of products and companies mentioned in this material belong to their respective owners.


Download ppt "3/25/2017 Richard Kuo Assistant Professor"

Similar presentations


Ads by Google