📄 复件 tx433.c
字号:
/*******************************************
无线接收的发送收端程序:
将从串口接收到的字符发送出去。
******************************************/
#include <Nordic\reg24e1.h>
struct RFConfig
{
unsigned char n;
unsigned char buf[15];
};
typedef struct RFConfig RFConfig;
const RFConfig txconf =
{
15,
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x12, 0x34, 0x56, 0x78, 0x83, 0x6c, 0x04
};
const RFConfig rxconf =
{
15,
0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x87, 0x65, 0x43, 0x21, 0x83, 0x6c, 0x05
};
#define ADDR_INDEX 8 // Index to address bytes in RFConfig.buf
#define ADDR_COUNT 4 // Number of address bytes
#define UART_BUFFER_SIZE 8 //定义串口接收缓冲区大小 8 bytes,最大为31
unsigned char UART_BUFFER[UART_BUFFER_SIZE+1]; //定义串口接收数据缓冲区
unsigned char countt=0; //
bit Overtime_Flag=0;
unsigned int Tnum=500;
//延时函数
void Delay100us(volatile unsigned char n)
{
unsigned char i;
while(n--)
for(i=0;i<35;i++)
;
}
//超时计时定时器T0初始化
void Time0_Init(void)
{
TMOD &= 0xF0; //定时器/计数器T0为定时器,工作于方式2,以TR0启动定时器
TMOD |= 0x02;
CKCON |= 0x08; // T0M=1 (/4 timer clock)
TL0=0x38; //16MHz下定时时间为50us
TH0=0x38;
TR0=1; //启动定时器T0
ET0=1; //允许定时器T0中断
Overtime_Flag=0; //清除超时标志
}
//定时器T0中断服务子程序
void Timer0_ISR (void) interrupt 1
{
TF0=0;
Tnum--; //计时100ms(中断次数计数),实际需34ms
if(Tnum==0) //超时?
{
// ET0=0; //T0关中断
TR0=0; //关闭定时器T0
Overtime_Flag=1; //置位超时标志,开始发送
P06=~P06;
}
}
//串口接收中断函数
void serial_ISR (void) interrupt 4
{
if(RI)
{
unsigned char ch;
RI = 0;
ch = SBUF;
//TR0=1;
if(Overtime_Flag==0) //未超时就接收
{
if(countt==UART_BUFFER_SIZE)
countt=0;
countt++;
UART_BUFFER[countt]=ch;
UART_BUFFER[0]=countt;
}
}
if(TI)
{
// TI=0;
return;
}
}
unsigned char SpiReadWrite(unsigned char b)
{
EXIF &= ~0x20; // Clear SPI interrupt
SPI_DATA = b; // Move byte to send to SPI data register
while((EXIF & 0x20) == 0x00) // Wait until SPI hs finished transmitting
;
return SPI_DATA;
}
void Transmitter(unsigned char *str,unsigned char strlen)
{
unsigned char j=0;
CE = 1;
Delay100us(0);
for(i=0;i<ADDR_COUNT;i++) // Start with the address of the receiver:
SpiReadWrite(rxconf.buf[ADDR_INDEX+i]);
SpiReadWrite(b);
CE = 0;
Delay100us(3); // Wait ~300us
P00=~P00;
//countt=0;
}
void Init(void)
{
unsigned char b;
PWR_UP=1;
Delay100us(30); // Wait > 3ms
SPICLK = 0; // Max SPI clock (XTAL/8)
SPI_CTRL = 0x02; // Connect internal SPI controller to Radio
TH1 = 0xE6; // 9600@16MHz (when T1M=1 and SMOD=1)
CKCON |= 0x10; // T1M=1 (/4 timer clock)
PCON = 0x80; // SMOD=1 (double baud rate)
SCON = 0x50; // Serial mode1, enable receiver
TMOD = 0x20; // Timer1 8bit auto reload
TR1 = 1; // Start timer1
P0_ALT |= 0xC6; // Select alternate functions on pins P0.1 and P0.2
P0_DIR |= 0x02; // P0.1 (RxD) is input
// Configure Radio:
CS = 1;
Delay100us(0);
for(b=0;b<txconf.n;b++)
{
SpiReadWrite(txconf.buf[b]);
}
CS = 0;
}
void main(void)
{
Init();
Time0_Init(); //T0初始化
EA=1;
ES=1;
//PT0=1; //定时器T0为高优先级
while(1)
{
if(Overtime_Flag) //如果取数标志已置位,就将读到的数无线发送
{
ES=0;
Overtime_Flag=0; //取数标志清0
if(countt)
Transmitter(UART_BUFFER,(UART_BUFFER[0]+1));
countt=0;
Tnum=500;
TL0=0x38; //16MHz下定时时间为50us
TR0=1;
ES=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -