模块化编程


5-1模块化编程

模块化编程框图

模块化编程注意事项

#include "Delay.h"

void main()
{
	Delay(10);//Delay函数的声明就在第一行那
}

C预编译

#ifndef _DELAY_H_//如果没有定义就执行编译(第一次没有定义才参与编译定义)防止多次编译
#define _DELAY_H_

void Delay(unsigned int xms);

#endif

实例操作

main.c

#include <REGX52.H>
//调用函数
#include "Delay.h"
#include "Nixie.h"

void main()
{
	while(1)
	{
		Nixie(1,1);
		Nixie(2,2);
		Nixie(3,3);
	}
}

Delay.c

void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms)
	{
		i = 2;
		j = 199;
		do
		{
			while (--j);
		} while (--i);	
	xms--;
	}
}

Delay.h

#ifndef __DELAY_H__//如果没有定义过这个函数
#define __DELAY_H__//则开始定义

void Delay(unsigned int xms);//声明

#endif

Nixie.c

#include <REGX52.H>//因为在声明时用到了P0 P2这种没有被说明的量,所以要引入这个头文件说明
#include "Delay.h"

unsigned char NixieTable[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x5E,0x6E};

void Nixie(unsigned char Location,Number)
{
	switch(Location)
	{
		
		case 1:P2_4 = 1;P2_3 = 1; P2_2 = 1;break;//111-8
		case 2:P2_4 = 1;P2_3 = 1; P2_2 = 0;break;//110-7
		case 3:P2_4 = 1;P2_3 = 0; P2_2 = 1;break;
		case 4:P2_4 = 1;P2_3 = 0; P2_2 = 0;break;
		case 5:P2_4 = 0;P2_3 = 1; P2_2 = 1;break;
		case 6:P2_4 = 0;P2_3 = 1; P2_2 = 0;break;
		case 7:P2_4 = 0;P2_3 = 0; P2_2 = 1;break;
		case 8:P2_4 = 0;P2_3 = 0; P2_2 = 0;break;
	}
	P0=NixieTable[Number];
	Delay(1);
	P0 = 0x00;
}

Nixie.h

#ifndef __NIXIE_H__
#define __NIXIE_H__

void Nixie(unsigned char Location,Number);

#endif

文章作者: WB
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 WB !
  目录