当前位置:网站首页>[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
[Blue Bridge Road -- bug free code] analysis of AT24C02 storage code
2022-06-30 05:22:00 【The journey of a bald girl is the sea of stars】
subject : Store boot times in AT24C02 in , Range 0~100, And displayed by nixie tube
main.c
#include"STC15F2K60S2.h"
#include"iic.h"
#include"intrins.h"
typedef unsigned char uchar;
typedef unsigned int uint;
uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar dsp_Val[8]={0xff,0xff,0xff,0xff,0xff,0xff};
uchar value;
void Delay300ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 13;
j = 156;
k = 83;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Timer0Init(void) //1 millisecond @11.0592MHz
{
AUXR |= 0x80; // Timer clock 1T Pattern
TMOD &= 0xF0; // Set timer mode
TL0 = 0xCD; // Set initial value of timing
TH0 = 0xD4; // Set initial value of timing
TF0 = 0; // eliminate TF0 sign
TR0 = 1; // Timer 0 Start timing
ET0=1;
EA=1;
}
void serviceTimer() interrupt 1
{
static uchar dsp_com=0;
P0=0;P2=0xc0;P2=0;
P0=dsp_Val[dsp_com];P2=0xe0;P2=0;
P0=1<<dsp_com;P2=0xc0;P2=0;
if(++dsp_com==8)dsp_com=0;
}
void Initsystem()
{
P0=0xff;P2=0x80;P2=0;
P0=0x00;P2=0xa0;P2=0;
}
void main()
{
Initsystem();
Delay300ms();
value=read_24c02(0x00);
if(value>99)
{
value=0;
}
write_24c02(0x00,++value);
Timer0Init();
while(1)
{
if(value>9)
dsp_Val[6]=dsp_code[value/10];
else
dsp_Val[6]=0xff;
dsp_Val[7]=dsp_code[value%10];
}
}iic.c
/*
Program description : IIC Bus driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform 8051,12MHz
Japan period : 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
// Bus pin definition
sbit SDA = P2^1; /* cable */
sbit SCL = P2^0; /* Clock line */
void IIC_Delay(unsigned char i)
{
do
{
_nop_();
}
while(i--);
}
// Bus start condition
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
// Bus stop condition
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Waiting for an answer
bit IIC_WaitAck(void)
{
bit ackbit;
SCL=1;
IIC_Delay(DELAY_TIME);
ackbit=SDA;
SCL=0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
// adopt I2C The bus sends data
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCL=0;
IIC_Delay(DELAY_TIME);
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
IIC_WaitAck();
}
// from I2C Data is received on the bus
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
IIC_WaitAck();
return da;
}
void IIC_SendAck(bit ackbit)
{
SCL=0;
SDA=ackbit;
IIC_Delay(DELAY_TIME);
SCL=1;
IIC_Delay(DELAY_TIME);
SCL=0;
IIC_Delay(DELAY_TIME);
SDA=1;
IIC_Delay(DELAY_TIME);
}
unsigned char read_24c02(unsigned char add)
{
unsigned char val;
IIC_Start();
IIC_SendByte(0x90);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0x91);
val=IIC_RecByte();
IIC_Stop();
return val;
}
void write_24c02(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_SendByte(add);
IIC_SendByte(dat);
IIC_Stop();
} iic.h
#ifndef _IIC_H
#define _IIC_H
// Function declaration
void IIC_Start(void);
void IIC_Stop(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
bit IIC_WaitAck(void);
unsigned char IIC_RecByte(void);
unsigned char read_24c02(unsigned char add);
write_24c02(unsigned char add,unsigned char dat);
#endif边栏推荐
- Introduction to Redux: initial experience of Redux
- Nestjs introduction and environment construction
- PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
- Gradient clip in dqn
- 2021-10-31
- Unity- the camera follows the player
- VFPBS在IIS下调用EXCEL遇到的Access is denied
- Chapter 7 vertex processing and drawing commands of OpenGL super classic (7th Edition)
- Detailed explanation of the loss module of mmdet
- [typescript] cannot redeclare block range variables
猜你喜欢

Ugui uses its own function to realize reverse mask

MinGW-w64下载文件失败the file has been downloaded incorrectly!

Super comprehensive summary | related improvement codes of orb-slam2 / orb-slam3!

【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)

Why can transformer break into the CV world and kill CNN?

中文版PyCharm改为英文版PyCharm

Unity ugui text value suspended enlarged display add text background

Records of some problems encountered during unity development (continuously updated)

Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself

Chinese pycharm changed to English pycharm
随机推荐
MinGW-w64下载文件失败the file has been downloaded incorrectly!
Super comprehensive summary | related improvement codes of orb-slam2 / orb-slam3!
网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
3D rotation album
Records of some problems encountered during unity development (continuously updated)
使用码云PublicHoliday项目判断某天是否为工作日
Configuration and use of controllers and routes in nestjs
Win10 vs2015 compiling curaengine
[notes] unity Scrollview button page turning
[note] usage model tree of the unity resource tree structure virtualizingtreeview
Redistemplate common method summary
旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)
Unity limited time use limited trial time and use times
Log writing specification
2021-06-17 solve the problem of QML borderless window stretching, window jitter and flicker when stretching and shrinking
Chapter 7 vertex processing and drawing commands of OpenGL super classic (7th Edition)
Intellj idea jars projects containing external lib to other project reference methods - jars
Unity publishing /build settings
[Motrix] download Baidu cloud files using Motrix
Unity shortcut key