📄 cc1000avr.c
字号:
/****************************************************************************/
/* Application note AN009 */
/* CC1000 interface library */
/* */
/* File: cc1000avr.c */
/* Revision: 2.1 */
/* */
/* Microcontroller: */
/* Atmel AVRmega8L */
/* Written for the IAR AVR compiler */
/* */
/* Author: Karl H. Torvmark, Field Applications Engineer, Chipcon */
/* */
/* Contact: Chipcon AS +47 22 95 85 44 */
/* wireless@chipcon.com */
/* */
/* Changes: */
/* 2.1 : First AVR version */
/****************************************************************************/
/****************************************************************************/
/* This library contains functions for configuring the CC1000. These */
/* routines use bit-banging to program the CC1000. */
/* Routines to read and write the calibration values in the CC1000 are */
/* provided, they aree useful in 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. */
/****************************************************************************/
/* *
* Revision history: *
* *
* $Log: cc1000avr.c,v $
* Revision 2.5 2003/05/08 10:51:52 tos
* Corrected LOCK monitor in Calibrate.
*
* Revision 2.4 2003/05/08 10:05:26 tos
* Corrections according to Errata Note 01: reset freq.synth if unable to lock PLL.
*
* Revision 2.3 2003/04/28 08:21:14 tos
* Corrected inconsistent monitoring of CC1000: [calibration complete] + [lock].
*
*
* *
****************************************************************************/
#include "CC1000.h"
#include "iom8.h"
#include <stdio.h>
/* Pin usage definitions */
#define PDATA 3 // PORTD, pin 3
#define PCLK 4 // PORTD, pin 4
#define PALE 5 // PORTD, pin 5
#define DIO 1 // PORTD, pin 1
#define DCLK 2 // PORTD, pin 2
#define CAL_TIMEOUT 0x7FFE
#define LOCK_TIMEOUT 0x7FFE
/* Contents of CURRENT register for TX and RX, use SmartRF(R) Studio */
/* to find values for your application */
#define TX_CURRENT 0xF3
#define RX_CURRENT 0x8C
/* Contents of PLL register for TX and RX, use SmartRF(R) Studio */
/* to find values for your application */
#define TX_PLL 0x00
#define RX_PLL 0x00
#define PA_VALUE 0xF0
/****************************************************************************/
/* This routine sends new configuration data to the CC1000 */
/* Based on bit bashing (general I/O pin use) */
/****************************************************************************/
void ConfigureCC1000(char Count, short Configuration[])
{
char BitCounter;
char WordCounter;
short Data;
PORTD|=(1<<PALE); //PALE=1
for (WordCounter=0;WordCounter<Count;WordCounter++)
{
Data=Configuration[WordCounter];
PORTD&=~(1<<PALE); // PALE=0
/* Send address bits */
for (BitCounter=0;BitCounter<7;BitCounter++)
{
PORTD|=(1<<PCLK); // PCLK=1
if ((Data&0x8000)==0) {
PORTD&=~(1<<PDATA); // PDATA=0
}
else {
PORTD|=(1<<PDATA); // PDATA=1
}
Data=Data<<1;
PORTD&=~(1<<PCLK); //PCLK=0;
}
/* Send read/write bit */
/* Ignore bit in data, always use 1 */
PORTD|=(1<<PCLK); //PCLK=1
PORTD|=(1<<PDATA); //PDATA=1
PORTD&=~(1<<PCLK); //PCLK=0
Data=Data<<1;
PORTD|=(1<<PCLK); //PCLK=1
PORTD|=(1<<PALE); //PALE=1
/* Send data bits */
for (BitCounter=0;BitCounter<8;BitCounter++)
{
PORTD|=(1<<PCLK); //PCLK=1
if ((Data&0x8000)==0) {
PORTD&=~(1<<PDATA); // PDATA=0
}
else {
PORTD|=(1<<PDATA); // PDATA=1
}
Data=Data<<1;
PORTD&=~(1<<PCLK); //PCLK=0
}
PORTD|=(1<<PCLK); //PCLK=1
} /* Finished with word */
}
/****************************************************************************/
/* This routine writes to a single CC1000 register */
/****************************************************************************/
void WriteToCC1000Register(char addr, char data)
{
short val;
val=(addr&0x7F)<<9 | (data&0xFF);
ConfigureCC1000(1,&val);
}
/****************************************************************************/
/* This routine writes to a single CC1000 register, with data and address */
/* given in the same variable */
/****************************************************************************/
void WriteToCC1000RegisterWord(short addranddata)
{
ConfigureCC1000(1,&addranddata);
}
/****************************************************************************/
/* This routine reads from a single CC1000 register */
/****************************************************************************/
char ReadFromCC1000Register(char addr)
{
char BitCounter;
char Data;
char Debug;
PORTD|=(1<<PALE); //PALE=1
Data=addr<<1;
PORTD&=~(1<<PALE);
/* Send address bits */
for (BitCounter=0;BitCounter<7;BitCounter++)
{
PORTD|=(1<<PCLK); // PCLK=1
if ((Data&0x80)==0) {
PORTD&=~(1<<PDATA); // PDATA=0
}
else {
PORTD|=(1<<PDATA); // PDATA=1
}
Data=Data<<1;
PORTD&=~(1<<PCLK); //PCLK=0;
}
/* Send read/write bit */
/* Ignore bit in data, always use 0 */
PORTD|=(1<<PCLK); //PCLK=1
PORTD&=~(1<<PDATA); //PDATA=0
PORTD&=~(1<<PCLK); //PCLK=0
PORTD|=(1<<PCLK); //PCLK=1
PORTD|=(1<<PALE); //PALE=1
/* Receive data bits */
PORTD|=(1<<PDATA); //PDATA=1
DDRD&=~(1<<PDATA); /* Set up PDATA as an input */
for (BitCounter=0;BitCounter<8;BitCounter++)
{
PORTD&=~(1<<PCLK); //PCLK=0
Data=Data<<1;
Debug=(1<<PDATA);
if ((PIND&Debug)==0) {
Data&=0xFE;
} else {
Data|=0x01;
}
PORTD|=(1<<PCLK); //PCLK=1
}
DDRD|=(1<<PDATA); /* Set up PDATA as an output again */
return Data;
}
/****************************************************************************/
/* This routine resets the CC1000, clearing all registers. */
/****************************************************************************/
void ResetCC1000(void)
{
char MainValue;
MainValue=ReadFromCC1000Register(CC1000_MAIN);
WriteToCC1000Register(CC1000_MAIN,MainValue & 0xFE); // Reset CC1000
WriteToCC1000Register(CC1000_MAIN,MainValue | 0x01); // Bring CC1000 out of reset
}
/****************************************************************************/
/* This routine calibrates the CC1000 */
/* Returns 0 if calibration fails, non-zero otherwise. Checks the LOCK */
/* to check for success. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -