功能:PTR8000循环发送数据0-255
时间:2009-06-20第一次用MEGA8L调试成功
电源:3.3V
晶振:1MHz内部
方式:IO模式模拟SPI时序,可以根据需要自行修改!
*****************************************/
#include
#include
#define uint unsigned int
#define uchar unsigned char
//*************配置口定义*************//
#define ptTXEN (1<
#define PTR8_TXEN_clr PORTB&=~ptTXEN;
#define TRX_CE (1<
#define PTR8_TRX_CE_clr PORTB&=~TRX_CE;
#define CSN (1<
#define PTR8_CSN_clr PORTB&=~CSN;
#define ptrMOSI (1<
#define PTRx_MOSI_clr PORTB&=~ptrMOSI;
#define MISO (1<
#define PTR8_MISO_clr PORTB&=~MISO;
#define SCK (1<
#define PTR8_SCK_clr PORTB&=~SCK;
#define PWR (1<
#define PTR8_PWR_clr PORTB&=~PWR;
/**************状态输出口**************/
#define AM (PIND&(1<
uchar da=0;
uchar TxRxBuffer[5];
void port_init(void)
{
PORTB = 0xff;
DDRB = 0x6F;
PORTC = 0x4f; //m103 output only
DDRC = 0x7F;
PORTD = 0xff;
DDRD = 0xcF;
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
///////////延时/////////////////
void Delay(uint x)
{uint i;
for(i=0;i
////////////用SPI口写数据至NRF905内//////////
void SpiWrite(unsigned char b)
{
unsigned char i=8;
while (i--)
{
Delay(10);
PTR8_SCK_clr;//SCK=0;
if ((b&0x80)) PTRx_MOSI_set;
if (!(b&0x80)) PTRx_MOSI_clr;
b<<=1 ;
Delay(10);
PTR8_SCK_set;//SCK=1;
Delay(10);
PTR8_SCK_clr;//SCK=0;
}
PTR8_SCK_clr;//SCK=0;
}
/********************
;写发射数据命令:20H
;读发射数据命令:21H
;写发射地址命令:22H
;读发射地址命令:23H
;读接收数据命令:24H
*********************/
void TxPacket(uchar da)
{ PTR8_PWR_set; //PWR=1;
PTR8_TXEN_set; //TXEN=1;
PTR8_CSN_clr; //CSN=0;
SpiWrite(0x22); //写发送地址,后面跟4字节地址//
SpiWrite(0xE7);
SpiWrite(0xE7);
SpiWrite(0xE7);
SpiWrite(0xE7);
PTR8_CSN_set; //CSN=1;
Delay(5);
PTR8_CSN_clr; //CSN=0;
SpiWrite(0x20); //写发送数据命令,后面跟1字节数据//
SpiWrite(da);
PTR8_CSN_set; //CSN=1;
Delay(5);
PTR8_TRX_CE_set;//TRX_CE=1;使能发射模式//
Delay(100); //等带发送完成//
while(!DR);
PTR8_TRX_CE_clr;//TRX_CE=0;
}
////////////初始化配置寄存器////////////////
void Ini_System(void)
{
PTR8_CSN_set; //CSN=1;
PTR8_SCK_clr; //SCK=0;
PTR8_PWR_set; //PWR=1;
PTR8_TRX_CE_clr;//TRX_CE=0;
PTR8_TXEN_clr; //TXEN=0;
Delay(5);
PTR8_CSN_clr; //CSN=0;
SpiWrite( 0x00); //配置命令//
SpiWrite( 0x6C); //CH_NO,配置频段在433.2MHZ
SpiWrite( 0x0C); //输出功率为10db,不重发,节电为正常模式
SpiWrite( 0x44); //地址宽度设置,为4字节
SpiWrite( 0x01);SpiWrite( 0x01);//接收发送有效数据长度为1字节
SpiWrite( 0xE7);SpiWrite( 0xE7);SpiWrite( 0xE7);SpiWrite( 0xE7);//接收地址
SpiWrite( 0xDE);
PTR8_CSN_set; //CSN=1;
PTR8_PWR_set; //PWR=1;
PTR8_TRX_CE_clr; //TRX_CE=0;
PTR8_TXEN_clr; //TXEN=0;
Delay(1000);
}
void main(void)
{
init_devices();
Ini_System();
PTR8_PWR_clr; //PWR=0;
while(1)
{ TxPacket(da);
PORTC^=(3<<4);
Delay(50000);Delay(30000);
da++; //SEND 0-255
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』