|
本帖最后由 npn 于 2020-7-23 22:43 编辑
STC15F104E/STC15F104W 单片机芯片,字符串写入EEPROM的第1个扇区 (限511字节,最后1字节为空字符0)。
由于该型号芯片的RAM容量有限,使用EEPROM的第2个扇区扩展RAM空间。
晶振频率:11.0592Mhz 波特率:9600,如有问题请在楼下跟帖回复。
(2020-07-23) V1.1 删改部分函数变量,优化RAM占用。
单片机源程序如下:
- #include "stc15f104e.h"
- #include "intrins.h"
- #define ReverseIntBytes(l) l<<24|l>>24|(l&0xFF0000)>>8|(l&0xFF00)<<8;
- #define XchgVar(A,B) A=A^B;B=A^B;A=A^B;
- #define U8 unsigned char
- #define U16 unsigned int
- #define U32 unsigned long
- U8 code *eeprom = 0x1000;
- U32 code *eeprom32 = 0x1000;
- U32 code ti[]={
- 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
- 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,0x698098d8,
- 0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,
- 0xa679438e,0x49b40821,0xf61e2562,0xc040b340,0x265e5a51,
- 0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
- 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,
- 0xfcefa3f8,0x676f02d9,0x8d2a4c8a,0xfffa3942,0x8771f681,
- 0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,
- 0xbebfbc70,0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
- 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,0xf4292244,
- 0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,
- 0xffeff47d,0x85845dd1,0x6fa87e4f,0xfe2ce6e0,0xa3014314,
- 0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391
- };
- U8 code ss[4][4]={
- {7,12,17,22},{5,9,14,20},{4,11,16,23},{6,10,15,21}
- };
- union md5_key {
- U32 l[4];
- U8 c[16];
- };
- union udata {
- U32 l;
- U8 c[4];
- };
- union udata2 {
- U32 l[2];
- U8 c[8];
- };
- sbit txd = P3^1;
- void delay_uart(){
- U8 data a,b;
- for(b=3;b>0;b--)
- for(a=94;a>0;a--);
- }
- void txd_data(U8 i) {
- U8 j = 1;
- delay_uart();
- txd = 0;
- do {
- delay_uart();
- txd = i & j;
- j <<= 1;
- } while (j);
- delay_uart();
- txd = 1;
- }
- void send_text(U8 *text){
- for(;*text;text++){
- txd_data(*text);
- }
- }
- void eeprom_open(){
- IAP_CONTR = 0x83;
- IAP_CMD = 0;
- IAP_ADDRH = 0x00;
- IAP_ADDRL = 0x00;
- }
- void eeprom_close(){
- IAP_CONTR = 0;
- IAP_CMD = 0;
- IAP_TRIG = 0;
- IAP_ADDRH = 0;
- IAP_ADDRL = 0;
- }
- void eeprom_write(U16 addr,U8 dat){
- IAP_CONTR = 0x83;
- IAP_CMD = 2;
- IAP_ADDRL = addr;
- IAP_ADDRH = addr >> 8;
- IAP_DATA = dat;
- IAP_TRIG = 0x5a;
- IAP_TRIG = 0xa5;
- _nop_();_nop_();_nop_();
- }
- void eeprom_erase(U16 addr){
- IAP_CONTR = 0x83;
- IAP_CMD = 3;
- IAP_ADDRL = addr;
- IAP_ADDRH = addr >> 8;
- IAP_TRIG = 0x5a;
- IAP_TRIG = 0xa5;
- _nop_(); _nop_();_nop_();
- }
- U8 block = 0;
- void witre_md5_data(U8 address,U8 byte){
- eeprom_write(0x200|((block<<6)|address),byte);
- }
- U8* md5(U8 *i){
- union md5_key md5_key;
- union udata2 md;
- U32 A,B,C,D,g;
- U16 z,pc=0,len=0,gn;
- U8 a,c,r,m;
- bit f,s128=0;
- md5_key.l[0] = 0x67452301;
- md5_key.l[1] = 0xefcdab89;
- md5_key.l[2] = 0x98badcfe;
- md5_key.l[3] = 0x10325476;
- for(;*i;i++) len++;
- gn=((len+8)>>6)+1;
- i -= len;
- for(z=1;z<=gn;z++){
- for(c = 0;c < (z == gn ? 56 : 64); c++){
- f = pc >= len;
- a = pc & 63 ^ 3;
- if(f && !s128){
- s128=1;
- witre_md5_data(a,128);
- } else {
- witre_md5_data(a,f ? 0 : *i++);
- }
- pc++;
- }
- if(z == gn) {
- md.l[0]=len;md.l[1]=0;
- for(a=0;a<3;a++){
- f = 0;
- for(c=0;c<8;c++){
- m=md.c[c^3];
- r= m<<1|f;
- f= m>>7;
- md.c[c^3]=r;
- }
- }
- for(c=0;c<8;c++){
- witre_md5_data(56|c,md.c[c]);
- }
- }
- A=md5_key.l[0];B=md5_key.l[1];
- C=md5_key.l[2];D=md5_key.l[3];
- for(a=0;a<64;a++){
- switch(a>>4){
- case 0:{g=(B&C)|((~B)&D);c=a;break;}
- case 1:{g=(D&B)|((~D)&C);c=5*a+1;break;}
- case 2:{g=B^C^D;c=3*a+5;break;}
- case 3:{g=C^(B|(~D));c=7*a;break;}
- }
- A=B+_lrol_(A+g + eeprom32[0x80 | (block << 4) | (c & 15)] + ti[a],ss[a>>4][a&3]);
- XchgVar(A, D); XchgVar(B, D); XchgVar(C, D);
- }
- if(++block > 7){
- eeprom_erase(0x200);
- block = 0;
- }
- md5_key.l[0]+=A;md5_key.l[1]+=B;
- md5_key.l[2]+=C;md5_key.l[3]+=D;
- }
- for(c=0;c<4;c++){
- md5_key.l[c]=ReverseIntBytes(md5_key.l[c]);
- }
- return md5_key.c;
- }
- void main(){
- U8 z;
- U8* md5_answer;
- eeprom_open();
- eeprom_erase(0x200);
- send_text("STC15F104E MD5 V1.1\r\n");
- send_text("md5(\"");
- send_text(eeprom);
- send_text("\")=");
- md5_answer = md5(eeprom);
- for (z = 0; z < 16; z++) {
- txd_data(((md5_answer[z] >> 4) < 10 ? 48 : 87) + (md5_answer[z] >> 4));
- txd_data(((md5_answer[z] & 15) < 10 ? 48 : 87) + (md5_answer[z] & 0xF));
- }
- send_text("\r\n");
- eeprom_close();
- while (1) {
- WDT_CONTR = 0x37;
- }
- }
复制代码
所有资料51hei提供下载:
MD5-STC15F104E V1.1.zip
(13.15 KB, 下载次数: 39)
|
评分
-
查看全部评分
|