|
#include<STC15f2k60s2.H>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define t0hl 65536-1000
#define t0l_s t0hl
#define t0h_s t0hl>>8
uchar code table_duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //数码管0~9显示数字
uchar table_wei[] ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //数码管位码
uchar table[]={0,0,0,0,0,0,0,0}; //数码管显示缓冲区
uint m=0;
#define IAP_ADDRESS 0x0400 //EEPROM操作首地址
/*----------------------------
关闭IAP
----------------------------*/
void IAP_close()
{
IAP_CONTR = 0; //关闭IAP功能
IAP_CMD = 0; //清除命令寄存器
IAP_TRIG = 0; //清除触发寄存器
IAP_ADDRH = 0x80; //将地址设置到非IAP区域
IAP_ADDRL = 0;
}
/*----------------------------
从ISP/IAP/EEPROM区域读取一字节
----------------------------*/
uchar IAP_read(uint addr)
{
uchar dat; //数据缓冲区
IAP_CONTR = 0x82; //使能IAP
IAP_CMD = 1; //设置IAP命令
IAP_ADDRL = addr; //设置IAP低地址
IAP_ADDRH = addr >> 8; //设置IAP高地址
IAP_TRIG = 0x5a; //写触发命令(0x5a)
IAP_TRIG = 0xa5; //写触发命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
dat = IAP_DATA; //读ISP/IAP/EEPROM数据
IAP_close(); //关闭IAP功能
return dat; //返回
}
/*----------------------------
写一字节数据到ISP/IAP/EEPROM区域
----------------------------*/
void IAP_write(uint addr, uchar dat)
{
IAP_CONTR = 0x82; //使能IAP
IAP_CMD = 2; //设置IAP命令
IAP_ADDRL = addr; //设置IAP低地址
IAP_ADDRH = addr >> 8; //设置IAP高地址
IAP_DATA = dat; //写ISP/IAP/EEPROM数据
IAP_TRIG = 0x5a; //写触发命令(0x5a)
IAP_TRIG = 0xa5; //写触发命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
IAP_close(); //关闭IAP
}
/*----------------------------
扇区擦除
----------------------------*/
void IAP_Erase(uint addr)
{
IAP_CONTR = 0x82; //使能IAP
IAP_CMD = 3; //设置IAP命令
IAP_ADDRL = addr; //设置IAP低地址
IAP_ADDRH = addr >> 8; //设置IAP高地址
IAP_TRIG = 0x5a; //写触发命令(0x5a)
IAP_TRIG = 0xa5; //写触发命令(0xa5)
_nop_(); //等待ISP/IAP/EEPROM操作完成
IAP_close(); //关闭IAP
}
/*----------------------------
初始化定时器,对EEPROM数据进行读取和擦除
----------------------------*/
void Init_timer0()
{
TL0=t0l_s;
TH0=t0h_s;
TMOD=0x00;
ET0=1;
TR0=1;
EA=1;
}
/*----------------------------
对EEPROM数据进行读取和擦除
----------------------------*/
void EEPROM_read()
{
for(m=0;m<8;m++) //每位读
{
table[m]=IAP_read(IAP_ADDRESS+m);//读取eeprom数据
}
}
/*----------------------------
对74hc595进行数据输入
----------------------------*/
void Send_Data(uint data1) //送段位选
{
uchar i=0;
for(i=0;i<8;i++)
{
P01=0; //SRCLK初始化
P02=data1&0x0001; //SER1赋值
P01=1;; //SRCLK上升沿
data1=data1>>1;
}
}
/*----------------------------
数码管显示
----------------------------*/
void display()
{
static uchar j;
P00=0;//RCLK1初始化
Send_Data(table_duan[table[j]]);
Send_Data(table_wei[j]);
P00=1; //RCLK1 上升沿
if (++j >= 8) j=0;
}
/*----------------------------
中断执行程序
----------------------------*/
void timer0() interrupt 1
{
display();
}
/*----------------------------
主函数
----------------------------*/
void main()
{
IAP_Erase(IAP_ADDRESS); //eeprom擦除
for(m=0;m<8;m++) //每位写
IAP_write(IAP_ADDRESS+m,8-m); //eeprom写
EEPROM_read(); //读EEPROM
Init_timer0(); //初始化
while(1);
}
|
|