R1UWaterRS485/hardinterface/src/libbase.c

211 lines
4.5 KiB
C
Raw Normal View History

2024-06-03 15:49:39 +08:00
/*******************************************************************************
*
* Copyright(C) 2012 WANG XIBING All rights reserved.
*
* libbase.c
* wxb
* V1.00
* 2012-1-1
*
* RL78
* V1.00, 2012-1-1, wxb,
*
*
*
*
*******************************************************************************/
#include <string.h>
#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);
}
}