问题
单片机内存有限,若功能过多,会导致C程序过大超过内存容量。
某些子程序需要快速执行,可用汇编程序替代。
以STC89C52RC单片机为例,功能为独立数码管变换显示数字,用C语言写时:
#include //数码管0~9的段码code unsigned char ledCode[] = {0xC0, 0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98 };void Delay1S(void);//声明函数/* 主函数 */int main(){
unsigned char count = 0; P0 = 0x00;//熄灭数码管 while(1) { P0 = ledCode[count++];//修改P0口电平 Delay1S(); //汇编程序延时1秒 if(count == 10) {
count = 0; } } }/* 延时子函数 */void Delay1S(void){
unsigned char a, b, c; for(a=0 ; a<150; a++) {
for(b=0 ; b<150; b++) {
for(c=0; c<5; c++); } }}
编译后所占内存大小: