嵌入式入门小工程
此代码基于s3c2440
1.点灯
//led.c
void init_led(void)
{unsigned int t;t = GPBCON;t &= ~((3 << 10) | (3 << 12) | (3 << 14) | (3 << 16));t |= (1 << 10) | (1 << 12) | (1 << 14) | (1 << 16);GPBCON = t;
}void led_on(unsigned char n)
{unsigned int t;t = GPBDAT;t &= ~(0x0F << 5);n &= 0x0F;n ^= 0x0F;t |= (n << 5);GPBDAT = t;
}
int main(void)
{ init_wdt(); //初始化看门狗init_led(); //初始化led灯led_on(0);led_on(1);//led1点亮
}
2.走马灯
并初始化蜂鸣器,灯闪一次响一次,去时响,来时不响。
//初始化蜂鸣器
void init_beep(void)
{unsigned int t;t = GPBCON;t &= ~(3 << 0);t |= (1 << 0);GPBCON = t;
}
//打开蜂鸣器
void beep_on(void)
{GPBDAT |= (1 << 0);
}
//关闭蜂鸣器
void beep_off(void)
{GPBDAT &= ~(1 << 0);
}
3.按下按键1,led1亮。以此类推
void init_key(void)
{GPGCON &= ~((3 << 0) | (3 << 6) | (3 << 10));
}int key_pressed(void)
{if((GPGDAT & 0x01) == 0){return 1;}else if((GPGDAT & 0x08) == 0)// 1000{return 2;}else if((GPGDAT & 0x20) == 0) //10 0000{return 3;}return 0;
}
int main(void)
{unsigned char i;int n = 0; int key;init_key();init_beep();init_wdt();init_led();led_on(0);while(1){init_beep();key = key_pressed();led_on(key);}
}
4.利用中断控制,按下按键1led1亮,按下按键2蜂鸣器响,按下按键3蜂鸣器关。
void init_wdt(void)
{WTCON &= ~(1 << 5);
}
void irq_handler(void)
{if (INTOFFSET == 5) // 检查外部中断号{if ((EINTPEND & (1 << 8)) != 0) // 按键一{led_on(1); // 点亮LED 1}else if ((EINTPEND & (1 << 11)) != 0) // 按键二{beep_on(); // 打开蜂鸣器}else if ((EINTPEND & (1 << 13)) != 0) // 按键三{beep_off(); // 关闭蜂鸣器}EINTPEND = EINTPEND;}SRCPND = SRCPND; // 清除源中断INTPND = INTPND; // 清除中断状态
}void delay(unsigned int n)
{while (n--);
}void init_key(void)
{unsigned int t;INTMOD &= ~(1 << 5); // 设置为外部中断模式INTMSK &= ~(1 << 5); // 允许外部中断5// 配置GPG引脚为外部中断输入t = GPGCON;t &= ~((3 << 0)|(3 << 6)|(3 << 10));t |= ((2 << 0)|(2 << 6)|(2 << 10)); GPGCON = t;// 配置EXTINT1t = EXTINT1;t &= ~(0x0FFFFFF); // 清除配置t |= ((1 << 3)|(1 << 15)|(1 << 23)); // GPG3上升沿触发t |= ((2 << 0)|(2 << 12)|(2 << 20)); // GPG0上升沿触发EXTINT1 = t;// 解除中断屏蔽EINTMASK &= ~((1 << 8)|(1 << 11)|(1 << 13)); // 解除中断8屏蔽}int main(void)
{ init_wdt(); //初始化看门狗init_led(); //初始化led灯init_beep();init_key();led_on(0);while (1){delay(0xFFFF); // 主循环延迟}
}