📄 master._c
字号:
//master
//最关键的是SPI的初始化,MSB首位和极性方式00,极性方式错误,数据则不正确
#include <iom16v.h>
#include <macros.h>
#include "config.h"
#include "nrf905.h"
#define MASTER //编译控制是主机还是从机
#define T0N 25
void DelayUs(uint);
void DelayMs(uchar);
//32字节数据包发送时间
//=650us_StartUp+200us_Preamble+(4+32+2)Byts*8Bit/50000bps*1000000us=6.6ms
#define RFTN 25 //发送测试间隔 10*20ms
bit bTimer,bRfSend;
uchar T0Cnt,RfCnt;
void port_init(void)
{
PORTA = 0x0F;
DDRA = 0xF0;
PORTB = BIT(AM)|BIT(CD)|BIT(DR)|BIT(MISO);
DDRB = BIT(CSN)|BIT(MOSI)|BIT(SCK);
PORTC = 0x00;
DDRC = 0x00;
PORTD = BIT(uPCLK)|BIT(2);
DDRD = BIT(TRXCE)|BIT(TX_EN)|BIT(PWR)|BIT(LED);
}
//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 = 0xD1; //SPI中断允许,SPI允许,主机模式,MSB,极性方式00,1/16系统时钟速率
SPCR = 0x51; //不使用SPI中断,其它同上
SPSR = 0x00; //setup SPI
temp = SPSR; //!!!
temp = SPDR; //清空SPI,和中断标志,使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();
MCUCR = BIT(ISC01); //下降沿触发
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时表示数据接收对而且Crc正确
//已经接收到数据
nrf905_ReadData();//读出...
for (i=0;i<SIZE;i++)
{ if (RxBuf[i]!=(i+i))
return;
}
//数据接收正确...灯指示
CPL(PORTD,LED);
//从机回送数据包,这样双方都能指示是否收到有效数据包
#ifndef MASTER
//RfSendProc();
#endif
}
//发送测试
void RfSendProc()
{uchar i;
for (i=0;i<SIZE;i++) TxBuf[i]=key;
nrf905_SendData();//发送测试数据
nrf905_RxOn();//重新回到接收状态
}
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();
}
}
////////////////////////////key interrupt/////////////////////////////////
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
uchar key_buf=16,i;
GICR=0x00; //关闭中断使能
DDRA=0xf0; //行列反转读取键盘的值
PORTA=0x0f;
if((~PINA)&0x01)
key_buf=0;
else if((~PINA)&0x02)
key_buf=1;
else if((~PINA)&0x04)
key_buf=2;
else if((~PINA)&0x08)
key_buf=3;
DDRA=0x0f;
PORTA=0xf0;
DelayUs(2000);
if((~PINA)&0x10)
key_buf+=0;
else if((~PINA)&0x20)
key_buf+=4;
else if((~PINA)&0x40)
key_buf+=8;
else if((~PINA)&0x80)
key_buf+=12;
else
key_buf=16;
if(key_buf!=16)
{
key=key_buf;
RfSendProc();
key_buf=16;
while((~PINA)&0xf0) ;//检测按键是否释放,若没有释放就一直循环
}
DDRA=0xf0;
PORTA=0x0f;
GIFR=0x40;
GICR=0x40; //中断复位
}
///////////////////////key over/////////////////////////////////////////
void main()
{
init_devices();
SystemIni();
nrf905_Init();
GICR = BIT(INT0);
while (1)
{
RfRecvProc();//接收处理
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -