蓝桥杯2024/1/26-----AT24C02笔记

本文围绕单片机工程开发展开,介绍了底层函数,包括初始化、Led、按键、数码管及IIC总线驱动程序文件的编写,还阐述了工程主函数内容,如头文件声明、变量声明、按键处理等函数,最后提及AT24C02的相关资料及使用注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每次建好工程文件夹,里边包含User(放工程文件,mian.c,可以在这里写如同我这个文章的文本文档)、Driver(存放底层文件如Led.c,Led.h等)
新建的工程先搭建框架,可以先书写底层函数(此次书写了五个函数并包含相应的头文件共十个底层文件)


底层函数内容:

1.初始化底层驱动专用文件

比如先用3个IO口控制74HC138译码器,控制Y4为低电平;当Y4为低电平时,或非门74HC02控制Y4C为高电平,使74HC573的OE端口有效,OE端口有效时,可使用P0口控制LED的亮灭。
可以去多了解74HC138译码器,74HC02或非门,74HC573八路输出透明锁存器的相关内容会更好理解
#include <Init.h>

//关闭外设
void System_Init()
{
    P0 = 0xff;
    P2 = P2 & 0x1f | 0x80;
    P2 &= 0x1f;
    P0 = 0x00;
    P2 = P2 & 0x1f | 0xa0;
    P2 &= 0x1f;
}
//头文件
#include <STC15F2K60S2.H>
void System_Init();

2.Led底层驱动专用文件

与初始化底层驱动专用文件同理,需要了解对应的锁存器控制,可以在使用的芯片数据手册查看
#include <Led.h>

void Led_Disp(unsigned char addr,enable)//LED
{
    static unsigned char temp = 0x00;
    static unsigned char temp_Old = 0xff;
    if(enable)
        temp |=0x01 << addr;
    else
        temp&= ~ (0x01 << addr);
    if(temp != temp_Old)
    {
        P0 = ~ temp;
        P2 = P2 & 0x1f | 0x80;
        P2 &= 0x1f;
        temp_Old = temp;
    }
}
void Beep(unsigned char flag)//蜂鸣器
{
    static unsigned char temp = 0x00;
    static unsigned char temp_Old = 0xff;
    if(flag)
        temp |=0x40 ;
    else
        temp &= ~ 0x40 ;
    if(temp != temp_Old)
    {
        P0 = ~ temp;
        P2 = P2 & 0x1f | 0xa0;
        P2 &= 0x1f;
        temp_Old = temp;
    }
}
void Relay(unsigned char flag)//继电器
{
    static unsigned char temp = 0x00;
    static unsigned char temp_Old = 0xff;
    if(flag)
        temp |= 0x10 ;
    else
        temp &= ~ 0x10 ;
    if(temp != temp_Old)
    {
        P0 = ~ temp;
        P2 = P2 & 0x1f | 0xa0;
        P2 &= 0x1f;
        temp_Old = temp;
    }
}

//头文件
#include <STC15F2K60S2.H>
void Led_Disp(unsigned char addr,enable);

3.按键底层驱动专用文件

(板子上的按键从按键4开始到按键19,可根据实际硬件修改)
#include <Key.h>

unsigned char Key_Read()
{
    unsigned char temp = 0;
    P44 = 0;P42 = 1; P35 = 1;P34 = 1;//这个仿真没有P4口,不适用,但是实际运行使用这个
    P37 = 0; P36 = 1; P35 = 1; P34 = 1;
    if(P33 == 0) temp = 4;
    if(P32 == 0) temp = 5;
    if(P31 == 0) temp = 6;
    if(P30 == 0) temp = 7;
    P37 = 1; P36 = 0; P35 = 1; P34 = 1;
    if(P33 == 0) temp = 8;
    if(P32 == 0) temp = 9;
    if(P31 == 0) temp = 10;
    if(P30 == 0) temp = 11;
    P37 = 1; P36 = 1; P35 = 0; P34 = 1;
    if(P33 == 0) temp = 12;
    if(P32 == 0) temp = 13;
    if(P31

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值