首先找出板子电路图,以LED为例
接下来就是电路的网标:
到对应的芯片手册里区找寄存器的描述
通过对相应寄存器的操作来操作LED灯:
首先将对应的寄存器的地址注册成一个文件:reg.h
1 2 3 4 5 #define gpiobase 0x11000000 6 7 #define GPM4CON (*(volatile unsigned long *)(gpiobase + 0x02E0)) 8 #define GPM4DAT (*(volatile unsigned long *)(gpiobase + 0x02E4)) 9 10 #define GPX3CON (*(volatile unsigned long *)(gpiobase + 0x0C60)) 11 #define GPX3DAT (*(volatile unsigned long *)(gpiobase + 0x0C64)) 12 13 #define GPD0CON (*(volatile unsigned long *)(gpiobase + 0x4000A0)) 14 #define GPD0DAT (*(volatile unsigned long *)(gpiobase + 0x4000A4))
然后是对寄存器的赋值:
1 2 3 #include "reg.h" 4 5 int(*delay)(int)=0xc3e25f90; 6 int(*print)(char*)=0xc3e114d8; 7 8 void led_off(void); 9 void led_on(void); 10 void pwm_off(void); 11 void pwm_on(void); 12 13 int test(void) 14 { 15 GPX3CON &= ~(0xf << 8);//设置12位K2为0 16 GPX3CON &= ~(0xf << 12);//设置12位K2为0 17 while(1) 18 { 19 if(GPX3DAT & (1 << 2))//如果3_2口为1,则说明按键没按 20 { 21 led_off(); 22 print("lease input "); 23 24 } 25 else 26 { 27 led_on(); 28 print("K1 put down ,LED TURN ON "); 29 } 30 if(GPX3DAT & (1 << 3))//如果3_2口为1,则说明按键没按 31 { 32 pwm_off(); 33 print("lease input "); 34 } 35 else 36 { 37 pwm_on(); 38 delay(5000); 39 print("K2 put down ,BWM: TURN ON "); 40 } 41 } 42 return 0; 43 } 44 45 void pwm_on(void) 46 { 47 GPD0CON &= ~0xffff; 48 GPD0CON |= 0x1;//配置寄存器为2 49 GPD0DAT |= 0x1;//date=0xf 50 } 51 52 void pwm_off(void) 53 { 54 GPD0CON &= ~0xffff; 55 GPD0CON |= 0x0; 56 // GPD0DAT &=0x0 ;//date=0xf 57 58 } 59 void led_off(void) 60 { 61 GPM4CON &= ~0xffff;//清零 62 GPM4CON |= 0x0000;//0---3位清零 63 GPM4DAT |= 0x0;//date=0xf关闭置一 64 } 65 66 void led_on(void) 67 { 68 GPM4CON &= ~0xffff; 69 GPM4CON |= 0x1111;//配置寄存器3-0-----3-3全为1111,全为输出模式 70 GPM4DAT &= ~0x1;//打开置0-4位为0000 71 } 72
这里写了一个while(1)循环,如果没有按键操作的时候将会一直打印“please input”,当K1按下的时候LED会两起,输出“print("K1 put down ,LED TURN ON
");”当K2按下的时候,蜂鸣器会响起,然后打印 print("K2 put down ,BWM: TURN ON
");
dnw下载到板子测试程序:
K1按下:
K2按下: