⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contact.c

📁 51单片机C语言常用模块与综合系统设计实例精讲
💻 C
字号:
#include <./Atmel/at89x52.h>
#include <stdio.h>
#include <absacc.h>
#include <string.h>
#include "source.h"
/**为下载协议,与第14章的下载协议是一样的**/
#define FSUCCESS 0
#define FERROR   1  
#define FDOVER  2
void print_sbuf(unsigned char c){
	dst_buf[dst_head]=c; 
	dst_head ++;
	dst_head %=DST_LEN; 
}
#define REPLAY_LEN 9
void reply_dwn(unsigned char flag,unsigned char *rbuf){//rbuf is the buf that downlaod data.
	unsigned char reply[REPLAY_LEN];
	struct  yushi rply_crc;
	unsigned char i;
	reply[0]=FDWN_FHD;
	reply[1]=flag;
	reply[2]=rbuf[2];
	reply[3]=rbuf[3];
	reply[4]=rbuf[4];
	reply[5]=0x00;
	reply[6]=0x00;
	init_crccheck(&rply_crc);
	for(i=0;i<7;i++){
		crccheck(reply[i],&rply_crc);
	}
	reply[7]=rply_crc.h;
	reply[8]=rply_crc.l;
	for(i=0;i<REPLAY_LEN;i++){	
		print_sbuf(*(reply+i));
	}
	TI=1;
}
void flash_download(void){
	union SFADD   dwn_adres;
	unsigned char fdata[FDWN_LEN];
	unsigned char fsm_fdl;
	unsigned char cnt;
	unsigned char erase_sec=0;
	unsigned int  sector_section=0;
	struct yushi fcrc;
	fsm_fdl=0;
	while(1){
		if(dsr_tail!=dsr_head){		
			if(fsm_fdl==0){//search frame head
				if(dsr_buf[dsr_tail]==FDWN_FHD){
					fsm_fdl=1;//head had found.
					fdata[0]=FDWN_FHD;
					cnt=1;
				}
			}
			else if(fsm_fdl==1){//receive type byte
				if(cnt<FDWN_LEN-1){
						fdata[cnt]=dsr_buf[dsr_tail];
						cnt++;
				}
				else{//finish receive data.
					fdata[cnt]=dsr_buf[dsr_tail];
					if(1){//mult_crccheck(fdata,FDWN_LEN,struct &fcrc)==0){//crc ok
						dwn_adres.c[1]=fdata[2];
						dwn_adres.c[2]=fdata[3];
						dwn_adres.c[3]=fdata[4];
						sector_section=dwn_adres.c[1];//convert to sector.
						sector_section <<=8;
						sector_section &=0xff00;
						sector_section |=dwn_adres.c[2];
						sector_section >>=5;
						if(((dwn_adres.c[2]&0x1f)==0x00)&&(dwn_adres.c[3]==0x00)){//A0~A11 IS 0 it indicaate across the sector
 					    	if(sector_section<8){//is sector SA0~SA7
								erase_sec=(unsigned char)sector_section;
								delay_10us(15);
								if(erase_sector(erase_sec)){//erase sector ok.
								}
								else{
									reply_dwn(erase_sec,fdata);
								}
							}
							else if((sector_section&0x7)==0){//sector is biger than 8.
								sector_section >>=3;
								erase_sec=(unsigned char)(sector_section&0xff);
								erase_sec +=7;
								delay_10us(15);
								if(erase_sector(erase_sec)){//erase sector ok.
								}
								else{
									reply_dwn(erase_sec,fdata);
								}
							}
						}
						for(cnt=0;cnt<128;cnt++){
							write_flash(&dwn_adres,fdata[5+cnt]);
							dwn_adres.l ++;
						}
						if(fdata[1]==0x00){//download data
							reply_dwn(FSUCCESS,fdata);
						}
						else if(fdata[1]==0x01){//download finsish.
							reply_dwn(FDOVER,fdata);//for mutil file download ,power down again to into normal mode.
						}
					}
					else{//CRC error
						reply_dwn(FERROR,fdata);
					}
					fsm_fdl=0;//search frame head again.					
				}
			}	
			dsr_tail++;
			dsr_tail %=DSR_LEN;
		}
		else{
			//wait receive serial data.
		}
	}
}
/**为信息传输协议***/
void write_remote(unsigned char *a,unsigned char len){//向串口发送数据
	unsigned char  i;
	for(i=0;i<len;i++){
		dst_buf[dst_head]=*(a+i);
		dst_head++;
		dst_head%=DST_LEN;
	}
	if(TDBdata_over){
		TI=1;
		TDBdata_over=0;
	}
}
/*存储串口传输的信息*/
void store_remote(struct NOTICE *info,unsigned char *notice,unsigned int *eep_add){
	unsigned int eeprom_address;
	unsigned char buf[32];
	eeprom_address=*eep_add;
	eeprom_address *=NOTICE_EEPCONTENT_LEN;
	eeprom_address +=EERPOM_NOTICE_START;
	info->len=notice[2]-8;
	if(info->len>62){
		//remote len >62 error
		info->len=62;
	}
	buf[0]=info->len;
	info->font=notice[5];
	buf[1]=info->font;
	if(buf[0]>30){//write 2 page data
		memcpy(buf+2,notice+6,30);
		AT24C64_wbytes((unsigned char *)&eeprom_address,32,buf);
		memcpy(buf,notice+36,info->len-30);
		memcpy(info->buf,notice+6,info->len);
		eeprom_address +=32;
		AT24C64_wbytes((unsigned char *)&eeprom_address,info->len-30,buf);//write another page .
	}
	else{
		memcpy(buf+2,notice+6,info->len);
		memcpy(info->buf,notice+6,info->len);
		AT24C64_wbytes((unsigned char *)&eeprom_address,info->len+2,buf);
	}
}
unsigned char notice[256];
unsigned char rnotlen;
bit read_remote(void){//读串口传输的数据
	static unsigned char fsm_remote=0x00;
	static unsigned char rcnt;
	unsigned char temp;
	unsigned int eeprom_address;
	struct yushi rmcrc;
	if(dsr_tail!=dsr_head){//接收串口数据缓冲区不为空
		if(dsr_buf[dsr_tail]==REMOTE_HEAD){//是帧的帧头
				notice[0]=REMOTE_HEAD;
				fsm_remote=0x01;//下一个接收长度字节
				rcnt=1;
		}
		else if(fsm_remote==0x01){//接收帧长字节 
				notice[1]=dsr_buf[dsr_tail];
				rnotlen=dsr_buf[dsr_tail];//把接收的帧长度给rnotlen
				if(rnotlen>128){//统一定义长度小于128字节,如果大于128则表明上个接收的字节不是帧头
					fsm_remote=0x00;//重新搜索帧头
				}
				else
					fsm_remote=0x02;//接收帧数据
				rcnt=2;	
		}
		else if(fsm_remote==0x02){//接收帧内容
			notice[rcnt]=dsr_buf[dsr_tail];
			rcnt++;
			if(rcnt>=rnotlen){//接收了rnotlen个字节,帧接收完毕
				fsm_remote=0x00;//状态转到重新搜索帧头.
						switch(notice[2]){
							case 0x00://改变消息 
								eeprom_address=notice[4];
								eeprom_address=0;//串口传输的信息从地址0开始
								store_remote(show_notice.ognd+notice[4],notice,&eeprom_address);
								break;
							case 0x01://调整RTC的时间
								temp=0;
								if((notice[6]<100)&&(notice[7]<13)){
									if((notice[8]<32)&&(notice[9]<24)){
										if((notice[10]<60)&&(notice[11]<60)){
											temp=1;
										}
									}
								}
								if(temp==1){
									rtc.year=chtobcd(notice[6]);
									rtc.month=chtobcd(notice[7]);
									rtc.dayom=chtobcd(notice[8]);
									rtc.hour=chtobcd(notice[9]);
									rtc.minute=chtobcd(notice[10]);
									rtc.second=chtobcd(notice[11]);
									SD2000_set_time(&rtc);
								}
								else{
									//Set time error
								}
								break;
						}
						write_remote(notice,8);
					}
				}
				else{
					fsm_remote=0x00;
				}
				dsr_tail++; 
				dsr_tail %=DSR_LEN;//把串口接收的数据减一
						return 1;
		}
	else{
		return 0;
	}
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -