Guest User

Untitled

a guest
Mar 16th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <stdio.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5.  
  6. #include "nrf24l01.h"
  7. #include "my_usartlib.h"
  8.  
  9. void initSPI();
  10. uint8_t writeByteSPI(uint8_t);
  11. uint8_t readReg(uint8_t);
  12. void writeReg(uint8_t, uint8_t);
  13. uint8_t *readRegArray(uint8_t, uint8_t *data, uint8_t);
  14. void nrf24l01_init();
  15. void nrf_transmit(uint8_t * W_buff);
  16. void reset_IRQ();
  17.  
  18.  
  19. uint8_t rx_address[] = {0x12, 0x12, 0x12, 0x12, 0x12};
  20. uint8_t tx_address[] = {0x12, 0x12, 0x12, 0x12, 0x12};
  21.    
  22. volatile uint8_t tx_flag = 0x00;
  23. uint8_t tx_buffer[5];
  24.  
  25. int main(void){
  26.    
  27.     usart_init();
  28.    
  29.     DDRD |= (1<<6);     //D6 (LED) input
  30.     DDRD &= ~(1<<2);    //D2 (INT0) input
  31.    
  32.     initSPI();
  33.    
  34.     for(int i = 0; i < 5; i++){
  35.         tx_buffer[i] = 2*i;
  36.     }
  37.    
  38.     nrf24l01_init();
  39.  
  40.     PORTD |= (1<<6);
  41.     _delay_ms(500);
  42.     PORTD &= ~(1<<6);
  43.     _delay_ms(500);     //flash LED to denote success
  44.    
  45.     EICRA |= (1<<ISC01);    //Falling edge detect
  46.     EIMSK |= (1<<INT0);
  47.    
  48.     usart_tx_string("CONFIG = 0x");
  49.     usart_tx_val(readReg(CONFIG), 16); usart_tx('\n');
  50.    
  51.     sei();
  52.     usart_tx_string("TX Init Success\n");
  53.    
  54.     while (1) {
  55.         _delay_ms(2000);
  56.            
  57.         nrf_transmit(tx_buffer);
  58.         _delay_ms(10);          // allow STATUS to update
  59.        
  60.         if((readReg(STATUS) & (1<<4)) != 0){
  61.             usart_tx_string("TX Failure\n");
  62.         } else{
  63.             usart_tx_string("TX Success\n");
  64.         }
  65.        
  66.         usart_tx_string("STATUS = 0x");
  67.         usart_tx_val(readReg(STATUS), 16); usart_tx('\n');
  68.        
  69.         reset_IRQ();
  70.     }
  71. }
  72.  
  73. void initSPI(){
  74.     DDRB |= (1<<DDB5) | (1<<DDB3) | (1<<DDB2) | (1<<DDB1);
  75.    
  76.     SPCR |= (1<<SPE) | (1<<MSTR);
  77.    
  78.     PORTB |= (1 << 2);  // CSN High
  79.     PORTB &= ~(1 << 1); // CE low
  80. }
  81.  
  82. uint8_t writeByteSPI(uint8_t cData){
  83.     SPDR = cData;
  84.     while(!(SPSR & (1<<SPIF)));
  85.     return SPDR;
  86. }
  87.  
  88. uint8_t readReg(uint8_t reg){
  89.     _delay_us(10);
  90.     PORTB &= ~(1 << 2);
  91.    
  92.     _delay_us(10);
  93.     writeByteSPI(R_REGISTER + reg);
  94.    
  95.     _delay_us(10); 
  96.     reg = writeByteSPI(NOP);
  97.    
  98.     _delay_us(10);
  99.     PORTB |= (1 << 2);
  100.     return reg;
  101. }
  102.  
  103. void writeReg(uint8_t reg, uint8_t data){
  104.     _delay_us(10);
  105.     PORTB &= ~(1 << 2);
  106.    
  107.     _delay_us(10);
  108.     writeByteSPI(W_REGISTER + reg);
  109.    
  110.     _delay_us(10);
  111.     writeByteSPI(data);
  112.    
  113.     _delay_us(10);
  114.     PORTB |= (1 << 2); 
  115. }
  116.  
  117. void writeRegArray(uint8_t reg, uint8_t *data, uint8_t data_length){
  118.     _delay_us(10);
  119.     PORTB &= ~(1 << 2);
  120.     _delay_us(10);
  121.     writeByteSPI(W_REGISTER + reg);
  122.     _delay_us(10);
  123.    
  124.     for(int i = 0; i < data_length; i++){
  125.         writeByteSPI(data[i]);
  126.         _delay_us(10);
  127.     }
  128.        
  129.     PORTB |= (1 << 2);
  130. }
  131.  
  132. uint8_t *readRegArray(uint8_t reg, uint8_t *data, uint8_t data_length){
  133.     static uint8_t ret[32];
  134.    
  135.     _delay_us(10);
  136.     PORTB &= ~(1 << 2);
  137.     _delay_us(10);
  138.     writeByteSPI(reg);
  139.     _delay_us(10);
  140.    
  141.     for(int i = 0; i < data_length; i++){
  142.         ret[i] = writeByteSPI(NOP);
  143.         _delay_us(10);
  144.     }
  145.    
  146.     PORTB |= (1 << 2);
  147.    
  148.     return ret;
  149. }
  150.  
  151. void nrf24l01_init(){
  152.     _delay_ms(100);
  153.    
  154.     writeReg(EN_AA, 0x3F);      //enable auto-ack
  155.     writeReg(SETUP_RETR, 0x2F); //15 retries at 750us for EN_AA
  156.    
  157.     writeReg(EN_RXADDR, 0x03);  //enable data pipe 0 & 1
  158.    
  159.     writeReg(SETUP_AW, 0x03);   //5 bytes of rx address
  160.    
  161.     writeReg(RF_CH, 0x01);      //2.401GHz frequency
  162.    
  163.     writeReg(RF_SETUP, 0x07);   //power and range modes
  164.    
  165.     writeRegArray(RX_ADDR_P0, tx_address, 5);
  166.     writeRegArray(TX_ADDR, tx_address, 5);
  167.    
  168.     writeReg(RX_PW_P0, 5);  //5 byte payload width
  169.     writeReg(RF_SETUP, 0x08);   //lowest power
  170.     writeReg(CONFIG, 0x5E); //transmitter, TX_DS interrupt
  171.     _delay_ms(100);
  172. }
  173.  
  174. void nrf_transmit(uint8_t * W_buff){
  175.     readRegArray(FLUSH_TX, W_buff, 0);
  176.     readRegArray(W_TX_PAYLOAD, W_buff, 5);
  177.    
  178.     _delay_ms(10);
  179.     PORTB |= (1 << 1);  //CE high, transmit data
  180.     _delay_us(20);     
  181.     PORTB &= ~(1 << 1); //CE low, stop transmitting
  182.     _delay_ms(10);
  183.    
  184. }
  185.  
  186. void reset_IRQ(void){
  187.     writeReg(STATUS, 0x70);
  188. }
  189.  
  190. ISR(INT0_vect){
  191.     PORTD |= (1<<6);
  192.     _delay_ms(50);
  193.     PORTD &= ~(1<<6);
  194.     //tx_flag = 0xFF;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment