📄 main.c
字号:
//-- Includes ----------------------------------------------------------------
#include <absacc.h>
#include <reg51.h>
#include <intrins.h>
#include <math.h>
#include "cc1100.h"
#include "io.h"
//按键检测时间间隔
byte data time=0;
// 数码管位驱动和指示灯驱动信号输出缓存,定义了一个可位寻址的变量 */
byte bdata output_sel;
sbit led_1 = output_sel^5;
sbit led_2 = output_sel^6;
sbit led_3 = output_sel^7;
sbit led_4 = output_sel^4;
void keyboard();
//public variable------------------------------------------------------------
byte data receive[8]; //command packet received from wireless
byte data transmit[8]; //command packet transmitted to wireless
byte data InstructBit; //transmit byte
byte data ReadBit; //received byte
byte data txfifo_num; //number of bytes in txfifo
byte data rxfifo_num; //number of bytes in rxfifo
//----------------------------------------------------------------------------
timer0() interrupt 1 using 0
{
EA=0; /*关中断*/
/* 重新对计数器赋初值,并启动定时计数 */
TH0=V_TH0;
TL0=V_TL0;
output_sel = 0xD0; /*初值,令数码管驱动位无效,指示灯全灭*/
/* 检测到按键被按下(0)时,相应的指示灯亮(0) */
keyboard();
EA=1; /*开中断*/
}
void keyboard()
{
byte i;
if (time==15)
{
if (KEY1==0 ||KEY2==0 ||KEY3==0||KEY4==0) {
if (KEY3==0) //read 8bytes from RXFIFO only for test
{
led_3 = 0;
SpiReadData(8);
SpiWriteData(0,8);
}
else if (KEY4==0) //initialize the cc1100 chip
{
led_4 = 0;
SpiCommandStrobe(SRES);
for (i=0; i<=50; i++)
_nop_();
RfWriteRfSettings();
_nop_();
_nop_();
_nop_();
_nop_();
SpiCommandStrobe(SRX);//shift to receive state
Csn=1;
IE1=0;
EX1=1; //open int1 wait for command packet
TR0=0;
}
else if (KEY2==0) //read the TXFIFO and RXFIFO states (only for test)
{
led_2=0;
txfifo_num=SpiReadReg(TXBYTES|0x40);
rxfifo_num=SpiReadReg(RXBYTES|0x40);
}
}
time=0;
}
else
time ++;
ADDR_SEL = output_sel;
}
int1()interrupt 2 using 0 { //when the command packet has arrived
EA=0;
rxfifo_num=SpiReadReg(RXBYTES|0x40);
if (rxfifo_num>=8) {
SpiReadData(8);
transmit[0]=0x7E; //soi
transmit[1]=0x00; //addr
transmit[2]=receive[2]; //rspd
transmit[3]=0x00; //rtn
transmit[4]=receive[1]; //info1: addr
transmit[5]=receive[4]; //info2
transmit[6]=0;
transmit[7]=0;
SpiWriteData(0,8);
SpiCommandStrobe(STX);
}
EA=1;
}
//============================================================================
//P1.4--SO
//P1.5--SI
//P1.6--Csn
//P1.7--SCLK
//P3.2--GDO0
//P3.3--GDO2
void main() {
P1=0XFF;
P3=0XFF;
// 中断工作寄存器初始化
IE=0x80;
IP=0x04; //INT1高优先级
IT1=1;
EX1=0;
GDO2=1;
TMOD=0X11;
TH0=V_TH0;
TL0=V_TL0;
ET0=1;
TR0=1;
for(;;) {
}
}
//============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -