/******************************************************************************* * * Copyright(C) 2012 WANG XIBING All rights reserved. * * 文件:libbase.c * 作者:wxb * 版本:V1.00 * 日期:2012-1-1 * 描述:基本的数据类型和函数库 * 属性:RL78 * 修改:V1.00, 2012-1-1, wxb, 创建本文件 * * 声明:本程序所有权归作者所有。 * 本程序仅用于实现特定产品的功能,任何修改或其它目的的应用均不作保证, * 任何人未经作者同意不得将本程序的全部或部分用于商业目的。 *******************************************************************************/ #include #include "libbase.h" /******************************************************************************* Function : add_check Description : 计算和校验 Input : Output : Return : void History : *******************************************************************************/ u8 add_check(void *buffer, u8 len) { u8 i; u8 check = 0; for(i = 0; i < len; i++) { check += ((PU8)buffer)[i]; } return check; } /******************************************************************************* Function : xor_check Description : 计算异或校验 Input : Output : Return : void History : *******************************************************************************/ u8 xor_check(void *buffer, u8 len) { u8 i; u8 check = 0; for(i = 0; i < len; i++) { check ^= ((PU8)buffer)[i]; } return check; } /******************************************************************************* Function : u8_to_bcd Description : 字节转BCD Input : Output : Return : void History : *******************************************************************************/ u8 u8_to_bcd(u8 n) { return ((n / 10) << 4) | (n % 10); } /******************************************************************************* Function : bcd_to_u8 Description : BCD转字节 Input : Output : Return : void History : *******************************************************************************/ u8 bcd_to_u8(u8 b) { return (b >> 4) * 10 + (b & 0x0F); } u32 bytes_to_hex(const u8 *bytes, u8 len) { u32 hex = 0; u32 temp; u8 i; for(i = 0; i < len; i++) { temp = bytes[i]; temp = temp << (i * 8); hex |= temp; } return hex; } void hex_to_byte(u32 hex, u8 *bytes, u8 len) { u8 i; for(i = 0; i < len; i++) { bytes[ i] = (u8)(hex & 0xFF); hex >>= 8; } } u32 bytes_to_bcd(const u8 *bytes, u8 len) { u32 bcd = 0; u8 i; for(i = 0; i < len; i++) { bcd *= 100; bcd += (bytes[i] >> 4) * 10 + (bytes[i] & 0x0F); } return bcd; } void bcd_to_byte(u32 bcd, u8 *bytes, u8 len) { u8 i; for(i = 0; i < len; i++) { bytes[len - i - 1] = (u8)((bcd % 10) + ((bcd / 10 % 10) << 4)); bcd /= 100; } return; } //void u32_to_time(u32 t, DATE_TIME *time) //{ // time->second = u8_to_bcd((u8)((t >> 0) & 0x3F)); // time->minute = u8_to_bcd((u8)((t >> 6) & 0x3F)); // time->hour = u8_to_bcd((u8)((t >> 12) & 0x1F)); // time->day = u8_to_bcd((u8)((t >> 17) & 0x1F)); // time->month = u8_to_bcd((u8)((t >> 22) & 0x0F)); // time->year = u8_to_bcd((u8)((t >> 26) & 0x3F)); //} //u32 time_to_u32(DATE_TIME *time) //{ // u32 t; // t = bcd_to_u8(time->second) & 0x3F; // t |= (u32)(bcd_to_u8(time->minute) & 0x3F) << 6; // t |= (u32)(bcd_to_u8(time->hour) & 0x1F) << 12; // t |= (u32)(bcd_to_u8(time->day) & 0x1F) << 17; // t |= (u32)(bcd_to_u8(time->month) & 0x0F) << 22; // t |= (u32)(bcd_to_u8(time->year) & 0x3F) << 26; // return t; //} // DELAY /******************************************************************************* Function : delay_us Description : 延时1us,4M Input : Output : Return : History : *******************************************************************************/ void delay_us(u16 us) { while(us > 2) { us -= 2; nop(); nop(); } } /******************************************************************************* Function : delay_ms Description : 延时1ms,4M or 32.768K Input : Output : Return : History : *******************************************************************************/ void delay_ms(u16 ms) { uint16_t i; for(i = 0; i < ms; i++) { delay_us(1000); } }