-
- #include <stdio.h>
- #include "NUC1xx.h"
- #include "DrvGPIO.h"
- #include "DrvSYS.h"
- #include "led.h"
- int L_num=0,R_num=0;
- int flag=0;
- int i;
- char j;
- void EINT0CALLBACK(void)
- {
- DrvGPIO_SetBit(E_GPB,10);
- L_num++;
- flag=1;
- }
- void EINT1CALLBACK(void)
- {
- DrvGPIO_SetBit(E_GPB,10);
- R_num++;
- flag=2;
- }
- void Sys_Init()
- {
- UNLOCKREG();
- SYSCLK->PWRCON.XTL12M_EN = 1;//设定12M外部晶振
- DrvSYS_Delay(5000);//等待时钟就绪
- 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,10,E_IO_OUTPUT);
- DrvGPIO_ClrBit(E_GPB,10);
- DrvGPIO_EnableInt(E_GPB,14,E_IO_FALLING,E_MODE_EDGE); //设置端口GPB14为外部中断模式
- DrvGPIO_SetDebounceTime(10,E_DBCLKSRC_10K);
- DrvGPIO_EnableDebounce(E_GPB,14);
- DrvGPIO_InitFunction(E_FUNC_EXTINT0); //指定外部中断0引脚功能
- DrvGPIO_EnableEINT0(E_IO_FALLING,E_MODE_EDGE,EINT0CALLBACK);
- DrvGPIO_EnableInt(E_GPB,15,E_IO_FALLING,E_MODE_EDGE); //设置端口GPB15为外部中断模式
- DrvGPIO_SetDebounceTime(10,E_DBCLKSRC_10K);
- DrvGPIO_EnableDebounce(E_GPB,15);
- DrvGPIO_InitFunction(E_FUNC_EXTINT1); //指定外部中断1引脚功能
- DrvGPIO_EnableEINT1(E_IO_FALLING,E_MODE_EDGE,EINT1CALLBACK);
- }
- int main(void)
- {
- Sys_Init();
- while(1)
- {
- if(flag==1)
- {
- DrvGPIO_ClrBit(E_GPB,10);
- if(L_num>=10) L_num=0;
- flag=0;
- switch(L_num)
- {
- case 0:
- led_allon();
- break;
- case 1:
- led_one(1);
- break;
- case 2:
- led_one(2);
- break;
- case 3:
- led_one(3);
- break;
- case 4:
- led_one(4);
- break;
- case 5:
- led_l2r_one();
- break;
- case 6:
- led_l2r_two();
- break;
- case 7:
- led_l2r_three();
- break;
- case 8:
- led_l2r_four();
- break;
- case 9:
- led_all();
- break;
- default:break;
- }
- }
- if(flag==2)
- {
- DrvGPIO_ClrBit(E_GPB,10);
- if(R_num>=10) R_num=0;
- flag=0;
- switch(R_num)
- {
- case 0:
- led_allon();
- break;
- case 1:
- led_one(1);
- break;
- case 2:
- led_one(2);
- break;
- case 3:
- led_one(3);
- break;
- case 4:
- led_one(4);
- break;
- case 5:
- led_r2l_one();
- break;
- case 6:
- led_r2l_two();
- break;
- case 7:
- led_r2l_three();
- break;
- case 8:
- led_r2l_four();
- break;
- case 9:
- led_all();
- break;
- default:break;
- }
- }
- }
- }
-
led.h
- #ifndef _LED_H_
- #define _LED_H_
- void led_one(int num);
- void led_allon(void);
- void led_all(void);
- void led_l2r_one(void);
- void led_l2r_two(void);
- void led_l2r_three(void);
- void led_l2r_four(void);
- void led_r2l_one(void);
- void led_r2l_two(void);
- void led_r2l_three(void);
- void led_r2l_four(void);
- #endif
led.c
-
- #include <stdio.h>
- #include "NUC1xx.h"
- #include "DrvGPIO.h"
- #include "DrvSYS.h"
- #include "led.h"
- extern int flag;
- //将第num个LED点亮,其他灭
- void led_one(int num)
- {
- int i;
- DrvGPIO_ClrBit(E_GPA,num+1);
- for(i=1;i<5;i++) //其他灯灭
- {
- if(i!=num)
- {
- DrvGPIO_SetBit(E_GPA,i+1);
- }
- }
- while(1)
- {
- if(flag) break;
- }
- }
- //LED全亮
- void led_allon(void)
- {
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- }
- }
- //四个LED一起闪烁
- void led_all(void)
- {
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,2);
- DrvGPIO_ClrBit(E_GPA,3);
- DrvGPIO_ClrBit(E_GPA,4);
- DrvGPIO_ClrBit(E_GPA,5);
- DrvSYS_Delay(250000);
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- DrvSYS_Delay(250000);
- }
- }
- //从左到右依次点亮一个LED,循环执行
- void led_l2r_one(void)
- {
- int i=0;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,i+2);
- if(i==0) DrvGPIO_SetBit(E_GPA,5);
- else DrvGPIO_SetBit(E_GPA,i+1);
- DrvSYS_Delay(250000);
- i++;
- if(i==4) i=0;
- }
- }
- //从左到右依次点亮两个LED,循环执行
- void led_l2r_two(void)
- {
- int i=0;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,i+2);
- if(i==0|i==1) DrvGPIO_SetBit(E_GPA,i+4);
- else DrvGPIO_SetBit(E_GPA,i);
- DrvSYS_Delay(250000);
- i++;
- if(i==4) i=0;
- }
- }
- //从左到右依次点亮三个LED,循环执行
- void led_l2r_three(void)
- {
- int i=0;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,i+2);
- if(i==3) DrvGPIO_SetBit(E_GPA,2);
- else DrvGPIO_SetBit(E_GPA,i+3);
- DrvSYS_Delay(250000);
- i++;
- if(i==4) i=0;
- }
- }
- //从左到右依次每个LED,然后依次熄灭,循环执行
- void led_l2r_four(void)
- {
- int i=0;
- char j=0;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- if(!j) DrvGPIO_ClrBit(E_GPA,i+2);
- else DrvGPIO_SetBit(E_GPA,i+2);
- DrvSYS_Delay(250000);
- i++;
- if(i==4){i=0; j=~j;}
- }
- }
- //从右到左依次点亮一个LED,循环执行
- void led_r2l_one(void)
- {
- int i=3;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,i+2);
- if(i==3) DrvGPIO_SetBit(E_GPA,2);
- else DrvGPIO_SetBit(E_GPA,i+3);
- DrvSYS_Delay(250000);
- if(i==0) i=4;
- i--;
- }
- }
- //从右到左依次点亮两个LED,循环执行
- void led_r2l_two(void)
- {
- int i=3;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,i+2);
- if(i==3|i==2) DrvGPIO_SetBit(E_GPA,i);
- else DrvGPIO_SetBit(E_GPA,i+4);
- DrvSYS_Delay(250000);
- if(i==0) i=4;
- i--;
- }
- }
- //从右到左依次点亮三个LED,循环执行
- void led_r2l_three(void)
- {
- int i=3;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- DrvGPIO_ClrBit(E_GPA,i+2);
- if(i==0) DrvGPIO_SetBit(E_GPA,5);
- else DrvGPIO_SetBit(E_GPA,i+1);
- DrvSYS_Delay(250000);
- if(i==0) i=4;
- i--;
- }
- }
- //从右到左依次每个LED,然后依次熄灭,循环执行
- void led_r2l_four(void)
- {
- int i=3;
- char j=0;
- DrvGPIO_SetBit(E_GPA,2);
- DrvGPIO_SetBit(E_GPA,3);
- DrvGPIO_SetBit(E_GPA,4);
- DrvGPIO_SetBit(E_GPA,5);
- while(1)
- {
- if(flag) break;
- if(!j) DrvGPIO_ClrBit(E_GPA,i+2);
- else DrvGPIO_SetBit(E_GPA,i+2);
- DrvSYS_Delay(250000);
- if(i==0){i=4; j=~j;}
- i--;
- }
- }
工程: