?? board.c
字號:
/****************************************************/
/* Application note */
/* Reference design : CC1100 RF Test Board */
/* File: board.c */
/* Revision: 1.0 */
/* Description: */
/* Microcontroller:ATmega48/48V */
/* Author: Zcg, Field Applications Engineer, SunRay*/
/****************************************************/
#include "iom88v.h"
#include "macros.h"
#include "main.h"
#include "CC1100.h"
#include "board.h"
#include "simpleio.h"
INT8U Test_RFTx_Buf[MAX_DATA_TEST]; //測試數據緩沖區
INT8U UartReceive_Buf[MAX_UT_BUFF]; //射頻發送(串口接收)緩存
INT8U RFReceive_Buf[MAX_RF_BUFF]; //射頻接收(串口發送)緩存
INT8U RFSend_Start;
INT8U RFSendCnt; //射頻發送計數器,記錄要發送的字節個數
INT8U RFReceiveCnt; //串口接收計數器
INT8U RFReceiveLen; //串口發送字節長度
INT8U RFSendFlg; //當串口接收完一包數據,置該標志
INT8U RFReceiveFlg; //當射頻接收完一包數據,置該標志
INT8U RXOTFlg; //串口接收數據超時標志
INT8U RXOTCnt; //串口接收數據超時計數器
INT8U Baud_Set_Num; //跳線開關波特率
INT8U Chno_Set_Num; //跳線開關信道值
INT8U Beep_Flag; //蜂鳴器開啟標志
INT16U Beep_Time; //蜂鳴器開啟時間
const SYS_SETTINGS Sys_Set[8] =
{
{T_T0_18MS,0x40,0x03,0xC4,0x86,0x10,600},//000 600
{T_T0_10MS,0x9F,0x01,0xF5,0x83,0x15,1200},//001 1200
{T_T0_8MS ,0xCF,0x00,0xC6,0x83,0x15,2400},//010 2400
{T_T0_4MS ,0x67,0x00,0xC7,0x83,0x40,4800},//011 4800
{T_T0_2MS ,0x33,0x00,0xC8,0x83,0x34,9600},//100 9600
{T_T0_2MS ,0x19,0x00,0xC9,0x83,0x34,19200},//101 19200
{T_T0_2MS ,0x0C,0x00,0xCA,0x83,0x34,38400},//110 38400
{T_T0_2MS ,0x08,0x00,0xCB,0x83,0x45,57600},//111 57600
};
INT8U paTable[8] = {0x8D,0x8D,0x8D,0x8D,0x8D,0x8D,0x8D,0x8D};
#if 0
INT8U rfSettings[39] = {
0x06, //*0x0B IOCFG2 GDO2 output pin configuration.
0x2E, //*0x2E IOCFG1 GDO1 output pin configuration.
0x2E, //*0x0C IOCFG0 GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.
0x07, // FIFOTHR RX FIFO and TX FIFO thresholds
0xD3, // SYNC1 Sync word, high byte
0x91, // SYNC0 Sync word, low byte
0xFF, // PKTLEN Packet length.
0x24, //*0x00 PKTCTRL1 Packet automation control.
0x45, //*0x12 PKTCTRL0 Packet automation control.
0x00, // ADDR Device address.
0x00, // CHANNR Channel number.
0x0C, //*0x06 FSCTRL1 Frequency synthesizer control.
0x00, // FSCTRL0 Frequency synthesizer control.
0x10, // FREQ2 Frequency control word, high byte.
0xA7, // FREQ1 Frequency control word, middle byte.
0x62, // FREQ0 Frequency control word, low byte.
0xF5, //* MDMCFG4 Modem configuration.
0x83, //* MDMCFG3 Modem configuration.
0x03, //*0x00 MDMCFG2 Modem configuration.
0x22, // MDMCFG1 Modem configuration.
0xF8, // MDMCFG0 Modem configuration.
0x15, // DEVIATN Modem deviation setting (when FSK modulation is enabled).
0x0E, //*0x07 MCSM2 Main Radio Control State Machine configuration.
0x30, // MCSM1 Main Radio Control State Machine configuration.
0x18, // MCSM0 Main Radio Control State Machine configuration.
0x15, //*0x16 FOCCFG Frequency Offset Compensation Configuration.
0x6C, // BSCFG Bit synchronization Configuration.
0x03, // AGCCTRL2 AGC control.
0x40, // AGCCTRL1 AGC control.
0x91, // AGCCTRL0 AGC control.
0x87, // WOREVT1 0x1E //定義EVENT0時間
0x6B, // WOREVT0 0x1F //...
0xF8, // WORCTRL 0x20 //關閉RC,定義EVENT1時間
0x56, // FREND1 Front end RX configuration.
0x10, // FREND0 Front end RX configuration.
0xA9, //* FSCAL3 Frequency synthesizer calibration.
0x2A, //* FSCAL2 Frequency synthesizer calibration.
0x0D, //* FSCAL1 Frequency synthesizer calibration.
0x11, //* FSCAL0 Frequency synthesizer calibration.
};
#endif
#define RF_CENTER 429
#define RF_429
#if 1
const RF_SETTINGS rfSettings = {
0x06, //*0x0B IOCFG2 GDO2 output pin configuration.
0x2E, //*0x2E IOCFG1 GDO1 output pin configuration.
0x2E, //*0x0C IOCFG0 GDO0 output pin configuration. Refer to SmartRF?Studio User Manual for detailed pseudo register explanation.
0x07, // FIFOTHR RX FIFO and TX FIFO thresholds
0xD3, // SYNC1 Sync word, high byte
0x91, // SYNC0 Sync word, low byte
0xFF, // PKTLEN Packet length.
0x04, //*0x00 PKTCTRL1 Packet automation control.
0x45, //*0x12 PKTCTRL0 Packet automation control.
0x00, // ADDR Device address.
0x00, // CHANNR Channel number.
0x06,//0x0C, //*0x06 FSCTRL1 Frequency synthesizer control.
0x00, // FSCTRL0 Frequency synthesizer control.
#ifdef RF_429
0x10, // FREQ2 Frequency control word, high byte.
0x80, // FREQ1 Frequency control word, middle byte.
0x83, // FREQ0 Frequency control word, low byte.
#endif
#ifdef RF_433
0x10, // FREQ2 Frequency control word, high byte.
0xA7, // FREQ1 Frequency control word, middle byte.
0x62, // FREQ0 Frequency control word, low byte.
#endif
#ifdef RF_461
0x11, // FREQ2 Frequency control word, high byte.
0xBB, // FREQ1 Frequency control word, middle byte.
0x13, // FREQ0 Frequency control word, low byte.
#endif
0xF5, // MDMCFG4 Modem configuration.
0x83, // MDMCFG3 Modem configuration.
0x03, //*0x00 MDMCFG2 Modem configuration.
0x22, // MDMCFG1 Modem configuration.
0xF8, // MDMCFG0 Modem configuration.
0x15, // DEVIATN Modem deviation setting (when FSK modulation is enabled).
0x0E, //*0x07 MCSM2 Main Radio Control State Machine configuration.
0x30, // MCSM1 Main Radio Control State Machine configuration.
0x18, // MCSM0 Main Radio Control State Machine configuration.
0x16,//0x15, //*0x16 FOCCFG Frequency Offset Compensation Configuration.
0x6C, // BSCFG Bit synchronization Configuration.
0x03, // AGCCTRL2 AGC control.
0x40, // AGCCTRL1 AGC control.
0x91, // AGCCTRL0 AGC control.
0x87, // WOREVT1 0x1E //定義EVENT0時間
0x6B, // WOREVT0 0x1F //...
0xF8, // WORCTRL 0x20 //關閉RC,定義EVENT1時間
0x56, // FREND1 Front end RX configuration.
0x10, // FREND0 Front end RX configuration.
0xE9,//0xA9, //*0xE9 FSCAL3 Frequency synthesizer calibration.
0x2A,//0x2A, //*0x2A FSCAL2 Frequency synthesizer calibration.
0x00,//0x0D, //*0x00 FSCAL1 Frequency synthesizer calibration.
0x1F,//0x11, //*0x1F FSCAL0 Frequency synthesizer calibration.
};
#endif
INT8U RFState; //保存射頻狀態,共兩種狀態:發送、接收狀態
INT8U CC1100IntHFlg; //CC1100產生高電平中斷標志
INT8U CC1100IntLFlg;
/*
************************************************************
*Description:RF收發中斷處理函數
*Arguments :none
*Returns :none
*Notes :
************************************************************
*/
#pragma interrupt_handler IntPCINT2:iv_PCINT2
void IntPCINT2 (void)
{
if (GDO2_STATE != 0x00)
{
CC1100IntHFlg = TRUE; //上升沿中斷,等待下降沿中斷
if(RFSend_Start==TRUE)
{
//TXEN_SET_L;//打開功率放大
//RXEN_SET_L;//打開功率放大
}
}
if ((CC1100IntHFlg == TRUE) && (GDO2_STATE == 0x00))
{
CC1100IntHFlg = FALSE; //下降沿中斷
CC1100IntLFlg = TRUE;
if(RFSend_Start==TRUE)
{
//TXEN_SET_H;//關閉功率放大
//RXEN_SET_H;//關閉功率放大
RFSend_Start=FALSE;
}
}
}
/*
************************************************************
*Description:初始化IO端口
*Arguments :none
*Returns :none
*Notes :
************************************************************
*/
void port_init(void)
{
DDRB =0x00;
PORTB=0x3F;
DDRD =0x08;//PD3 KEY
PORTD=0xD0;
BEEP_INIT();
BEEP_OFF();
//TXEN_OUT;
//TXEN_SET_H;
//RXEN_OUT;
//RXEN_SET_H;
}
/*
************************************************************
*Description:初始化串口
*Arguments :none
*Returns :none
*Notes :
************************************************************
*/
void uart0_init(void)
{
UBRR0H = Sys_Set[Baud_Set_Num].ubrr0h_val;
UBRR0L = Sys_Set[Baud_Set_Num].ubrr0l_val;
UCSR0C = BIT(UCSZ01) | BIT(UCSZ00); //Set frame format: 8data, 1stop bit, asyn mode
UCSR0B = BIT(RXEN0) | BIT(TXEN0); //使能串口發送、接收
EN_RX_INT();
}
/*
************************************************************
*Description:串口發送中斷函數
*Arguments :none
*Returns :none
*Notes :
************************************************************
*/
#pragma interrupt_handler uart0_tx_isr:iv_USART0_UDRE
void uart0_tx_isr (void)
{
UDR0 = RFReceive_Buf[RFReceiveCnt];
RFReceiveCnt++;
if (RFReceiveCnt == RFReceiveLen)
{
DIS_TX_INT(); //串口數據發送完畢,關閉發送空中斷
}
}
/*
************************************************************
*Description:串口接收中斷函數
*Arguments :none
*Returns :none
*Notes :利用超時處理來決定一包數據是否接收完成
************************************************************
*/
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr (void)
{
RXOTCnt = 0;
RXOTFlg = TRUE;
UartReceive_Buf[RFSendCnt] = UDR0;
RFSendCnt++;
if (RFSendCnt == MAX_DATA_NUM) //1字節的包長 + 60字節數據(共61字節)最大60字節
{
RXOTFlg = FALSE;
RFSendFlg = TRUE; //接收完最大一包數據,準備射頻發送
DIS_RX(); //禁止串口接收
DIS_RX_INT(); //禁止串口接收中斷
}
}
/*
************************************************************
*Description:初始化軟件定時器
*Arguments :none
*Returns :none
*Notes :
************************************************************
*/
void timer0_init(void)
{
TCCR0B |= (BIT(CS02) | BIT(CS00)); //1024分頻,開始計數
TIMSK0 |= BIT(TOIE0); //使能Timer0溢出中斷
TCNT0 = Sys_Set[Baud_Set_Num].tcnt0_val;
}
/*
************************************************************
*Description:串口接收的超時處理中斷時間函數
*Arguments :none
*Returns :none
*Notes :超時10ms
************************************************************
*/
#pragma interrupt_handler IntTimer0:iv_TIMER0_OVF
void IntTimer0 (void)
{
TCNT0 = Sys_Set[Baud_Set_Num].tcnt0_val;
if(Beep_Flag==TRUE)
{
if(Beep_Time++>50)
{
BEEP_OFF();
Beep_Time=0;
Beep_Flag=FALSE;
}
}
if (RXOTFlg == TRUE)
{
RXOTCnt++;
if (RXOTCnt == 2)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -