|
本帖最后由 dagu 于 2014-12-27 19:48 編輯
STC單片機其中的一個大特點就是片內(nèi)可觀的存儲空間,雖然大部分資深的單片機"老鳥"對于宏鑫公司將Flash描述成EEPROM頗為不滿,對我來說能夠利用好這部分空間就是勝利...
前面已經(jīng)介紹了我手頭上的芯片型號為STC89C52RC,其中的52就是表示它的內(nèi)部Flash程序存儲容量為8K,EEPROM為2K. 不說廢話貼程序.........
/*********************************************************
;----P1.7---P1.6---P1.5---P1.4-------
;----03H----02H----01H----00H----P1.0
;----07H----06H----05H----04H----P1.1
;----0BH----0AH----09H----08H----P1.2
;----0FH----0EH----0DH----0CH----P1.3
先將鍵盤的值存入EEPROM然后再去讀取
**********************************************************/
#include <reg51.h>
#include <stdio.h>
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sfr isp_data =0xe2;
sfr isp_addrh =0xe3;
sfr isp_addrl =0xe4;
sfr isp_cmd =0xe5;
sfr isp_trig =0xe6;
sfr isp_contr =0xe7;
unsigned char eeprom_read(unsigned int addres); //讀EEPROM
void eeprom_write(unsigned int address,unsigned char write_data);//寫EEPROM.
void eeprom_eares(unsigned int addres); //扇區(qū)擦除。
void KeyScan();
void delay10ms(unsigned char time);
void Dispaly(unsigned char k);
void Send(unsigned char s);
unsigned char key,temp; //全局變量
unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90, //0~9
0x88,0x83,0xA7,0xA1,0x86,0x8E}; //A~F 數(shù)碼管顯示數(shù)組
//防抖動延時程序
void delay10ms(unsigned char time)
{
unsigned char a,b,c;
for(a=0;a<time;a++)
for(b=0;b<10;b++)
for(c=0;c<120;c++)
;
}
void Dispaly(unsigned char k) //顯示程序
{
P0=table[k];
P2=0x00;
}
/**********************************
eeprom擦除
***********************************/
void eeprom_eares(unsigned int addres)//扇區(qū)擦除。
{
unsigned i;
isp_addrl=addres; //低位地址
isp_addrh=addres>>8; //高位地址
isp_contr=0x01;
isp_contr=isp_contr|0x80; //設(shè)時間與充ISP操作。
isp_cmd=0x03; //扇區(qū)命命令
isp_trig=0x46; //觸發(fā)
isp_trig=0xb9; //觸發(fā)啟動。
for(i=0;i<3;i++);
isp_addrl=0xff;
isp_addrh=0xff;
isp_contr=0x00;
isp_cmd=0x00;
isp_trig=0x00;
}
/******************************************
EEPROM寫
*******************************************/
void eeprom_write(unsigned int addres,unsigned char write_data)
{
unsigned char i;
isp_data=write_data; //要寫入的數(shù)據(jù)。
isp_addrl=addres; //低位地址
isp_addrh=addres>>8; //高位地址
isp_contr=0x01;
isp_contr=isp_contr|0x80; //設(shè)時間與充ISP操作。
isp_cmd=0x02; //寫命令
isp_trig=0x46; //觸發(fā)
isp_trig=0xb9; //觸發(fā)啟動。
for(i=0;i<3;i++);
isp_addrl=0xff;
isp_addrh=0xff;
isp_contr=0x00;
isp_cmd=0x00;
isp_trig=0x00;
}
/**********************************************
EEPROM讀
***********************************************/
unsigned char eeprom_read(unsigned int addres)
{
unsigned char i,z;
isp_addrl=addres;
isp_addrh=addres>>8;
isp_contr=0x01;
isp_contr=isp_contr|0x80;
isp_cmd=0x01;
isp_trig=0x46;
isp_trig=0xb9;
for(i=0;i<3;i++);
isp_addrl=0xff;
isp_addrh=0xff;
isp_contr=0x00;
isp_cmd=0x00;
isp_trig=0x00;
z=isp_data;
return(z);
}
/*****************
主程序
*****************/
void main() //主程序
{
while(1)
{
KeyScan();
}
}
void KeyScan() //按鍵掃描子程序
{
P1=0xFF;
P1_3=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
delay10ms(1);
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
temp=P1;
temp&=0xF0;
switch(temp)
{
case 0x70:
key=15;
eeprom_eares(0x2200);
eeprom_write(0x2200,key);break;
case 0xB0:
key=14;
eeprom_eares(0x2200);
eeprom_write(0x2200,key);break;
case 0xD0:
key=13;
eeprom_eares(0x2200);
eeprom_write(0x2200,key);break;
case 0xe0:
key=eeprom_read(0x2200);break;
}
Dispaly(key);
}
}
P1=0xFF;
P1_2=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
delay10ms(1);
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
temp=P1;
temp&=0xF0;
switch(temp)
{
case 0x70:
key=11;break;
case 0xB0:
key=10;break;
case 0xD0:
key=9;break;
case 0xe0:
key=8;break;
}
eeprom_eares(0x2200);
eeprom_write(0x2200,key);
Dispaly(key);
}
}
P1=0xFF;
P1_1=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
delay10ms(1);
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
temp=P1;
temp&=0xF0;
switch(temp)
{
case 0x70:
key=7;break;
case 0xB0:
key=6;break;
case 0xD0:
key=5;break;
case 0xe0:
key=4;break;
}
eeprom_eares(0x2200);
eeprom_write(0x2200,key);
Dispaly(key);
}
}
P1=0xFF;
P1_0=0;
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
delay10ms(1);
temp=P1;
temp&=0xF0;
if(temp !=0xF0)
{
temp=P1;
temp&=0xF0;
switch(temp)
{
case 0x70:
key=3;break;
case 0xB0:
key=2;break;
case 0xD0:
key=1;break;
case 0xe0:
key=0;break;
}
eeprom_eares(0x2200);
eeprom_write(0x2200,key);
Dispaly(key);
}
}
}
|
|