单片机成长之路(51基础篇) - 015 关于sdcc的多文件编译范例二

  本文是续 单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例(一)编写的。 

  在实际的工作中,单片机的头文件和功能函数不可能同全部放在同一个文件夹下面,我们把单片机成长之路(51基础篇) - 009 关于sdcc的多文件编译范例的代码进行重新拆分,然后把头文件归类到一个文件夹(include/)下面,便于管理。

拆分前文件目录结构:

├── delay.c
├── delay.h
├── main.c
└── makefile

拆分侯生成以下文件:

├── include      //头文件文件夹
│   ├── delay.c    //延时文件的头文件
│   ├── delay.h
│   ├── led.c     //led灯头文件
│   ├── led.h
│   └── macro.h    //宏定义头文件
├── main.bin      //编译结果
├── main.c        //主函数
└── makefile      //make文件

文件内容如下:

main.c

#include "include/delay.h"
#include "include/macro.h"
#include "include/led.h"

void main(void){
    while(1){
        led_0_7(100);
    }
}

makefile(本文件做了改动,增加个头文件路径的变量include)

# CC 编译工具声明
# stc 烧录工具位置
# 功能是编译并完成下载
CC    =sdcc
stc =sudo python /home/jikexianfeng/51-demo/.stcflash/stcflash.py
include = ./include/

all : main.bin 

download: main.bin
    $(stc) main.bin

#main.bin : main.hex
#    ~/51-demo/.Hex2bin-2.5/hex2bin main.hex

main.bin : main.hex
    objcopy -I ihex -O binary main.hex main.bin
    
main.hex : main.ihx
    packihx main.ihx > main.hex

main.ihx : main.rel delay.rel led.rel
    $(CC) main.rel led.rel delay.rel

main.rel : main.c $(include)delay.c $(include)macro.h
    $(CC) -c main.c
    
led.rel : $(include)led.c $(include)delay.c $(include)macro.h
    $(CC) -c $(include)led.c
    
delay.rel : $(include)delay.c $(include)macro.h
    $(CC) -c $(include)delay.c

cleanall:
    rm -rf *.lk *.bin *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map
clean:
    rm -rf *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map *.lk

include/macro.h

1 #ifndef MACRO_H
2 #define MACRO_H
3 
4 #include "8051.h"
5 
6 #define uint unsigned int 
7 #define uchar unsigned char
8 
9 #endif

include/macro.h

1 #ifndef LED_H
2 #define LED_H
3 #include"macro.h"
4 
5 void led_0_7(uint xms);
6 
7 #endif

include/led.h

#include "delay.h"
#include "macro.h"

uchar tab[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

void led_0_7(uint xms){
    uchar i;
    for(i=0;i<8;i++){
        P1 = tab[i];
        Delay(xms);
    }
}

include/delay.c

1 #include "delay.h"
2 #include"macro.h"
3 
4 void Delay(uint xms){
5         uint i,j;
6         for(i=xms;i>0;i--)
7         for(j=110;j>0;j--);
8 }

include/delay.h

1 #ifndef DELAY_H
2 #define DELAY_H
3 
4 #include"macro.h"
5 
6 void Delay(uint xms);
7 
8 #endif

 

posted @ 2018-09-06 00:14  极客先锋  阅读(1656)  评论(0编辑  收藏  举报