完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本科生做毕业设计,如题的无线采集传输,编完程序不知遇到什么问题,就是发送不了,已经调试了好长时间了,有没有高手可以指导我下,谢谢谢谢啊。我觉得程序应该没大问题,不知是哪个小问题导致的,就是查不出来。求助啊求助~有米有懂的
|
|
相关推荐
|
|
#include "pic.h"
#include "def.h" __CONFIG(0x3b31); #define uchar unsigned char #define uint unsigned int #define LED_TRANSMIT RA1 //绿灯,发送 #define LED_RETRANSMIT RA2 //黄灯,重发 static volatile uchar stat=0x0; static volatile uchar stat @ 0x20; //tmpData定位在地址0x20 static volatile bit RX_DR @ (unsigned)&stat*8+6; static volatile bit TX_DS @ (unsigned)&stat*8+5; static volatile bit MAX_RT @ (unsigned)&stat*8+4; //定义通用寄存器,存放SATUS中的状态位 /**************************************************/ /* unsigned char FLAG @ 0XEF; #define FLAGIT(adr,bit) ((unsigned)(&adr)*8+(bit)) //绝对寻址位操作指令 static bit FLAG1 @ FLAGIT(FLAG,0); static bit FLAG2 @ FLAGIT(FLAG,1); static bit FLAG3 @ FLAGIT(FLAG,2); */ #define TX_ADR_WIDTH 5 // 5 bytes TX(RX) address width 5字节地址 #define TX_PLOAD_WIDTH 5 // 5 bytes TX payload 一次发5bytes数据 uchar const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x63}; // Define a static TX address uchar rx_buf[TX_PLOAD_WIDTH]; uchar tx_buf[TX_PLOAD_WIDTH]; /************************************************** Function: Port_init(); Description: flash led one time,chip enable(ready to TX or RX Mode), Spi disable,Spi clock line init high /**************************************************/ void Port_init(void){ TRISA=0b00000001; //RA0 INPUT RA1 output RA2 output TRISC=0b00010001; //MISO & IRQ as input PORTC=0x00; PORTB=0x00; PORTA=0x00; /////*******************初始化SPI SSPSTAT=0x40; //对SPI设置 SSPCON=0x20; //使能SPI,时钟为Fosc/4 SSPIF=0; ///////******** 初始化nrf24L01 CE=0; // chip enable 待机模式低电平 CSN=1; // Spi disable 片选低有效 SCK=0; // Spi clock line init low } /**************************************************/ /************************************************** Function: Description: delay ms , 30us /**************************************************/ void delayms(uint ms) //定义延时函数 4MHz下 { int i,j; for(i=0;i {;} } void delay30us(uchar us) //定义延时函数,延时30us 4MHz下 { uchar i; for(i=us;i>0;i--) {;} } /**************************************************/ void inited(void) { ///初始化AD ADCON1=0b00000000; // ADFM=1; 右对齐,ADRESL保存低八位 // PCFG3=0; PCFG2=0; PCFG1=0; PCFG0=0 // PCFG3:PCFG0=0000,全为模拟引脚,参考电压为VDD和VSS ADCS1=1; ADCS0=1; // 使用A/D转换模块内部RC振荡器作为时钟 CHS2=0; CHS1=0; CHS0=0; // CHS2:CHS0=000, 选择通道0进行A/D转换 } // 进行A/D转换,采集数据 void adinput(void) { int i; ADON=1; // 开启A/D转换模块 delay30us(1); // 为了采样保持电路充电的延时,超过25us即可 GO=1; // 启动一次A/D转换 while(GO) // 查询A/D转换是否完成 {;} tx_buf[1]=ADRESH; tx_buf[2]=ADRESL; } /************************************************** Function: SPI_RW(); pic向nrf中写入一个字节 Description: Writes one byte to nRF24L01, and return the byte read from nRF24L01 during write, according to SPI protocol /**************************************************/ uchar SPI_RW(uchar byte) { ////通过spi写一个字节数据 SSPBUF=byte; while(!SSPIF); SSPIF=0; return 0; } /************************************************** Function: SPI_Write(); Description: Writes value 'value' to register 'reg' /**************************************************/ uchar SPI_Write(uchar reg, uchar value) { CSN = 0; // CSN low, init SPI transaction SPI片选 SPI_RW(reg); // select register SPI_RW(value); // ..and write value to it.. CSN = 1; // CSN high again return(0); } /**************************************************/ /************************************************** Function: SPI_Read(); Description: Read one byte from nRF24L01 register, 'reg' /**************************************************/ uchar SPI_Read(uchar reg) { uchar reg_value; CE=0; CSN = 0; // CSN low, initialize SPI communication... SPI_RW(reg); // Select register to read from.. reg_value=SPI_RW(0); // ..then read registervalue CSN = 1; // CSN high, terminate SPI communication return(reg_value); // return register value } /**************************************************/ /************************************************** Function: SPI_Read_Buf(); Description: Reads 'bytes' #of bytes from register 'reg' Typically used to read RX payload, Rx/Tx address /**************************************************/ uchar SPI_Read_Buf(uchar reg,uchar *pBuf,uchar bytes) { uchar byte_ctr; CSN = 0; // Set CSN low, init SPI tranaction SPI_RW(reg); //Select register to read for(byte_ctr=0;byte_ctr CSN = 1; // Set CSN high again return(0); } /**************************************************/ /************************************************** Function: SPI_Write_Buf(); Description: Writes contents of buffer '*pBuf' to nRF24L01 Typically used to write TX payload, Rx/Tx address /**************************************************/ uchar SPI_Write_Buf(uchar reg, const uchar *pBuf, uchar bytes) { uchar byte_ctr; CSN = 0; // Set CSN low, init SPI tranaction SPI_RW(reg); //Select register to write to for(byte_ctr=0;byte_ctr CSN = 1; // Set CSN high again return(0); } /**************************************************/ /************************************************** Function: RX_Mode(); Description: This function initializes one nRF24L01 device to RX Mode, set RX address, writes RX payload width, select RF channel, datarate & LNA HCURR. After init, CE is toggled high, which means that this device is now ready to receive a datapacket. /**************************************************/ void RX_Mode(void){ CE=0; SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device SPI_Write(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 SPI_Write(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 SPI_Write(WRITE_REG + RF_CH, 40); // Select RF channel 40 SPI_Write(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width SPI_Write(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR SPI_Write(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabled.. CE = 1; // Set CE pin high to enable RX device // This device is now ready to receive one packet of 16 bytes payload from a TX device sending to address // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps. } /**************************************************/ /************************************************** Function: TX_Mode(); Description: This function initializes one nRF24L01 device to TX mode, set TX address, set RX address for auto.ack, fill TX payload, select RF channel, datarate & TX pwr. PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX. ToDo: One high pulse(>10us) on CE will now send this packet and expext an acknowledgment from the RX device. /**************************************************/ void TX_Mode(void) { CE=0; SPI_Write(WRITE_REG+STATUS,0x70); // clear all interrupt flag,****write 1 to clear bit SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload SPI_Write(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 SPI_Write(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 SPI_Write(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans... SPI_Write(WRITE_REG + RF_CH, 40); // Select RF channel 40 total 128 SPI_Write(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR SPI_Write(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) &reflect:TX. MAX_RT & TX_DS enabled.. CE=1; delay30us(1); CE=0; } /**************************************************/ /************************************************** Function: Transmit(); Description: Transmit the data in tx_buf[]. /**************************************************/ void Transmit(void) { CE=0; SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址 SPI_Write(FLUSH_TX,0x00); SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据 CE=1; delay30us(1); CE=0; } /************************************************** Function: Nrf_power_down(); Description: For the since of energy conservation,turn nRF24L01 in Power Down mode. /**************************************************/ void Nrf_power_down(void) { CE=0; SPI_Write(WRITE_REG + CONFIG, SPI_Read(READ_REG + CONFIG)&0xFD); } void Nrf_power_up(void) { CE=0; SPI_Write(WRITE_REG + CONFIG, SPI_Read(READ_REG + CONFIG)|0x02); //delay for 1.5 ms for Start up PWR_UP置1 } /************************************************** Function: check_ACK(); Description: check if have "Data sent TX FIFO interrupt",if TX_DS=1, all led light and after delay 100ms all led close /**************************************************/ void Check_ACK(void) { stat=SPI_Read(READ_REG+STATUS); // read register STATUS's while(!TX_DS) { if(MAX_RT) { SPI_Write(WRITE_REG+STATUS,0x10); // clear interrupt flag(MAX_RT) ,write 1 to clear bit LED_RETRANSMIT=0; //Turn led on delayms(200); LED_RETRANSMIT=1; //Turn led off delayms(200); CE=1; delay30us(1); } stat=SPI_Read(READ_REG+STATUS); // read register STATUS's } CE=0; SPI_Write(WRITE_REG+STATUS,stat); // clear all interrupt flag,****write 1 to clear bit LED_TRANSMIT=0; //Turn led on delayms(200); LED_TRANSMIT=1; //Turn led off delayms(200); } /**************************************************/ /************************************************** Function: main(); Description: control all subprogrammes; /**************************************************/ void main(void) { Port_init(); inited(); TX_Mode(); // set TX Mode delayms(1); //delay for , let nrf24L01 POR reliably while(1) { adinput(); //AD转换 Nrf_power_up(); Transmit(); Check_ACK(); Nrf_power_down(); delayms(500); //延迟500ms>100ms,节能 } } |
|
|
|
|
|
只有小组成员才能发言,加入小组>>
求解外围电路实现的是4脚给持续低电平复位并正常工作,高电平不工作的原因
2076 浏览 1 评论
3489 浏览 3 评论
PIC1946程序有一个变量在运行过程中恢复初始值其他变量保持不变
2329 浏览 2 评论
2754 浏览 0 评论
PIC16F1825的RC5引脚,在主程序中操作无效,在中断中可以改变是为什么?
4012 浏览 5 评论
960浏览 0评论
用XC8编译PIC18F25K80时提示下面Error,求怎么解决这个问题
6345浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 05:52 , Processed in 0.734141 second(s), Total 80, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号