📄 89lv51.lst
字号:
204 1 CSN = 1; // Set CSN high again
205 1 return(status); // return nRF24L01 status byte
206 1 }
207 /**************************************************/
208
209 /**************************************************
210 Function: RX_Mode();
211
212 Description:
213 This function initializes one nRF24L01 device to
214 RX Mode, set RX address, writes RX payload width,
215 select RF channel, datarate & LNA HCURR.
216 After init, CE is toggled high, which means that
217 this device is now ready to receive a datapacket.
218 /**************************************************/
219 void RX_Mode(void)
220 {
221 1 CE=0;
222 1 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX devi
-ce as the TX device
223 1
224 1 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
225 1 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
226 1 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
227 1 SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width
228 1 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
229 1 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:RX. RX_DR enabl
-ed..
230 1
231 1 CE = 1; // Set CE pin high to enable RX device
232 1
233 1 // This device is now ready to receive one packet of 16 bytes payload from a TX device sending to addre
-ss
234 1 // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps.
235 1
236 1 }
237 /**************************************************/
C51 COMPILER V8.02 89LV51 05/25/2009 10:15:16 PAGE 5
238
239 /**************************************************
240 Function: TX_Mode();
241
242 Description:
243 This function initializes one nRF24L01 device to
244 TX mode, set TX address, set RX address for auto.ack,
245 fill TX payload, select RF channel, datarate & TX pwr.
246 PWR_UP is set, CRC(2 bytes) is enabled, & PRIM:TX.
247
248 ToDo: One high pulse(>10us) on CE will now send this
249 packet and expext an acknowledgment from the RX device.
250 /**************************************************/
251 void TX_Mode(void)
252 {
253 1 CE=0;
254 1
255 1 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // Writes TX_Address to nRF24L01
256 1 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ac
-k
257 1 SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // Writes data to TX payload
258 1
259 1 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
260 1 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0
261 1 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
262 1 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40
263 1 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
264 1 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // Set PWR_UP bit, enable CRC(2 bytes) & Prim:TX. MAX_RT & TX
-_DS enabled..
265 1 CE=1;
266 1
267 1 }
268 /**************************************************/
269
270 /**************************************************
271 Function: check_ACK();
272
273 Description:
274 check if have "Data sent TX FIFO interrupt",if TX_DS=1,
275 all led light and after delay 100ms all led close
276 /**************************************************
277 void check_ACK()
278 {
279 uchar test;
280 test=SPI_Read(READ_REG+STATUS); // read register STATUS's
281 test=test&0x20; // check if have Data sent TX FIFO interrupt (TX_DS=1)
282 if(test==0x20) // TX_DS =1
283 {
284 P0=0x00; // turn on all led
285 delay100(); // delay 100ms
286 P0=0xff;
287 }
288 }
289 /**************************************************/
290
291 /**************************************************
292 Function: TxData();
293
294 Description:
295 write data x to SBUF
296 /**************************************************/
297 void TxData (uchar x)
C51 COMPILER V8.02 89LV51 05/25/2009 10:15:16 PAGE 6
298 {
299 1 SBUF=x; // write data x to SBUF
300 1 while(TI==0);
301 1 TI=0;
302 1 }
303 /**************************************************/
304
305 /**************************************************
306 Function: CheckButtons();
307
308 Description:
309 check buttons ,if have press,read the key values,
310 turn on led and transmit it; after transmition,
311 if received ACK, clear TX_DS interrupt and enter RX Mode;
312 turn off the led
313 /**************************************************/
314 void CheckButtons()
315 {
316 1 uchar Temp,xx,Tempi;
317 1
318 1 P0=0xff;
319 1 Temp=P0&0x0f; //read key value from port P0
320 1 if (Temp!=0x0f)
321 1 {
322 2 delay_ms(10);
323 2 Temp=P0&0x0f; // read key value from port P0
324 2 if (Temp!=0x0f)
325 2 {
326 3 xx=Temp;
327 3 Tempi=Temp<<4; // Left shift 4 bits
328 3 P0=Tempi; // Turn On the led
329 3 tx_buf[0]=Tempi; // Save to tx_buf[0]
330 3 TX_Mode(); // set TX Mode and transmitting
331 3 TxData(xx); // send data to uart
332 3 //check_ACK(); // if have acknowledgment from RX device,turn on all led
333 3 SPI_RW_Reg(WRITE_REG+STATUS,SPI_Read(READ_REG+STATUS)); // clear interrupt flag(TX_DS)
334 3 delay_ms(200);
335 3 P0=0xff; // Turn off the led
336 3 RX_Mode(); // set receive mode
337 3
338 3 while((P0&0x0f)!=0x0f);
339 3 }
340 2 }
341 1 }
342 /**************************************************/
343
344 /**************************************************
345 Function: main();
346
347 Description:
348 control all subprogrammes;
349 /**************************************************/
350 void main(void)
351 {
352 1 uchar xx;
353 1 init_io(); // Initialize IO port
354 1 Inituart(); // initialize 232 uart
355 1 init_int0(); // enable int0 interrupt
356 1 RX_Mode(); // set RX mode
357 1 while(1)
358 1 {
359 2 CheckButtons(); // scan key value and transmit
C51 COMPILER V8.02 89LV51 05/25/2009 10:15:16 PAGE 7
360 2 if(flag) // finish received
361 2 {
362 3 flag=0; // set flag=0
363 3 P0=rx_buf[0]; // turn on led
364 3
365 3 delay_ms(200);
366 3 P0=0xff; // turn off led
367 3 xx=rx_buf[0]>>4;// right shift 4 bits
368 3 TxData(xx); // send data to uart
369 3 }
370 2 }
371 1 }
372 /**************************************************/
373
374 /**************************************************
375 Function: ISR_int0() interrupt 0;
376
377 Description:
378 if RX_DR=1 or TX_DS or MAX_RT=1,enter this subprogram;
379 if RX_DR=1,read the payload from RX_FIFO and set flag;
380 /**************************************************/
381 void ISR_int0(void) interrupt 0
382 {
383 1 sta=SPI_Read(STATUS); // read register STATUS's value
384 1 if(RX_DR) // if receive data ready (RX_DR) interrupt
385 1 {
386 2 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
387 2 flag=1;
388 2 }
389 1 if(MAX_RT)
390 1 {
391 2 SPI_RW_Reg(FLUSH_TX,0);
392 2 }
393 1 SPI_RW_Reg(WRITE_REG+STATUS,sta);// clear RX_DR or TX_DS or MAX_RT interrupt flag
394 1 }
395 /**************************************************/
396
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 577 ----
CONSTANT SIZE = 17 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 48 11
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -