下面是網(wǎng)上找的程序,但是編譯就通不過,一直說端口定義無法識別,求大神幫忙修改一下,能用到STM8S003f3p6單片機上。STM8S003f3p6單片機就是某一個寶上買的幾塊錢的那種20個引腳的最小核心板 #include #define uint unsigned int #define uchar unsigned char //****************************************IO端口定義*************************************** //****************************************NRF24L01端口定義***************************** /////////////引腳定義////////////// #define CE PD_ODR_ODR3 #define CSN PD_ODR_ODR2 #define SCK PC_ODR_ODR7 #define MOSI PC_ODR_ODR6 #define MISO PC_IDR_IDR5 //輸入 #define IRQ PD_IDR_IDR4 //輸入 /***************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 uchar TX_ADDRESS[TX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x90}; //本地地址 uchar RX_ADDRESS[RX_ADR_WIDTH]= {0x12,0x34,0x56,0x78,0x90}; //接收地址 //***************************************NRF24L01寄存器指令******************************************************* #define READ_REG 0x00 // 讀寄存器指令 #define WRITE_REG 0x20 // 寫寄存器指令 #define RD_RX_PLOAD 0x61 // 讀取接收數(shù)據(jù)指令 #define WR_TX_PLOAD 0xA0 // 寫待發(fā)數(shù)據(jù)指令 #define FLUSH_TX 0xE1 // 沖洗發(fā)送 FIFO指令 #define FLUSH_RX 0xE2 // 沖洗接收 FIFO指令 #define REUSE_TX_PL 0xE3 // 定義重復裝載數(shù)據(jù)指令 #define NOP 0xFF // 保留 //*************************************SPI(nRF24L01)寄存器地址**************************************************** #define CONFIG 0x00 // 配置收發(fā)狀態(tài),CRC校驗模式以及收發(fā)狀態(tài)響應方式 #define EN_AA 0x01 // 自動應答功能設置 #define EN_RXADDR 0x02 // 可用信道設置 #define SETUP_AW 0x03 // 收發(fā)地址寬度設置 #define SETUP_RETR 0x04 // 自動重發(fā)功能設置 #define RF_CH 0x05 // 工作頻率設置 #define RF_SETUP 0x06 // 發(fā)射速率、功耗功能設置 #define STATUS 0x07 // 狀態(tài)寄存器 #define OBSERVE_TX 0x08 // 發(fā)送監(jiān)測功能 #define CD 0x09 // 地址檢測 #define RX_ADDR_P0 0x0A // 頻道0接收數(shù)據(jù)地址 #define RX_ADDR_P1 0x0B // 頻道1接收數(shù)據(jù)地址 #define RX_ADDR_P2 0x0C // 頻道2接收數(shù)據(jù)地址 #define RX_ADDR_P3 0x0D // 頻道3接收數(shù)據(jù)地址 #define RX_ADDR_P4 0x0E // 頻道4接收數(shù)據(jù)地址 #define RX_ADDR_P5 0x0F // 頻道5接收數(shù)據(jù)地址 #define TX_ADDR 0x10 // 發(fā)送地址寄存器 #define RX_PW_P0 0x11 // 接收頻道0接收數(shù)據(jù)長度 #define RX_PW_P1 0x12 // 接收頻道0接收數(shù)據(jù)長度 #define RX_PW_P2 0x13 // 接收頻道0接收數(shù)據(jù)長度 #define RX_PW_P3 0x14 // 接收頻道0接收數(shù)據(jù)長度 #define RX_PW_P4 0x15 // 接收頻道0接收數(shù)據(jù)長度 #define RX_PW_P5 0x16 // 接收頻道0接收數(shù)據(jù)長度 #define FIFO_STATUS 0x17 // FIFO棧入棧出狀態(tài)寄存器設置 void init_NRF24L01(void); uchar SPI_RW(uchar byte); uchar SPI_Read(uchar reg); void SetRX_Mode(void); uchar SPI_RW_Reg(uchar reg, uchar value); uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num); uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num); unsigned char nRF24L01_RxPacket(unsigned char* rx_buf); void nRF24L01_TxPacket(unsigned char * tx_buf); void IO_config(void); void delayms(unsigned int count); uchar sta; #define RX_DR (sta & 0x40) #define TX_DS (sta & 0x20) #define MAX_RT (sta & 0x10) void delayms(unsigned int count) { unsigned int i,j; for(i=0;i<count;i++) for(j=0;j<450;j++); }</count;i++) //NRF24L01初始化 void init_NRF24L01(void) { delayms(1); CE=0; // chip enable CSN=1; // Spi disable SCK=0; // Spi clock line init high SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 寫本地地址 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 寫接收端地址 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 頻道0自動 ACK應答允許 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允許接收地址只有頻道0,如果需要多頻道可以參考Page21 SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 設置信道工作為2.4GHZ,收發(fā)必須一致 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //設置接收數(shù)據(jù)長度,本次設置為32字節(jié) SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //設置發(fā)射速率為1MHZ,發(fā)射功率為最大值0dB } //函數(shù):uint SPI_RW(uint uchar) //功能:NRF24L01的SPI寫時序 uchar SPI_RW(uchar byte) { uchar i; for(i=0;i<8;i++) // output 8-bit { if((byte & 0x80)==0) {MOSI=0;} else {MOSI=1;}
byte = (byte << 1); // shift next bit into MSB.. SCK = 1; // Set SCK high.. if(MISO == 0) {byte |= 0;} // capture current MISO bit else {byte |= 1;} SCK = 0; // ..then set SCK low again } return(byte); // return read uchar } /*uchar SPI_RW(uchar byte) { uchar i; for(i=0;i<8;i++) // output 8-bit { MOSI = (byte & 0x80); // output 'uchar', MSB to MOSI byte = (byte << 1); // shift next bit into MSB.. SCK = 1; // Set SCK high.. byte |= MISO; // capture current MISO bit SCK = 0; // ..then set SCK low again } return(byte); // return read uchar }*/ //函數(shù):uchar SPI_Read(uchar reg) //功能:NRF24L01的SPI時序 uchar SPI_Read(uchar reg) { uchar 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 } //功能:NRF24L01讀寫寄存器函數(shù) uchar SPI_RW_Reg(uchar reg, uchar value) { uint 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 } //函數(shù):uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars) //功能: 用于讀數(shù)據(jù),reg:為寄存器地址,pBuf:為待讀出數(shù)據(jù)地址,uchars:讀出數(shù)據(jù)的個數(shù) uchar SPI_Read_Buf(uchar reg, uchar *pBuf, uchar num) { uchar status,i; CSN = 0; // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status uchar for(i=0;i<num;i++) pBuf = SPI_RW(0); // CSN = 1; return(status); // return nRF24L01 status uchar }</num;i++) //函數(shù):uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) //功能: 用于寫數(shù)據(jù):為寄存器地址,pBuf:為待寫入數(shù)據(jù)地址,uchars:寫入數(shù)據(jù)的個數(shù) uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar num) { uchar status,i; CSN = 0; //SPI使能 status = SPI_RW(reg); for(i=0; i<num; SPI_RW(*pBuf++); CSN = 1; //關閉SPI return(status); // } //函數(shù):void SetRX_Mode(void) //功能:數(shù)據(jù)接收配置 void SetRX_Mode(void) { CE=0; SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收發(fā)完成中斷響應,16位CRC ,主接收 CE = 1; delayms(1); } /******************************************************************************************************/ //函數(shù):unsigned char nRF24L01_RxPacket(unsigned char* rx_buf) //功能:數(shù)據(jù)讀取后放如rx_buf接收緩沖區(qū)中 unsigned char nRF24L01_RxPacket(unsigned char* rx_buf) { unsigned char revale=0; sta=SPI_Read(STATUS); // 讀取狀態(tài)寄存器來判斷數(shù)據(jù)接收狀況 if(RX_DR) // 判斷是否接收到數(shù)據(jù) { CE = 0; //SPI使能 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer revale =1; //讀取數(shù)據(jù)完成標志 } SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到數(shù)據(jù)后RX_DR,TX_DS,MAX_PT都置高為1,通過寫1來清除中斷標志 return revale; } //函數(shù):void nRF24L01_TxPacket(unsigned char * tx_buf) //功能:發(fā)送 tx_buf中數(shù)據(jù) void nRF24L01_TxPacket(unsigned char * tx_buf) { CE=0; //StandBy I模式 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 裝載接收端地址 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 裝載數(shù)據(jù) SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收發(fā)完成中斷響應,16位CRC,主發(fā)送 CE=1; //置高CE,激發(fā)數(shù)據(jù)發(fā)送 delayms(1); } //***********端口配置******************/ void IO_config(void) { PC_DDR_DDR5=0; PC_DDR_DDR6=1; PC_DDR_DDR7=1;
PD_DDR_DDR2=1; PD_DDR_DDR3=1; PD_DDR_DDR4=0;
PC_CR1_C15=1;//輸入上拉 PC_CR1_C16=1; PC_CR1_C17=1;
PD_CR1_C12=1; PD_CR1_C13=1; PD_CR1_C14=0;//輸入上拉 PC_CR2 = 0x00; PD_CR2 = 0x00;
} /***********************************/ /*****************主程序********************/ void main(void) { unsigned char TxBuf[20]={0}; // unsigned char RxBuf[20]={0}; unsigned char led_num; // unsigned char rx_temp;
delayms(100); IO_config();
init_NRF24L01() ; TxBuf[1] =0x55 ; nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data delayms(1000); led_num=0x00; while(1) {
TxBuf[1] =led_num ; nRF24L01_TxPacket(TxBuf); // Transmit Tx buffer data led_num++; delayms(500); //*********************************************************************************************** SetRX_Mode(); // 每次開始發(fā)送 然后無按鍵直接跳轉(zhuǎn)到接收 一直循環(huán)。 nRF24L01_RxPacket(RxBuf); if(RX_DR==0) { // rx_temp=0x55; delayms(10); } else { delayms(10); }
}
}
|