芯片是S3C2440
首先看原理图,led_1234分别对应引脚GPB 5678
设置引脚为输出
向寄存器相应位写入
#define GPBCON (*(volatile unsigned long *)0x56000010) //p5 6 7 8
void led_init(void)
{GPBCON &= ~(0x3 << 10);GPBCON |= (0x1 << 10);GPBCON &= ~(0x3 << 12);GPBCON |= (0x1 << 12);GPBCON &= ~(0x3 << 14);GPBCON |= (0x1 << 14);GPBCON &= ~(0x3 << 16);GPBCON |= (0x1 << 16);}
查看手册描述
大意为我们在相应引脚赋1就是高电平,0就是低电平。学过51单片机的同学应该很熟悉。只不过这里用了寄存器。
void led_off(int led_num)
{led_num += 5;GPBDAT |= (0x1 << led_num);
}led_on(int led_num)
{led_num += 5;GPBDAT &= ~(0x1 << led_num);
}
按键也是同理
#define GPBCON (*(volatile unsigned long *)0x56000010UL) //p5 6 7 8
#define GPBDAT (*(volatile unsigned long *)0x56000014UL)#define GPGCON (*(volatile unsigned long *)0x56000060UL)
#define GPGDAT (*(volatile unsigned long *)0x56000064UL)void c_deal_swi(int num)
{switch (num){case 5/* constant-expression */:/* code */break;default:break;}
}//微秒级的延时
void delay_us(unsigned int delay_us)
{volatile unsigned int num;volatile unsigned int t;for (num = 0; num < delay_us; num++){t = 11;while (t != 0){t--;}}
}void led_init(void)
{GPBCON &= ~(0x3 << 10);GPBCON |= (0x1 << 10);GPBCON &= ~(0x3 << 12);GPBCON |= (0x1 << 12);GPBCON &= ~(0x3 << 14);GPBCON |= (0x1 << 14);GPBCON &= ~(0x3 << 16);GPBCON |= (0x1 << 16);GPBDAT |= (0x1 << 5); //初始设置灯灭GPBDAT |= (0x1 << 6);GPBDAT |= (0x1 << 7);GPBDAT |= (0x1 << 8);
}void led_off(int led_num)
{led_num += 5;GPBDAT |= (0x1 << led_num);
}led_on(int led_num)
{led_num += 5;GPBDAT &= ~(0x1 << led_num);
}void led_start(void)
{int i = 0;for(i = 0; i < 4; ++i){led_on(i);delay_us(1000);}for(i = 0; i < 4; ++i){led_off(i);delay_us(1000);}}void key_init(void) //k0\k1 g5 g3
{GPGCON &= ~(0x3 << 5);GPGCON &= ~(0x1 << 5);GPGCON &= ~(0x3 << 3);GPGCON &= ~(0x1 << 3);
}int key_find(int key_num)
{if(key_num == 1){if((GPGDAT & ~(0x1 << 5)) == GPGDAT){return 1;}return 0; }else if(key_num == 2){if((GPGDAT & ~(0x1 << 3)) == GPGDAT){return 1;}return 0; }}int main(void)
{int flag;if(key_find(1)){flag = 1; }else if(key_find(2)){flag = 0; }if(flag == 1){ led_on(1); }else if(flag == 0){led_off(1); }
}