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