?? nfr401.c.txt
字號:
nrf401實(shí)驗(yàn)c51程序
/*----------------------------------------------------------------------
nrf401實(shí)驗(yàn)程序
雁塔菜農(nóng)HotPower@126.com
-----------------------------------------------------------------------*/
#include <AT89X52.h>
#include <stdio.h>
#include <absacc.h>
#include <intrins.h>
#include <nrf401.h>
#include <hotins.h>
#define T0_8mS -8000
#define T1_8mS -8000
#define T2_1_25mS -576 //-1250*0.9216=1.25MS
/*----------------------------------------------------------------------
系統(tǒng)函數(shù)
-----------------------------------------------------------------------*/
void MainInit(void);//系統(tǒng)初始化
void ClrWdt(void);//喂狗
void nRFInit(void);
void SystemInit(void);//系統(tǒng)初始化
void SystemSetup(void);//系統(tǒng)設(shè)置
void SystemIoInit(void);//系統(tǒng)接口初始化
void UserSetup(void);//用戶運(yùn)行環(huán)境設(shè)置
void TimeInit(void);
unsigned char crc8r(unsigned char crcbyte);
unsigned int crc16r(unsigned int crcword, unsigned int crc);
unsigned int GetTXBuffWord(unsigned char ledpos);
void SetTXBuffWord(unsigned char ledpos, unsigned int val);
unsigned int GetRXBuffWord(unsigned char ledpos);
void SetRXBuffWord(unsigned char ledpos, unsigned int val);
SystemData SystemBuffers;//申請系統(tǒng)數(shù)據(jù)結(jié)構(gòu)
SioData SioBuffers;
/*-----------------------------------------
主程序初始化
-------------------------------------------*/
void MainInit(void)//系統(tǒng)初始化
{
SystemIoInit();//系統(tǒng)接口初始化
ClrWdt();//清除看門狗計(jì)數(shù)器
if (SystemBuffers.RamTest != 0x55aa) {//內(nèi)存測試
SystemInit();//系統(tǒng)上電初始化
}
SystemSetup();//系統(tǒng)運(yùn)行環(huán)境設(shè)置
UserSetup();//用戶運(yùn)行環(huán)境設(shè)置
}
void SystemInit(void)//系統(tǒng)初始化
{
SystemBuffers.RamTest = 0x55aa;//內(nèi)存初始化
}
void SystemSetup(void)//系統(tǒng)設(shè)置
{
AUXR = 0x01;//關(guān)閉EMI
nRFInit();
TimeInit();
IP = 0x10;//中斷優(yōu)先級EX1>ET2>ET0>EX0>ES
IE = 0xb4;//開中斷EA,ET2,ET1,EX1,ET0
}
void UserSetup(void)//用戶運(yùn)行環(huán)境設(shè)置
{
}
void SystemIoInit(void)
{
IE = 0x00;//關(guān)閉中斷
P0 = 0xff;//P0口初始化
P1 = 0xff;//P1口初始化
P2 = 0xff;//P2口初始化
P3 = 0xff;//P3口初始化
}
void TimeInit()
{
TMOD = 0x21;//定時器0,1為方式1(十六位定時器)
TCON = 0x55;//啟動定時器TR1EQUTR0EQU1,IT1EQUIT0EQU1
/*----------------------------------
定時器0定時參數(shù)設(shè)置
----------------------------------*/
TL0 = 0;
TH0 = 0;
TR0 = 1;//啟動定時器0
/*----------------------------------
定時器0定時參數(shù)設(shè)置
----------------------------------*/
TL1 = SIO_BPS;
TH1 = SIO_BPS;
PCON = 0x00;
TR1 = 1;//啟動定時器1
/*----------------------------------
定時器2定時參數(shù)設(shè)置
----------------------------------*/
TIMEER2 = T2_1_25mS;
RCAP = T2_1_25mS;
TR2 = 1;//啟動定時器2
}
void nRFInit(void)
{
nRFCS = 0;//433.92MHz//1//434.33MHz
nRFTXEN = 0;//接收
nRFPWR = 1;//上電
SioBuffers.TXMAXCount = 0;
SioBuffers.TXCount = 0;
// SioBuffers.RXCount = 0;
SCON = 0x50; /* SCON: mode 2, 9-bit UART, enable rcvr */
SioBuffers.RXCount = 16;
}
/*------------------------------------
外部INT0中斷服務(wù)程序
------------------------------------*/
void int0proc() interrupt IE0_VECTOR// using 1
{
}
/*------------------------------------
定時器T0中斷服務(wù)程序
------------------------------------*/
void t0proc() interrupt TF0_VECTOR// using 1
{
}
/*------------------------------------
外部INT1中斷服務(wù)程序
------------------------------------*/
void int1proc() interrupt IE1_VECTOR using 1
{
}
/*------------------------------------
定時器T1中斷服務(wù)程序
------------------------------------*/
void t1proc() interrupt TF1_VECTOR// using 1
{
}
/*------------------------------------
串口SIO中斷服務(wù)程序
------------------------------------*/
void sioproc() interrupt SIO_VECTOR// using 1
{
unsigned char i;
unsigned int crc;
if (RI) {//接收中斷
RI = 0;
if (!nRFTXEN && SioBuffers.RXCount) {//每次接收20個數(shù)據(jù)
i = SBUF;
SioBuffers.RXCount --;
SioBuffers.RXBuffers[19 - SioBuffers.RXCount] = i;
switch(SioBuffers.RXCount) {
case 19:
if (i != 0x55) SioBuffers.RXCount = 20;
break;
case 18:
if (i != 0xaa) SioBuffers.RXCount = 20;
break;
case 0:
crc = 0;
for (i = 1; i <= 8; i ++) {
crc = crc16r(GetRXBuffWord(i + i), crc);
}
if (crc != GetRXBuffWord(18))
SioBuffers.RXCount = 20;
else {
nRFTXEN = 1;//發(fā)送
for (i = 0; i < 20; i ++) {
SioBuffers.TXBuffers[i] = SioBuffers.RXBuffers[i];
}
SioBuffers.TXMAXCount = TX_MAXCount;
SioBuffers.TXCount = TX_Count + 32;
TI = 1;//接收結(jié)束立即轉(zhuǎn)為發(fā)送
}
break;
}
}
}
if (TI) {//發(fā)送中斷
TI = 0;
if (nRFTXEN && SioBuffers.TXCount) {
SioBuffers.TXCount --;
if (SioBuffers.TXCount > 19) {
SBUF = 0x00;
// SBUF = 0xff;
}
else {
SBUF = SioBuffers.TXBuffers[19 - SioBuffers.TXCount];
}
if (SioBuffers.TXCount == 0) {
if (SioBuffers.TXMAXCount) {
SioBuffers.TXMAXCount --;
SioBuffers.TXCount = TX_Count;
}
else {
nRFTXEN = 0;//發(fā)送結(jié)束立即轉(zhuǎn)為接收
SioBuffers.RXCount = 20;
}
}
}
}
}
/*------------------------------------
定時器T2中斷服務(wù)程序(1.25mS)
------------------------------------*/
void t2proc() interrupt TF2_VECTOR// using 2
{
//unsigned char i;
//unsigned int crc;
TF2 = 0;
if (PCON & GF0) {//中斷是從主循環(huán)內(nèi)跳入的才能喂狗
ClrWdt();//清除看門狗計(jì)數(shù)器
PCON &= ~GF0;//清除標(biāo)志
}
SystemBuffers.T2Count ++;
// if (SystemBuffers.T2Count >= 80) {//100mS到
if (SystemBuffers.T2Count >= 400) {//500mS到
SystemBuffers.T2Count = 0;
//if (((SystemBuffers.T2Count & 3) == 0) && (SioBuffers.TXCount == 0)) {//10mS
//if (SioBuffers.TXCount == 0) {//10mS
/*
if (!ES && (SioBuffers.TXCount == 0)) {//10mS
SioBuffers.TXCount = 18;
crc =GetTXBuffWord(2);
crc = crc16r(crc);
SetTXBuffWord(4, crc);
nRFTXEN = 1;//發(fā)送
TI = 1;
ES = 1;
}
*/
// SioBuffers.TXCount = 8;
// nRFTXEN = 1;//發(fā)送
// SBUF = SioBuffers.TXBuffers[0];
// ES = 1;
}
}
void ClrWdt(void)//喂狗
{
WDTRST = 0x1e;//89s52內(nèi)狗
WDTRST = 0xe1;//89s52內(nèi)狗
}
unsigned char crc8r(unsigned char crcbyte)
{
unsigned char i,crc;
crc = 0;
for(i = 0; i < 8; i++) {
if(((crc ^ crcbyte) & 0x01) == 0) crc >>= 1;
else{
crc ^= 0x18;//0x18;//CRC=X8+X5+X4+1
crc >>= 1;
crc |= 0x80;
}
crcbyte >>= 1;
}
return crc;
}
unsigned int crc16r(unsigned int crcword, unsigned int crc)
{
unsigned char i;
for(i = 0; i < 16; i++){
if(((crc ^ crcword ) & 0x0001) == 0) crc >>= 1;
else{
crc ^= 0x810;//0x0810;//CRC=X16+X12+X5+1
crc >>= 1;
crc |= 0x8000;
}
crcword >>= 1;
}
return crc;
}
unsigned int GetTXBuffWord(unsigned char ledpos)
{
unsigned char val1, val2;
unsigned int val;
val1 = SioBuffers.TXBuffers[ledpos];
val2 = SioBuffers.TXBuffers[ledpos + 1];
val = (val1 << 8) + val2;
return val;
}
void SetTXBuffWord(unsigned char ledpos, unsigned int val)
{
SioBuffers.TXBuffers[ledpos] = (val >> 8);
SioBuffers.TXBuffers[ledpos + 1] = val & 0xff;
}
unsigned int GetRXBuffWord(unsigned char ledpos)
{
unsigned char val1, val2;
unsigned int val;
val1 = SioBuffers.RXBuffers[ledpos];
val2 = SioBuffers.RXBuffers[ledpos + 1];
val = (val1 << 8) + val2;
return val;
}
void SetRXBuffWord(unsigned char ledpos, unsigned int val)
{
SioBuffers.RXBuffers[ledpos] = (val >> 8);
SioBuffers.RXBuffers[ledpos + 1] = val & 0xff;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -