?? ptr8000.c
字號:
//ICC-AVR application builder : 2006-4-4 11:28:26
// Target : M16 PTR8000模塊簡單測試
// Crystal: 11.059Mhz
//最關(guān)鍵的是SPI的初始化,MSB首位和極性方式00,極性方式錯誤,數(shù)據(jù)則不正確
#include <iom16v.h>
#include <macros.h>
#include "config.h"
#include "nrf905.h"
#define MASTER //編譯控制是主機還是從機
#define T0N 25
//32字節(jié)數(shù)據(jù)包發(fā)送時間
//=650us_StartUp+200us_Preamble+(4+32+2)Byts*8Bit/50000bps*1000000us=6.6ms
#define RFTN 10 //發(fā)送測試間隔 10*20ms
bit bTimer,bRfSend;
uchar T0Cnt,RfCnt;
void port_init(void)
{
PORTA = 0x08;
DDRA = 0x37;
PORTB = 0x50;
DDRB = 0xBF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x1C;
DDRD = 0x80;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 20mSec
// actual value: 19.908mSec (0.5%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x29; //set count
OCR0 = 0xD7; //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;
}
}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 10mSec
// actual value: 9.999mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xCA; //setup
TCNT1L = 0x01;
OCR1AH = 0x35;
OCR1AL = 0xFF;
OCR1BH = 0x35;
OCR1BL = 0xFF;
ICR1H = 0x35;
ICR1L = 0xFF;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xCA; //reload counter high value
TCNT1L = 0x01; //reload counter low value
}
//SPI initialize
void spi_init(void)
{uchar temp;
//SPCR = 0xD1; //SPI中斷允許,SPI允許,主機模式,MSB,極性方式00,1/16系統(tǒng)時鐘速率
SPCR = 0x51; //不使用SPI中斷,其它同上
SPSR = 0x00; //setup SPI
temp = SPSR; //!!!
temp = SPDR; //清空SPI,和中斷標(biāo)志,使SPI空閑
}
#pragma interrupt_handler spi_stc_isr:11
void spi_stc_isr(void)
{uchar data;
//byte in SPDR has been sent/received
//data=SPDR;
//SPDR=0x66;
}
//UART0 initialize
// desired baud rate: 19200
// actual: baud rate:19200 (0.0%)
// char size: 5 bits
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x00;
UBRRL = 0x23; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}
//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();
timer1_init();
spi_init();
uart0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x05; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main()
{
init_devices();
SystemIni();
nrf905_Init();
while (1)
{
if (bTimer) TimerFunc();
#ifdef MASTER
if (bRfSend)
{ bRfSend=0;
RfSendProc();//發(fā)送測試
}
#endif
RfRecvProc();//接收處理
};
}
//接收處理
void RfRecvProc()
{uchar i;
//if ((PIND&(1<<AM))==0) return;//一般先AM=1指示地址匹配對
if ((PIND&(1<<DR))==0) return;//DR=1時表示數(shù)據(jù)接收對而且Crc正確
//已經(jīng)接收到數(shù)據(jù)
nrf905_ReadData();//讀出...
for (i=0;i<32;i++)
{ if (RxBuf[i]!=(i+i))
return;
}
//數(shù)據(jù)接收正確...燈指示
CPL(PORTD,LED);
//從機回送數(shù)據(jù)包,這樣雙方都能指示是否收到有效數(shù)據(jù)包
#ifndef MASTER
RfSendProc();
#endif
}
//發(fā)送測試
void RfSendProc()
{uchar i;
for (i=0;i<32;i++) TxBuf[i]=i+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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -