最近学习stm8,于是想把51的24L01的程序移植到stm8,我把引脚定义好,初始化io口,但是程序不好用,spi读写寄存器都不好用,这里请教大家;
#include "IOSTM8S103F3.h"
/************************I/O口定义********************************/
#define LED PB_ODR_ODR5
#define CSN PB_ODR_ODR4
#define SCK PC_ODR_ODR3
#define MOSI PC_ODR_ODR4
#define MISO PC_IDR_IDR5
#define IRQ PC_IDR_IDR6
#define CE PC_ODR_ODR7
//#define uchar unsigned char
//#define uint unsigned char
//*********************************************NRF24L01*************************************
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
//***************************************NRF24L01寄存器指令*******************************************************
#define READ_REG 0x00 // 读寄存器指令
#define WRITE_REG 0x20 // 写寄存器指令
#define RD_RX_PLOAD 0x61 // 读取接收数据指令
#define WR_TX_PLOAD 0xA0 // 写待发数据指令
#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令
#define NOP 0xFF // 保留
//*************************************SPI(nRF24L01)寄存器地址****************************************************
#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式
#define EN_AA 0x01 // 自动应答功能设置
#define EN_RXADDR 0x02 // 可用信道设置
#define SETUP_AW 0x03 // 收发地址宽度设置
#define SETUP_RETR 0x04 // 自动重发功能设置
#define RF_CH 0x05 // 工作频率设置
#define RF_SETUP 0x06 // 发射速率、功耗功能设置
#define STATUS 0x07 // 状态寄存器
#define OBSERVE_TX 0x08 // 发送监测功能
#define CD 0x09 // 地址检测
#define RX_ADDR_P0 0x0A // 频道0接收数据地址
#define RX_ADDR_P1 0x0B // 频道1接收数据地址
#define RX_ADDR_P2 0x0C // 频道2接收数据地址
#define RX_ADDR_P3 0x0D // 频道3接收数据地址
#define RX_ADDR_P4 0x0E // 频道4接收数据地址
#define RX_ADDR_P5 0x0F // 频道5接收数据地址
#define TX_ADDR 0x10 // 发送地址寄存器
#define RX_PW_P0 0x11 // 接收频道0接收数据长度
#define RX_PW_P1 0x12 // 接收频道0接收数据长度
#define RX_PW_P2 0x13 // 接收频道0接收数据长度
#define RX_PW_P3 0x14 // 接收频道0接收数据长度
#define RX_PW_P4 0x15 // 接收频道0接收数据长度
#define RX_PW_P5 0x16 // 接收频道0接收数据长度
#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置
//**************************************************************************************
void Delay(unsigned int x);
void InerDelay_us(unsigned char n);
void Init_NRF24L01(void);
unsigned char SPI_RW(unsigned char uuchar);
unsigned char SPI_Read(unsigned char reg);
void SetRX_Mode(void);
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value);
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars);
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars);
unsigned char NRF24L01_RxPacket(unsigned char* rx_buf);
unsigned char sta; //状态标志
//uint sta;
#define RX_DR (sta>>6)&0x01;
#define TX_DS (sta>>5)&0x01;
#define MAX_RT (sta>>4)&0x01;
//uchar const TX_ADDRESS[TX_ADR_WIDTH]= {0x10,0x10,0x10,0x10,0x10}; //发送出去的地址
unsigned char TX_ADDRESS[TX_ADR_WIDTH]= {0x10,0x10,0x10,0x10,0x10}; //发送出去的地址
/***************************延时函数********************************************************/
void InerDelay_us(unsigned char n)
{
for(;n>0;n--)
{
++n;
--n;
}
}
//*****************************************长延时*****************************************
void Delay(unsigned int x)
{
unsigned int i,j;
for(i=x; i>0; i--)
{
for(j=110; j>0; j--)
{
++j;
--j;
}
}
}
//函数:uchar SPI_Read(uchar reg)
//功能:NRF24L01的SPI时序
/********************************************************///********************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
CSN = 0; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read registervalue
CSN = 1; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
//函数:uint SPI_RW(uint uchar)
//功能:NRF24L01的SPI写时序
/****************************************************************************************************/
unsigned char SPI_RW(unsigned char uuchar)
{
unsigned char bit_ctr;
for(bit_ctr=0;bit_ctr<8;bit_ctr++) // output 8-bit
{
MOSI = (uuchar & 0x80)>>7; // output 'uchar', MSB to MOSI
uuchar = (uuchar << 1); // shift next bit into MSB..
SCK = 1; // Set SCK high..
uuchar |= MISO; // capture current MISO bit
SCK = 0; // ..then set SCK low again
}
return(uuchar); // return read uchar
}
/****************************************************************************************************/
//功能:NRF24L01读写寄存器函数
/****************************************************************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned char status;
CSN = 0; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
CSN = 1; // CSN high again
return(status); // return nRF24L01 status uchar
}
//函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
/*********************************************************************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)
{
unsigned char status,uchar_ctr;
CSN = 0; //SPI使能
status = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //
{
SPI_RW(*pBuf++);
}
CSN = 1; //关闭SPI
return(status); //
}
//函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
//功能:发送 tx_buf中数据
/*************************************************************************************/
void nRF24L01_TxPacket(unsigned char *tx_buf)
{
CE = 0; //StandBy I模式
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
CE = 1; //置高CE,激发数据发送
Delay(100);
}
//*************************初始化24L01****************************************************/
void Init_NRF24L01(void)
{
InerDelay_us(100);
CE=0;
CSN=1;
SCK=0;
//IRQ=1;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写发送端地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, RX_ADR_WIDTH); // 写自动应答频道0地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0x00); // 频道0自动 ACK应答禁止 1
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x00); // 频道0接收禁止 2
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x00); //禁止自动重发 3 调试时注意这三点
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为2MHZ,发射功率为最大值0dB
}
/*****************初始I/O口****************************************/
void Init_GPIO(void)
{
//#define LED PB_ODR_ODR5
//#define CSN PB_ODR_ODR4
//#define SCK PC_ODR_ODR3
//#define MOSI PC_ODR_ODR4
//#define MISO PC_IDR_IDR5
//#define IRQ PC_IDR_IDR6
//#define CE PC_ODR_ODR7
PB_DDR_DDR5 = 1;
PB_DDR_DDR4 = 1;
PB_CR1 = 0xff;
PB_CR2 = 0x00;
PC_DDR_DDR3 = 1;
PC_DDR_DDR4 = 1;
PC_DDR_DDR5 = 0;
PC_DDR_DDR6 = 0;
PC_DDR_DDR7 = 1;
PC_CR1 = 0xff;
PC_CR2 = 0x00;
}
/*****************************初始化时钟***************************/
/*void Init_Cloc(void)
{
CLK_ICKR|=0x01; //开启内部HSI
while(!(CLK_ICKR&0x02));//HSI准备就绪
CLK_SWR=0xe1; //HSI为主时钟源
CLK_CKDIVR=0x18; //HSI 8 分频 2M
}*/
int main( void )
{
//Init_Cloc();
Init_GPIO();
LED = 1;
unsigned char TxBuf[20]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20};
/*if(MISO == 1)
while(1)
{
LED = 0;
Delay(1000);
LED = 1; Delay(1000);
}*/
Init_NRF24L01();
//nRF24L01_TxPacket(TxBuf);
while(1)
{
//nRF24L01_TxPacket(TxBuf);
sta = SPI_Read(STATUS);
if(sta == 0x2e) //查看是否发送成功
{
LED = 0;
Delay(60000);
LED = 1;
Delay(60000);
}
//SPI_RW_Reg(WRITE_REG+STATUS,0XFF); //清状态寄存器
//Delay(1000); //延时
}
}
|
|