本帖最后由 tendence 于 2011-11-18 19:48 编辑
再次感谢lixiaoxu2meng的帮助,其实这个程序真的是我写的,只是写好之后发现跟lixiaoxu2meng的key程序很像
该程序总共使用key1,key2两个键,key1使led亮灯数目增加,key2减少
直接上程序
includes.h-
- #include <stdio.h>
- #include "NUC1xx.h"
- #include "variables.h"
- #include "hw_config.h"
- #include "Driver\DrvGPIO.h"
- #include "Driver\DrvSYS.h"
hw_config.c- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 系统
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void Set_System(void)
- {
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
- }
- /*************************************************************************************
- ** Function name: RCC_Configuration
- ** Descriptions: 系统时钟源设置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void RCC_Configuration(void)
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->WRCON.XTL12M_EN = 1; 等同
- // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
- // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
- // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
- delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: 端口配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void GPIO_Configuration()
- {
- DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
- DrvGPIO_Open( E_GPB, 16, E_IO_INPUT );
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void delay_ms(uint32_t count)
- {
- uint32_t i,j;
- for(i=count;i>0;i--)
- for(j=2395;j>0;j--);
- }
main.c- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: 4个LED交替点亮
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- uint8_t lednum,flag_key1,flag_key2; //计数亮灯的数目
- Set_System();
- lednum=0;
- while(1)
- {
- while(!DrvGPIO_GetBit(E_GPB,15))
- {
- delay_ms(30);
- if(!DrvGPIO_GetBit(E_GPB,15))
- {
- flag_key1=1;
- while(!DrvGPIO_GetBit(E_GPB,15)) ;
- }
- }
- if(!DrvGPIO_GetBit(E_GPB,14))
- {
- delay_ms(30);
- if(!DrvGPIO_GetBit(E_GPB,14))
- {
- flag_key2=1;
- while(!DrvGPIO_GetBit(E_GPB,14)) ;
- }
- }
- if(flag_key1)
- {
- flag_key1=0;
- lednum++;
- if(lednum==5) lednum=0;
- }
- if(flag_key2)
- {
- flag_key2=0;
- if(lednum==0) lednum=5;
- lednum--;
- }
- switch(lednum)
- {
- case 0:
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- break;
- case 1:
- DrvGPIO_ClrBit(E_GPA,2);
- break;
- case 2:
- DrvGPIO_ClrBit(E_GPA,3);
- break;
- case 3:
- DrvGPIO_ClrBit(E_GPA,4);
- break;
- case 4:
- DrvGPIO_ClrBit(E_GPA,5);
- break;
- }
- }
- }
|