单片机小白篇1 -51单片机代码加密程序

利用AT24c02芯片进行保存数据
通过读取芯片的数据,密码为9,若从芯片读到的数据是9,那么8个小灯全亮
原理是i2c总线的知识,单片机模拟

下面是代码
learning.c
#include<reg52.h>//控制四个独立按键对at24c02进行读写
#include"i2c.h"
typedef unsigned int u16;
typedef unsigned char u8;

sbit k1=P3^1;//保存显示的数据
sbit k2=P3^0;//读取上次保存的数据
sbit k3=P3^2;//对显示的数据进行累加
sbit k4=P3^3;//清零
sbit La=P2^2;//分别表示A0,A1,A2
sbit Lb=P2^3;
sbit Lc=P2^4;
unsigned char code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};
u8 num=0,disp[4];
void delay(u16 i)
{
while(i–);
}
void Keypros()//按键处理函数
{
if(k10)
{
delay(1000);//消抖
if(k1
0)
{
At24c02Write(1,num);//1是地址
}
while(!k1);
}
if(k20)
{
delay(1000);
if(k2
0)
{
num=At24c02Read(1);
if (num9) P2=0;//补充代码,密码为9,效果为小灯全亮,代码的简易加密程序
}
while(!k2);
}
if(k3
0)
{
delay(1000);
if(k30)
{
num++;
if(num>255)num=0; //因为我们定义的num是一个字节的,所以最大也就是256
}
while(!k3);
}
if(k4
0)
{
delay(1000);
if(k4==0)
{
num=0;
}
while(!k4);
}

}
void datapros()
{
disp[0]=smgduan[num/1000];//千位
disp[1]=smgduan[num%1000/100];//百位
disp[2]=smgduan[num%1000%100/10];//十位
disp[3]=smgduan[num%1000%100%10];//个位
}
void Display()
{
int i;
for(i=0;i<4;i++)
{
switch (i) //需要用到74HC138译码器的真值表
{
case 0:La=0;Lb=0;Lc=0;break;
case 1:La=1;Lb=0;Lc=0;break;
case 2:La=0;Lb=1;Lc=0;break;
case 3:La=1;Lb=1;Lc=0;break;
}
P0=disp[3-i];
delay(100);
P0=0x00; //起到消影的作用
}
}

void main()
{
while(1)
{
Keypros();
datapros();
Display();
}
}
i2c.c
#include"i2c.h"

void Delay10us() //误差 -0.234375us,延时10us
{
unsigned char a;
for(a=3;a>0;a–);
}

void I2cStart()//起始信号函数
{
SDA=1;
//Delay10us();
SCL=1;
Delay10us();
SDA=0;
Delay10us();
SCL=0;
Delay10us();
}
void I2cStop()//终止信号
{
SDA=0;
//Delay10us();
SCL=1;
Delay10us();
SDA=1;
Delay10us();
}
unsigned char I2cSendByte(unsigned char dat)//发送字节的函数
{
unsigned char a=0;
for(a=0;a<8;a++)
{
SDA=dat>>7;//因为数据从最高位开始传送
dat=dat<<1;
Delay10us();
SCL=1;
Delay10us();//让数据保存好
SCL=0;
Delay10us();

}//发送出去一字节数据
SDA=1;
SCL=1;//让数据线和时钟线释放出来
Delay10us();
while(SDA) //非应答则会进入循环
{
	SCL=0;
	Delay10us();
	return 0;
}
SCL=0;
Delay10us();
return 1;

}
unsigned char I2cReadByte()
{
unsigned char a=0,dat=0;
SDA=1;
Delay10us();
for(a=0;a<8;a++)
{

	dat<<=1;//一定要先移位
	dat|=SDA;
	Delay10us();   //想改
	SCL=1; //先让数据稳定
	Delay10us();
	SCL=0;
	Delay10us();
}
return dat;	   

}

void At24c02Write(unsigned char addr,unsigned char dat)
{
I2cStart();//起始信号
I2cSendByte(0xa0);//有无应答在发送函数里面就有体现了
I2cSendByte(addr);
I2cSendByte(dat);
I2cStop();
}
unsigned char At24c02Read(unsigned char addr)
{
unsigned char num;
I2cStart();//起始信号
I2cSendByte(0xa0);
I2cSendByte(addr);
I2cStart();//方向变了,所以又需要起始信号
I2cSendByte(0xa1);
num=I2cReadByte();
I2cStop();
return num;
}
i2c.h
#ifndef _I2C_H //定义头文件的格式
#define _I2C_H

#include <reg52.h>

sbit SCL=P2^1;
sbit SDA=P2^0;//定义管脚
void I2cStart();
void I2cStop();
unsigned char I2cSendByte(unsigned char dat);
unsigned char I2cReadByte();
void At24c02Write(unsigned char addr,unsigned char dat);
unsigned char At24c02Read(unsigned char addr);

#endif

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值