?? slave._c
字號(hào):
//SLAVE
//最關(guān)鍵的是SPI的初始化,MSB首位和極性方式00,極性方式錯(cuò)誤,數(shù)據(jù)則不正確
#include "config.h"
#include "nrf905.h"
#define MASTER //編譯控制是主機(jī)還是從機(jī)
#define T0N 25
void DelayUs(uint);
void DelayMs(uchar);
//32字節(jié)數(shù)據(jù)包發(fā)送時(shí)間
//=650us_StartUp+200us_Preamble+(4+32+2)Byts*8Bit/50000bps*1000000us=6.6ms
#define RFTN 25 //發(fā)送測試間隔 10*20ms
bit bTimer,bRfSend;
uchar T0Cnt,RfCnt;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xff;
PORTB = BIT(AM)|BIT(CD)|BIT(DR)|BIT(MISO);
DDRB = BIT(CSN)|BIT(MOSI)|BIT(SCK);
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = BIT(uPCLK);
DDRD = BIT(TRXCE)|BIT(TX_EN)|BIT(PWR)|BIT(LED)|0x07;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 20mSec
// actual value: 19.861mSec (0.7%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x71; //set count
OCR0 = 0x8F; //set compare
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x29; //reload counter value
if (--T0Cnt==0)
{T0Cnt=T0N;
bTimer=1;
}
if (--RfCnt==0)
{RfCnt=RFTN;
bRfSend=1;
}
}
//SPI initialize
void spi_init(void)
{uchar temp;
SPCR = 0x51; //不使用SPI中斷,SPI允許,主機(jī)模式,MSB,極性方式00,1/16系統(tǒng)時(shí)鐘速率
SPSR = 0x00; //setup SPI
temp = SPSR; //!!!
temp = SPDR; //清空SPI,和中斷標(biāo)志,使SPI空閑
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
spi_init();
init_lcd();
DISLCD();
MCUCR = BIT(ISC01); //下降沿觸發(fā)
GICR = 0x00;
TIMSK = 0x05; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//接收處理
void RfRecvProc()
{uchar i;
if ((PINB&(1<<AM))==0) return;//一般先AM=1指示地址匹配對
if ((PINB&(1<<DR))==0) return;//DR=1時(shí)表示數(shù)據(jù)接收對而且Crc正確
//已經(jīng)接收到數(shù)據(jù)
nrf905_ReadData();//讀出...
//數(shù)據(jù)接收正確...燈指示
CPL(PORTD,LED);
for (i=0;i<SIZE;i++){seg(RxBuf[i]);}
LCD(RxBuf[0]);
//從機(jī)回送數(shù)據(jù)包,這樣雙方都能指示是否收到有效數(shù)據(jù)包
#ifndef MASTER
//RfSendProc();
#endif
}
//發(fā)送測試
void RfSendProc()
{uchar i;
for (i=0;i<SIZE;i++) TxBuf[i]=2*i;
nrf905_SendData();//發(fā)送測試數(shù)據(jù)
nrf905_RxOn();//重新回到接收狀態(tài)
}
void TimerFunc()
{
bTimer=0;
//WDR();//clear WDT
CPL(PORTD,LED);
//SPDR=66;
}
void SystemIni()
{
T0Cnt=T0N;
RfCnt=RFTN;
}
void DelayMs(uchar ms)
{char i;
for (i=0;i<ms;i++)
{DelayUs(1000);
}
return;
}
void DelayUs(uint us)
{uint i;
for (i=0;i<us;i++)
{NOP();NOP();NOP();NOP();NOP();NOP();
}
}
void main()
{
init_devices();
SystemIni();
nrf905_Init();
ENLCD();
PutChar('W');
PutChar('e');
PutChar('l');
PutChar('c');
PutChar('o');
PutChar('m');
PutChar('e');
PutChar(' ');
PutChar('t');
PutChar('o');
PutChar(' ');
PutChar('X');
PutChar('G');
PutChar('C');
DelayMs(1000);
DelayMs(1000);
DelayMs(1000);
DelayMs(1000);
clear_lcd();
DISLCD();
while (1)
{
// if (bTimer) TimerFunc();
#ifdef MASTER
if (bRfSend)
{ bRfSend=0;
RfSendProc();//發(fā)送測試
}
#endif
RfRecvProc();//接收處理
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -