?? host.c
字號:
/******************************************************************
程序名:host
功能:測量電流,并發(fā)數(shù)據(jù)發(fā)送給LED595程序,接受用戶鍵盤輸入跟設(shè)置。
創(chuàng)建時(shí)間:2004-3-30
作者:
******************************************************************/
#include <reg52.h>
#include <intrins.h>
#include "host.h"
#include "SEGCODE.h"
#define SIZE_OF_KEYBD_MENU 30 //菜單長度
//#define nop2() _nop_();_nop_();
KbdTabStruct code KeyTab[SIZE_OF_KEYBD_MENU] =
{
{0, 0, 0, 0, 1, (*OptionProg)},
{1, 5, 2, 0, 6, (*OptionAYS)},
{2, 1, 3, 0, 7, (*OptionIn_Out)},
{3, 2, 4, 0, 9, (*OptionALR)},
{4, 3, 5, 0, 11, (*OptionSecr)},
{5, 4, 1, 0, 12, (*OptionAdBd)},
{6, 6, 6, 1, 1, (*AutoZero)},
{7, 8, 8, 2, 14, (*InputX)},
{8, 7, 7, 2, 15, (*OutputX)},
{9, 10, 10, 3, 3, (*ALROFF)},
{10, 9, 9, 3, 16, (*ALRON)},
{11,11, 11, 4, 4, (*Passward)},
{12,13, 13, 5, 18, (*AddrX)},
{13,12, 12, 5, 19, (*BaudX)},
{14,14, 14, 7, 20, (*Inp)},
{15,15, 15, 8, 21, (*Oup)},
{16,17, 17, 10, 22, (*ALRLX)},
{17,16, 16, 10, 23, (*ALRHX)},
{18,18, 18, 12, 24, (*Address)},
{19,19, 19, 13, 25, (*Baud)},
{20,20, 20, 14, 7, (*SetInput)},
{21,21, 21, 15, 26, (*SetOutput)},
{22,22, 22, 16, 27, (*AL_L)},
{23,23, 23, 17, 28, (*AL_H)},
{24,24, 24, 18, 19, (*SetAddress)},
{25,25, 25, 19, 18, (*SetBaud)},
{26,26, 26, 21, 29, (*Point)},
{27,27, 27, 22, 23, (*SetAL_L)},
{28,28, 28, 23, 22, (*SetAL_H)},
{29,29, 29, 26, 8, (*SetPoint)}
};
/*25045操作子程序集*/
/*;*******************************************************************************************
*
;* Name: WREN_CMD
;* Description: Set write enable latch
;* Function: This routine sends the command to enable writes to the EEPROM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;* Register Usage: A
;*******************************************************************************************
*/
/*寫使能子程序*/
void wren_cmd(void)
{
uchar aa;
SCK=0;/* Bring SCK low */
CS=0;/* Bring /CS low */
aa=WREN_INST;
outbyt(aa);/* Send WREN instruction */
SCK=0;/* Bring SCK low */
CS=1;/* Bring /CS high */
}
/*;*******************************************************************************************
*
;* Name: WRDI_CMD
;* Description: Reset write enable latch
;* Function: This routine sends the command to disable writes to the EEPROM memory array or
;* status register
;* Calls: outbyt
;* Input: None
;* Outputs: None
;* Register Usage: A
;*******************************************************************************************
*/
/*寫使能復(fù)位子程序*/
//void wrdi_cmd(void)
//{
// uchar aa;
// SCK=0;/* Bring SCK low */
// CS=0;/* Bring /CS low */
// aa=WRDI_INST;
// outbyt(aa);/* Send WRDI instruction */
// SCK=0;/* Bring SCK low */
// CS=1;/* Bring /CS high */
//}
/*;*******************************************************************************************
*
;* Name: WRSR_CMD
;* Description: Write Status Register
;* Function: This routine sends the command to write the WD0, WD1, BP0 and BP0 EEPROM
;* bits in the status register
;* Calls: outbyt, wip_poll
;* Input: None
;* Outputs: None
;* Register Usage: A
;*******************************************************************************************
*/
/*寫狀態(tài)寄存器子程序*/
void wrsr_cmd(void)
{
uchar aa;
SCK=0;/* Bring SCK low */
CS=0;/* Bring /CS low */
aa=WRSR_INST;
outbyt(aa) ;/* Send WRSR instruction */
aa=STATUS_REG;
outbyt(aa);/* Send status register */
SCK=0;/* Bring SCK low */
CS=1;/* Bring /CS high */
wip_poll();/* Poll for completion of write cycle */
}
/*;*******************************************************************************************
*
;* Name: RDSR_CMD
;* Description: Read Status Register
;* Function: This routine sends the command to read the status register
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: A = status registerXicor Application Note AN21
;* Register Usage: A
;*******************************************************************************************
*/
/*讀狀態(tài)寄存器,讀出的數(shù)據(jù)放入到aa中*/
uchar rdsr_cmd (void)
{
uchar aa;
SCK=0;
CS=0;
aa=RDSR_INST;
outbyt(aa);
aa=inputbyt();
SCK=0;
CS=1;
return aa;
}
/*;*******************************************************************************************
*
;* Name: BYTE_WRITE
;* Description: Single Byte Write
;* Function: This routine sends the command to write a single byte to the EEPROM memory
array
;* Calls: outbyt, wip_poll
;* Input: None
;* Outputs: None
;* Register Usage: A, B
;*******************************************************************************************
*/
/*字節(jié)寫入,aa為寫入的數(shù)據(jù),dd為寫入的地址,對于25045而言為000-1FF*/
void byte_write(aa,dd)
uchar aa;
uint dd;
{
SCK=0;
CS=0;
outbyt((uchar)(dd>255?(0x08|WRITE_INST):WRITE_INST));
//outbyt((((uchar)(dd-0XFF))<<3)|WRITE_INST);/* Send WRITE instruction including MSB of address */
/*將高位地址左移3位與寫入先導(dǎo)字相或,得到正確的先導(dǎo)字寫入25045*/
outbyt((uchar)(dd&0x00ff));
/*輸出低位地址到25045*/
outbyt(aa);
/*寫入數(shù)據(jù)到25045的對應(yīng)單元*/
SCK=0;
CS=1;
wip_poll();
/*檢測是否寫完*/
}
/*;*******************************************************************************************
*
;* Name: BYTE_READ
;* Description: Single Byte Read
;* Function: This routine sends the command to read a single byte from the EEPROM memory
array
;* Calls: outbyt, inputbyt
;* Input: None
;* Outputs: A = read byte
;* Register Usage: A, BXicor Application Note AN21
;*******************************************************************************************
*/
/*字節(jié)讀出,其中dd為讀出的地址,返回的值為讀出的數(shù)據(jù)*/
uchar byte_read(dd)
uint dd;
{
uchar cc;
SCK=0;
CS=0;
outbyt((uchar)(dd>255?(0x08|READ_INST):READ_INST));
//outbyt((((uchar)(dd-0XFF))<<3)|READ_INST);/* Send READ_INST instruction including MSB of address */
/*將高位地址左移3位與讀出先導(dǎo)字相或,得到正確的先導(dǎo)字寫入25045*/
outbyt((uchar)(dd&0x00ff));
/*輸出低位地址到25045*/
cc=inputbyt();/*得到讀出的數(shù)據(jù)*/
SCK=0;
CS=1;
return cc;
}
/*;*******************************************************************************************
*
;* Name: PAGE_WRITE
;* Description: Page Write
;* Function: This routine sends the command to write three consecutive bytes to the EEPROM
;* memory array using page mode
;* Calls: outbyt, wip_poll
;* Input: None
;* Outputs: None
;* Register Usage: A, B
;*******************************************************************************************
*/
/*頁面寫入,其中aa1,aa2,aa3,aa4為需要寫入的4個(gè)數(shù)據(jù)(最大也就只能一次寫入4個(gè)字,dd為寫入的首地址*/
void page_write(aa1,aa2,aa3,aa4,dd)
uchar aa1,aa2,aa3,aa4;
uint dd;
{
SCK=0;
CS=0;
outbyt((uchar)(dd>255?(0x08|WRITE_INST):WRITE_INST));
//outbyt((((uchar)(dd-0XFF))<<3)|WRITE_INST);/* Send WRITE instruction including MSB of address */
/*將高位地址左移3位與寫入先導(dǎo)字相或,得到正確的先導(dǎo)字寫入25045*/
outbyt((uchar)(dd&0x00ff));
/*寫入低位地址到25045*/
outbyt(aa1);
/*寫入數(shù)據(jù)1到25045的對應(yīng)單元*/
outbyt(aa2);
/*寫入數(shù)據(jù)2到25045的對應(yīng)單元*/
outbyt(aa3);
/*寫入數(shù)據(jù)3到25045的對應(yīng)單元*/
outbyt(aa4);
/*寫入數(shù)據(jù)4到25045的對應(yīng)單元*/
SCK=0;
CS=1;
wip_poll();
}
/*;*******************************************************************************************
*
;* Name: RST_WDOG
;* Description: Reset Watchdog Timer
;* Function: This routine resets the watchdog timer without sending a command
;* Calls: None
;* Input: None
;* Outputs: None
;* Register Usage: None
;*******************************************************************************************
*/
/*復(fù)位DOG*/
void rst_wdog (void)
{
CS=0;
CS=1;
}
/*;*******************************************************************************************
*
;* Name: WIP_POLL
;* Description: Write-In-Progress Polling
;* Function: This routine polls for completion of a nonvolatile write cycle by examining the
;* WIP bit of the status register
;* Calls: rdsr_cmdXicor Application Note AN21
;* Input: None
;* Outputs: None
;* Register Usage: R1, A
;*******************************************************************************************
*/
/*檢測寫入的過程是否結(jié)束*/
void wip_poll(void)
{
uchar aa;
uchar idata my_flag;
for (aa=1;aa<MAX_POLL;aa++)
{
my_flag=rdsr_cmd();
if ((my_flag&&0x01)==0) {aa=MAX_POLL;}/*判斷是否WIP=0,即判斷是否寫入過程已經(jīng)結(jié)束,若結(jié)束就跳出,否則繼續(xù)等待直到達(dá)到最大記數(shù)值*/
}
}
/*;*******************************************************************************************
*
;* Name: OUTBYT
;* Description: Sends byte to EEPROM
;* Function: This routine shifts out a byte, starting with the MSB, to the EEPROM
;* Calls: None
;* Input: A = byte to be sent
;* Outputs: None
;* Register Usage: R0, A
;*******************************************************************************************
*/
/*輸出一個(gè)數(shù)據(jù)到25045,此數(shù)據(jù)可能為地址,先導(dǎo)字,寫入的數(shù)據(jù)等*/
void outbyt(aa)
uchar aa;
{
uchar Data, i;
Data = aa;
for (i=0; i<8; i++)
{
SCK=0;
SI = (bit)(Data & 0x80);
Data = Data << 1;
SCK=1;
}
SI=0;/*使SI處于確定的狀態(tài)*/
}
/*;*******************************************************************************************
*
;* Name: INPUTBYT
;* Description: Recieves byte from EEPROM
;* Function: This routine recieves a byte, MSB first, from the EEPROM
;* Calls: None
;* Input: None
;* Outputs: A = recieved byte
;* Register Usage: R0, A
;*******************************************************************************************
*/
/*得到一個(gè)數(shù)據(jù),此數(shù)據(jù)可能為狀態(tài)寄存器數(shù)據(jù),讀出的單元數(shù)據(jù)等*/
uchar inputbyt(void)
{
uchar uData;
char i;
for (i=0; i<8; i++)
{
SCK=1;
SCK=0;
uData *= 2;
if (SO)
uData ++;
}
return uData;
}
void WriteParams() //保存參數(shù)
{
//保存零位到地址0;
wren_cmd();
page_write(Param.wZero.b[0], Param.wZero.b[1],
Param.wZero.b[2], Param.wZero.b[3], 0);
//保存校準(zhǔn)信號到地址4;
wren_cmd();
page_write(Param.wInCount.b[0], Param.wInCount.b[1],
Param.wInCount.b[2], Param.wInCount.b[3], 4);
//保存輸入值到地址8, 輸出值到10;
wren_cmd();
page_write(Param.wInput.b[0], Param.wInput.b[1],
Param.wOutput.b[0], Param.wOutput.b[1], 8);
//保存報(bào)警下限到地址12,報(bào)警上限到地址14;
wren_cmd();
page_write(Param.wALR_L.b[0], Param.wALR_L.b[1],
Param.wALR_H.b[0], Param.wALR_H.b[1], 12);
//保存報(bào)警標(biāo)志位,波特率下標(biāo),小數(shù)點(diǎn)位置,地址到地址16
wren_cmd();
page_write(Param.bAlert, Param.BaudIndex,
Param.PointPos, Param.bAddress, 16);
//保存密碼下標(biāo)到地址20
wren_cmd();
byte_write(Param.Password.b[0], 20);
wren_cmd();
byte_write(Param.Password.b[1], 21);
}
void ReadParams() //讀參數(shù)
{
uchar i;
//讀零位
for (i=0; i<4; i++)
Param.wZero.b[i] = byte_read(i);
//讀校準(zhǔn)信號
for (i=0; i<4; i++)
Param.wInCount.b[i] = byte_read(i+4);
//讀輸入值
Param.wInput.b[0] = byte_read(8);
Param.wInput.b[1] = byte_read(9);
//讀輸出值
Param.wOutput.b[0] = byte_read(10);
Param.wOutput.b[1] = byte_read(11);
//讀報(bào)警下限
Param.wALR_L.b[0] = byte_read(12);
Param.wALR_L.b[1] = byte_read(13);
//讀報(bào)警上限
Param.wALR_H.b[0] = byte_read(14);
Param.wALR_H.b[1] = byte_read(15);
//讀報(bào)警標(biāo)志位
Param.bAlert = byte_read(16);
//讀波特率下標(biāo)
Param.BaudIndex = byte_read(17);
//讀小數(shù)點(diǎn)位置
Param.PointPos = byte_read(18);
//讀地址
Param.bAddress = byte_read(19);
//讀密碼
Param.Password.b[0] = byte_read(20);
Param.Password.b[1] = byte_read(21);
}
void Delay(uint utime, uint utime2) reentrant
{
uint utimein;
while (utime --)
{
rst_wdog();
utimein = utime2;
while(utimein --)
rst_wdog();
}
return;
}
void I_Send(byte Data)
{
register byte i;
//發(fā)送8位數(shù)據(jù)
for (i=0; i<8; i++)
{
CLK = 0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
DATA = (bit)(Data & 0x80);
Data = Data << 1;
CLK = 1;
}
CLK = 0;
return;
}
//顯示數(shù)據(jù)
void Display(dword Data, uchar PointIndex) reentrant
{
byte Value, i;
uchar Array[5];
//換算段碼
for (i=0; i<5; i++)
{
Value = Data % 10;
Data = Data / 10;
if ((i == 4 - PointIndex) && (PointIndex < 4))
Array[i] = uLEDChar[Value] | 0x40;
else
Array[i] = uLEDChar[Value];
rst_wdog();
}
//去除開始的0
while (i--)
{
if ((Array[i] == 0x3f) && (i > 4-PointIndex))
{
Array[i] = 0x00;
rst_wdog();
}
else
{
rst_wdog();
break;
}
}
TRG = 1;
TRG = 0;
I_Send('T');
Delay(1, 5);
for (i=0; i<5; i++)
{
I_Send(Array[i]);
rst_wdog();
}
return;
}
void GetKeyInput()
{
uchar KeyValue;
KeyValue = P2 & 0xF0; //去掉低4位
if (KeyValue != 0xF0)
{
flag = 0;
Delay(15, 500);
KeyValue = P2 & 0xF0;
while ((P2 & 0xF0) != 0xF0)
rst_wdog();
switch (KeyValue)
{
case 0xE0: //左鍵
KeyFuncIndex = KeyTab[KeyFuncIndex].KeyLeftState;
if (KeyFuncIndex == 0)
flag = 1;
break;
case 0xD0: //右鍵
KeyFuncIndex = KeyTab[KeyFuncIndex].KeyRightState;
if (KeyFuncIndex == 0)
flag = 1;
break;
case 0xB0: //MENU
KeyFuncIndex = KeyTab[KeyFuncIndex].KeyMenuState;
break;
case 0x70: //回車
KeyFuncIndex = KeyTab[KeyFuncIndex].KeyEnterState;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -