?? code.c
字號:
/*******************************************************************************************
科創四第15小組FSK通信控制程序
此乃連著本地主系統的模塊的控制程序
/*******************************************************************************************/
#include <reg51.h>
#include <intrins.h>
#include <absacc.h>
#include "set.h"
#define SB 16 //sent bytes 一個數據包里的payload字節數
sbit retest=P2^0;
int trsec;
uchar jj=0;
uchar r_flag=1;// flag of receiving 指示芯片處于接受狀態的flag
uchar retrieving_flag=0; //高則指示單片機從芯片取數據的那段時間
code RFConfig RxTxConf =
{
10,
0x01,
0x2c,
0x44,
0x10,
0x10,
0xcc, 0xcc, 0xcc,0xcc, // 4 bytes address
0x5c
};
// The content of this struct is nRF905's initialize data.初始化數據
// CH_NO=1;433MHZ;Normal Opration,No Retrans;RX,TX Address is 4 Bytes
// RX TX Payload Width is SB Bytes;Disable Extern Clock;Fosc=16MHZ
// 8 Bits CRC And enable
//四個按鍵對應的動作
Key1()//
{
trsec = 410;
}
Key2()//
{ uchar i,flag=1;
CSN=0;PB8255=PB;
SpiWrite(RC);
for (i=0;i<10;i++) // Write configration words 寫放配置字
{
temp.buf[i]=SpiRead();
}
CSN=1; PB8255=PB;
for(i=0;i<10;i++)
{
if ( temp.buf[i] == RxTxConf.buf[i]) flag++;
}
sec_dis=flag;
}
Key3()//
{
int i;
for(i=0;i<SB;i++)
TxBuf[i]=i+1;
SetTxMode(); // Set Tx Mode
//for (i=0;i<SB;i++) TxData(TxBuf[i]);
Delay(100);
TxPacket(); // Transmit Tx buffer data
SetRxMode();
}
Key4()//
{
trsec = 0; SetRxMode();
}
/*
void Resend(void)
{
int i;
SetTxMode();
for(i=0;i<SB;i++)
TxBuf[i]=RxBuf[i];
TxPacket();
} */
/**** T0時鐘中斷服務程序
在本例中,每5ms被執行一次
*****/
timer0() interrupt 1 using 0
{
//EA=0;
int i;
/* 重新對計數器賦初值,并啟動定時計數 */
TH0=V_TH0;
TL0=V_TL0;
TR0=1;
if(p_counter<30) p_counter++;
/* 檢測到按鍵被按下(0)時,相應的指示燈亮(0) */
if (KEY1==0)
{if (p_counter==30) {p_counter=0;Key1();} }
if (KEY2==0)
{if (p_counter==30) {p_counter=0;Key2();} }
if (KEY3==0)
{if (p_counter==30) {p_counter=0;Key3();} }
if (KEY4==0)
{if (p_counter==30) {p_counter=0;Key4();} }
//the next section is about display
Display();
// if(r_flag>0) r_flag--;
/* if(trsec>0)
{
if (trsec%400==0)
{ SetTxMode();
for(i=0;i<SB;i++)
TxBuf[i]=trsec/400;
TxPacket();
SetRxMode();
}
trsec--;
} */
if(trsec>0 && (!retrieving_flag))
{
if (trsec%180==0)
{
SetTxMode();
for(i=0;i<SB;i++)
TxBuf[i]=jj;
jj++;
if(jj>15) jj=0;
TxPacket();
SetRxMode();
}
trsec--;
if (trsec==0) trsec=200;
}
if(s_break>0)
{
s_break--;
if (s_break<10) time_up=1;
}
// EA=1;
}
/*******************************************************************************************/
void main()
{
SCON=0x50; //模式1
TMOD=0x21; //定時模式2
IE=0x90;
//=====9600=======設置串口通訊寄存器
PCON=0x00; //波特率9600BPS,晶振11.0592MHZ
TH1=0xFD; //
TL1=0xFD; //計數器初值
TH0=V_TH0; //5ms 鍵盤顯示中斷
TL0=V_TL0;
// 8255初始化
RST8255=0;
COM8255=0x81;
//================NRF905初始化
CSN=1; // Spi disable
SCK=0; // Spi clock line init high
DR=1; // Init DR for input
AM=1;
// TEST=1;
// Init AM for input
PWR_UP=1; // nRF905 power on
TRX_CE=0; // Set nRF905 in standby mode
TX_EN=0; // set radio in Rx mode
///////////////////////////////////////////
TI = 0;//串行中斷標志位清零
TR1 = 1;//打開計數器1
TR0 = 1;//開計數器0
ET0=1; //允許計數器T0中斷
PB8255=PB;
trsec=0;
//================
Config905(); // Config nRF905 module
SetRxMode(); // Set nRF905 in Rx mode
retest=0;
while(1)
{
//=====================等待NRF905接收
if (r_flag && DR) // Ifr recive data ready...
{//Delay(500);
//if(DR)
//retest=1;
retest=1;
r_flag=0;
retrieving_flag=1;
RxPacket(); // ... recive data
fir_dis = RxBuf[1]; //jsq++;
sec_dis = RxBuf[2];
thi_dis = RxBuf[3];
for_dis = jsq;if (jsq<15)jsq++; else jsq=0;
/* Delay(10000);
Delay(10000);
Delay(10000);
retest=0;
Resend(); */
// Config905();
SetRxMode();
// Delay(1000);
retrieving_flag=0;
r_flag=1;
}
if(ready2send||time_up)
{
SetTxMode();
Delay(100);
TxPacket();
SetRxMode();
ready2send=0;
time_up=0;
} //=====================
}
}
/*******************************************************************************************/
/*******************************************************************************************/
//function Config905();
/*******************************************************************************************/
void Config905(void)
{
uchar i,flag=1;
CSN=0; PB8255=PB; // Spi enable for write a spi command
SpiWrite(WC); // Write config command寫放配置命令
for (i=0;i<RxTxConf.n;i++) // Write configration words 寫放配置字
{
SpiWrite(RxTxConf.buf[i]);
}
CSN=1; PB8255=PB; // Disable Spi
CSN=0;PB8255=PB;
SpiWrite(RC);
for (i=0;i<RxTxConf.n;i++) // Write configration words 寫放配置字
{
temp.buf[i]=SpiRead();
}
CSN=1; PB8255=PB;
for(i=0;i<RxTxConf.n;i++)
{
if ( temp.buf[i] == RxTxConf.buf[i]) flag++;
}
fir_dis=flag;
if(flag!=11)
Config905();
}
/*******************************************************************************************/
void FixConfig(void) //fix the configuration command if it goes wrong
{
uchar i,flag=1;
TRX_CE=0;PB8255=PB;Delay(100);
CSN=0; PB8255=PB;
SpiWrite(RC);
for (i=0;i<RxTxConf.n;i++) // Write configration words 寫放配置字
{
temp.buf[i]=SpiRead();
}
CSN=1; PB8255=PB;
for(i=0;i<RxTxConf.n;i++)
{
if ( temp.buf[i] == RxTxConf.buf[i]) flag++;
}
if (flag!=(RxTxConf.n+1))
{
for_dis=10;
TRX_CE=0;PB8255=PB;Delay(100);
Config905();
}
}
/*******************************************************************************************/
//function SpiWrite();
/*******************************************************************************************/
//function TxPacket();
/*******************************************************************************************/
void TxPacket(void)
{
uchar i;
TRX_CE=0;
//Config905();
CSN=0; PB8255=PB; // Spi enable for write a spi command
SpiWrite(WTP); // Write payload command
for (i=0;i<SB;i++)
{
SpiWrite(TxBuf[i]); // Write 32 bytes Tx data
}
CSN=1; PB8255=PB; // Spi disable
Delay(1);
CSN=0; PB8255=PB; // Spi enable for write a spi command
SpiWrite(WTA); // Write address command
for (i=0;i<(RxTxConf.n-6);i++) // Write 4 bytes address
{
SpiWrite(RxTxConf.buf[i+5]);
}
CSN=1; // Spi disable
TRX_CE=1; PB8255=PB; // Set TRX_CE high,start Tx data transmission
Delay(1000); // while (DR!=1);
TRX_CE=0; PB8255=PB; // Set TRX_CE low
}
/*******************************************************************************************/
//function RxPacket();
/*******************************************************************************************/
void RxPacket(void)
{
uchar i;
TRX_CE=0; PB8255=PB; // Set nRF905 in standby mode
Delay(100);
CSN=0; PB8255=PB; // Spi enable for write a spi command
SpiWrite(RRP); // Read payload command
for (i=0;i<SB;i++)
{
RxBuf[i]=SpiRead(); // Read data and save to buffer
}
CSN=1; PB8255=PB; // Disable spi
while(DR||AM) _nop_();
//P0= RxBuf[0]; // Buffer[0] output from P0 port...
TRX_CE=1; PB8255=PB;
/* if (!(RxBuf[0]==0&&RxBuf[1]==0) )
for (i=0;i<SB;i++)
{
TmpBuf[i]=RxBuf[i]; // Read data and save to buffer
} */
//xx=(RxBuf[0]>>4)&0x0f;
for (i=0;i<SB;i++) TxData(RxBuf[i]);
}
/*******************************************************************************************/
//function SetTxMode();
/*******************************************************************************************/
void SetTxMode(void)
{
TX_EN=1;
TRX_CE=0; PB8255=PB;
Delay(100); // delay for mode change(>=650us)
r_flag=0;
}
/*******************************************************************************************/
//function SetRxMode();
/*******************************************************************************************/
void SetRxMode(void)
{
TRX_CE=0; PB8255=PB;
Delay(100);
Config905();
//FixConfig();
Delay(100);
TX_EN=0;
TRX_CE=1; PB8255=PB;
Delay(100); // delay for mode change(>=650us)
r_flag=1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -