|
楼主 |
发表于 2017-9-17 04:28
|
显示全部楼层
本帖最后由 skypup 于 2017-9-17 04:30 编辑
折腾了一整天,得到下面的代码,只能简单的调通,发送一个 "Hello.", 用的是IO口软件模拟SPI的方式,程序里面好多循环等待语句, 也没有跳频。
Arduino例程_20170916.rar
(10.08 KB, 下载次数: 37)
[kenrobot_code]// A7106 Tx
/*********************************************************************
** Device: A7106 **
** File: main.c **
** **
** Description: **
** This file is a sample code for your reference. **
** **
** Copyright (C) 2008 AMICCOM Corp. **
** **
*********************************************************************/
// #include <reg52.h>
#include <arduino.h>
#include "define.h"
#include "a7106reg.h"
#define NOP do { __asm__ __volatile__ ("nop"); } while (0)
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
// I/O Declaration //
// TODO: USER NEED CHANGE THIS SET TO FIT SYSTEM. //
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//sbit SCS = P1^1;
//sbit SCK = P3^0;
//sbit SDIO = P3^1;
//sbit GPIO1 = P3^3;
//sbit GPIO2 = P3^4;
//#define RF_WTR GPIO2
//#define SDO GPIO1
#define SCS 10
#define SCK 13
#define SDIO 11
#define GPIO1 12
#define GPIO2 3
#define RF_WTR GPIO2
#define SDO GPIO1
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
// Global Variable Declaration //
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
//Uint8 idata RfBuf[64];
//Uint8 idata TmpUint8;
Uint8 RfBuf[64];
Uint8 TmpUint8;
//-----------------------------------------------------------------------------
// 64 bytes PN9 pseudo random code
//-----------------------------------------------------------------------------
//const Uint8 code PN9_Tab[]=
//{ 0xFF,0x83,0xDF,0x17,0x32,0x09,0x4E,0xD1,
// 0xE7,0xCD,0x8A,0x91,0xC6,0xD5,0xC4,0xC4,
// 0x40,0x21,0x18,0x4E,0x55,0x86,0xF4,0xDC,
// 0x8A,0x15,0xA7,0xEC,0x92,0xDF,0x93,0x53,
// 0x30,0x18,0xCA,0x34,0xBF,0xA2,0xC7,0x59,
// 0x67,0x8F,0xBA,0x0D,0x6D,0xD8,0x2D,0x7D,
// 0x54,0x0A,0x57,0x97,0x70,0x39,0xD2,0x7A,
// 0xEA,0x24,0x33,0x85,0xED,0x9A,0x1D,0xE0
//};
const Uint8 PN9_Tab[] =
{
'H', 'e', 'l', 'l', 'o', '.', ' ', 0x0D,
0x0A, 0xCD, 0x8A, 0x91, 0xC6, 0xD5, 0xC4, 0xC4,
0x40, 0x21, 0x18, 0x4E, 0x55, 0x86, 0xF4, 0xDC,
0x8A, 0x15, 0xA7, 0xEC, 0x92, 0xDF, 0x93, 0x53,
0x30, 0x18, 0xCA, 0x34, 0xBF, 0xA2, 0xC7, 0x59,
0x67, 0x8F, 0xBA, 0x0D, 0x6D, 0xD8, 0x2D, 0x7D,
0x54, 0x0A, 0x57, 0x97, 0x70, 0x39, 0xD2, 0x7A,
0xEA, 0x24, 0x33, 0x85, 0xED, 0x9A, 0x1D, 0xE0
};
//-----------------------------------------------------------------------------
// RF ID code
//-----------------------------------------------------------------------------
//const Uint8 code ID_Tab[4] =
//{
// 0x54, 0x75, 0xC5, 0x2A
//};
const Uint8 ID_Tab[4] =
{
0x54, 0x75, 0xC5, 0x2A
};
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// NOTE !!
// !! THIS CONFIG TABLE ONLY USE ON RF CRYSTAL = 16MHz !!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//const Uint16 code A7106Config[] =
//{
// // address name Descript
// // ------- ---- ---------
// 0x00, //0x00 ; MODE_REG
// 0x62, //0x01 ; MODE_CONTROL_REG FIFO mode, Enable ARSSI, Enable AIF
// 0x00, //0x02 ; CALIBRATION_REG
// 0x3F, //0x03 ; FIFO1_REG FIFO end = 63+1
// 0x40, //0x04 ; FIFO2_REG
// 0x00, //0x05 ; FIFO_REG
// 0x00, //0x06 ; IDCODE_REG
// 0x00, //0x07 ; RCOSC1_REG
// 0x00, //0x08 ; RCOSC2_REG
// 0x00, //0x09 ; RCOSC3_REG
// 0x00, //0x0A ; CKO_REG
// 0x19, // 0x0B GPIO1 register SDO
// 0x01, // 0x0C GPIO2 register, WTR
// 0x05, //0x0D ; CLOCK_REG
// 0x00, //0x0E ; DATARATE_REG 500K bps
// 0x64, //0x0F ; PLL1_REG
// 0x9E, //0x10 ; PLL2_REG
// 0x4B, //0x11 ; PLL3_REG
// 0x00, //0x12 ; PLL4_REG
// 0x02, //0x13 ; PLL5_REG
// 0x16, //0x14 ; TX1_REG f(dev) = 186kHz
// 0x2B, //0x15 ; TX2_REG
// 0x12, //0x16 ; DELAY1_REG TX, PLL setting time = 60us
// 0x40, //0x17 ; DELAY2_REG Crystal Turn on delay = 600us
// 0x62, //0x18 ; RX_REG BW = 500kHz, Up side band
// 0x80, //0x19 ; RXGAIN1
// 0x80, //0x1A ; RXGAIN2
// 0x00, //0x1B ; RXGAIN3
// 0x0A, //0x1C ; RXGAIN4
// 0x32, //0x1D ; RSSI_REG
// 0x03, //0x1E ; ADC_REG
// 0x0F, //0x1F ; CODE1_REG ID=4bytes, Preamble=4bytes, enable CRC
// 0x16, //0x20 ; CODE2_REG
// 0x00, //0x21 ; CODE3_REG
// 0x00, //0x22 ; IFCAL1_REG
// 0x00, //0x23 ; IFCAL2_REG
// 0x00, //0x24 ; VCOCCAL_REG VCO current auto calibration
// 0x00, //0x25 ; VCOCAL1_REG
// 0x23, //0x26 ; VCOCAL2_REG
// 0x70, //0x27 ; BATTERY_REG Regulator = 1.8V, Enable Quick discharge
// 0x17, //0x28 ; TXTEST_REG TX power = 0dBm
// 0x47, //0x29 ; RXDEM1_REG
// 0x80, //0x2A ; RXDEM2_REG
// 0x57, //0x2B ; CPC_REG
// 0x01, //0x2C ; CRYSTALTEST_REG
// 0x45, //0x2D ; PLLTEST_REG
// 0x19, //0x2E ; VCOTEST1_REG
// 0x00, //0x2F ; VCOTEST2_REG
// 0x01, //0x30 ; IFAT_REG
// 0x0F, //0x31 ; RSCALE_REG
// 0x00, //0x32 ; FILTERTEST_REG
// 0x7F, //0x33 ; TMV_REG Ramp Up / Ramp Down enable
//};
const Uint16 A7106Config[] =
{
// address name Descript
// ------- ---- ---------
0x00, //0x00 ; MODE_REG
0x62, //0x01 ; MODE_CONTROL_REG FIFO mode, Enable ARSSI, Enable AIF
0x00, //0x02 ; CALIBRATION_REG
0x3F, //0x03 ; FIFO1_REG FIFO end = 63+1
0x40, //0x04 ; FIFO2_REG
0x00, //0x05 ; FIFO_REG
0x00, //0x06 ; IDCODE_REG
0x00, //0x07 ; RCOSC1_REG
0x00, //0x08 ; RCOSC2_REG
0x00, //0x09 ; RCOSC3_REG
0x00, //0x0A ; CKO_REG
0x19, // 0x0B GPIO1 register SDO
0x01, // 0x0C GPIO2 register, WTR
0x05, //0x0D ; CLOCK_REG
0x00, //0x0E ; DATARATE_REG 500K bps
0x64, //0x0F ; PLL1_REG
0x9E, //0x10 ; PLL2_REG
0x4B, //0x11 ; PLL3_REG
0x00, //0x12 ; PLL4_REG
0x02, //0x13 ; PLL5_REG
0x16, //0x14 ; TX1_REG f(dev) = 186kHz
0x2B, //0x15 ; TX2_REG
0x12, //0x16 ; DELAY1_REG TX, PLL setting time = 60us
0x40, //0x17 ; DELAY2_REG Crystal Turn on delay = 600us
0x62, //0x18 ; RX_REG BW = 500kHz, Up side band
0x80, //0x19 ; RXGAIN1
0x80, //0x1A ; RXGAIN2
0x00, //0x1B ; RXGAIN3
0x0A, //0x1C ; RXGAIN4
0x32, //0x1D ; RSSI_REG
0x03, //0x1E ; ADC_REG
0x0F, //0x1F ; CODE1_REG ID=4bytes, Preamble=4bytes, enable CRC
0x16, //0x20 ; CODE2_REG
0x00, //0x21 ; CODE3_REG
0x00, //0x22 ; IFCAL1_REG
0x00, //0x23 ; IFCAL2_REG
0x00, //0x24 ; VCOCCAL_REG VCO current auto calibration
0x00, //0x25 ; VCOCAL1_REG
0x23, //0x26 ; VCOCAL2_REG
0x70, //0x27 ; BATTERY_REG Regulator = 1.8V, Enable Quick discharge
0x17, //0x28 ; TXTEST_REG TX power = 0dBm
0x47, //0x29 ; RXDEM1_REG
0x80, //0x2A ; RXDEM2_REG
0x57, //0x2B ; CPC_REG
0x01, //0x2C ; CRYSTALTEST_REG
0x45, //0x2D ; PLLTEST_REG
0x19, //0x2E ; VCOTEST1_REG
0x00, //0x2F ; VCOTEST2_REG
0x01, //0x30 ; IFAT_REG
0x0F, //0x31 ; RSCALE_REG
0x00, //0x32 ; FILTERTEST_REG
0x7F, //0x33 ; TMV_REG Ramp Up / Ramp Down enable
};
/*********************************************************************
** function Declaration
*********************************************************************/
void A7106_WriteReg(Uint8, Uint8);
Uint8 A7106_ReadReg(Uint8);
void StrobeCmd(Uint8 src);
void ByteSend(Uint8 src);
void A7106_Reset(void);
void A7106_WriteID(void);
void initRF(void);
void A7106_Config(void);
void A7106_Cal(void);
void WriteFIFO(Uint8 length);
Uint8 ByteRead(void);
void ReadFIFO(Uint8 length);
void Delay1ms(Uint8 n);
void Delay100us_WTR(Uint8 n);
void setup()
{
//initHW
// P0 = 0xFF;
// P1 = 0xFF;
// P2 = 0xFF;
// P3 = 0xFF;
Serial.begin(9600);
pinMode(SCS, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(SDIO, OUTPUT);
pinMode(RF_WTR, INPUT);
pinMode(SDO, INPUT);
initRF();
StrobeCmd(CMD_STBY);
A7106_WriteReg( TXTEST_REG, 0x17 ); // TX power = 0dBm
A7106_WriteReg( DATARATE_REG, 0 ); // Data rate = 500K A7106_WriteReg( DATARATE_REG, 1 ); // Data rate = 250K A7106_WriteReg( DATARATE_REG, 4 ); // Data rate = 100K A7106_WriteReg( DATARATE_REG, 9 ); // Data rate = 50K
A7106_WriteReg( MODECTRL_REG, 0x62 ); // FIFO mode
A7106_WriteReg( PLL1_REG, 100 ); // set radio channel
A7106_WriteReg( CODE1_REG, 0x0F); // enable CRC check function
}
/*********************************************************************
* main loop
*********************************************************************/
void loop()
{
int nVal = 0;
StrobeCmd( CMD_SLEEP ); // let RF to sleep mode
Delay1ms( 10 );
Serial.println("1 CMD_SLEEP");
StrobeCmd( CMD_STBY ); // wakeup RFIC
Delay1ms( 1 ); // wait RFIC's crystal ready
Serial.println("2 CMD_STBY");
WriteFIFO( 8 ); // write data to TX FIFO
Serial.println("3 CMD_TX");
StrobeCmd( CMD_TX );
do
{
nVal = digitalRead(RF_WTR);
Serial.print(" RF_WTR = ");
Serial.println(nVal, HEX);
}
while( nVal ) ; // wait TX finish
//
// A7106_WriteReg( FIFO1_REG, 8-1 ); // set FIFO length
// StrobeCmd( CMD_RFR ); // reset RX FIFO point
// Serial.println("4 CMD_RFR");
// StrobeCmd( CMD_RX );
// Serial.println("5 CMD_RX");
// Delay100us_WTR( 40 );
//
// nVal = digitalRead(RF_WTR);
// if ( nVal == 0 )
// {
// // Get ACK, check CRC
// TmpUint8 = A7106_ReadReg(MODE_REG);
// if ( (TmpUint8 & 0x20) == 0 )
// {
// // CRC pass, read RX FIFO, Link success.
// ReadFIFO( 8 );
// Serial.println("CRC pass, read RX FIFO, Link success.");
// }
// else
// {
// // CRC failed, link failed.
// Serial.println("CRC failed, link failed.");
// }
// }
// else
// {
// // NO ACK response, link failed.
// StrobeCmd( CMD_STBY ); // EXIT RX MODE
// Serial.println("NO ACK response, link failed.");
// }
}
/*********************************************************************
** Err_State
*********************************************************************/
void Err_State(void)
{
//ERR display
Serial.println("Err_State");
;
//Error Proc...
while(1)
{
}
}
/************************************************************************
** A7106_WriteReg
************************************************************************/
void A7106_WriteReg(Uint8 addr, Uint8 dataByte)
{
Uint8 i;
digitalWrite(SCS, 0);
addr |= 0x00; //bit cmd=0,r/w=0
for(i = 0; i < 8; i++)
{
if(addr & 0x80)
digitalWrite(SDIO, 1);
else
digitalWrite(SDIO, 0);
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
addr = addr << 1;
}
NOP;
//send data byte
for(i = 0; i < 8; i++)
{
if(dataByte & 0x80)
digitalWrite(SDIO, 1);
else
digitalWrite(SDIO, 0);
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
dataByte = dataByte << 1;
}
digitalWrite(SCS, 1);
}
/************************************************************************
** A7106_ReadReg
************************************************************************/
Uint8 A7106_ReadReg(Uint8 addr)
{
Uint8 i;
Uint8 tmp;
int nVal = 0;
digitalWrite(SCS, 0);
addr |= 0x40; //bit cmd=0,r/w=1
for(i = 0; i < 8; i++)
{
if(addr & 0x80)
digitalWrite(SDIO, 1);
else
digitalWrite(SDIO, 0);
NOP;
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
addr = addr << 1;
}
NOP;
// digitalWrite(SDO, 1);
;
// pinMode(SDO, INPUT);
//read data
for(i = 0; i < 8; i++)
{
nVal = digitalRead(SDO);
if(nVal)
tmp = (tmp << 1) | 0x01;
else
tmp = tmp << 1;
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
}
digitalWrite(SCS, 1);
// pinMode(SDO, OUTPUT);
return tmp;
}
/*********************************************************************
** Strobe Command
*********************************************************************/
void StrobeCmd(Uint8 src)
{
Uint8 i;
digitalWrite(SCS, 0);
for(i = 0; i < 8; i++)
{
if(src & 0x80)
digitalWrite(SDIO, 1);
else
digitalWrite(SDIO, 0);
NOP;
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
src = src << 1;
}
digitalWrite(SCS, 1);
}
/************************************************************************
** ByteSend
************************************************************************/
void ByteSend(Uint8 src)
{
Uint8 i;
for(i = 0; i < 8; i++)
{
if(src & 0x80)
digitalWrite(SDIO, 1);
else
digitalWrite(SDIO, 0);
NOP;
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
src = src << 1;
}
}
/************************************************************************
** WriteID
************************************************************************/
void A7106_WriteID(void)
{
Uint8 i;
Uint8 addr;
addr = IDCODE_REG; //send address 0x06, bit cmd=0, r/w=0
digitalWrite(SCS, 0);
ByteSend(addr);
for (i = 0; i < 4; i++)
ByteSend(ID_Tab);
digitalWrite(SCS, 1);
}
/************************************************************************
** Reset_RF
************************************************************************/
void A7106_Reset(void)
{
A7106_WriteReg(MODE_REG, 0x00); //reset RF chip
}
/*********************************************************************
** calibration
*********************************************************************/
void A7106_Cal(void)
{
Uint8 tmp;
Uint8 fb, vcb, vb, rh, rl;
//==================================================
// calibration IF, VB, VCB, RSSI
StrobeCmd( CMD_STBY );
A7106_WriteReg( CALIBRATION_REG, 0x0F );
do
{
tmp = A7106_ReadReg( CALIBRATION_REG );
Serial.print("CALIBRATION_REG = ");
Serial.println(tmp, HEX);
}
while ( tmp & 0x0F );
tmp = A7106_ReadReg( IFCAL1_REG );
if ( tmp & 0x10 )
Err_State();
fb = tmp & 0x0F;
tmp = A7106_ReadReg( VCOCCAL_REG );
if ( tmp & 0x10 )
Err_State();
vcb = tmp & 0x0F;
tmp = A7106_ReadReg( VCOCAL1_REG );
if ( tmp & 0x08 )
Err_State();
vb = tmp & 0x07;
rh = A7106_ReadReg( RXGAIN2_REG );
rl = A7106_ReadReg( RXGAIN3_REG );
}
/*********************************************************************
** A7106_RCO_Cal
*********************************************************************/
void A7106_RCO_Cal(void)
{
Uint8 tmp, rcoc;
//==================================================
// calibration RC osc
A7106_WriteReg( RCOSC3_REG, 0x00 ); // RCO oscillator disable
A7106_WriteReg( RCOSC3_REG, 0x0C ); // RC oscillator calibration enable
do
{
tmp = A7106_ReadReg( RCOSC1_REG ) & 0x80;
}
while( tmp );
rcoc = A7106_ReadReg( RCOSC1_REG ) & 0x3F;
if ( rcoc < 10 )
Err_State(); // RCOC too small
}
/*********************************************************************
** A7106_Config
*********************************************************************/
void A7106_Config(void)
{
Uint8 i;
//0x00 mode register, for reset
//0x02 calibration register
//0x05 fifo data register
//0x06 id code register
//0x23 IF calibration II, only read
//0x32 filter test register
for ( i = 1; i <= 0x33; i++ )
{
if ( (i == 0x02) || (i == 0x05) || (i == 0x06) || (i == 0x23) || (i == 0x32) )
continue;
A7106_WriteReg( i, A7106Config );
}
}
/*********************************************************************
** initRF
*********************************************************************/
void initRF(void)
{
//init io pin
digitalWrite(SCS, 1);
digitalWrite(SCK, 0);
digitalWrite(SDIO, 1);
digitalWrite(GPIO1, 1);
digitalWrite(GPIO2, 1);
A7106_Reset(); //reset A7106 RF chip
A7106_WriteID(); //write ID code
A7106_Config(); //config A7106 chip
A7106_Cal(); //calibration IF,vco, vcoc
A7106_RCO_Cal(); // calibration RC Osc (OPTION)
}
//**********************************************************************
// WriteFIFO
//**********************************************************************
void WriteFIFO(Uint8 length)
{
Uint8 i;
A7106_WriteReg( FIFO1_REG, length - 1); // set FIFO length
StrobeCmd(CMD_TFR); // Reset TX FIFO point
digitalWrite(SCS, 0);
ByteSend(FIFO_REG);
for(i = 0; i < length; i++)
ByteSend(PN9_Tab);
digitalWrite(SCS, 1);
}
//**********************************************************************
// ByteRead
//**********************************************************************
Uint8 ByteRead(void)
{
Uint8 i, tmp;
int nVal = 0;
// digitalWrite(SDO, 1);
; //sdo pull high
for(i = 0; i < 8; i++)
{
nVal = digitalRead(SDO);
if(nVal)
tmp = (tmp << 1) | 0x01;
else
tmp = tmp << 1;
digitalWrite(SCK, 1);
NOP;
digitalWrite(SCK, 0);
}
return tmp;
}
//**********************************************************************
// ReadFIFO
//**********************************************************************
void ReadFIFO(Uint8 length)
{
Uint8 i;
digitalWrite(SCS, 0);
ByteSend(FIFO_REG | 0x40);
for(i = 0; i < length; i++)
RfBuf = ByteRead();
digitalWrite(SCS, 1);
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
// DELAY 1MS FUNCTION //
// USER NEED CHANGE THIS FUNCTION TO FIT SYSTEM. //
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
void Delay1ms(Uint8 n)
{
Uint8 i, j;
while(n--)
{
for(j = 0; j < 6; j ++)
{
for(i = 0; i < 235; i ++)
{
NOP;
NOP;
NOP;
NOP;
}
}
}
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
// DELAY 100US WITH WTR FUNCTION //
// USER NEED CHANGE THIS FUNCTION TO FIT SYSTEM. //
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
void Delay100us_WTR(Uint8 n)
{
Uint8 i;
int nVal = 0;
pinMode(RF_WTR, INPUT);
while(n--)
{
for(i = 0; i < 100; i ++)
{
nVal = digitalRead(RF_WTR);
if ( nVal == 0 )
return;
NOP;
NOP;
NOP;
}
}
}
[/kenrobot_code]
|
|