📄 weight.c
字号:
//display
DelayMsec(200);
DisplayDataEx(index, *value, dotNum, ucBlinkBit);
DelayMsec(250);
DisplayDataEx(index, *value, dotNum, 6); //data blink bright
//Add Key
if(K_ADD == 0)
{
DelayMsec(30);
if(K_ADD == 0) //make sure Add Key is Low
{
KeyAddB(value);
DisplayData(*value, dotNum, 6); //data blink bright
}
}
// Shift key
if(K_SHIFT == 0) //shift Key is Low
{
DelayMsec(30);
if(K_SHIFT == 0) //make sure shift Key is Low
{
KeyShift(bitNum);
///////////////////////////
DisplayData(*value, dotNum, 6); //data blink dark
//wait for shift up
while(1)
{
DelayMsec(60);
if(K_SHIFT == 1)
{
DelayMsec(30);
if(K_SHIFT == 1)
break;
}
}
}
}
// Set key
if(K_SET == 0) //Key set is low(jump out)
{
DelayMsec(30);
if(K_SET == 0)
{
while(1) //wait for Key set high
{
DelayMsec(60);
if(K_SET == 1)
{
DelayMsec(30);
if(K_SET == 1)
return;
}
}
}
}
}
}
////////////////////////
void KeyAddA(WORD *uiData)
{
WORD uiDataTmp = *uiData;
BYTE ucTmp;
//cycle add the blink bit
switch(ucBlinkBit)
{
case 0:
ucTmp = uiDataTmp%10;
uiDataTmp -= ucTmp;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
uiDataTmp += ucTmp;
break;
case 1:
ucTmp = (uiDataTmp/10)%10;
uiDataTmp -= ucTmp*10;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
uiDataTmp += ucTmp*10;
break;
case 2:
ucTmp = (uiDataTmp/100)%10;
uiDataTmp -= ucTmp*100;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
uiDataTmp += ucTmp*100;
break;
case 3:
ucTmp = (uiDataTmp/1000)%10;
uiDataTmp -= ucTmp*1000;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
uiDataTmp += ucTmp*1000;
break;
case 4:
ucTmp = (uiDataTmp/10000)%10;
uiDataTmp -= ucTmp*10000;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
uiDataTmp += ucTmp*10000;
break;
default:
break;
}
*uiData = uiDataTmp;
}
void KeyAddB(BYTE * value)
{
BYTE ucTmp, ucDetectTmp = *value;
//cycle add the blink bit
switch(ucBlinkBit)
{
case 0:
ucTmp = ucDetectTmp%10;
ucDetectTmp -= ucTmp;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
ucDetectTmp += ucTmp;
break;
case 1:
ucTmp = (ucDetectTmp/10)%10;
ucDetectTmp -= ucTmp*10;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
ucDetectTmp += ucTmp*10;
break;
case 2:
ucTmp = (ucDetectTmp/100)%10;
ucDetectTmp -= ucTmp*100;
if(ucTmp == 9)
ucTmp = 0;
else
ucTmp += 1;
ucDetectTmp += ucTmp*100;
break;
default:
break;
}
*value = ucDetectTmp;
}
void KeyShift(BYTE bitNum)
{
//coeff only has three bit
if(bitNum == 0) //Num bit is 3
{
if(ucBlinkBit == 2)
ucBlinkBit = 0;
else
ucBlinkBit += 1;
}
else if(bitNum == 1) //Num bit is 5
{
if(ucBlinkBit == 4)
ucBlinkBit = 0;
else
ucBlinkBit += 1;
}
else //Num bit is 2
{
if(ucBlinkBit == 1)
ucBlinkBit = 0;
else
ucBlinkBit += 1;
}
}
////////////////////////////////////////////////////////////
//Parameters save as WORD format; address must increase by 2
/////////////////////////////////////////////////////////////
void ParamsSaveWord(WORD address, WORD value)
{
BYTE byteVal[2];
byteVal[0] = value >> 8;
byteVal[1] = value & 0x00FF;
x5045_wren_cmd();
x5045_page_write(address, byteVal, 2);
}
////////////////////////////////////////////////////////////
//Parameters Load as WORD format; address must increase by 2
/////////////////////////////////////////////////////////////
void ParamsLoadWord(WORD address, WORD *value)
{
BYTE byteVal[2];
x5045_page_read(address, byteVal, 2);
*value = byteVal[0];
*value = (*value) << 8;
(*value) += byteVal[1];
}
/////////////////////////////////////////////////////////////
//Load all parameters from machine start
//////////////////////////////////////////////////////////////
void ParamsLoadALL()
{
//read
ParamsLoadWord(0, &uiDataHighLimit);//read high limit parameters from X5045
ParamsLoadWord(2, &uiDataLowLimit); //read low limit parameters from X5045
ParamsLoadWord(4, &uiDataCoeff); //read low limit parameters from X5045
//read C_PAR parameters
x5045_page_read(6, &ucDetectHighTime, 1); //
x5045_page_read(7, &ucDetectHighSpan, 1); //
x5045_page_read(8, &ucDetectLowTime, 1); //
x5045_page_read(9, &ucDetectLowSpan, 1); //
x5045_page_read(10, &ucZeroScope, 1); //
x5045_page_read(11, &ucCheckScope, 1); //
x5045_page_read(12, &ucStableScope, 1); //
//read ratio parameters
ParamsLoadWord(14, &uiRatioValL); //Ratio Low value
ParamsLoadWord(16, &uiRatioValH); //Ratio High value
x5045_page_read(18, &ucRatioPreDefIndex, 1); //Ratio High value
uiRatioVal = uiRatioValL - uiRatioValH;
}
////////////////////////////////////
//display a unsigned char
////////////////////////////////////
void DisplayByte(BYTE Alpha)
{
//SCON = 0;
SBUF = Alpha;
while(TI == 0)
;
TI = 0;
}
////////////////////////////////////////
//Display the parameters to the 7Seg tube:
////////////////////////////////////////
void DisplayAlpha(BYTE index)
{
SCON = 0;
switch(index)
{
case 1: //display "C_PAR"
DisplayByte(0x63);
DisplayByte(0xEF);
DisplayByte(0x31);
DisplayByte(0x11);
DisplayByte(0xF5);
break;
case 2: //diaply " SET "
DisplayByte(0xFF);
DisplayByte(0x49);
DisplayByte(0x61);
DisplayByte(0x73);
DisplayByte(0xFF);
break;
case 3: //display "LOAD "
DisplayByte(0xE3);
DisplayByte(0x03);
DisplayByte(0x11);
DisplayByte(0x85);
DisplayByte(0xFF);
break;
case 4: //display "H----"
DisplayByte(0x91);
DisplayByte(0x7F);
DisplayByte(0x7F);
DisplayByte(0x7F);
DisplayByte(0x7F);
break;
case 5: //display "L____"
DisplayByte(0xE3);
DisplayByte(0xEF);
DisplayByte(0xEF);
DisplayByte(0xEF);
DisplayByte(0xEF);
break;
default:
break;
}
}
////////////////////////////////////////
//Display the C_PAR parameters to the 7Seg tube
////////////////////////////////////////
void DisplayDataEx(BYTE index, BYTE ucRef, BYTE ucPosDot, BYTE ucPosBlink)
{
//set serial port to shift register
BYTE tmp[4];
char i = 0;
//compute five BCD code
for(i = 0; i < 3; i ++)
{
tmp[i] = ucRef%10;
ucRef = ucRef/10;
if((tmp[i] == 0) && (ucRef == 0))
tmp[i] = 10;
}
//////////////////////////////First two byte
DisplayByte(g7SegCodeDot[index]);
DisplayByte(g7SegCode[10]); //for making place
////////display through serial
SCON = 0;
for(i = 2; i >= 0; i --) //data
{
if(ucPosDot == i)
{
if(ucPosBlink == i)
tmp[3] = g7SegCodeDot[10]; //Blink with dot data
else
tmp[3] = g7SegCodeDot[tmp[i]];
}
else
{
if(ucPosBlink == i)
tmp[3] = g7SegCode[10]; //Blink with no dot data
else
tmp[3] = g7SegCode[tmp[i]];
}
SBUF = tmp[3];
while(TI == 0)
;
TI = 0; //display byte
}
}
////////////////////////////////////////
//Display the weight to the 7Seg tube
////////////////////////////////////////
void DisplayData(WORD uiRef, BYTE ucPosDot, BYTE ucPosBlink)
{
//set serial port to shift register
char i;
BYTE index[6];
//compute five BCD code
for(i = 0; i < 5; i ++)
{
index[i] = uiRef%10;
uiRef = uiRef/10;
if((index[i] == 0) && (uiRef == 0))
index[i] = 10;
}
////////display through serial
SCON = 0;
for(i = 4; i >= 0; i --)
{
if(ucPosDot == i)
{
if(ucPosBlink == i)
index[5] = g7SegCodeDot[10]; //???Blink with dot data
else
index[5] = g7SegCodeDot[index[i]];
}
else
{
if(ucPosBlink == i)
index[5] = g7SegCode[10]; //Blink with no dot data
else
index[5] = g7SegCode[index[i]];
}
SBUF = index[5];
while(TI == 0)
;
TI = 0; //display byte
}
}
/////////////////////////////////////////
//Read AD interrupt
/////////////////////////////////////////
void IsrReadAD(void) interrupt 0
{
WORD i, uiADData;
//AD_SCLK = 0; //idle
AD_CS = 0; //select the ad
////////////////first high bit
i ++;
AD_SCLK = 1; //Up
if(AD_DOUT == 1)
uiADData = 1;
else
uiADData = 0;
AD_SCLK = 0; //DOWN
//next 15 bit
for(i = 0; i < 14; i ++)
{
uiADData <<= 1;
AD_SCLK = 1; //Up
if(AD_DOUT == 1)
uiADData = uiADData + 1;
AD_SCLK = 0; //DOWN
}
AD_CS = 1; //the data out line in high-impedance
/////////////make total ad data
ulADTotal = ulADTotal + uiADData;
uiNumIndex ++;
}
////////////////////////////////////////
//function: delay timeMsec ms[function contains watchdog]
//Parameters: timeMsec, the unit is ms
////////////////////////////////////////
void DelayMsec(WORD timeMsec)
{
int i, j;
for(i = 0; i < timeMsec; i ++)
{
//if time too long, must give the watchdog food;
if(i % 50 == 0)
x5045_rst_wdog();
for(j = 0; j < 50; j ++)
;
}
}
///////////////////////////////////////////////////////////////////////////
void x5045_outbyte(BYTE wr_data)
{
BYTE loop;
BYTE tmp_x;
tmp_x=1;
for(loop=8;loop>0;loop--)
{
WD_SCK=0;
if((wr_data&(tmp_x<<(loop-1)))!=0)
WD_SI=1;
else
WD_SI=0;
WD_SCK=1;
}
WD_SI=0;
}
BYTE x5045_inbyte(void)
{
BYTE loop;
BYTE tmp_x;
tmp_x=0;
for(loop=8;loop>0;loop--)
{
WD_SCK=1;
WD_SCK=0;
tmp_x<<=1;
if(WD_SO!=0)
tmp_x+=1;
}
return tmp_x;
}
void x5045_wren_cmd()
{
WD_SCK=0;
WD_CS=0;
x5045_outbyte(WREN);
WD_SCK=0;
WD_CS=1;
}
BYTE x5045_rdsr_cmd()
{
BYTE tmp_x;
WD_SCK=0;
WD_CS=0;
x5045_outbyte(RSDR);
tmp_x=x5045_inbyte();
WD_SCK=0;
WD_CS=1;
return tmp_x;
}
void x5045_wip_poll(void)
{
BYTE loop;
BYTE tmp_x;
for(loop=0;loop<MAX_POLL;loop++)
{
tmp_x=x5045_rdsr_cmd();
if((tmp_x&0x01)==0)
return;
}
}
void x5045_wrsr_cmd()
{
WD_SCK=0;
WD_CS=0;
x5045_outbyte(WRSR);
x5045_outbyte(STATUS_REG);
WD_SCK=0;
WD_CS=1;
x5045_wip_poll();
}
void x5045_page_write(WORD address,BYTE *p,BYTE number)
{
BYTE loop;
BYTE d1,d2;
BYTE *k;
k=p;
WD_SCK=0;
WD_CS=0;
number&=0x1f;
d1=(address>>8)&0x01;
d1<<=3;
d1|=WRITE;
d2=(address&0x00ff);
x5045_outbyte(d1);
x5045_outbyte(d2);
for(loop=0;loop<number;loop++)
{
x5045_outbyte(*k);
k++;
}
WD_SCK=0;
WD_CS=1;
x5045_wip_poll();
return;
}
void x5045_page_read(WORD address,BYTE *p,BYTE number)
{
BYTE loop;
BYTE d1,d2;
BYTE *k;
k=p;
WD_SCK=0;
WD_CS=0;
number&=0x1f;
d1=(address>>8)&0x01;
d1<<=3;
d1|=READ;
d2=(address&0x00ff);
x5045_outbyte(d1);
x5045_outbyte(d2);
for(loop=0;loop<number;loop++)
{
*k=x5045_inbyte();
k++;
}
WD_SCK=0;
WD_CS=1;
return;
}
void x5045_rst_wdog()
{
//WD_WP=1;
WD_CS=0;
WD_CS=1;
//WD_WP=1;
return;
}
/////////////////////
void x5045_initial()
{
//WD_WP=1;
WD_CS=1;
WD_SO=1;
WD_SCK=0;
WD_SI=0;
x5045_wren_cmd();
x5045_wrsr_cmd();
//WD_WP=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -