连线方式如代码注释所示. 基于STC12的nRF24L01代码修改. 通过修改CURRENT_SCEN的值可以切换发送, 接收和发送接收半双工方式
寄存器初始化方法使用python工具生成, 工具地址 https://github.com/IOsetting/stcmx
main.c
- /*****************************************************************************/
- /**
- * \file spi_nrf24l01_tx.c
- * \brief Example code of SPI driving NRF24L01 module in all scenarios
- * \note Pin connection:
- * P12(SS, Ignored) => CSN,
- * P13(MOSI) => MOSI,
- * P14(MISO) => MISO,
- * P15(SPCLK) => CLK,
- * P32(INT0) => IRQ,
- * P37(IO) => CE,
- *
- * \version v0.1
- * \ingroup example
- * \remarks test-board: Minimum System; test-MCU: STC8A8K64S4A12
- * In my test STC12 cannot achive the best performance of nRF24L01,
- * you have to put 20~40ms delay betwen each sending, or error rate
- * will increase dramatically.
- ******************************************************************************/
- #include "stc8a.h"
- #include "uart.h"
- #include "nrf24l01.h"
- #include "util.h"
- #include "spi.h"
- const NRF24_SCEN CURRENT_SCEN = NRF24_SCEN_HALF_DUPLEX;
- extern uint8_t idata xbuf[NRF24_PLOAD_WIDTH + 1];
- void clock_init()
- {
- // [ BAH,0,0x00]: 外设端口切换控制寄存器2,串口2/3/4,I2C,比较器
- P_SW2 = 0x80;
- // [FE01H,1,0x00]: 时钟分频寄存器,ISP可能写入预设值
- CLKDIV = 0x00;
- // [ 9FH,0,0x00]: IRC频率调整寄存器, ISP可能写入预设值, 0x75:24MHz
- IRTRIM = 0x75;
- // [ 9EH,0,0x00]: IRC频率微调寄存器, ISP可能写入预设值
- LIRTRIM = 0x00;
- // [ BAH,0,0x00]: 外设端口切换控制寄存器2,串口2/3/4,I2C,比较器
- P_SW2 = 0x00;
- }
- void timer_init()
- {
- // [ D6H,0,0x00]: 定时器2高字节
- T2H = 0xFF;
- // [ D7H,0,0x00]: 定时器2低字节
- T2L = 0xCB;
- // [ 87H,0,0x30]: 电源控制寄存器
- PCON = 0xB0;
- // [ 8EH,0,0x01]: 辅助寄存器
- AUXR = 0x15;
- }
- void uart_init()
- {
- // [ 98H,0,0x00]: 串口1控制寄存器
- SCON = 0x50;
- // [ 87H,0,0x30]: 电源控制寄存器
- PCON = 0xB0;
- // [ 8EH,0,0x01]: 辅助寄存器
- AUXR = 0x15;
- }
- void spi_init()
- {
- // [ CEH,0,0x00]: SPI控制
- SPCTL = 0xD0;
- }
- void int_init()
- {
- IT0 = 1; // 低电平触发
- EX0 = 1; // 使能INT0中断
- EA = 1; // 使能全局中断
- }
- INTERRUPT(int0_isr, 0)
- {
- NRF24L01_handelIrqFlag(xbuf);
- }
- void main(void)
- {
- uint8_t sta;
- uint8_t code tmp[] = {
- 0x1F, 0x80, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
- 0x21, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x28,
- 0x31, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x38,
- 0x41, 0x12, 0x13, 0x14, 0x15, 0x16, 0x37, 0x48};
- clock_init();
- timer_init();
- uart_init();
- PrintString("UART Initialized\r\n");
- spi_init();
- PrintString("SPI Initialized\r\n");
-
- while (NRF24L01_check() == 1)
- {
- PrintString("Check failed\r\n");
- Delay1000ms();
- }
- PrintString("NRF24L01 Checked\r\n");
- switch (CURRENT_SCEN)
- {
- case NRF24_SCEN_TX:
- NRF24L01_init(NRF24_MODE_TX);
- PrintString("NRF24L01 Initialized\r\n");
- //int_init();
- while (1)
- {
- if (NRF24L01_writeFast(tmp) == 0)
- {
- PrintChar('E');
- }
- else
- {
- PrintChar('.');
- }
- Delay30ms();
- }
- break;
- case NRF24_SCEN_RX:
- NRF24L01_init(NRF24_MODE_RX);
- int_init();
- while(1);
- break;
- case NRF24_SCEN_HALF_DUPLEX:
- NRF24L01_init(NRF24_MODE_RX);
- int_init();
- while (1)
- {
- NRF24L01_tx(tmp);
- Delay1000ms();
- }
- break;
- default:
- PrintString("Unknown scen\r\n");
- break;
- }
- }
复制代码
完整Keil代码下载:
stc8a8k64s4_test02.zip
(17 KB, 下载次数: 28)
|