搜档网

搜档网

当前位置:搜档网 > STM8软件模拟SPI与NRF24L01通信

STM8软件模拟SPI与NRF24L01通信

//========================================
// PROJECT: 24L01发送实验(软件模拟SPI)
// AUTHOR: MENGJICHENG
// MODULE: main.c
// COMPILER: STM8 Cosmic C Compiler
// DATE: 2010-9-12 16:41
//=========================================
#include "STM8S103F.h" //Registers and memory
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned int

#define countof(a) (sizeof(a)/sizeof(*(a)))//带参数的定义
#define BufferSize (countof(Tx_Buffer)-1)//减掉一个空字符
u8 Tx_Buffer[] ="123456789";//"STM8Sxxx SPI Firmware Library Example: communication with a microSD card";

//-----------------------------------
_Bool f_250us=0;

_Bool power_pin @PB_ODR:5;
_Bool key1_pin @PC_IDR:4;
_Bool key2_pin @PD_IDR:5;
//////////////////////////////////////////
//NRF24L01脚定义
_Bool SCK_PIN @PC_ODR:5;
_Bool MOSI_PIN @PC_ODR:6;
_Bool MISO_PIN @PC_IDR:7;
_Bool IRQ_PIN @PD_IDR:3; //为低表示:1已接收到数据,数据移完时自动变为高,2发送完变低,3从发送次数超过设定值
_Bool CSN_PIN @PA_ODR:3; //为低时激活SPI发送数据(芯片的片选线,CSN为低电平芯片工作),此时可设置24L01
_Bool CE_PIN @PD_ODR:4; //低时系统进入待机模式I,为高时启动发射与接收,要保持10US
// SPI(nRF24L01) registers(addresses)
#define CONFIG 0x00 //;'Config' register address
#define EN_AA 0x01 //;'Enable Auto Acknowledgment' register address
#define EN_RXADDR 0x02 // 'Enabled RXaddresses' register address
#define SETUP_AW 0x03 // 'Setup address width' register address
#define SETUP_RETR 0x04 // 'Setup Auto. Retrans' register address
#define RF_CH 0x05 // 'RF channel' register address
#define RF_SETUP 0x06 // 'RF setup' register address
#define STATUS 0x07 // 'Status' register address
#define OBSERVE_TX 0x08 // 'Observe TX' register address
#define CD 0x09 // 'Carrier Detect' register address
#define RX_ADDR_P0 0x0A // 'RX address pipe0' register address
#define RX_ADDR_P1 0x0b // 'RXaddress pipe1' register address
#define RX_ADDR_P2 0x0c // 'RXaddress pipe2' register address
#define RX_ADDR_P3 0x0d // 'RX address pipe3' register address
#define RX_ADDR_P4 0x0e // 'RX address pipe4' register address
#define RX_ADDR_P5 0x0f // 'RX address pipe5' register address
#define TX_ADDR 0x10 // 'TX address' register address
#define RX_PW_P0 0x11 // 'RX payload width, pipe0' register address
#define RX_PW_P1 0x12 // 'RX payload width, pipe1' register address
#define RX_PW_P2 0x13 // 'RX payload width, pipe2' register address
#define RX_PW_P3 0x14 // 'RX payload width, pipe3' register address
#define RX_PW_P4 0x15 // 'RX payload width, pipe4' register address
#define RX_PW_P5 0x16 // 'RX payload width, pipe5' register address
#define FIFO_STATUS 0x17 // 'FIFO Status Register' register address
//nRF24L01 SPI Command constant 写命令时要CSN

TOP相关主题