📄 xp_vc33_body.c
字号:
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
#include "xp_VC33_header.h"
/* this will make the program in the external ram run as fast as in the internal!!
* take the timing controlship to CPLD.If you like it please pay it :)
* or you have to take a software control over them,which is dangerous and tedious
*/
#define _HARD_TIMING_
//////////////////////////////////////////////////////////////////////////
//char debugMsg[128];
/*some function not completed in _LTC33C_,PLEASE LOOK FOR THEM in MAIN.C*/
/********************系统初始化******************************************/
void xp_VC33_SysInitialize(void)
{
int* pBusCtrl =(int*)0x808064;
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
int read = R_OPENIRQ;
#endif
#ifdef _HARD_TIMING_
/* don't edit the codes here,reserved for timing control
* otherwise the board will stop work!!!!!!!!!!!
*
*/
*pBusCtrl = 0x1000; /*☆★○●◎◇◆□■△▲※§№*/
/* this will make the program in the external ram run as fast as in the internal!!
* take the timing controlship to CPLD.If you like it please pay it :)
* or you have to take a software control over them,which is dangerous and tedious
*/
#endif
;
}
bool xp_VC33_TestNvram(void)//测试NVRAM
{
int i,readRes;
for(i = 0x600000; i <= 0x61ffff; i=i+2)
{
*(int*)i = 0x5a;
}
for(i = 0x600001; i <= 0x61ffff; i=i+2)
{
*(int*)i = 0xa5;
}
for(i = 0x600000; i <= 0x61ffff; i=i+2)
{
readRes = (*(int*)i) & BYTE0;
if(readRes != 0x5a)
return false;
}
for(i = 0x600001; i <= 0x61ffff; i=i+2)
{
readRes = (*(int*)i) & BYTE0;
if(readRes != 0xa5)
return false;
}
for(i = 0x600000; i <= 0x61ffff; i=i+2)
{
*(int*)i = 0xa5;
}
for(i = 0x600001; i <= 0x61ffff; i=i+2)
{
*(int*)i = 0x5a;
}
for(i = 0x600000; i <= 0x61ffff; i=i+2)
{
readRes = (*(int*)i) & BYTE0;
if(readRes != 0xa5)
return false;
}
for(i = 0x600001; i <= 0x61ffff; i=i+2)
{
readRes = (*(int*)i) & BYTE0;
if(readRes != 0x5a)
return false;
}
return true;
}
bool xp_VC33_TestRam(unsigned int base,unsigned int length)//ram测试
{
int i,readRes,end;
end = base + length - 1;
for(i = base; i < end; i=i+2)
{
*(int*)i = 0x55aa55aa;
}
for(i = base + 1; i < end; i=i+2)
{
*(int*)i = 0xaa55aa55;
}
for(i = base; i < end; i=i+2)
{
readRes = *(int*)i;
if(readRes != 0x55aa55aa)
return false;
}
for(i = base + 1; i < end; i=i+2)
{
readRes = *(int*)i;
if(readRes != 0xaa55aa55)
return false;
}
for(i = base; i < end; i=i+2)
{
*(int*)i = 0xaa55aa55;
}
for(i = base + 1; i < end; i=i+2)
{
*(int*)i = 0x55aa55aa;
}
for(i = base; i < end; i=i+2)
{
readRes = *(int*)i;
if(readRes != 0xaa55aa55)
return false;
}
for(i = base + 1; i < end; i=i+2)
{
readRes = *(int*)i;
if(readRes != 0x55aa55aa)
return false;
}
return true;
}
unsigned int xp_VC33_RomCheckSum()//Rom校验和
{
unsigned int checksum = 0,i;
for(i = 0x400000; i <= 0x47ffff; i++)
{
checksum += (*(int*)i) & BYTE0;
}
return checksum;
}
/********************VC33中断********************************************/
void xp_VC33_GlobalIntEnable(bool yesorno)//中断全局使能或禁止
{
if (yesorno)
ST |= BIT13;
else
ST &= ~BIT13;
}
void xp_VC33_IntEnable(int intNo,bool yesorno)//中断使能或禁止
{
if(yesorno)
IE |= intNo;
else
IE &= ~intNo;
}
/********************VC33定时器******************************************/
void xp_VC33_SetTimer(int timerNo,int period)
{
switch(timerNo)
{
case TIMER0:
TIM0_PRD = period;
TIM0_CTL |= BIT0|BIT8|BIT9;
break;
case TIMER1:
TIM1_PRD = period;
TIM1_CTL |= BIT0|BIT8|BIT9;
break;
default:
break;
}
}
void xp_VC33_StartTimer(int timerNo)
{
switch(timerNo)
{
case TIMER0:
TIM0_CTL |= BIT6|BIT7;
break;
case TIMER1:
TIM1_CTL |= BIT6|BIT7;
break;
default:
break;
}
}
int xp_VC33_StopTimer(int timerNo)
{
switch(timerNo)
{
case TIMER0:
TIM0_CTL &= ~(BIT6|BIT7);
return TIM0_CNT;
break;
case TIMER1:
TIM0_CTL &= ~(BIT6|BIT7);
return TIM1_CNT;
break;
default:
break;
}
return 0;
}
/*--------------------------------------------------------------------*/
/*
* take 8*25 = 200 ns as unit for the slowest bus speed,
* else take 25 ns as unit for the fastest bus speed.
*/
void Delay_Cycles(unsigned int cycles)
{
int i = 0;
for(i = 0; i < cycles; i++)
{
;
}
}
double ADResConv(int result)
{
double conv = 0.0;
if(BIT13 & result)
conv = -10.0 * (~result & 0x1fff)/ADMAXCODE;
else
conv = 10.0 * result / ADMAXCODE;
}
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
unsigned Bcd2Hex(unsigned bcd) /* 将BCD码转换成相对应的16进制数,如果返回-1,则表示被转换的数不是合法的BCD码 */
{
unsigned i,hex=0,pow=1,break_flag=0;
for(i=0;i<8;i++)
{
switch(break_flag)
{
case 0:
if(((bcd>>(i*4)) & 0xf)>0x9)
{
break_flag=1;
hex=bcd;
}
else
{
hex+=((bcd>>(i*4)) & 0xf)*pow;
pow*=10;
}
break;
case 1:
break;
}
}
if(break_flag==1)
return -1;
else
return hex;
}
unsigned Hex2Bcd(unsigned hex)
{
unsigned i,bcd=0,pow=1;
if(hex>99999999)
return (unsigned)99999999;
for(i=0;i<8;i++)
{
pow*=10;
bcd|=(hex % 10)<<(i*4);
hex/=pow;
}
return bcd;
}
int BitCopy(int x,unsigned int BitMask)
{
int i;
int y;
for(i = 0; i < 8; i++)
{
if(BitMask & BIT0)
{
switch(i)
{
case 0:
if(x & BIT0)
y |= BIT0;
else
y &= ~BIT0;
break;
case 1:
if(x & BIT1)
y |= BIT1;
else
y &= ~BIT1;
break;
case 2:
if(x & BIT2)
y |= BIT2;
else
y &= ~BIT2;
break;
case 3:
if(x & BIT3)
y |= BIT3;
else
y &= ~BIT3;
break;
case 4:
if(x & BIT4)
y |= BIT4;
else
y &= ~BIT4;
break;
case 5:
if(x & BIT5)
y |= BIT5;
else
y &= ~BIT5;
break;
case 6:
if(x & BIT6)
y |= BIT6;
else
y &= ~BIT6;
break;
case 7:
if(x & BIT7)
y |= BIT7;
else
y &= ~BIT7;
break;
default:
break;
}
}
BitMask >>= 1;
}
return y;
}
#endif
void xp_SetSclkOut(void)
{
SRCTRL &= ~BIT8;
SRCTRL |= BIT9;//Set sclk as output
}
void xp_SetSdaOut(void)
{
SXCTRL &= ~BIT8;
SXCTRL |= BIT9;
}
void xp_SetSdaIn(void)
{
SXCTRL &= ~BIT8;
SXCTRL &= ~BIT9;
}
/*******************************************************************************/
/************************************************************************/
/* 看门狗和电源监测 */
/************************************************************************/
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
int xp_SYS_ReadReset(void)//读复位状态位
{
return R_SYS_RESET & 0x3;
}
/*
*返回值:0 -> 无复位
1 -> 电源越限复位
2 -> 看门狗复位
3 -> 可能出现吗?
*/
void xp_SYS_ClearReset(void)//清复位状态位
{
W_CLEAR_RESET = 0;
}
#endif
/************************************************************************/
/* 实时时钟 */
/************************************************************************/
void xp_RT_Start(void)//启动实时时钟
{
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
int mon = 0;
mon = RTMONTH;
RTMONTH &= ~BIT7;
#else
//
#endif
}
void xp_RT_Stop(void)//停止实时时钟
{
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
int mon = 0;
mon = RTMONTH;
RTMONTH |= BIT7;
#else
//
#endif
}
void xp_RT_SetTime(XP_RT_TIME rt_time)//设置实时时钟时间和参数
{
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
RTREGB &= ~BIT7;
if(rt_time.h12)
{
RTHOUR |= BIT6;
if(rt_time.pm)
RTHOUR |= BIT5;
else
RTHOUR &= ~BIT5;
}
else
RTHOUR &= ~BIT6;
RTCENTURY = rt_time.century;
RTYEAR = rt_time.year;
RTMONTH = BitCopy(rt_time.month,0x1f);
RTDATE = BitCopy(rt_time.date,0x3f);
RTDAY = BitCopy(rt_time.day,0x7);
if(rt_time.h12)
RTHOUR = BitCopy(rt_time.hour,0x1f);
else
RTHOUR = BitCopy(rt_time.hour,0x3f);
RTMINUTE = rt_time.minute;
RTSECOND = rt_time.second;
RTREGB |= BIT7;
#else
//
#endif
}
XP_RT_TIME xp_RT_ReadTime(void)//读取实时时钟时间和配置
{
XP_RT_TIME rt_time;
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
RTREGB &= ~BIT7;
rt_time.pm = RTHOUR & BIT5;
rt_time.h12 = RTHOUR & BIT6;
rt_time.century = RTCENTURY & BYTE0;
rt_time.year = RTYEAR & BYTE0;
rt_time.month = RTMONTH & 0x1f;
rt_time.date = RTDATE & 0x3f;
rt_time.day = RTDAY & 0x7;
if(RTHOUR & BIT6) /*12*/
{
rt_time.hour = RTHOUR & 0x1f;
}
else /*24*/
{
rt_time.hour = RTHOUR & 0x3f;
}
rt_time.minute = RTMINUTE & BYTE0;
rt_time.second = RTSECOND & BYTE0;
RTREGB |= BIT7;
#else
//
#endif
return rt_time;
}
int xp_RT_ReadUserData(int address)//读取用户区数据
{
int data;
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
data = *(int*)address;
#else
//
#endif
return data;
}
void xp_RT_WriteUserData(int address,int data)//向用户区存数据
{
#if _LTC33_EDITION_ == (_LTC33A_ || _LTC33B_)
*(int*)address = data;
#else
//
#endif
}
/************************************************************************/
/* GPS 对时 */
/************************************************************************/
bool xp_GPS_ReadPulse(void)//读取GPS 状态位
{
return((R_GPS_ADST_FREST & BYTE0) & BIT0);
}
int xp_GPS_ReadMs(void)//读取GPS 毫秒计数器
{
return R_GPS_MS & 0xffff;
}
/************************************************************************/
/* 存储器 */
/************************************************************************/
int xp_MEM_Read(int address)//存储器读
{
return *(int*)address;
}
void xp_MEM_Write(int address,int data)//存储器写
{
*(int*)address = data;
}
/************************************************************************/
/* 串行EEPROM */
/************************************************************************/
void Delay_N_uS(int n_usecond)
{
int count = 6;/*--what's suitable?only know the num of instructions--*/
int i = n_usecond;
while(i--)
{
while(count--)
{
;
}
count = 6;
}
}
void Delay_N_mS(int n_milisecond) /* n mS delay */
{
int i = n_milisecond;
while(i--)
{
Delay_N_uS(1000);
}
}
bit I2C_Start(void)
{
xp_SetSdaOut();
xp_SetSclkOut();
Delay_N_uS(6);
RESET_SCLK;
Delay_N_uS(6);
SET_SDA;//I2C_SDA =1;
Delay_N_uS(6);
SET_SCLK;//I2C_SCK =1;
Delay_N_uS(6);
if(READ_SDAOUT == 0)
return 0;
if(READ_SCLKOUT == 0)
return 0;
RESET_SDA;//I2C_SDA = 0;
Delay_N_uS(6);
RESET_SCLK;
Delay_N_uS(6);
return 1;
}
void I2C_Stop(void)
{
xp_SetSdaOut();
RESET_SCLK;
Delay_N_uS(6);
RESET_SDA;//I2C_SDA = 0;
Delay_N_uS(6);
SET_SCLK;//I2C_SCK = 1;
Delay_N_uS(6);
//SET_SCLK;
//Delay_N_uS(6);
SET_SDA;//I2C_SDA = 1;
Delay_N_uS(6);
}
void I2C_Ack(void)
{
RESET_SCLK;
Delay_N_uS(6);
xp_SetSdaOut();
RESET_SDA;//I2C_SDA=0;
Delay_N_uS(6);
SET_SCLK; //I2C_SCK=1;
Delay_N_uS(6);
RESET_SCLK;//I2C_SCK=0;
Delay_N_uS(6);
}
void I2C_Nack(void)
{
RESET_SCLK;
Delay_N_uS(6);
xp_SetSdaOut();
SET_SDA;//I2C_SDA=1;
Delay_N_uS(6);
SET_SCLK;//I2C_SCK=1;
Delay_N_uS(6);
RESET_SCLK;//I2C_SCK=0;
Delay_N_uS(6);
}
bit I2C_Send_Byte( uchar d)
{
bit bit_ack = 1;
uint i = 8;
xp_SetSdaOut();
RESET_SCLK;
while( i-- )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -