?? nrf905c51demo.txt
字號:
SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] ++;//壓鍵計數(shù)
if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] == 2) {//短壓鍵
SystemBuffers.WaitTime = 0;
KeyCommandExec(0, SystemBuffers.TimeCount & 0x03);//單擊鍵Key00()~Key03()
}
else if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] >= SysPushKeyTime) {//長壓鍵150 * 20mS = 3。0S
SystemBuffers.WaitTime = 0;
KeyCommandExec(2, SystemBuffers.TimeCount & 0x03);//長壓鍵Key20()~Key23()
SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] = 3;//清除長壓鍵計數(shù)器(躲過單擊鍵)
}
}
else {//無鍵壓下(包括放鍵)
if (SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] >= 2) {//放鍵事件
SystemBuffers.WaitTime = 0;
KeyCommandExec(1, SystemBuffers.TimeCount & 0x03);//鍵釋放Key10()~Key13()
}
else {
nRFReadWrite();//無線收發(fā)模塊
}
SystemBuffers.KeyCount[SystemBuffers.TimeCount & 0x03] = 0;//清除壓鍵計數(shù)器
}
SystemBuffers.TimeCount++;//繼續(xù)下1個5mS延時
if ((SystemBuffers.TimeCount & ~0x80) >= 100){//5mS*100=500mS
SystemBuffers.WaitTime ++;
if (SystemBuffers.WaitTime >= SysWaitTime) {//0.5*20=10S
P0 = 0xff;//關(guān)顯示
SystemBuffers.WaitTime = 0;
}
SystemBuffers.TimeCount &= 0x80;
SystemBuffers.TimeCount ^= 0x80;//取反閃爍位
}
}
/*------------------------------------*/
void sioproc() interrupt SIO_VECTOR using 1
{
unsigned char i;
if (RI) {//接收中斷
RI = 0;
if (SioBuffers.SioRXCount && (SioBuffers.SioRXCount <= 32)) {//允許串口接收數(shù)據(jù)
SioBuffers.SioRxBuff[32 - SioBuffers.SioRXCount] = SBUF;
SioBuffers.SioRXCount --;
if (SioBuffers.SioRXCount == 0) {
for (i = 0; i < 32; i ++) {
SioBuffers.SioTxBuff[i] = SioBuffers.SioRxBuff[i];//自收自發(fā)
SystemBuffers.nRFTxBuff[i] = SioBuffers.SioTxBuff[i];//更新無線發(fā)送數(shù)據(jù)
}
/*
switch(SystemBuffers.nRFTxBuff[9]) {
case 0x81: LED0 = 0;break;
case 0x82: LED1 = 0;break;
case 0x83: LED2 = 0;break;
case 0x84: LED3 = 0;break;
case 0x03: LED2 = 0;break;
case 0x04: LED3 = 0;break;
}
*/
SioBuffers.SioRXCount = 32;//串口接收緩沖區(qū)滿
SioBuffers.SioTXCount = 32;//串口發(fā)送緩沖區(qū)滿
TI = 1;//立即發(fā)送
SystemBuffers.nRFTxCount = 32;//要求間接無線發(fā)送
}
}
else SioBuffers.SioRXCount = 0;//不允許串口接收數(shù)據(jù)
}
if (TI) {//發(fā)送中斷
TI = 0;
if (SioBuffers.SioTXCount && (SioBuffers.SioTXCount <= 32)) {//允許串口發(fā)送數(shù)據(jù)
SBUF = SioBuffers.SioTxBuff[32 - SioBuffers.SioTXCount];//串口顯示
SioBuffers.SioTXCount --;
}
else SioBuffers.SioTXCount = 0;//不允許串口發(fā)送數(shù)據(jù)
}
}
//執(zhí)行鍵盤命令
void KeyCommandExec(unsigned char commgroup, unsigned char commnum) using 0
{
unsigned int i;
unsigned key;
code void *KeyArray[3][4] = {//二維函數(shù)數(shù)組指針陣列表(散轉(zhuǎn)命令地址表)
{(void *)Key00 + 0x5b7d, (void *)Key01 + 0xa6ea, (void *)Key02 + 0xf157, (void *)Key03 + 0x4cc4},
{(void *)Key10 + 0x9731, (void *)Key11 + 0xe2ae, (void *)Key12 + 0x3d1b, (void *)Key13 + 0x8888},
{(void *)Key20 + 0xd3f5, (void *)Key21 + 0x2e62, (void *)Key22 + 0x79df, (void *)Key23 + 0xc44c}
};
if ((commgroup < 3) && (commnum < 4)) {
ClrWdt();//清除看門狗計數(shù)器
key = commgroup * 4 + commnum + 1;
i = ((key * 53 & 0xf) * 0x1000)+ ((key * 43 & 0xf) * 0x100) + ((key * 23 & 0xf) * 0x10) + (key * 13 & 0xf);
_icall_((void *)KeyArray[commgroup][commnum] - i);//鍵盤命令散轉(zhuǎn)
}
}
void BeepSetup(unsigned char mode)
{
// BeepBuffers.BeepCount = 1;
// BeepBuffers.BeepMode = mode;
// BeepBuffers.BeepValue = mode;
}
/*----------------------------------
蜂鳴器100mS發(fā)聲程序
----------------------------------*/
void Beep(void)//蜂鳴器發(fā)聲
{
// BeepBuffers.BeepCount --;
// if (BeepBuffers.BeepCount == 0){
// BeepBuffers.BeepCount = 0;
// BeepBuffers.BeepValue = BeepBuffers.BeepMode;
// if (BeepBuffers.BeepValue >= 0x80)//單聲
// BeepBuffers.BeepMode = 0; //單聲清除
// }
// BeepBuffers.BeepValue <<= 1;
// CY = ~CY; //硬件反向
// BeepOut = CY;//發(fā)聲
}
void KeyInit(void) using 0//鍵盤初始化
{
unsigned char i;
for (i = 0; i < 4; i++){
SystemBuffers.KeyCount[i] = 0;//清除鍵盤壓鍵次數(shù)緩沖區(qū)
}
}
//收發(fā)無線讀寫模塊
void nRFReadWrite(void)
{
unsigned char i;
unsigned int t;
if (!TX_EN && DR && AM) {//收到數(shù)據(jù)
ClrWdt();
TRX_CE = 0;//設(shè)置nRF905為待機方式
_nop_();
CSN=0; // Spi enable for write a spi command
SpiReadWrite(RRP); // Read payload command
for (i = 0; i < 32; i++)
{
SystemBuffers.nRFRxBuff[i]=SpiReadWrite(0xff);//讀入無線接收緩沖區(qū)
}
CSN=1; // Disable spi
TRX_CE = !TX_EN;//再回到原來的無線收發(fā)模式
// TRX_CE = 1;//再回到接收模式
switch(SystemBuffers.nRFRxBuff[9]) {
case 0x81: LED0 = 0;break;
case 0x82: LED1 = 0;break;
case 0x83: LED2 = 0;break;
case 0x84: LED3 = 0;break;
case 0x01: LED0 = 0;break;
case 0x02: LED1 = 0;break;
case 0x03: LED2 = 0;break;
case 0x04: LED3 = 0;break;
}
SystemBuffers.WaitTime = 0;
while(DR || AM);
SystemBuffers.nRFRxCount = 32;//收到32個字節(jié)數(shù)據(jù)
if (SioBuffers.SioTXCount == 0){//串口數(shù)據(jù)已發(fā)送完
for (i = 0; i < 32; i ++)
SioBuffers.SioTxBuff[i] = SystemBuffers.nRFRxBuff[i];//串口顯示
SioBuffers.SioTXCount = 32;//發(fā)送32個字節(jié)的顯示數(shù)據(jù)
TI = 1;//立即顯示
}
}
else if(!CD && (SystemBuffers.nRFRxCount == 32)) {//接收命令處理
SystemBuffers.nRFRxCount = 0;
for (i = 0; i < 4; i ++)
SystemBuffers.nRFTxBuff[i] = SystemBuffers.nRFRxBuff[i + 4];//寫入串口接收緩沖區(qū)
for (i = 4; i < 8; i ++)
SystemBuffers.nRFTxBuff[i] = SystemBuffers.nRFRxBuff[i - 4];//寫入串口接收緩沖區(qū)
for (i = 8; i < 32; i ++)
SystemBuffers.nRFTxBuff[i] = SystemBuffers.nRFRxBuff[i];//寫入串口接收緩沖區(qū)
if (SystemBuffers.nRFRxBuff[9] == 0x80) {//系統(tǒng)消息請求
SystemBuffers.nRFTxBuff[9] = 0x00;//主機系統(tǒng)消息應(yīng)答
ReadnRFConfig(SystemBuffers.nRFTxBuff);
SystemBuffers.nRFTxCount = 32;//主機要求間接無線發(fā)送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x81) {//子機請求
// LED0 = 0;//主機燈亮
SystemBuffers.nRFTxBuff[9] = 0x01;//主機應(yīng)答
SystemBuffers.nRFTxBuff[10] = 0x20;
SystemBuffers.nRFTxBuff[11] = 0x04;
SystemBuffers.nRFTxBuff[12] = 0x10;
SystemBuffers.nRFTxBuff[13] = 0x18;
SystemBuffers.nRFTxBuff[14] = 0x11;
SystemBuffers.nRFTxBuff[15] = 0x28;
SystemBuffers.nRFTxCount = 32;//主機要求間接無線發(fā)送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x82) {//子機請求
// LED1 = 0;//主機燈亮
SystemBuffers.nRFTxBuff[9] = 0x02;//主機應(yīng)答
SystemBuffers.nRFTxBuff[10] = 0x20;
SystemBuffers.nRFTxBuff[11] = 0x04;
SystemBuffers.nRFTxBuff[12] = 0x10;
SystemBuffers.nRFTxBuff[13] = 0x26;
SystemBuffers.nRFTxBuff[14] = 0x21;
SystemBuffers.nRFTxBuff[15] = 0x09;
SystemBuffers.nRFTxCount = 32;//主機要求間接無線發(fā)送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x83) {//子機變址請求
// LED2 = 0;//主機變址燈亮
SystemBuffers.nRFTxBuff[9] = 0x03;//主機變址應(yīng)答
SystemBuffers.nRFTxBuff[10] = TH0;//
SystemBuffers.nRFTxBuff[11] = TL0;
SystemBuffers.nRFTxBuff[12] = SystemBuffers.WaitTime;
SystemBuffers.nRFTxBuff[13] = SystemBuffers.TimeCount;
SystemBuffers.nRFTxCount = 32;//主機要求間接無線發(fā)送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x84) {//子機跳頻請求
// LED3 = 0;//主機跳頻燈亮
SystemBuffers.nRFTxBuff[9] = 0x04;//主機跳頻應(yīng)答
SystemBuffers.nRFTxBuff[10] = SystemBuffers.nRFTxBuff[8] + 1;//主機跳頻
SystemBuffers.nRFTxCount = 32;//主機要求間接無線發(fā)送
}
else if (SystemBuffers.nRFRxBuff[9] == 0x03) {//主機變址應(yīng)答
LED2 = 0;//子機變址燈亮
SystemBuffers.nRFRxBuff[0] = SystemBuffers.nRFRxBuff[10];//改寫接收地址第1字節(jié)
SystemBuffers.nRFRxBuff[1] = SystemBuffers.nRFRxBuff[11];//改寫接收地址第2字節(jié)
SystemBuffers.nRFRxBuff[2] = SystemBuffers.nRFRxBuff[12];//改寫接收地址第3字節(jié)
SystemBuffers.nRFRxBuff[3] = SystemBuffers.nRFRxBuff[13];//改寫接收地址第4字節(jié)
WritenRFConfig(SystemBuffers.nRFRxBuff);//子機重新配置nRF905接收地址
}
else if (SystemBuffers.nRFRxBuff[9] == 0x04) {//主機跳頻應(yīng)答
// LED3 = 0;//子機跳頻燈亮
SystemBuffers.nRFRxBuff[8] = SystemBuffers.nRFRxBuff[10];
FastWritenRFConfig(SystemBuffers.nRFRxBuff[8]);//快速配置nRF905,子機跳頻
}
}
else if (!CD && (SystemBuffers.nRFTxCount == 32)) {//要求間接無線發(fā)送(發(fā)送命令處理)
SetTxMode();//設(shè)置發(fā)送模式
WriteTXBuffer(SystemBuffers.nRFTxBuff);
ClrWdt();//清除看門狗計數(shù)器
StartTx();//開始發(fā)送
SystemBuffers.nRFTxCount = 0;//結(jié)束發(fā)送模式
if (SystemBuffers.nRFTxBuff[9] == 0x03) {//主機變址應(yīng)答
SystemBuffers.nRFRxBuff[4] = SystemBuffers.nRFTxBuff[10];//改寫呼叫地址第1字節(jié)
SystemBuffers.nRFRxBuff[5] = SystemBuffers.nRFTxBuff[11];//改寫呼叫地址第2字節(jié)
SystemBuffers.nRFRxBuff[6] = SystemBuffers.nRFTxBuff[12];//改寫呼叫地址第3字節(jié)
SystemBuffers.nRFRxBuff[7] = SystemBuffers.nRFTxBuff[13];//改寫呼叫地址第4字節(jié)
}
else if (SystemBuffers.nRFTxBuff[9] == 0x04) {//主機跳頻應(yīng)答
SystemBuffers.nRFRxBuff[8] = SystemBuffers.nRFTxBuff[10];//工作頻率低8位
for (t = 0; t < 2500; t ++);//延時,等待發(fā)送結(jié)束
FastWritenRFConfig(SystemBuffers.nRFRxBuff[8]);//快速配置nRF905,主機跳頻
}
SetRxMode();//設(shè)置接收模式
}
}
//鍵
void Key00(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//無線發(fā)送緩沖區(qū)為空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字節(jié)
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字節(jié)
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字節(jié)
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字節(jié)
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本機地址第1字節(jié)
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本機地址第2字節(jié)
SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本機地址第3字節(jié)
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本機地址第4字節(jié)
SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作頻率低8位
SystemBuffers.nRFTxBuff[9] = 0x81;//請求命令
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
SystemBuffers.nRFTxCount = 32;//要求間接無線發(fā)送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
SioBuffers.SioTXCount = 32;//要求間接串口發(fā)送
TI = 1;//送串口顯示
// LED0 = 0;//壓鍵燈亮
}
}
//鍵
void Key01(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//無線發(fā)送緩沖區(qū)為空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字節(jié)
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字節(jié)
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字節(jié)
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字節(jié)
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本機地址第1字節(jié)
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本機地址第2字節(jié)
SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本機地址第3字節(jié)
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本機地址第4字節(jié)
SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作頻率低8位
SystemBuffers.nRFTxBuff[9] = 0x82;//請求
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
SystemBuffers.nRFTxCount = 32;//要求間接無線發(fā)送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
SioBuffers.SioTXCount = 32;//要求間接串口發(fā)送
TI = 1;//送串口顯示
// LED1 = 0;//壓鍵燈亮
}
}
void Key02(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//無線發(fā)送緩沖區(qū)為空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字節(jié)
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字節(jié)
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字節(jié)
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字節(jié)
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本機地址第1字節(jié)
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本機地址第2字節(jié)
SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本機地址第3字節(jié)
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本機地址第4字節(jié)
SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作頻率低8位
SystemBuffers.nRFTxBuff[9] = 0x83;//變址請求
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
SystemBuffers.nRFTxCount = 32;//要求間接無線發(fā)送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
SioBuffers.SioTXCount = 32;//要求間接串口發(fā)送
TI = 1;//送串口顯示
// LED2 = 0;//壓鍵燈亮
}
}
void Key03(void) using 0
{
unsigned char i;
if (SystemBuffers.nRFTxCount == 0) {//無線發(fā)送緩沖區(qū)為空
SystemBuffers.nRFTxBuff[0] = SystemBuffers.nRFRxBuff[4];//呼叫地址第1字節(jié)
SystemBuffers.nRFTxBuff[1] = SystemBuffers.nRFRxBuff[5];//呼叫地址第2字節(jié)
SystemBuffers.nRFTxBuff[2] = SystemBuffers.nRFRxBuff[6];//呼叫地址第3字節(jié)
SystemBuffers.nRFTxBuff[3] = SystemBuffers.nRFRxBuff[7];//呼叫地址第4字節(jié)
SystemBuffers.nRFTxBuff[4] = SystemBuffers.nRFRxBuff[0];//本機地址第1字節(jié)
SystemBuffers.nRFTxBuff[5] = SystemBuffers.nRFRxBuff[1];//本機地址第2字節(jié)
SystemBuffers.nRFTxBuff[6] = SystemBuffers.nRFRxBuff[2];//本機地址第3字節(jié)
SystemBuffers.nRFTxBuff[7] = SystemBuffers.nRFRxBuff[3];//本機地址第4字節(jié)
SystemBuffers.nRFTxBuff[8] = SystemBuffers.nRFRxBuff[8];//工作頻率低8位
SystemBuffers.nRFTxBuff[9] = 0x84;//跳頻請求
for (i = 10; i < 32; i ++) SystemBuffers.nRFTxBuff[i] = 0;
SystemBuffers.nRFTxCount = 32;//要求間接無線發(fā)送
for (i = 0; i < 32; i ++) SioBuffers.SioTxBuff[i] = SystemBuffers.nRFTxBuff[i];
SioBuffers.SioTXCount = 32;//要求間接串口發(fā)送
TI = 1;//送串口顯示
// LED3 = 0;//壓鍵燈亮
}
}
void Key10(void) using 0
{
LED0 = 1;//放鍵滅燈
}
void Key11(void) using 0
{
LED1 = 1;//放鍵滅燈
}
void Key12(void) using 0
{
LED2 = 1;//放鍵滅燈
}
void Key13(void) using 0
{
LED3 = 1;//放鍵滅燈
}
void Key20(void) using 0
{
LED0 = 1;//長壓鍵滅燈
Key00();//連發(fā)
}
void Key21(void) using 0
{
LED0 = 1;//長壓鍵滅燈
Key01();//連發(fā)
}
void Key22(void) using 0
{
LED0 = 1;//長壓鍵滅燈
Key02();//連發(fā)
}
void Key23(void) using 0
{
LED0 = 1;//長壓鍵滅燈
Key03();//連發(fā)
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -