/*************************************************************************
MSP430锁相环初始化
*************************************************************************/
void CLK_Init()
{
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
// Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
UCSCTL2 |= 121; // Set DCO Multiplier for 8MHz
// (N + 1) * FLLRef = Fdco
// (249 + 1) * 32768 = 8MHz
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle
__delay_cycles(250000);
/***************************spi程序**************************/
uchar SPI_RW(uchar data)
{
uchar bit;
for(bit=0;bit<8;bit++)
{ //先高位,后低位
if(data & BIT7) MOSI_H;
else MOSI_L;
delay_us(2); //数据建立时间 1us
SCK_H;
delay_us(2);
data <<= 1; //一条指令的时间
if(SPI_IN) data |= BIT0; // capture current MISO bit
else data &=~BIT0;
SCK_L;
delay_us(1);
}
delay_us(1);
return(data); // return read data
}
uchar SPI_WR_Reg(uchar reg, uchar value)
{
uchar status;
CSN_L; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN_H; // CSN high again
return(status); // return nRF24L01 status byte
}
uchar SPI_RD_Reg(uchar reg)
{
uchar reg_val;
CSN_L; // CSN low, initialize SPI communication
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN_H; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/*接收缓冲区访问函数:主要用来在接收时读取FIFO 缓冲区中的值。
基本思路就是通过READ_REG 命令把数据从接收FIFO(RD_RX_PLOAD)中读出并存到数组里面去。*/
uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar bytes)
{
uchar status,byte_ctr;
CSN_L; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
for(byte_ctr=0;byte_ctr<bytes;byte_ctr++)
pBuf[byte_ctr] = SPI_RW(0); // Perform SPI_RW to read byte from nRF24L01
CSN_H; // Set CSN high again
return(status); // return nRF24L01 status byte
}
/*发射缓冲区访问函数:主要用来把数组里的数放到发射FIFO 缓冲区中。
基本思路就是通过WRITE_REG 命令把数据存到发射FIFO(WR_TX_PLOAD)中去。*/
uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes)
{
uchar status,byte_ctr;
CSN_L; // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status byte
delay_us(1);
for(byte_ctr=0; byte_ctr<bytes; byte_ctr++) // then write all byte in buffer(*pBuf)
SPI_RW(*pBuf++);
CSN_H; // Set CSN high again
return(status); // return nRF24L01 status byte
}