STC15的内部EEPROM奇怪问题
思路如下:
清除地址0x00的内容;
地址0x00 写入 0x22数据;
地址0x01 写入 0x44数据;
地址0x02 写入 0x66数据;
地址0x03 写入 0x88数据;
通过按键P11,再读出地址0x00、地址0x01、地址0x02、地址0x03的数据值是22、44、66、88. 通过按键P10,赋值0xEE给num的内容。
清除地址0x00的内容;
并将num的数值写入地址0x00;
再按P11读出值是EE、FF、FF、FF.
我又没有对地址0x01、0x02、0x03进行操作,为什么会变成全FF了呢?
- #include "reg51.h"
- #include "intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- sfr P5 = 0xC8; //xxxx,1111 端口5
- sfr P0M0 = 0x94; //0000,0000 端口0模式寄存器0
- sfr P0M1 = 0x93; //0000,0000 端口0模式寄存器1
- sfr P1M0 = 0x92; //0000,0000 端口1模式寄存器0
- sfr P1M1 = 0x91; //0000,0000 端口1模式寄存器1
- sfr P2M0 = 0x96; //0000,0000 端口2模式寄存器0
- sfr P2M1 = 0x95; //0000,0000 端口2模式寄存器1
- sfr P3M0 = 0xB2; //0000,0000 端口3模式寄存器0
- sfr P3M1 = 0xB1; //0000,0000 端口3模式寄存器1
- sfr P5M0 = 0xCA; //0000,0000 端口5模式寄存器0
- sfr P5M1 = 0xC9; //0000,0000 端口5模式寄存器1
- sfr IE2 = 0xAF; //中断使能寄存器2
- sfr AUXR = 0x8e; //辅助寄存器
- sfr T2H = 0xD6; //定时器2高8位
- sfr T2L = 0xD7; //定时器2低8位
- sfr ADC_CONTR = 0xBC; //ADC控制寄存器
- sfr ADC_RES = 0xBD; //ADC高8位结果
- sfr ADC_LOW2 = 0xBE; //ADC低2位结果
- sfr P1ASF = 0x9D; //P1口第2功能控制寄存器
- sfr IAP_DATA = 0xC2; //IAP数据寄存器
- sfr IAP_ADDRH = 0xC3; //IAP地址寄存器高字节
- sfr IAP_ADDRL = 0xC4; //IAP地址寄存器低字节
- sfr IAP_CMD = 0xC5; //IAP命令寄存器
- sfr IAP_TRIG = 0xC6; //IAP命令触发寄存器
- sfr IAP_CONTR = 0xC7; //IAP控制寄存器
- sbit LCD5110_RES = P3^7; //LCD5110 复位,0复位
- sbit LCD5110_SCLK = P3^6; //LCD5110 时钟
- sbit LCD5110_SDIN = P3^5; //LCD5110 数据
- sbit LCD5110_DC = P3^4; //LCD5110 1写数据,0写指令
- sbit LCD5110_CE = P3^3; //LCD5110 片选
- sbit P10 = P1^0; //开关
- sbit P11 = P1^1; //开关
- sbit P54 = P5^4; //开关
- sbit LED = P3^1; //开关
- const unsigned char code F6x8[][6] =
- {
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp 0
- { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // ! 1
- { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // " 2
- { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // # 3
- { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $ 4
- { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // % 5
- { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // & 6
- { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // ' 7
- { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // ( 8
- { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // ) 9
- { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // * 10
- { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // + 11
- { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // , 12
- { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // - 13
- { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // . 14
- { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // / 15
- { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0 16
- { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1 17
- { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2 18
- { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3 19
- { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4 20
- { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5 21
- { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6 22
- { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7 23
- { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8 24
- { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9 25
- { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // : 26
- { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ; 27
- { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // < 28
- { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // = 29
- { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // > 30
- { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ? 31
- { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @ 32
- { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A 33
- { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B 34
- { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C 35
- { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D 36
- { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E 37
- { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F 38
- { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G 39
- { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H 40
- { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I 41
- { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J 42
- { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K 43
- { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L 44
- { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M 45
- { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N 46
- { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O 47
- { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P 48
- { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q 49
- { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R 50
- { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S 51
- { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T 52
- { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U 53
- { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V 54
- { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W 55
- { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X 56
- { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y 57
- { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z 58
- { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [ 59
- { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55 60
- { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ] 61
- { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^ 62
- { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _ 63
- { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // ' 64
- { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a 65
- { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b 66
- { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c 67
- { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d 68
- { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e 69
- { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f 70
- { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g 71
- { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h 72
- { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i 73
- { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j 74
- { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k 75
- { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l 76
- { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m 77
- { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n 78
- { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o 79
- { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p 80
- { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q 81
- { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r 82
- { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s 83
- { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t 84
- { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u 85
- { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v 86
- { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w 87
- { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x 88
- { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y 89
- { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z 90
- { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 } // = 91
- };
- void Delayms(uint ms) //1mS@12.000MHz
- {
- unsigned char i, j;
- while(ms--)
- {i = 12;
- j = 169;
- do
- {
- while (--j);
- } while (--i);
- }
- }
- void IapIdle()//关闭IAP
- {
- IAP_CONTR = 0; //关闭IAP功能
- IAP_CMD = 0; //清除命令寄存器
- IAP_TRIG = 0; //清除触发寄存器
- IAP_ADDRH = 0x80; //将地址设置到非IAP区域
- IAP_ADDRL = 0;
- }
- uchar IapReadByte(uint addr)//从ISP/IAP/EEPROM区域读取一字节
- {
- uchar dat; //数据缓冲区
- IAP_CONTR = 0x82; //使能IAP
- IAP_CMD = 0x01; //设置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数据
- IapIdle(); //关闭IAP功能
- return dat; //返回
- }
- void IapEraseSector(uint addr)
- {
- IAP_CONTR = 0x82; //使能IAP
- IAP_CMD = 0x03; //设置IAP命令
- IAP_ADDRL = addr; //设置IAP低地址
- IAP_ADDRH = addr >> 8; //设置IAP高地址
- IAP_TRIG = 0x5a; //写触发命令(0x5a)
- IAP_TRIG = 0xa5; //写触发命令(0xa5)
- _nop_(); //等待ISP/IAP/EEPROM操作完成
- IapIdle();
- }
- void IapProgramByte(uint addr, uchar dat)//写一字节数据到ISP/IAP/EEPROM区域
- {
- IAP_CONTR = 0x82; //使能IAP
- IAP_CMD = 0x02; //设置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操作完成
- IapIdle();
- }
- void LCD_write_byte(unsigned char dt, unsigned char command)//LCD5110写入数据
- {
- unsigned char i;
- LCD5110_CE=0; // 关闭LCD
- LCD5110_DC=command; // 0是写命令,1是写数据
- for(i=0;i<8;i++)
- {
- if(dt&0x80)
- LCD5110_SDIN=1;
- else
- LCD5110_SDIN=0;
- dt=dt<<1; //dt为暂存数据
- LCD5110_SCLK=0;_nop_();
- LCD5110_SCLK=1;_nop_();
- }
- LCD5110_DC=1;
- LCD5110_CE=1;
- LCD5110_SDIN=1;
- }
- void LCD_clear(void) //5110LCD清屏
- {
- unsigned int i;
- LCD_write_byte(0x0c, 0);
- LCD_write_byte(0x80, 0);
- for (i=0; i<504; i++)
- LCD_write_byte(0, 1);
- }
- void LCD_set_XY(unsigned char X, unsigned char Y) //x 左右共0~83列可移,Y由上至下共0~5行可移
- {
- LCD_write_byte(0x40 | Y, 0); // column Y+=64
- LCD_write_byte(0x80 | X, 0); // row X+=128
- }
- void LCD_init(void) //5110LCD初始化LCD_init
- {
- LCD5110_RES=0; // 产生一个让LCD复位的低电平脉冲
- Delayms(10);
- LCD5110_RES=1;
- LCD5110_CE = 0; // 关闭LCD
- Delayms(10);
- LCD5110_CE = 1;// 使能LCD
- Delayms(10);
- LCD_write_byte(0x21, 0); // 使用扩展命令设置LCD模式
- LCD_write_byte(0xC2, 0); // 设置偏置电压//不同的屏须要微调此参数B9~C9
- LCD_write_byte(0x06, 0); // 温度校正
- LCD_write_byte(0x13, 0); // 1:48
- LCD_write_byte(0x20, 0); // 使用基本命令
- LCD_clear(); // 清屏
- LCD_write_byte(0x0C, 0); // 设定显示模式,正常显示
- LCD5110_CE = 0; // 关闭LCD
- }
- void LCD_P6x8Str(unsigned char x,unsigned char y,unsigned char ch)
- {
- unsigned char i;
- LCD_set_XY(x,y);
- for(i=0;i<6;i++){ LCD_write_byte(F6x8[ch][i],1); }
- }
- void LCD_P6x8Str4wita(unsigned char x,unsigned char y,unsigned char dat)
- {
- uchar buff;
- buff=(dat%4096/256);
- if(buff<10)
- {LCD_P6x8Str(x,y,buff+16);}
- else { LCD_P6x8Str(x,y,buff+23);}
- buff=(dat%256/16);
- if(buff<10)
- {LCD_P6x8Str(x+6,y,buff+16);}
- else { LCD_P6x8Str(x+6,y,buff+23);}
- buff=(dat%16/1);
- if(buff<10)
- {LCD_P6x8Str(x+12,y,buff+16);}
- else { LCD_P6x8Str(x+12,y,buff+23);}
- }
- void main()
- {
- uchar num;
- P0M0 = 0x00;
- P0M1 = 0x00;
- P1M0 = 0x00;
- P1M1 = 0x00;
- P3M0 = 0x00;
- P3M1 = 0x00;
- P5M0 = 0x00;
- P5M1 = 0x00;
- LCD_init();//LCD初始化
- LED=0;
-
- IapEraseSector(0);//清除数据
- IapProgramByte(0,0x22); //写入数据
- IapProgramByte(1,0x44); //写入数据
- IapProgramByte(2,0x66); //写入数据
- IapProgramByte(3,0x88);//写入数据
- while(1)
- {
-
- if(!P10)
- {
- Delayms(40);
- if(!P10)
- {
- while(!P10);
- num=0xEE;
- IapEraseSector(0);//清除数据
- IapProgramByte(0,num);//写入数据
- }
- }
- if(!P11)
- {
- Delayms(40);
- if(!P11)
- {
- while(!P11);
- LCD_P6x8Str4wita(30,1,IapReadByte(0));//读出数据
- LCD_P6x8Str4wita(30,2,IapReadByte(1));//读出数据
- LCD_P6x8Str4wita(30,3,IapReadByte(2));//读出数据
- LCD_P6x8Str4wita(30,4,IapReadByte(3));//读出数据
- }
- }
-
-
-
- }
-
- }
复制代码
|