完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/******************************************************************************
* @file main.c * @brief * This sample code supports to use GPIO to simulate key input. * @note * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved. ******************************************************************************/ #include #include "NUC122.h" #include "hid.h" #include "string.h" #include "usbd_user.h" /*--------------------------------------------------------------------------*/ extern uint8_t volatile g_u8Suspend; uint8_t volatile g_u8EP2Ready = 0; uint8_t volatile g_u8EP3Ready = 0; void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); /* Waiting for Internal RC clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); /* Enable external XTAL 12MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); /* Waiting for external XTAL clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); /* Set core clock */ CLK_SetCoreClock(48000000); /* Enable module clock */ CLK_EnableModuleClock(UART0_MODULE); CLK_EnableModuleClock(USBD_MODULE); /* Select module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV_USB(2)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set GPB multi-function pins for UART0 RXD and TXD */ SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk); SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD); } void UART0_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset IP */ SYS->IPRSTC2 |= SYS_IPRSTC2_UART0_RST_Msk; SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk; /* Configure UART0 and set UART0 Baudrate */ UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200); UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1; } void PowerDown() { printf("Enter power down ...\n"); SYS_UnlockReg(); /* Wakeup Enable */ USBD->INTEN |= USBD_INTEN_WAKEUP_EN_Msk; /* To check if all the debug messages are finished */ UART_WAIT_TX_EMPTY(UART0); CLK_PowerDown(); if(g_usbd_RemoteWakeupEn && g_u8Suspend) { /* Enable PHY before sending Resume('K') state */ USBD->ATTR |= USBD_ATTR_PHY_EN_Msk; /* Keep remote wakeup for 1 ms */ USBD->ATTR |= USBD_ATTR_RWAKEUP_Msk; CLK_SysTickDelay(1000); /* Delay 1ms */ USBD->ATTR ^= USBD_ATTR_RWAKEUP_Msk; printf("Remote Wakeup!!\n"); } printf("device wakeup!\n"); } void GPCD_IRQHandler(void) { /* To check if PD1 or PD2 interrupt occurred */ if(GPIO_GET_INT_FLAG(PD, BIT1)) { GPIO_CLR_INT_FLAG(PD, BIT1); printf("PD.1 INT occurred.\n"); } else if(GPIO_GET_INT_FLAG(PD, BIT2)) { GPIO_CLR_INT_FLAG(PD, BIT2); printf("PD.2 INT occurred.\n"); } else { /* Un-expected interrupt. Just clear all PA and PB interrupts */ PC->ISRC = PC->ISRC; PD->ISRC = PD->ISRC; printf("Un-expected interrupts.\n"); } } /*---------------------------------------------------------------------------------------------------------*/ /* Main Function */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); printf("\n"); printf("+-------------------------------------------------------+\n"); printf("| NuMicro USB HID Sample Code |\n"); printf("+-------------------------------------------------------+\n"); printf("Use GPD1 to move mouse pointer right when GPD1 = 0\n"); printf("Use GPD2 to report a key 'a' when GPD2 = 0\n"); printf("Use GPD3 to report key - Volume + when GPD3 = 0\n"); printf("Use GPD4 to report key - Volume - when GPD4 = 0\n"); /* Set GPD5 output */ GPIO_SetMode(PD, BIT5, GPIO_PMD_OUTPUT); /* Enable PD Interrupt for Remote Wakeup */ GPIO_EnableInt(PD, 1, GPIO_INT_FALLING); GPIO_EnableInt(PD, 2, GPIO_INT_FALLING); NVIC_EnableIRQ(GPCD_IRQn); USBD_User_Open(&gsUserInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_Init(); USBD_Start(); NVIC_EnableIRQ(USBD_IRQn); /* start to IN data */ g_u8EP2Ready = 1; g_u8EP3Ready = 1; while(1) { HID_UpdateKbData(); HID_UpdateMsData(); /* Enter power down when USB suspend */ if(g_u8Suspend) PowerDown(); if(memcmp(SetReport, PreSetReport, 2)) { if(SetReport[1] & 0x2) PD5 = 1; /* Turn On CapsLock LED */ else PD5 = 0; /* Turn Off CapsLock LED */ memcpy(PreSetReport, SetReport, 2); } } } /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/ |
|
相关推荐
1个回答
|
|
#include "NUC121.h"
#define LED_PIN PA0 #define BUTTON_PIN PA1 void delay(int count){ int i; for(i=0;i int main(void){ GPIO_SetMode(PA, BIT0, GPIO_MODE_OUTPUT); GPIO_SetMode(PA, BIT1, GPIO_MODE_INPUT); while(1){ if(!GPIO_GET_INPUT(PA, BIT1)){ GPIO_SET_OUTPUT_LOW(PA, BIT0); delay(1000000); GPIO_SET_OUTPUT_HIGH(PA, BIT0); }else{ GPIO_SET_OUTPUT_HIGH(PA, BIT0); } } } |
|
|
|
只有小组成员才能发言,加入小组>>
3278 浏览 9 评论
2956 浏览 16 评论
3458 浏览 1 评论
9002 浏览 16 评论
4051 浏览 18 评论
1115浏览 3评论
573浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
571浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2303浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1859浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 11:14 , Processed in 1.198158 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号