这是论坛上一个大佬发出来的,我想学习单片机,但不知道这段代码讲的是什么意思,有没有大佬能看懂并且给我简单的讲解一下,谢谢大家
单片机源程序如下:
/******************************************************************************
* Function: 24c02驱动
* Description: 256个寻址单元 00~ff 32页每页8b
* Parameter:
*
*
* Return: *
******************************************************************************/
/***************************************************
记录
5.5
1.二进制书写格式010101010 前面加个0即可
*******************************************************/
#include<reg51.h>
#include"intrins.h"
typedef unsigned char uint8;
typedef char int8;
typedef unsigned int uint16;
typedef unsigned char uchar;
sbit function=P3^5; //按键信号输入口
sbit buzzer=P3^3; //蜂鸣器信号口
sbit dula=P2^6; //数码管段选信号
sbit wela=P2^7; //数码管位选信号
sbit sda=P2^0 ; //24c02数据信号
sbit scl=P2^1 ; //24c02时钟信号
uint8 code table[]={ //数字代码
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
float speed,mileage; //测得的速度 及 里程
uint8 count; //定时器0 中断计数
uint8 frontcount; //外部中断0 计数
/*****************************************
* Function: 微秒级延时函数
****************************************/
void us_delay( uint16 count )
{
while( --count )
{
_nop_();
}
}
/**************************************
* Function: 毫秒延时函数
***************************************/
void ms_delay(uint8 t)
{
uint8 x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
/**************************************
* Function: 蜂鸣器函数
***************************************/
void beep()
{
buzzer=0;
ms_delay(100);
buzzer=1;
}
/******************************************************************************
* Function: 数码管显示函数
* Description: 共三位数码管,显示速度及里程 *
******************************************************************************/
void display(uint8 shu)
{
uint8 bai,shi,ge;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
dula=1;
P0=table[bai]; //送段选数据
dula=0;
P0=0xff;
wela=1;
P0=0x7e; //送位选数据
wela=0;
ms_delay(1);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0x7d;
wela=0;
ms_delay(1);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0x7b;//原来是0xfb 第八位是高 现在要保证实验班上片选信号始终为低
wela=0;
ms_delay(1);
}
/************************************
* Function: 24c02初始化
*************************************/
void store_init()
{
sda=1;
us_delay(2);
scl=1;
us_delay(2);
}
/************************************
* Function: 启动信号
* Description: scl在高电平期间 sda地跳变
*************************************/
void store_start()
{
sda=1;
us_delay(2);
scl=1;
us_delay(2);
sda=0;
us_delay(2);
}
/************************************
* Function: 停止信号
* Description: scl在高电平期间 sda高跳变
*************************************/
void store_stop()
{
sda=0;
us_delay(2);
scl=1;
us_delay(2);
sda=1;
us_delay(2);
}
/************************************
* Function: 应答信号
*************************************/
void store_respons()
{
uchar i;
scl=1;
us_delay(2);
while((sda==1)&&(i<250))
i++;
scl=0;
us_delay(2) ;
}
/************************************
* Function: 写一个字节
*************************************/
void store_write_byte(uchar date)
{
uchar i ;
for(i=0;i<8;i++)
{
scl=0;
us_delay(2);
sda=date&0x80;
us_delay(2);
scl=1;
us_delay(2);
date<<=1;
}
scl=0; // 在scl=1 时间段 取sda的值
us_delay(2);
sda=1;
us_delay(2);
}
/************************************
* Function: 读一个字节
************************************/
uchar store_read_byte()
{
uchar date ,i;
for(i=0;i<8;i++)
{
scl=1;
us_delay(2) ;
date=(date<<1)|sda;
scl=0;
us_delay(2) ;
}
return date ;
}
/************************************
* Function: 写入指定位置 一个字节的数据
*************************************/
void store_write_add(uchar address,uchar date)
{
store_start();
store_write_byte(0xa0); //最后一位0代表 方向 写
store_respons();
store_write_byte(address); //写入的地址
store_respons();
store_write_byte(date); //写入的数据
store_respons();
store_stop();
}
/************************************
* Function: 读出指定位置 一个字节的数据
*************************************/
uchar store_read_add(uchar address)
{
uchar date;
store_start();
store_write_byte(0xa0); //最后一位0代表 方向 写
store_respons();
store_write_byte(address); //写入的地址
store_respons();
store_start();
store_write_byte(0xa1); //最后一位1代表 方向 读
store_respons();
date=store_read_byte(); //读 刚才写入的地址内 数据
store_stop();
return date;
}
/************************************
* Function: 按键信号检测程序
* Description: 1、按一次跳转到里程数 2、长按清除里程数
*************************************/
void keyscan()
{
if(function==0)
{
ms_delay(10);
if(function==0) //短按检测
{
while(!function);
ms_delay(8);
beep();
mileage=store_read_add(0x00);
display(mileage); //显示里程
if(function==0) //长按检测
{
ms_delay(50);
if(function==0)
{
while(!function);
ms_delay(8);
beep();
store_write_add(0x00,0); //清除 AT24c02上的数据
}
}
}
}
}
/************************************
* Function: 报警检测程序
* Description: 当速度大于 120KM/h时 蜂鸣器响
*************************************/
void alarm()
{
if(speed>120)
{
beep();
}
}
/************************************
* Function: 初始化函数
*************************************/
void init()
{
wela=0;
dula=0;
TMOD=0x01;
TH0=(65536-50000)/256; // 50ms计数一次
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
EX0=1; //外部中断0
IT0=1; //开启外部中断
store_init(); //AT24c02初始化
}
void main()
{
init();
while(1)
{
display(speed);
keyscan();
alarm();
}
}
/************************************
* Function: 外部中断0 函数
*************************************/
void w0() interrupt 0
{
frontcount++;
}
/************************************
* Function: 定时器0 中断函数
*************************************/
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20) //1秒钟采集一次
{
count=0;
speed= frontcount*2*3.14*0.3*3.6; //速度计算 KM/h
frontcount=0;
mileage=mileage+frontcount*2*3.14*0.3*0.001; //里程数 累加
store_write_add(0x00,mileage); //里程数 写入存储器
}
}
|