一、硬件知识
1.JoyStick摇杆
其实此模块就是一电位器, 模块特设二路模拟输出和一路数字输出接口。x、y维的数据输出就是模拟端口读出的电压值。此图上面没有画出z维的数据输出,z维只输出0和1,通过一按键就能实现。总而言之,它就是电位器和按键的组合体。x、y维我们接到两个模拟端口去读它们的值,而z维我们则接到数字口,接上电源和地。
2.NRF24L01 2.4G无线模块
#include "bsp_nrf24l01.h"
const unsigned char TX_ADDRESS[5] = {0x34, 0x43, 0x10, 0x10, 0x01};
const unsigned char RX_ADDRESS[5] = {0x34, 0x43, 0x10, 0x10, 0x01};
void NRF24L01_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1, ENABLE);
//PB10-CE, PB11-CSN CE:模式控制线。在 CSN为低的情况下,CE 协同CONFIG 寄存器共同决定NRF24L01 的状态
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//IRQ-PA4 中断信号线。中断时变为低电平,在以下三种情况变低:Tx FIFO 发完并且收到ACK(使能ACK情况下)、Rx FIFO收到数据、达到最大重发次数。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA5:SCK SPI时钟线
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA6:MISO SPI数据线(主机输入,从机输出)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA7:MOSI SPI数据线(主机输出,从机输入)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
unsigned char SPI1_ReadWriteByte(unsigned char Data)
{
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, Data);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
void NRF24L01_Write_Reg(unsigned char reg, unsigned char value)
{
NRF24L01_CSN_L;
SPI1_ReadWriteByte(reg);
SPI1_ReadWriteByte(value);
NRF24L01_CSN_H;
}
unsigned char NRF24L01_Read_Reg(unsigned char reg)
{
unsigned char reg_val;
NRF24L01_CSN_L;
SPI1_ReadWriteByte(reg);
reg_val = SPI1_ReadWriteByte(0x00);
NRF24L01_CSN_H;
return reg_val;
}
void NRF24L01_Write_Buf(unsigned char reg, const unsigned char *pbuf, unsigned int len)
{
int i;
NRF24L01_CSN_L;
SPI1_ReadWriteByte(reg);
for(i = 0; i < len; i++)
SPI1_ReadWriteByte(*pbuf++);
NRF24L01_CSN_H;
}
void NRF24L01_Read_Buf(unsigned char reg, unsigned char *pbuf, unsigned int len)
{
int i;
NRF24L01_CSN_L;
SPI1_ReadWriteByte(reg);
for(i = 0; i < len; i++)
pbuf[i] = SPI1_ReadWriteByte(0x00);
NRF24L01_CSN_H;
}
unsigned int NRF24L01_Check(void)
{
unsigned char ch;
NRF24L01_Write_Reg(NRF_WRITE_REG + TX_ADDR, 'A');
ch = NRF24L01_Read_Reg(NRF_READ_REG + TX_ADDR);
if(ch == 'A')
return 0;
else
return 1;
}
void Set_NRF24L01_TX_Mode(void)
{
NRF24L01_CE_L;
NRF24L01_Write_Buf(NRF_WRITE_REG + TX_ADDR, TX_ADDRESS, 5);
NRF24L01_Write_Reg(NRF_WRITE_REG + SETUP_RETR, 0x1a);
NRF24L01_Write_Reg(NRF_WRITE_REG + CONFIG, 0x0E);
NRF24L01_CE_H;
}
unsigned int NRF24L01_TxPacket(unsigned char *txbuf)
{
unsigned char stat;
NRF24L01_Write_Buf(NRF_WRITE_REG + RX_ADDR_P0, RX_ADDRESS, 5);
NRF24L01_Write_Buf(WR_TX_PLOAD, txbuf, 32);
while(NRF24L01_IRQ != 0);
stat = NRF24L01_Read_Reg(NRF_READ_REG + STATUS);
NRF24L01_Write_Reg(NRF_WRITE_REG + STATUS, stat);
if(stat & MAX_RT)
{
NRF24L01_Write_Reg(FLUSH_TX, 0xff);
return 1;
}
if(stat & TX_DS)
return 0;
return 1;
}
void Set_NRF24L01_RX_Mode(void)
{
NRF24L01_CE_L;
NRF24L01_Write_Buf(NRF_WRITE_REG + RX_ADDR_P0, RX_ADDRESS, 5);
NRF24L01_Write_Reg(NRF_WRITE_REG + RX_PW_P0, 32);
NRF24L01_Write_Reg(NRF_WRITE_REG + CONFIG, 0x0F);
NRF24L01_CE_H;
}
unsigned int NRF24L01_RxPacket(unsigned char *rxbuf)
{
unsigned char stat;
while(NRF24L01_IRQ != 0);
stat = NRF24L01_Read_Reg(NRF_READ_REG + STATUS);
NRF24L01_Write_Reg(NRF_WRITE_REG + STATUS, stat);
if(stat & RX_DR)
{
NRF24L01_Read_Buf(RD_RX_PLOAD, rxbuf, 32);
return 0;
}
return 1;
}