unsigned char parameter_save() { WORD i, result = 1; Delay(10); IapEraseSector(IAP_ADDRESS); //拆除EEPROM for (i=0; i<512; i++) //检测是否擦除成功 { if (IapReadByte(IAP_ADDRESS+i) != 0xff) result = 0; } Delay(10); //Delay for(i=0; i<8; i++) //写入EEPROM { IapProgramByte(IAP_ADDRESS+(i*2), (BYTE)(cistern[i].perSettingTime>>8)); IapProgramByte(IAP_ADDRESS+(i*2+1), (BYTE)cistern[i].perSettingTime); } IapProgramByte(IAP_ADDRESS+256, 0xEE); //写入标志位 Delay(10); for(i=0; i<8; i++) //校验保存值 { if(IapReadByte(IAP_ADDRESS+(i*2)) != (BYTE)(cistern[i].perSettingTime>>8)) { result = 0; } if(IapReadByte(IAP_ADDRESS+(i*2+1)) != (BYTE)cistern[i].perSettingTime) { result = 0; } } for(i=0; i<8; i++) //设置值为界面值 { cistern[i].settingTime = cistern[i].perSettingTime; } return result; }
void test() { WORD i; P1 = 0xfe; //1111,1110 System Reset OK Delay(10); //Delay IapEraseSector(IAP_ADDRESS); //Erase current sector for (i=0; i<512; i++) //Check whether all sector data is FF { if (IapReadByte(IAP_ADDRESS+i) != 0xff) goto Error; //If error, break } P1 = 0xfc; //1111,1100 Erase successful Delay(10); //Delay for (i=0; i<512; i++) //Program 512 bytes data into data flash { IapProgramByte(IAP_ADDRESS+i, (BYTE)i); } P1 = 0xf8; //1111,1000 Program successful Delay(10); //Delay for (i=0; i<512; i++) //Verify 512 bytes data { if (IapReadByte(IAP_ADDRESS+i) != (BYTE)i) goto Error; //If error, break } P1 = 0xf0; //1111,0000 Verify successful while (1); Error: P1 &= 0x7f; //0xxx,xxxx IAP operation fail while (1); }
unsigned char EEPROM_Save_Water_Bound(unsigned char bound) { unsigned int i; //P1 = 0xfe; //1111,1110 系统OK EEPDelay(10); //延时 IapEraseSector(IAP_ADDRESS); //扇区擦除 for (i=0; i<512; i++) //检测是否擦除成功(全FF检测) { if (IapReadByte(IAP_ADDRESS+i) != 0xff) goto Error; //如果出错,则退出 } //P1 = 0xfc; //1111,1100 擦除成功 EEPDelay(10); //延时 //for (i=0; i<512; i++) //编程512字节 //{ IapProgramByte(IAP_ADDRESS, (unsigned char)bound); //} //P1 = 0xf8; //1111,1000 编程完成 EEPDelay(10); //延时 //for (i=0; i<512; i++) //校验512字节 //{ if (IapReadByte(IAP_ADDRESS) != (unsigned char)bound) goto Error; //如果校验错误,则退出 //} //P1 = 0xf0; //1111,0000 测试完成 //while (1); return 0; Error: //P1 &= 0x7f; //0xxx,xxxx IAP操作失败 //while (1); return 1; }
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源 */ BYTE write_flash_with_protect_in_one_sector(WORD begin_addr, WORD counter, BYTE array[]) { WORD i = 0; WORD in_sector_begin_addr = 0; WORD sector_addr = 0; WORD byte_addr = 0; /* 判是否是有效范围,此函数不允许跨扇区操作 */ if(counter > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; in_sector_begin_addr = begin_addr & 0x01ff; /* 假定从扇区的第0个字节开始,到USED_BYTE_QTY_IN_ONE_SECTOR-1个字节结束,后面部分不用,程序易编写 */ if((in_sector_begin_addr + counter) > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; /* 将该扇区数据 0 - (USED_BYTE_QTY_IN_ONE_SECTOR-1) 字节数据读入缓冲区保护 */ sector_addr = (begin_addr & 0xfe00); byte_addr = sector_addr; for(i = 0; i < USED_BYTE_QTY_IN_ONE_SECTOR; i++) { protect_buffer[i] = IapReadByte(byte_addr++); } /* 将要写入的数据写入保护缓冲区的相应区域,其余部分保留 */ for(i = 0; i < counter; i++) { protect_buffer[in_sector_begin_addr++] = array[i]; } /* 擦除 要修改/写入 的扇区 */ IapEraseSector(sector_addr); /* 将保护缓冲区的数据写入 Data Flash, EEPROM */ byte_addr = sector_addr; for(i = 0; i< USED_BYTE_QTY_IN_ONE_SECTOR; i++) { /* 写一个字节 */ IapProgramByte(byte_addr, protect_buffer[i]); /* 比较对错 */ if (IapReadByte(begin_addr) != protect_buffer[i]) { IapIdle(); return ERROR; } byte_addr++; } IapIdle(); return OK; }
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源 */ BYTE sequential_write_flash_in_one_sector(WORD begin_addr, WORD counter, BYTE array[]) { WORD i = 0; WORD in_sector_begin_addr = 0; WORD sector_addr = 0; /* 判是否是有效范围,此函数不允许跨扇区操作 */ if(counter > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; in_sector_begin_addr = begin_addr & 0x01ff; if((in_sector_begin_addr + counter) > USED_BYTE_QTY_IN_ONE_SECTOR) return ERROR; /* 擦除 要修改/写入 的扇区 */ IapEraseSector(begin_addr); for(i=0; i<counter; i++) { /* 写一个字节 */ IapProgramByte(begin_addr, array[i]); /* 比较对错 */ if (IapReadByte(begin_addr) != array[i]) { IapIdle(); return ERROR; } begin_addr++; } IapIdle(); return OK; }
void test_eeprom(void) { unsigned int i; unsigned char dat; //清除数据 IapEraseSector(IAP_ADDRESS); for(i = 0; i < 512; i++) { if(IapReadByte(IAP_ADDRESS + i) != 0xff) { goto err; } } _nop_(); _nop_(); _nop_(); _nop_(); //写入数据 for(i = 0; i < 512; i++) { IapProgramByte(IAP_ADDRESS + i, (unsigned char)i); } _nop_(); _nop_(); _nop_(); _nop_(); //verify for(i = 0; i < 512; i++) { if((dat = IapReadByte(IAP_ADDRESS+i)) != (unsigned char)i) { goto err; } UART_Send_Char(dat); } err: while(1) { } }
void IAPRead() { unsigned char i; i=IapReadByte(0xE206); if(i==1) //判断数据是否已经更新过,更新过才读取数据 { g_y=IapReadByte(0xE200)<<8|IapReadByte(0xE201); g_x=IapReadByte(0xE202)<<8|IapReadByte(0xE203); g_z=IapReadByte(0xE204)<<8|IapReadByte(0xE205); } i=IapReadByte(0xE400); if(i==1) //判断数据是否已经更新过,更新过才读取数据 { g_fOffsetx=IapReadByte(0xE401); g_fOffsety=IapReadByte(0xE402); } }
unsigned char parameter_read() { WORD i, result = 1; Delay(10); if(IapReadByte(IAP_ADDRESS+256) == 0xEE) { for(i=0; i<8; i++) { cistern[i].settingTime = ((IapReadByte(IAP_ADDRESS+(i*2)) << 8) | IapReadByte(IAP_ADDRESS+(i*2+1))); } result = 1; } else { result = 0; } return result; }
/********************************************************************************************* 函数名:readEpdat 参 数:无 返回值:无 备 注:EEPROM 读数据 **********************************************************************************************/ unsigned char readEpdat(unsigned int addr, unsigned char* dat, unsigned char len) { unsigned char i; for(i = 0; i < len; i++) { dat[i] = IapReadByte(addr + i); } return 1; }
/********************************************************************************************* 函数名:writeEpdat 参 数:无 返回值:无 备 注:EEPROM 读数据 **********************************************************************************************/ void writeEpdat(unsigned int addr, unsigned char* dat, unsigned char len) { unsigned char i; //清除数据 IapEraseSector(addr); for(i = 0; i < len; i++) { if(IapReadByte(addr + i) != 0xff) { UART_Send_String("ERASE ERR!"); } } //写入数据 for(i = 0; i < len; i++) { IapProgramByte(addr + i, (unsigned char)dat[i]); } }
{ TI = 0; } */ RI = 0; //令接收中断标志位为0(软件清零) //pov_dat[num] = SBUF; //将接收到的数据送入变量 UART_data //P0=SBUF; UART_data= SBUF; //IapEraseSector(0x0001); //IapProgramByte(0x0001, UART_data); //SBUF=IapReadByte(0x0001); UART_Send_Byte(IapReadByte(0x0001)); //SBUF = UART_data; //将接收的数据发送回去(删除//即生效) // while(TI == 1); //检查发送中断标志位 //while(!TI); //TI = 0; //令发送中断标志位为0(软件清零) } /************************************************************************** - 功能描述:51单片机的串口发送0d 0a ,即回车换行 - 隶属模块:STC51串口操作 - 函数属性:外部,使用户使用 - 参数说明:无
unsigned char AFWS_Read_EEP() { return IapReadByte(IAP_ADDRESS); }
void main() { INT8U i=0; INT8U search_temp = 0x06; // search_temp 可以增加到6 在search_temp小于3的时候 开始发送广播唤醒 CpuInit(); POWER_UP_RESET_CC1100(); halRfWriteRfSettings(); halSpiWriteBurstReg(CCxxx0_PATABLE, PaTabel, 8); CC1101_Setwor(); G_IT_ON; // 开启单片机全局中断 Usart_printf(&g_module_id.Sn[0],1); Usart_printf(&g_module_id.Sn[1],1); Usart_printf(&g_gateway.Sn[0],1); Usart_printf(&g_gateway.Sn[1],1); Log_printf(" "); // 上电设置网关 // 只有外部中断没有打开,现在进行设置网关字节 地址和网管不能为全0xFFFF while( ( 0xFFFF == g_gateway.Sn_temp ) || ( 0xFFFF == g_module_id.Sn_temp ) ) { LED_D4 = ~LED_D4; delay(50000); if( 0x55 == g_rx_flag ) { g_rx_flag = 0x00; // 将网关数据写入 IapProgramByte(GATEWAY_ADDRESS,TxBuf[1]); IapProgramByte(GATEWAY_ADDRESS+1,TxBuf[2]); //g_gateway.Sn_temp = IapReadByte(GATEWAY_ADDRESS); // 将地址数据写入 IapProgramByte(MODEL_SN_ADDRESS,TxBuf[3]); IapProgramByte(MODEL_SN_ADDRESS+1,TxBuf[4]); //g_module_id.Sn_temp = IapReadByte(MODEL_SN_ADDRESS); Log_printf("GATEWAY OK\n"); Log_printf("MODEL_SN OK\n"); LED_D4 = 0; // 读取设置数据 IapReadModelSn(MODEL_SN_ADDRESS,&g_module_id); IapReadModelSn(GATEWAY_ADDRESS,&g_gateway); } } // g_module_rpl = IapReadByte(MODEL_RPL); // 默认模块id的路由等级1 模块ID 最高位为0 表示路由模块 g_module_id.Sn[0] |= ( (g_module_rpl<<4) & 0x7F ); g_pre_src = g_module_id.Sn_temp; Usart_printf(&g_module_id.Sn[0],1); Usart_printf(&g_module_id.Sn[1],1); Usart_printf(&g_gateway.Sn[0],1); Usart_printf(&g_gateway.Sn[1],1); Log_printf(" initialization ok "); // 地址网关设置完成 LED_D2 = ~LED_D2; // 读出搜索模式 首次上电为0xFF 则进行搜索 g_search = IapReadByte(SEARCH_MODE); if( 0xFF == g_search ) { SearchMode: while( search_temp-- != 0 ) { SearchData[2] = g_rid; // 网关地址 SearchData[4] = g_gateway.Sn[0]; SearchData[5] = g_gateway.Sn[1]; // 源地址(模块ID) SearchData[9] = g_module_id.Sn[0]; SearchData[10] = g_module_id.Sn[1]; // 目的地址(网关地址) SearchData[11] = g_gateway.Sn[0]; SearchData[12] = g_gateway.Sn[1]; for( i=0;i<13;i++) { SearchData[13] += SearchData[i]; } // 进行唤醒时,只需要把路由标识滤除即可 将路由标识高字节分出一位代表是模块还是基站 // 首先发送唤醒波,而后发送数据 进行路由搜索时,使用广播唤醒 if( search_temp < 3 ) CC1101_Wakeupcarry(WorCarry, 2,4); halRfSendPacket(SearchData, 14); g_rid++; // 发送完成后g_rid自增 //g_wor_flag = 0x55; timer = 0; Timer0_Init(10); TIMER0_ON; //g_search = 0x55; g_enter_rx = 0x55; // 存放校验和 SearchData[13] = 0x00; goto EnterRx; } g_search = 0x00; } // else // { // g_search = 0x00; // } while (1) { //Log_printf("Enter wor\n"); if( 0x55 == g_wor_flag ) { CC1101_Worwakeup(); // 将接收的数据存储到RxBuf数组中 EnterRx: while(g_enter_rx) halRfRxPacket(RfRecBuf); // 此处进行rf数据处理 if( 0x55 == g_rf_rx_flag ) { g_rf_rx_flag = 0x00; RfRouteManage(&rf_route_data); } if( 0xFF == g_search ) // 若没有搜索到路径,则跳转回搜索路径 goto SearchMode; } halSpiStrobe(CCxxx0_SWORRST); // 复位到 事件1 halSpiStrobe(CCxxx0_SWOR); // 启动WOR INT1_ON; // 开外部中断 PCON |= PD_ON; // 从掉电模式唤醒后,程序从这行开市 //Log_printf("Exit pd\n"); } }