📄 per.c
字号:
#include "MAIN.H"
#include "PER.H"
/*////////////////////////////functions for A/D convert//////////////////*/
//for c505c
void ad_init(unsigned char ChSelect)
{
SYSCON |= 0x10;
P1ANA=ChSelect; //P1.0~P1.2 used as analog input port
SYSCON &= 0xef;
ADM=0;
ADCON1|=0xb0;
}
unsigned int ad_single(unsigned char ch)
{
unsigned int ad_value,ad_value1;
unsigned char i=100;
ADCON0=ADCON0 & 0xf8 | ch;
ADDATL=0x00; //start AD
while(i!=0)
{
if(!BSY)
{
ad_value =ADDATH;
ad_value1 =ADDATL;
ad_value = (ad_value<<2) | (ad_value1>>6);
i=0; //if convertion over read the value,end loop
}
else
{
i--; //if converting polling several times
}
}
return ad_value;
}
//******************************************************************************************
//function: float sensor_value(unsigned int Rs1,unsigned int ad);
//description: calculate the sensor's resistance by given parameter.
//parameter: Rs1---resistance of the sensor bridge;
// ad---the A/D result
//return: the sensor's resistance.
//******************************************************************************************
float sensor_value(float Rs1,float ad)
{
float r;
r=Rs1*ad*VREF*8.6/3;
r=r/(VCC*AD_FULL_VALUE-ad*VREF*8.6/3);
if(r<0) r=0;
if(r>1000) r=1000;
return(r);
}
///////////////////////operations about X5045///////////////////////////////
//*****************************************************************************************
//function: unsigned char read_8bit(void);
//description: this function read 8 bit from x5045
//parameter: none
//return: the read byte
//*****************************************************************************************
unsigned char read_8bit(void)
{
unsigned char ReadTemp=0;
unsigned char i;
for(i=0;i<8;i++)//read 8 bit
{
sck5045=1;
_nop_();
sck5045=0;
ReadTemp=(ReadTemp<<1) | (unsigned char)so5045;
_nop_();
}
return(ReadTemp);
}
//*****************************************************************************************
//function: void write_8bit(unsigned char WriteValue);
//description: this function write 8 bit to x5045
//parameter: the value need to be write
//return: none
//*****************************************************************************************
void write_8bit(unsigned char WriteValue)
{
unsigned char TempValue;
unsigned char i;
for(i=0;i<8;i++)//
{
sck5045=0; //
TempValue=(WriteValue<<i) & 0x80;
if(TempValue>=0x80)
{
si5045=1;
}
else
{
si5045=0;
}
_nop_();
sck5045=1;
_nop_();
}
}
//*****************************************************************************************
//function: void write_8bit1(unsigned char WriteValue);
//description: this function write 8 bit to x5045
//parameter: the value need to be write
//return: none
//*****************************************************************************************
void write_8bit1(unsigned char WriteValue)
{
unsigned char TempValue;
unsigned char i;
for(i=0;i<8;i++)//
{
sck5045=0; //
TempValue=(WriteValue<<i) & 0x80;
if(TempValue>=0x80)
{
si5045=1;
}
else
{
si5045=0;
}
_nop_();
sck5045=1;
_nop_();
}
}
//*****************************************************************************************
//function: void init_5045wdt(unsigned char WatchDogTime);
//description: this function initialize the x5045 watch dog timer
//parameter: watch dog time
//return: none
//*****************************************************************************************
void init_5045wdt(unsigned char WatchDogTime)
{
unsigned int nopp;
////write watch dog time
cs5045=0;
write_8bit(WREN);//send write enable command
cs5045=1;
_nop_();
cs5045=0;
_nop_();
write_8bit(WRSR);//send write status register command
write_8bit(WatchDogTime);//write watch dog time
_nop_();
cs5045=1;
for(nopp=0;nopp<4000;nopp++)
{
;
}
write_8bit(WRDI);//send write disable command
}
//*****************************************************************************************
//function: void write_byte(unsigned char address,unsigned char content);
//description: this function write one byte to x5045
//parameter: address---address of memory need to be write ;content---content need to be write
//return: none
//*****************************************************************************************
void write_byte(unsigned char address,unsigned char content)
{
unsigned int nopp;
////write one byte to 00-ffh address
cs5045=0;
_nop_();
write_8bit(WREN);//send write enable command
_nop_();
cs5045=1;
_nop_();
cs5045=0;
_nop_();
write_8bit(WRITE0);//send write command
write_8bit(address);//send write address
write_8bit(content);//write content
_nop_();
cs5045=1;
for(nopp=0;nopp<4000;nopp++)
{
;
}
cs5045=0;
_nop_();
write_8bit(WRDI);//send write disable command
cs5045=0;
_nop_();
}
//*****************************************************************************************
//function: unsigned char read_byte(unsigned char address);
//description: this function read one byte from x5045
//parameter: address---address of memory need to be read
//return: the read content.
//*****************************************************************************************
unsigned char read_byte(unsigned char address)
{
unsigned char content;
cs5045=0;
write_8bit(READ0);//send read command
write_8bit(address);//send address
content=read_8bit();//read one byte
_nop_();
cs5045=1;
return(content);
}
//*****************************************************************************************
//function: void write_page_float4(unsigned char StartAddr,float value);
//description: this function write float data to x5045,the first byte address is specified
//parameter: StartAddr---the first address of the unit will be write;value---value to be write
//return: none
//*****************************************************************************************
void write_page_float4(unsigned char StartAddr,float value)//write 4 byte float data to x5045
{ //address from the parametre StartAddr
union {float x1;unsigned char x2[4];}store;
unsigned int nopp;
////write one byte to 00-ffh address
store.x1=value;
cs5045=0;
_nop_();
write_8bit(WREN);//send write enable command
_nop_();
cs5045=1;
_nop_();
cs5045=0;
_nop_();
write_8bit(WRITE0);//send write command
write_8bit(StartAddr);//send write address
write_8bit(store.x2[0]);
write_8bit(store.x2[1]);
write_8bit(store.x2[2]);
write_8bit(store.x2[3]);
_nop_();
cs5045=1;
for(nopp=0;nopp<4000;nopp++)
{
;
}
cs5045=0;
_nop_();
write_8bit(WRDI);//send write disable command
_nop_();
cs5045=1;
}
//*****************************************************************************************
//function: void write_page_array12(unsigned char StartAddr,unsigned char *p)
//description: this function write 12 byte data to x5045,the first byte address is specified
//parameter: StartAddr---the first address of the unit will be write;value---value to be write
// p---pointer to array
//return: none
//*****************************************************************************************
void write_page_array12(unsigned char StartAddr,unsigned char *p)//write 12 byte data to x5045
{ //address from the parametre StartAddr
unsigned int nopp;
unsigned char i;
////write one byte to 00-ffh address
cs5045=0;
_nop_();
write_8bit(WREN);//send write enable command
_nop_();
cs5045=1;
_nop_();
cs5045=0;
_nop_();
write_8bit(WRITE0);//send write command
write_8bit(StartAddr);//send write address
for(i=0;i<12;i++)
{
write_8bit(*(p+i));
}
_nop_();
cs5045=1;
for(nopp=0;nopp<4000;nopp++)
{
;
}
write_8bit(WRDI);//send write disable command
}
//*****************************************************************************************
//function: void write_page_array4(unsigned char StartAddr,unsigned char *p)
//description: this function write 4 byte data to x5045,the first byte address is specified
//parameter: StartAddr---the first address of the unit will be write;value---value to be write
// p---pointer to array
//return: none
//*****************************************************************************************
/*
void write_page_array4(unsigned char StartAddr,unsigned char *p)//write 12 byte data to x5045
{ //address from the parametre StartAddr
unsigned int nopp;
unsigned char i;
////write one byte to 00-ffh address
cs5045=0;
_nop_();
write_8bit(WREN);//send write enable command
_nop_();
cs5045=1;
_nop_();
cs5045=0;
_nop_();
write_8bit(WRITE0);//send write command
write_8bit(StartAddr);//send write address
for(i=0;i<4;i++)
{
write_8bit(*(p+i));
}
_nop_();
cs5045=1;
for(nopp=0;nopp<4000;nopp++)
{
;
}
write_8bit(WRDI);//send write disable command
}
*/
//*****************************************************************************************
//function: void write1_page_array4(unsigned char StartAddr,unsigned char *p)
//description: this function write 4 byte data to x5045,the first byte address is specified
//parameter: StartAddr---the first address of the unit will be write;value---value to be write
// p---pointer to array
//return: none
//*****************************************************************************************
void write1_page_array4(unsigned char StartAddr,unsigned char *p)//write 12 byte data to x5045
{ //address from the parametre StartAddr
unsigned int nopp;
unsigned char i;
////write one byte to 00-ffh address
cs5045=0;
_nop_();
write_8bit(WREN);//send write enable command
_nop_();
cs5045=1;
_nop_();
cs5045=0;
_nop_();
write_8bit(WRITE0);//send write command
write_8bit(StartAddr);//send write address
for(i=0;i<4;i++)
{
write_8bit(*(p+i));
}
_nop_();
cs5045=1;
for(nopp=0;nopp<4000;nopp++)
{
;
}
write_8bit(WRDI);//send write disable command
}
//*****************************************************************************************
//function: float read_page_float4(unsigned char FirstAddr);
//description: this function read float data from x5045,the first byte address is specified
//parameter: addr---the first address of the unit will be read;
//return: the storing float data
//*****************************************************************************************
float read_page_float4(unsigned char FirstAddr)
{
union{float x1;unsigned char x2[4];}ReadValue;
cs5045=0;
write_8bit(READ0);//send read command
write_8bit(FirstAddr);//send address
ReadValue.x2[0]=read_8bit();//read one byte
ReadValue.x2[1]=read_8bit();//read one byte
ReadValue.x2[2]=read_8bit();//read one byte
ReadValue.x2[3]=read_8bit();//read one byte
_nop_();
cs5045=1;
return(ReadValue.x1);
}
float read1_page_float4(unsigned char FirstAddr)
{
union{float x1;unsigned char x2[4];}ReadValue;
cs5045=0;
write_8bit(READ0);//send read command
write_8bit(FirstAddr);//send address
ReadValue.x2[0]=read_8bit();//read one byte
ReadValue.x2[1]=read_8bit();//read one byte
ReadValue.x2[2]=read_8bit();//read one byte
ReadValue.x2[3]=read_8bit();//read one byte
_nop_();
cs5045=1;
return(ReadValue.x1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -