您好,歡迎進入深圳市穎特新科技有限公司官方網(wǎng)站!
/**************************************************************************//** * @file main.c * @version V3.00 * $Revision: 3 $ * $Date: 15/09/02 10:03a $ * @brief Demonstrate how to set GPIO pin mode and use pin data input/output control. * @note * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #include "stdio.h" #include "M451Series.h" #include "NuEdu-Basic01.h" #define PLL_CLOCK 72000000 void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable HIRC clock (Internal RC 22.1184MHz) */ CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk); /* Wait for HIRC clock ready */ CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk); /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1)); /* Enable HXT clock (external XTAL 12MHz) */ CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk); /* Wait for HXT clock ready */ CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk); /* Set core clock as PLL_CLOCK from PLL */ CLK_SetCoreClock(PLL_CLOCK); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Select UART module clock source as HXT and UART module clock divider as 1 */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set PD multi-function pins for UART0 RXD(PD.6) and TXD(PD.1) */ SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD6MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk); SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD6MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD); } void UART0_Init() { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset UART module */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 baud rate */ UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* Main Function */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, peripheral clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("Key1 ~ Key4 detection:\n"); Open_Seven_Segment(); Initial_Key_Input(); while(1) { if(Get_Key_Input()&0XFF) { if(Get_Key_Input()&0x01) { Show_Seven_Segment(0,1); } if(Get_Key_Input()&0x02) { Show_Seven_Segment(2,1); } if(Get_Key_Input()&0x04) { Show_Seven_Segment(4,1); } if(Get_Key_Input()&0x08) { Show_Seven_Segment(8,1); } } //Show_Seven_Segment(Get_Key_Input(), 1); } }
這是我設(shè)計的一個小程序基于M451的庫函數(shù),可以下載到板子上試一試,努力去學(xué)習(xí)思考,反復(fù)驗證,
當(dāng)SW1按上時,B1出電壓為零。
我在把庫函數(shù)貼出來
#include <stdio.h> #include "M451Series.h" #include "NuEdu-Basic01_Button.h" #define KEY1 PE2 #define KEY2 PA8 #define KEY3 PB6 #define KEY4 PB7 void Initial_Key_Input(void) { GPIO_SetMode(PE, BIT2, GPIO_MODE_INPUT);//設(shè)為輸入 GPIO_SetMode(PA, BIT8, GPIO_MODE_INPUT); GPIO_SetMode(PB, BIT6, GPIO_MODE_INPUT); GPIO_SetMode(PB, BIT7, GPIO_MODE_INPUT); } unsigned char Get_Key_Input(void) { unsigned char temp = 0; if(KEY1 == 0) temp |= 0x1; if(KEY2 == 0) temp |= 0x2; if(KEY3 == 0) temp |= 0x4; if(KEY4 == 0) temp |= 0x8; return temp; }
/**************************************************************************//** * @file NuEdu-Basic01_7_Segment.c * @version V3.00 * $Revision: 4 $ * $Date: 15/09/02 10:02a $ * @brief M451 series NuEdu Basic01 7-Segment driver source file * * @note * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. *****************************************************************************/ #include <stdio.h> #include "M451Series.h" #include "NuEdu-Basic01_7_Segment.h" #ifdef __cplusplus extern "C" { #endif /** @addtogroup M451_Device_Driver M451 Device Driver @{ */ /** @addtogroup NuEdu-Basic01_7_Segment Driver @{ */ /** @addtogroup NuEdu-Basic01_7_Segment Exported Functions @{ */ /** * @brief Configure the 7-Segment module * * @param[in] None * * @return None * * @details Configure GPIOs of 7-Segment as output mode and turn off all 7-Segement.. */ void Open_Seven_Segment(void) { GPIO_SetMode(PB, BIT11, GPIO_MODE_OUTPUT);//設(shè)為輸出 GPIO_SetMode(PB, BIT12, GPIO_MODE_OUTPUT); GPIO_SetMode(PB, BIT13, GPIO_MODE_OUTPUT); GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT); GPIO_SetMode(PB, BIT15, GPIO_MODE_OUTPUT); GPIO_SetMode(PB, BIT5, GPIO_MODE_OUTPUT); GPIO_SetMode(PD, BIT11, GPIO_MODE_OUTPUT); GPIO_SetMode(PF, BIT2, GPIO_MODE_OUTPUT); GPIO_SetMode(PD, BIT8, GPIO_MODE_OUTPUT); GPIO_SetMode(PC, BIT8, GPIO_MODE_OUTPUT); SEG_A_OFF; SEG_B_OFF; SEG_C_OFF; SEG_D_OFF; SEG_E_OFF; SEG_F_OFF; SEG_G_OFF; SEG_H_OFF; } /** * @brief Show number at 7-Segment module * * @param[in] no The number that will be showed at the specified 7-Segment module. * @param[in] number The group number of the specified 7-Segment module. * * @return None * * @details Configure GPIOs of 7-Segment to show number at 7-Segment module */ void Show_Seven_Segment(unsigned char no, unsigned char number) { SEG_A_OFF; SEG_B_OFF; SEG_C_OFF; SEG_D_OFF; SEG_E_OFF; SEG_F_OFF; SEG_G_OFF; SEG_H_OFF; SEG_CONTROL1_OFF; SEG_CONTROL2_OFF; switch(no) { //show 0 case 0: SEG_A_ON; SEG_B_ON; SEG_C_ON; SEG_D_ON; SEG_E_ON; SEG_F_ON; break; //show 1 case 1: SEG_B_ON; SEG_C_ON; break; //show 2 case 2: SEG_A_ON; SEG_B_ON; SEG_G_ON; SEG_E_ON; SEG_D_ON; break; //show 3 case 3: SEG_A_ON; SEG_B_ON; SEG_G_ON; SEG_C_ON; SEG_D_ON; break; //show 4 case 4: SEG_F_ON; SEG_B_ON; SEG_G_ON; SEG_C_ON; break; //show 5 case 5: SEG_A_ON; SEG_F_ON; SEG_G_ON; SEG_C_ON; SEG_D_ON; break; //show 6 case 6: SEG_A_ON; SEG_F_ON; SEG_E_ON; SEG_G_ON; SEG_C_ON; SEG_D_ON; break; //show 7 case 7: SEG_A_ON; SEG_B_ON; SEG_C_ON; SEG_F_ON; break; //show 8 case 8: SEG_A_ON; SEG_B_ON; SEG_C_ON; SEG_D_ON; SEG_E_ON; SEG_F_ON; SEG_G_ON; break; //show 9 case 9: SEG_A_ON; SEG_B_ON; SEG_C_ON; SEG_F_ON; SEG_G_ON; break; } switch(number) { case 1: SEG_CONTROL1_ON; break; //show 1 case 2: SEG_CONTROL2_ON; break; } } /*@}*/ /* end of group NuEdu-Basic01_7_Segment_EXPORTED_FUNCTIONS */ /*@}*/ /* end of group NuEdu-Basic01_7_Segment_Driver */ /*@}*/ /* end of group M451_Device_Driver */ #ifdef __cplusplus } #endif /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/
多看看庫函數(shù),對你的編程有很大的提高
/*---------------------------------------------------------------------------------------------------------*/ /* 7-Segment GPIO constant definitions */ /*---------------------------------------------------------------------------------------------------------*/ #define SEG_A_ON PB11=0 /*!< 7_Segment setting for turning on 7-Segment A. */ #define SEG_B_ON PB12=0 /*!< 7_Segment setting for turning on 7-Segment B. */ #define SEG_C_ON PB13=0 /*!< 7_Segment setting for turning on 7-Segment C. */ #define SEG_D_ON PB14=0 /*!< 7_Segment setting for turning on 7-Segment D. */ #define SEG_E_ON PB15=0 /*!< 7_Segment setting for turning on 7-Segment E. */ #define SEG_F_ON PB5=0 /*!< 7_Segment setting for turning on 7-Segment F. */ #define SEG_G_ON PD11=0 /*!< 7_Segment setting for turning on 7-Segment G. */ #define SEG_H_ON PF2=0 /*!< 7_Segment setting for turning on 7-Segment H. */ #define SEG_CONTROL1_ON PD8=1 /*!< 7_Segment setting for turning on 7-Segment C1. */ #define SEG_CONTROL2_ON PC8=1 /*!< 7_Segment setting for turning on 7-Segment C2. */ #define SEG_A_OFF PB11=1 /*!< 7_Segment setting for turning off 7-Segment A. */ #define SEG_B_OFF PB12=1 /*!< 7_Segment setting for turning off 7-Segment B. */ #define SEG_C_OFF PB13=1 /*!< 7_Segment setting for turning off 7-Segment C. */ #define SEG_D_OFF PB14=1 /*!< 7_Segment setting for turning off 7-Segment D. */ #define SEG_E_OFF PB15=1 /*!< 7_Segment setting for turning off 7-Segment E. */ #define SEG_F_OFF PB5=1 /*!< 7_Segment setting for turning off 7-Segment F. */ #define SEG_G_OFF PD11=1 /*!< 7_Segment setting for turning off 7-Segment G. */ #define SEG_H_OFF PF2=1 /*!< 7_Segment setting for turning off 7-Segment H. */ #define SEG_CONTROL1_OFF PD8=0 /*!< 7_Segment setting for turning off 7-Segment C1. */ #define SEG_CONTROL2_OFF PC8=0 /*!< 7_Segment setting for turning off 7-Segment C2. */
按鍵設(shè)為輸入,斷碼管設(shè)為輸出。
輕觸開關(guān)的功能分為兩種:一個是常開型、一種是常閉型。最為常見的是常開型,常開型是指線路一般為斷開,輕按按鍵柄觸動彈片和底座的炮點接觸,這時開關(guān)視為導(dǎo)通,放開按鍵柄彈片和炮點分開,這時開關(guān)視為分開,產(chǎn)品的性能為OK。線路斷開時兩路的阻值為30m歐,當(dāng)開關(guān)導(dǎo)通時兩路的接觸電阻0,這時開關(guān)性能就是非常好的。常閉型是比較少見的,因為這樣比較不符合常規(guī)的產(chǎn)品設(shè)計,常閉型是指正常情況下開關(guān)是導(dǎo)通的,當(dāng)觸動開關(guān)的時候,線路和功能都是為斷開的。這種開關(guān)的阻值就是和常開的相反,所以一般不推薦使用。下圖都是常開輕觸開關(guān):
不論是常開型還是常閉輕觸開關(guān)必須由兩個引腳以上組成,這樣才能形成一組開關(guān)功能,正常為兩腳、四腳、五腳。為什么會有五腳開關(guān)的呢?因第五只腳是用來接地的,接地腳是用來干嘛的我想大家都知道。當(dāng)然還有三個腳的,一般三腳都是側(cè)壓的比較多,這個第三只腳就是為了固定設(shè)計的,所以大家可以放心使用,使勁的去按壓都是沒有問題的。下圖中兩腳線路我們一款就一目了然了,我來解釋一下輕觸開關(guān)的四個引腳是怎么接的吧!正常情況下1和2為一組導(dǎo)通,3和4為一組導(dǎo)通,當(dāng)觸動開關(guān)的時候的1和3為一組,2和4為一組,按照這樣的方式來設(shè)計開關(guān)的功能就是對的了。
下一篇:UART簡介
掃碼關(guān)注我們
傳真:0755-82591176
郵箱:vicky@yingtexin.net
地址:深圳市龍華區(qū)民治街道民治大道973萬眾潤豐創(chuàng)業(yè)園A棟2樓A08