📄 main.c.bak
字号:
/************************************************************************
;copyright :shenzhen coolwaveasia
;filename :main.c
;mcu :w78e52b
;crystal :33.177MHZ
;cycle :0.36us
;RF :7105
;rf crystal :16MHZ
;RF rate :250K
;startdate :2008.7.24
;enddate :2008.8.5
;writeby :dengyihong
;revision :A
;describe :7105 收发数据简单演示
;notice :
;complier :Keil C
;***********************************************************************
; --------------------
; P1.0 |1 40| VCC
; P1.1 |2 39| P0.0 ---- LCD_DATA0
; RF_SCK ---- P1.2 |3 38| P0.1 ---- LCD_DATA1
; RF_SDIO ---- P1.3 |4 37| P0.2 ---- LCD_DATA2
; P1.4 |5 36| P0.3 ---- LCD_DATA3
; RF_SCS ---- P1.5 |6 35| P0.4 ---- LCD_DATA4
; P1.6 |7 34| P0.5 ---- LCD_DATA5
; P1.7 |8 33| P0.6 ---- LCD_DATA6
; RST/Vpd |9 32| P0.7 ---- LCD_DATA7
; RXD/P3.0 |10 31| /EA/Vpp
; TXD/P3.1 |11 30| ALE/PROG
; RF_GPIO2 --- /INT0/P3.2 |12 29| /PSEN
; RF_GPIO1 --- /INT1/P3.3 |13 28| P2.7 ---- KEY4
; T0/P3.4 |14 27| P2.6 ---- KEY3
; RX\TX --- T1/P3.5 |15 26| P2.5 ---- KEY2
; /WR/P3.6 |16 25| P2.4 ---- KEY1
; /RD/P3.7 |17 24| P2.3
; XTAL2 |18 23| P2.2 ---- LCD_RW
; XTAL1 |19 22| P2.1 ---- LCD_RS
; VSS |20 21| P2.0 ---- LCD_EN
; --------------------
; W78E52B
;***********************************************************************/
#include "..\header\main.h"
#include "..\header\maindef.h"
bit Wait_Ack = 0; // 等待RF应答
bit Clr_Screen = 0; // 刷新屏幕
U8 Timer0_Cnt = 0; // 定时器0计数器
//U8 Mode_Key_Cnt = 0;
enum Rf_Status Rf_Mode_Cnt;
U8 Add_Key_Cnt = 0;
U8 Dec_Key_Cnt = 0;
U8 Rf_Rate_Cnt = 5; // default 250k
U8 Rf_Fdev_Cnt = 3; // default 80k
U8 Rf_Channel_Cnt = 3; // default 第4个通道
U8 Rf_Rec_Channel = (3 << 3); // default frequency 2.412GHZ(2.400G + (3*8)*500K))
U8 Rf_Rec_Buf[RF_DATA_PACKET_SIZE] = {0};
U8 Rf_Send_Buf[RF_DATA_PACKET_SIZE] = {0};
U8 const code HexDispArray[16] = "0123456789ABCDEF";
U8 const code Rf_Rate_Tab[7] = {19,9,7,4,3,1,0};
//25k,50k,62.5k,100k,125k,250k,500k
U8 const code Rf_Fdev_Tab[12] = {1,2,3,4,6,7,9,11,12,15,18,25};
// 31k,46k,62k,80k,108k,124k,155k,186k,200k,248k,294k,400k
// 主程序入口
void main(void)
{
EA = 0;
delay_2us(5000);
ini_mcu();
delay_2us(5000);
ini_lcd();
delay_2us(50000);
lcd_printf_string("COOLWAVE", ROW(0x00)|LEN(0x08)|COL(0x00));
lcd_printf_string(" A7105 ", ROW(0x01)|LEN(0x08)|COL(0x00));
ini_rf();
ini_rf();
ini_rf();
delay_2us(5000);
while(1)
{
key_scan();
dispose_key_info();
if(IE1) // 是否接收到RF数据(查询方式接收)
{
IE1 = 0;
rec_rf_data();
dispose_rf_data(); // 处理接收到的RF数据
}
dispose_timer0(); // 处理定时器
// send_parmeter_test();
}
}
/***********************************************************
name: ini_mcu
input: none
output: none
describe: 初始化MCU
notice:
creat date: 2008-7-24
creator: dengyihong
************************************************************/
void ini_mcu(void)
{
PSW = 0x00;
PCON = 0x00;
IP = 0x00; // 设置中断优先级
IE = 0x00; // EA - ET2 ES ET1 EX1 ET0 EX0 , disable all interrupt
ini_int1();
ini_timer0();
}
/**********************************************************
name: ini_int0
input: none
output: none
describe: 设置 int0中断方式
notice:
creat date: 2008-7-24
creator: dengyihong
**********************************************************/
void ini_int0(void)
{
IT0 = 1; // INT0 下降沿中断
IE0 = 0;
// EX0 = 1; // disable INT0
}
/**********************************************************
name: ini_int1
input: none
output: none
describe: 设置 int1中断方式
notice:
creat date: 2008-7-24
creator: dengyihong
**********************************************************/
void ini_int1(void)
{
IT1 = 1; //
IE1 = 0;
EX1 = 0; //disable int1
}
/***********************************************************
name: ini_timer0
input: none
output: none
describe: 初始化定时器0
notice:
creat date: 2008-7-24
creator: dengyihong
************************************************************/
void ini_timer0(void)
{
TF0 = 0;
TH0 = 0x94;
TL0 = 0x00; // 定时10ms
TMOD = 0x01; // timer0 工作在方式1
ET0 = 0x01; // 允许timer0中断
// TR0 = 1; // enable timer0
}
/**********************************************************
name: ini_timer1
input: none
output: none
describe: 初始化定时器1
notice:
creat date: 2008-7-24
creator: dengyihong
**********************************************************/
void ini_timer1(void)
{
}
/**********************************************************
name: ini_uart
input: none
output: none
describe: 设置 uart 格式
notice:
creat date: 2008-7-24
creator: dengyihong
**********************************************************/
void ini_uart(void)
{
}
/**********************************************************
name: dispose_key_info
input: none
output: none
describe: 处理按键信息
notice:
creat date: 2008-7-24
creator: dengyihong
**********************************************************/
void dispose_key_info(void)
{
U8 i = 0;
if(Key_Msg) // 是否有按键消息
{
Key_Msg = 0;
switch(Key_Value)
{
case MODE_KEY:
if(Rf_Mode_Cnt == SEND_KEY)
{
Rf_Mode_Cnt = REC_STATUS;
}
else
{
Rf_Mode_Cnt++;
}
switch(Rf_Mode_Cnt)
{
case REC_STATUS:
lcd_printf_string("Rec ", ROW(0x00)|LEN(0x08)|COL(0x00));
lcd_printf_string(" Status", ROW(0x01)|LEN(0x08)|COL(0x00));
A7105_entry_rx();
break;
case SET_RF_RATE:
lcd_printf_string("Set Rate", ROW(0x00)|LEN(0x08)|COL(0x00));
display_rf_rate();
break;
case SET_RF_FDEV:
lcd_printf_string("Set Fdev", ROW(0x00)|LEN(0x08)|COL(0x00));
display_rf_fdev();
break;
case SET_RF_CHANNEL:
lcd_printf_string("Set Freq", ROW(0x00)|LEN(0x08)|COL(0x00));
display_rf_channel();
break;
case SEND_STATUS:
lcd_printf_string("Send....", ROW(0x00)|LEN(0x08)|COL(0x00));
lcd_printf_string(" ", ROW(0x01)|LEN(0x08)|COL(0x00));
A7105_disable_tx_modulation(); // 关调制
A7105_setup_channel(Rf_Rec_Channel+1);
A7105_entry_tx(); // 进入发送状态
while(1)
{
// A7105_entry_tx(); // 进入发送状态
// send_parmeter_test();
/*
key_scan();
if(Key_Msg) // 按下任意键退出
{
lcd_printf_string("COOLWAVE", ROW(0x00)|LEN(0x08)|COL(0x00));
lcd_printf_string(" A7105 ", ROW(0x01)|LEN(0x08)|COL(0x00));
A7105_enable_tx_modulation();
A7105_setup_channel(Rf_Rec_Channel);
delay_2us(100);
A7105_entry_rx();
IE1 = 0;
break;
}
*/
}
break;
default:
break;
}
break;
case DEC_KEY:
switch(Rf_Mode_Cnt)
{
case SET_RF_RATE:
if(Rf_Rate_Cnt)
{
Rf_Rate_Cnt--;
}
A7105_setup_data_rate(Rf_Rate_Tab[Rf_Rate_Cnt]);
display_rf_rate();
// 设置 RF rate
break;
case SET_RF_FDEV:
if(Rf_Fdev_Cnt)
{
Rf_Fdev_Cnt--;
}
display_rf_fdev();
// 设置 RF fdev
A7105_setup_fdev(Rf_Fdev_Tab[Rf_Fdev_Cnt]);
break;
case SET_RF_CHANNEL:
if(Rf_Channel_Cnt)
{
Rf_Channel_Cnt--;
}
Rf_Rec_Channel = Rf_Channel_Cnt << 3;
A7105_setup_channel(Rf_Rec_Channel);
display_rf_channel();
break;
default:
break;
}
break;
case ADD_KEY:
switch(Rf_Mode_Cnt)
{
case SET_RF_RATE:
if(Rf_Rate_Cnt < 7)
{
Rf_Rate_Cnt++;
}
A7105_setup_data_rate(Rf_Rate_Tab[Rf_Rate_Cnt]);
display_rf_rate();
break;
case SET_RF_FDEV:
if(Rf_Fdev_Cnt < 12)
{
Rf_Fdev_Cnt++;
}
A7105_setup_fdev(Rf_Fdev_Tab[Rf_Fdev_Cnt]);
display_rf_fdev();
break;
case SET_RF_CHANNEL:
if(Rf_Channel_Cnt < 21)
{
Rf_Channel_Cnt++;
}
Rf_Rec_Channel = Rf_Channel_Cnt << 3;
A7105_setup_channel(Rf_Rec_Channel);
display_rf_channel();
break;
default:
break;
}
break;
case SEND_KEY: // 处理发送按键
Rf_Send_Buf[0] = RF_DOWN_CMD;
Rf_Send_Buf[1] = 0x08;
for(i=2; i<RF_DATA_PACKET_SIZE-1; i++)
{
Rf_Send_Buf[i] = rand();
}
Rf_Send_Buf[i] = 0xaa;
send_data_by_RF(RF_DATA_PACKET_SIZE); // 发送测试数据
lcd_printf_string("Wait Ack", ROW(0x00)|LEN(0x08)|COL(0x00));
lcd_printf_string(" ", ROW(0x01)|LEN(0x08)|COL(0x00));
TF0 = 0;
TH0 = 0x94;
TL0 = 0x00; // (65536 - 37888(0x9400))*12/33.177 = 10ms
Timer0_Cnt = 10; // 定时100ms
TR0 = 1; // enable timer0
Wait_Ack = 1; // 设置等待应答标志
break;
default:
break;
}
}
}
/**********************************************************
name: dispose_rf_data
input: none
output: none
describe: 处理接收到的RF数据
notice:
creat date: 2008-7-24
creator: dengyihong
**********************************************************/
void dispose_rf_data(void)
{
bit return_ack = 1;
U8 i = 0;
if(Rf_Rec_Buf[0] == RF_DOWN_CMD) // 是否为主机发送的数据
{
lcd_printf_string("Send Ack", ROW(0x00)|LEN(0x08)|COL(0x00));
lcd_printf_string(" ", ROW(0x01)|LEN(0x08)|COL(0x00));
Rf_Send_Buf[0] = RF_UP_CMD;
Rf_Send_Buf[1] = 0x08;
for(i=0; i<RF_DATA_PACKET_SIZE-3; i++)
{
Rf_Send_Buf[i+2] = Rf_Rec_Buf[i+2]; // 将接收到的数据返回作为应答
}
Rf_Send_Buf[i+2] = 0xaa; // 最后一个为无效数据
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -