Advertisement
RybaSG

Transmitter

Sep 5th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7.  
  8. #include "nRF24l01/nRF24L01.h"
  9. #include "UART/Uart.h"                  // UART library
  10. #include "Terminal/terminal.h"          // Terminal library using UART
  11. #include "OLED/OLED.h"                  // 128x64 OLED library
  12.  
  13. // Receiving function
  14. void RX_function ( void * nRF_RX_buff , uint8_t len );
  15.  
  16. // ADC measurement function
  17. uint8_t measurement(uint8_t channel);
  18.  
  19. // maping function
  20. //uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max);
  21.  
  22. // variables using for convert ADC measurement to voltage
  23. uint8_t IntegerPart = 0, FractionPart = 0;
  24. uint32_t Voltage = 0;
  25.  
  26. //Flag buffer
  27. uint8_t nRF_Flag = 0;
  28.  
  29. // Flags and other variables
  30. uint8_t XPotentiometerVoltage = 0, YPotentiometerVoltage = 0;
  31. //volatile uint8_t ms10_counter, ms10_delay_flag, seconds, seconds_flag;    // for setting delays
  32.  
  33. int main (void)
  34. {
  35.     // UART initialization
  36.     USART_Init(_UBRR);
  37.  
  38.     // nRF24L01+ initialization
  39.     nRF_init();
  40.     register_nRF_RX_Event_Callback(RX_function);
  41.  
  42.     //OLED initialization
  43.     //OLED_init( SSD1306_SWITCHCAPVCC, REFRESH_MID );
  44.     //OLED_Clear();
  45.  
  46.     // ADC settings
  47.     ADMUX |= (1<<REFS1) | (1<<REFS0) | (1<<ADLAR) ;                 // Vref = 2.56V, ADLAR using for read just one byte from ADC measurement
  48.     ADCSRA |= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);     // ADC enable, ADC interrupt enable Prescaler 128 ( sampling freq = 16MHz / 128 )
  49.  
  50.     // TIMER0 (delays) configuration
  51. //  TCCR0 |= (1<<WGM01);                // CTC
  52. //  TCCR0 |= (1<<CS02)|(1<<CS00);       // prescaler = 1024
  53. //  OCR0 = 155;                         // 16MHz / 1024 / 156 ~= 100Hz ( 10 ms )
  54. //  TIMSK |= (1<<OCIE0);                // Timer0 enable
  55.  
  56.     // permit for interrupts
  57.     sei();
  58.  
  59.     // Variables
  60.     char Buffer[32];
  61.  
  62.     while(1)
  63.     {
  64.         if ( nRF_Flag == 0)
  65.         {
  66.                 XPotentiometerVoltage = measurement(PA1);
  67.                 memset(Buffer, '\0', 32);
  68.                 sprintf(Buffer, "Xaxis%d", XPotentiometerVoltage);
  69.                 nRF_SendDataToAir(Buffer);
  70.  
  71.                 YPotentiometerVoltage = measurement(PA0);
  72.                 memset(Buffer, '\0', 32);
  73.                 sprintf(Buffer, "Yaxis%d", YPotentiometerVoltage);
  74.                 nRF_SendDataToAir(Buffer);
  75.  
  76.                 memset(Buffer, '\0', 32);
  77.                 strcpy(Buffer, "TX");
  78.                 nRF_SendDataToAir(Buffer);
  79.         }
  80.  
  81. //      if ( RX_Flag == 1 )
  82. //      {
  83. ////            if ( RXFlag == 1 )
  84. ////            {
  85. ////                nRF_RX_Power_Up();
  86. ////                RXFlag = 0;
  87. ////            }
  88. //
  89. //          nRF_RX_EVENT();
  90. //      }
  91.  
  92.  
  93. //      OLED_SendString(0, 35, "Voltage: " ,1, 1, 0);
  94. //      OLED_SendInt(0, 35, IntegerPart, 1, 1, 0);
  95. //      OLED_SendString(0, strlen("Voltage: ")+1, "." ,1, 1, 0);
  96. //      OLED_SendInt(0, strlen("Voltage: ")+2, FractionPart, 1, 1, 0);
  97. //      OLED_SendString(0, 35, "                             " ,1, 1, 0);
  98.  
  99. //      OLED_Display();
  100.     }
  101.  
  102. }
  103.  
  104. void RX_function ( void * nRF_RX_buff, uint8_t len )
  105. {
  106.     char BufferForNumbers[32] = "";
  107.     char key[] = "0123456789";                      //key using to searching numbers in received commands
  108.     char *pointer;
  109.  
  110.     uint8_t BufferIndexIncrement = 0;
  111.  
  112.     //data parsing
  113.     if (strncmp(nRF_RX_buff, "Vol", 3) == 0)    // if received data contains "Xaxis"
  114.     {
  115.         pointer = strpbrk(nRF_RX_buff, key);        // strpbrk returns pointer to the first searching character
  116.  
  117.             while( pointer != NULL )
  118.             {
  119.                 BufferForNumbers[BufferIndexIncrement++] = *pointer;
  120.                 pointer = strpbrk(pointer+1, key);
  121.             }
  122.  
  123. //      Voltage = atoi(BufferForNumbers) * 25 * 2;
  124. //      IntegerPart = Voltage / 10000;
  125. //      FractionPart = ( Voltage / 100 ) % 100;
  126.     }
  127.  
  128.     if (strncmp(nRF_RX_buff, "TX", 2) == 0)
  129.     {
  130.         nRF_Flag = 0;
  131.         _delay_ms(3);
  132.     }
  133.  
  134. //  if (nRF_RX_buff != 0) nRF_Clear_RX();
  135.  
  136.     PORTA ^= (1<<PA3);
  137.  
  138. }
  139.  
  140. uint8_t measurement(uint8_t channel)
  141. {
  142.     ADMUX = (ADMUX & 0xF8) | channel;   // choosen ADC channel
  143.  
  144.     ADCSRA |= (1<<ADSC);                // measurement
  145.  
  146.     while( ADCSRA & (1<<ADSC) );        // wait until conversion ends
  147.  
  148.     return ADCH;                        // result from ADCH
  149. }
  150.  
  151. //uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
  152. //{
  153. //  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  154. //}
  155.  
  156. // Timer using for delays
  157. //ISR(TIMER0_COMP_vect)
  158. //{
  159. //  ms10_delay_flag = 1;                // set flag in every 10ms
  160. //
  161. //  if ( ++ms10_counter > 99)           // if one second reached
  162. //  {
  163. //      seconds_flag = 1;               // set second flag
  164. //      seconds++;
  165. //      if( seconds > 59 ) seconds = 0; // if one minute reached
  166. //      ms10_counter = 0;
  167. //  }
  168. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement