单片机中的模块化编程

C语言中的预编译

b94b8e33ad0046e3a83dd9d2156c99f6.png

 本次模块化编程用到的代码为上个文件中数码管显示中的代码

1.主函数为main.c

#include <REGX52.H>
#include "Delay.h"    //包含Delay头文件
#include "Nixie.h"    //包含数码管头文件

void main()
{
    while(1)
    {
        Nixie(1,1);    //在数码管的第1位置显示1
        Nixie(2,2);    //在数码管的第2位置显示2
        Nixie(3,3);    //在数码管的第3位置显示3
        Nixie(4,4);    //在数码管的第4位置显示4
        Nixie(5,5);    //在数码管的第5位置显示5
        Nixie(6,6);    //在数码管的第6位置显示6
    }
}

2.Delay.c

//延时子函数
void Delay(unsigned int xms)
{
    unsigned char i, j;
    while(xms--)
    {
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}

3.Delay.h

//对延时子函数进行定义                                                                                    #ifndef __DELAY_H__
#define __DELAY_H__

void Delay(unsigned int xms);
    
#endif

4.Nixie.c

#include <REGX52.H>
#include "Delay.h"    //包含Delay头文件

//数码管段码表
unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

//数码管显示子函数
void Nixie(unsigned char Location,Number)
{
    switch(Location)        //位码输出
    {
        case 1:P2_4=1;P2_3=1;P2_2=1;break;
        case 2:P2_4=1;P2_3=1;P2_2=0;break;
        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;                //段码清0,消影


}

5.Nieix.h

//对数码管函数进行定义                                              
#ifndef __NIXIE_H__
#define __NIXIE_H__

void Nixie(unsigned char Location,Number);
    
#endif

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值