📄 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: *
*
*
*Changed to ATmega48 and Pin ReAssignment 2006-8-13 8:57 LY
*Change to CodeViosion AVR C compiler and IDE
*
*
*
*
* $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 "mega48.h"
#include <stdio.h>
#include <configure.h>
extern void putchar(char c);
extern void writeln(char flash*str);
/****************************************************************************/
/* 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;
PORTC|=(1<<PALE); //PALE=1
for (WordCounter=0;WordCounter<Count;WordCounter++)
{
Data=Configuration[WordCounter];
PORTC&=~(1<<PALE); // PALE=0
/* Send address bits */
for (BitCounter=0;BitCounter<7;BitCounter++)
{
PORTC|=(1<<PCLK); // PCLK=1
if ((Data&0x8000)==0) {
PORTC&=~(1<<PDATA); // PDATA=0
}
else {
PORTC|=(1<<PDATA); // PDATA=1
}
Data=Data<<1;
PORTC&=~(1<<PCLK); //PCLK=0;
}
/* Send read/write bit */
/* Ignore bit in data, always use 1 */
PORTC|=(1<<PCLK); //PCLK=1
PORTC|=(1<<PDATA); //PDATA=1
PORTC&=~(1<<PCLK); //PCLK=0
Data=Data<<1;
PORTC|=(1<<PCLK); //PCLK=1
PORTC|=(1<<PALE); //PALE=1
/* Send data bits */
for (BitCounter=0;BitCounter<8;BitCounter++)
{
PORTC|=(1<<PCLK); //PCLK=1
if ((Data&0x8000)==0) {
PORTC&=~(1<<PDATA); // PDATA=0
}
else {
PORTC|=(1<<PDATA); // PDATA=1
}
Data=Data<<1;
PORTC&=~(1<<PCLK); //PCLK=0
}
PORTC|=(1<<PCLK); //PCLK=1
} /* Finished with word */
}
/****************************************************************************/
/* This routine writes to a single CC1000 register */
/****************************************************************************/
void WriteToCC1000Register(char addr, char data)
{
short val;
val=(short)(addr&0x7F)<<9 | (short) (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;
PORTC|=(1<<PALE); //PALE=1
Data=addr<<1;
PORTC&=~(1<<PALE);
/* Send address bits */
for (BitCounter=0;BitCounter<7;BitCounter++)
{
PORTC|=(1<<PCLK); // PCLK=1
if ((Data&0x80)==0) {
PORTC&=~(1<<PDATA); // PDATA=0
}
else {
PORTC|=(1<<PDATA); // PDATA=1
}
Data=Data<<1;
PORTC&=~(1<<PCLK); //PCLK=0;
}
/* Send read/write bit */
/* Ignore bit in data, always use 0 */
PORTC|=(1<<PCLK); //PCLK=1
PORTC&=~(1<<PDATA); //PDATA=0
PORTC&=~(1<<PCLK); //PCLK=0
PORTC|=(1<<PCLK); //PCLK=1
PORTC|=(1<<PALE); //PALE=1
/* Receive data bits */
PORTC|=(1<<PDATA); //PDATA=1
DDRC&=~(1<<PDATA); /* Set up PDATA as an input */
for (BitCounter=0;BitCounter<8;BitCounter++)
{
PORTC&=~(1<<PCLK); //PCLK=0
Data=Data<<1;
Debug=(1<<PDATA);
if ((PINC&Debug)==0) {
Data&=0xFE;
} else {
Data|=0x01;
}
PORTC|=(1<<PCLK); //PCLK=1
}
DDRC|=(1<<PDATA); /* Set up PDATA as an output again */
return Data;
}
/****************************************************************************/
/* This routine resets the CC1000, clearing all registers. */
/****************************************************************************/
void ResetCC1000(void)
{
char MainValue;
int i;
MainValue=ReadFromCC1000Register(CC1000_MAIN);
WriteToCC1000Register(CC1000_MAIN,MainValue & 0xFE); // Reset CC1000
for (i=0;i<0x1000;i++);
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. */
/****************************************************************************/
char CalibrateCC1000(void)
{
int TimeOutCounter,i;
WriteToCC1000Register(CC1000_PA_POW,0x00); // Turn off PA to avoid spurs
// during calibration in TX mode
WriteToCC1000Register(CC1000_CAL,0xA6); // Start calibration
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -