📄 cc1020pic.c
字号:
/****************************************************************************/
/* Microcontroller (PIC) software library for CC1020 application */
/* */
/* File: cc1020pic.c */
/* */
/* Microcontroller: */
/* Microchip PIC16F876 */
/* */
/* Written for the IAR PIC16 compiler */
/* */
/* Author: Arne Rogndalen, Design Engineer, Chipcon */
/* Torgeir Sundet, FAE Software, Chipcon */
/* */
/* Contact: Chipcon AS +47 22 95 85 44 */
/* support@chipcon.com */
/* */
/****************************************************************************/
/****************************************************************************/
/* This library contains functions for configuring the CC1020. These */
/* routines use bit-banging to program the CC1020, faster configuration is */
/* possible by using a synchronous serial port such as a SPI interface. */
/* The header file "modemhw.h" contains definitions for the various I/O */
/* pins, the user should make a similar file to name the pins used to */
/* communicate with the CC1020. Routines to read and write the calibration */
/* values in the CC1020 are provided, they are not used in this reference */
/* application, but are useful in other applications, most notably */
/* frequency-agile and frequency hopping applications. See application */
/* note AN009 for more information. */
/* The routines in this file will have to be adapted depending on the MCU */
/* and compiler used. The method used for shifting data in and out may have */
/* to be changed if the bit ordering for bitfields is different from the */
/* IAR PIC compiler. */
/* */
/* Configuration routines are included in two versions: one using general */
/* I/O ports ("bit-banging"), and one using the built-in SPI interface of */
/* the PIC16F876. If possible, the SPI version should be used, as this is */
/* much faster. The SPI versions are used if the symbol "SPI" is defined, */
/* otherwise the general I/O-based version is used. */
/****************************************************************************/
/* *
* Revision history: *
* *
* $Log: cc1020pic.c,v $
* Revision 1.9 2005/11/22 18:23:18 tos
* Mods:
* - Implemented support for Errata Note 04.
* - Implemented support for RSSI measurement.
*
* Revision 1.8 2003/12/16 15:01:23 tos
* Removed 5 msec delay/wait before LOCK monitor.
*
* Revision 1.7 2003/09/19 12:14:57 tos
* Mod's in TX-setup:
* - always tun off DCLK squelch in TX.
* - turn on TX before PA is increased.
*
* Revision 1.6 2003/08/14 11:06:36 tos
* Modified some function prototypes to enable PA_POWER as input argument.
*
* Revision 1.5 2003/07/23 14:36:19 tos
* Correction of reset + power-down in accordance with design recommendation.
*
* Revision 1.4 2003/06/27 17:22:17 tos
* Modified setup routines to allow separat RX/TX settings in ANALOG register.
*
* Revision 1.3 2003/06/02 14:43:33 tos
* Recalibrate if LOCK failure after RX/TX setup.
*
* Revision 1.2 2003/05/23 13:28:54 tos
* Tuning+testing before release.
*
* Revision 1.1 2003/05/21 09:48:17 tos
* Initial version in CVS.
*
*
* *
****************************************************************************/
#include "io16f876.h"
#include "CC1020.h"
#include "modemhw.h"
/****************************************************************************/
/* This routine sends new configuration data to the CC1020 */
/****************************************************************************/
void ConfigureCC1020(char Count, short Configuration[])
{
short val;
char i;
for (i=0;i<Count;i++) {
val=Configuration[i];
WriteToCC1020RegisterWord(val);
}
}
/****************************************************************************/
/* SPI versions of configuration routines. The SPI interface must be */
/* initialised correctly before use */
/****************************************************************************/
#ifdef SPI
/****************************************************************************/
/* This routine sets up the CC1020 for SPI transfer */
/****************************************************************************/
void SetupCC1020ForSPI(void)
{
SSPSTAT=0x40;
SSPCON=0x20;
}
/****************************************************************************/
/* This routine writes to a single CC1020 register */
/****************************************************************************/
void WriteToCC1020Register(char addr, char data)
{
char dummy;
PSEL=0;
dummy=SSPBUF;
SSPBUF=(addr<<1)|0x01; // Write address to CC1020, write bit is always 1
// Wait until data is written
while (BF==0);
dummy=SSPBUF;
SSPBUF=data;
while (BF==0);
PSEL=1;
}
/****************************************************************************/
/* This routine writes to a single CC1020 register, with data and address */
/* given in the same variable */
/****************************************************************************/
void WriteToCC1020RegisterWord(short addranddata)
{
char dummy;
union {
unsigned short data;
struct {
char LowByte;
char HighByte;
};
};
data=addranddata;
PSEL=0;
dummy=SSPBUF;
SSPBUF=LowByte|0x01; // Write address to CC1020, write bit is always 1
// Wait until data is written
while (BF==0);
dummy=SSPBUF;
SSPBUF=HighByte;
while (BF==0);
PSEL=1;
}
/****************************************************************************/
/* This routine reads from a single CC1020 register */
/****************************************************************************/
char ReadFromCC1020Register(char addr)
{
char Value;
PSEL=0;
Value=SSPBUF;
SSPBUF=(addr<<1)&0xFE; // Write address to CC1020, write bit is always 0
// Wait until data is written
while (BF==0);
SSPOV=0;
// Switch direction
PDI=1;
TRISC|=0x20; // Set up PDATAOUT as an input
SSPBUF=0xFF; // Dummy write
while (BF==0);
Value=SSPBUF;
TRISC&=~0x20; // Set PDATAOUT as an output
PSEL=1;
return Value;
}
#else
/****************************************************************************/
/* General I/O pin "bit-bashing" versions of configuration routines. */
/****************************************************************************/
/****************************************************************************/
/* This routine writes to a single CC1020 register */
/****************************************************************************/
void WriteToCC1020Register(char addr, char data)
{
short val;
val=(short) (addr&0x7F)<<9 | (short) data &0x00FF;
WriteToCC1020RegisterWord(val);
}
/****************************************************************************/
/* This routine writes to a single CC1020 register, with address and data */
/* given in the same variable */
/****************************************************************************/
void WriteToCC1020RegisterWord(short addranddata)
{
char BitCounter;
char Low;
char High;
union {
unsigned short data;
struct
{
char LowByte;
char HighByte;
};
};
PSEL=1;
data=addranddata;
PSEL=0;
Low=LowByte;
// Send address bits
for (BitCounter=0;BitCounter<7;BitCounter++)
{
PCLK=0;
PDI=((Low&0x80)>>7);
Low=Low<<1;
PCLK=1;
}
// Send read/write bit
// Ignore bit in data, always use 1
PCLK=0;
PDI=1;
PCLK=1;
PCLK=0;
High=HighByte;
// Send data bits
for (BitCounter=0;BitCounter<8;BitCounter++)
{
PCLK=0;
PDI=((High&0x80)>>7);
High=High<<1;
PCLK=1;
}
PCLK=0;
PSEL=1;
}
/****************************************************************************/
/* This routine reads from a single CC1020 register */
/****************************************************************************/
char ReadFromCC1020Register(char addr)
{
char BitCounter;
char Byte;
PSEL=1;
Byte=addr<<1;
PSEL=0;
// Send address bits
for (BitCounter=0;BitCounter<7;BitCounter++)
{
PCLK=0;
PDI=((Byte&0x80)>>7);
Byte=Byte<<1;
PCLK=1;
}
// Send read/write bit
// Ignore bit in data, always use 0
PCLK=0;
PDI=0;
PCLK=1;
PCLK=0;
// Receive data bits
PDI=1;
TRISC|=0x20; // Set up PDATA as an input
for (BitCounter=0;BitCounter<8;BitCounter++)
{
PCLK=1;
Byte=Byte<<1;
Byte=Byte|PDO;
PCLK=0;
}
TRISC&=~0x20; // Set up PDATA as an output again
PSEL=1;
return Byte;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -