Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "nRF24l01/nRF24L01.h"
- #include "UART/Uart.h" // UART library
- #include "Terminal/terminal.h" // Terminal library using UART
- #include "OLED/OLED.h" // 128x64 OLED library
- // Receiving function
- void RX_function ( void * nRF_RX_buff , uint8_t len );
- // ADC measurement function
- uint8_t measurement(uint8_t channel);
- // maping function
- //uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max);
- // variables using for convert ADC measurement to voltage
- uint8_t IntegerPart = 0, FractionPart = 0;
- uint32_t Voltage = 0;
- //Flag buffer
- uint8_t nRF_Flag = 0;
- // Flags and other variables
- uint8_t XPotentiometerVoltage = 0, YPotentiometerVoltage = 0;
- //volatile uint8_t ms10_counter, ms10_delay_flag, seconds, seconds_flag; // for setting delays
- int main (void)
- {
- // UART initialization
- USART_Init(_UBRR);
- // nRF24L01+ initialization
- nRF_init();
- register_nRF_RX_Event_Callback(RX_function);
- //OLED initialization
- //OLED_init( SSD1306_SWITCHCAPVCC, REFRESH_MID );
- //OLED_Clear();
- // ADC settings
- ADMUX |= (1<<REFS1) | (1<<REFS0) | (1<<ADLAR) ; // Vref = 2.56V, ADLAR using for read just one byte from ADC measurement
- ADCSRA |= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // ADC enable, ADC interrupt enable Prescaler 128 ( sampling freq = 16MHz / 128 )
- // TIMER0 (delays) configuration
- // TCCR0 |= (1<<WGM01); // CTC
- // TCCR0 |= (1<<CS02)|(1<<CS00); // prescaler = 1024
- // OCR0 = 155; // 16MHz / 1024 / 156 ~= 100Hz ( 10 ms )
- // TIMSK |= (1<<OCIE0); // Timer0 enable
- // permit for interrupts
- sei();
- // Variables
- char Buffer[32];
- while(1)
- {
- if ( nRF_Flag == 0)
- {
- XPotentiometerVoltage = measurement(PA1);
- memset(Buffer, '\0', 32);
- sprintf(Buffer, "Xaxis%d", XPotentiometerVoltage);
- nRF_SendDataToAir(Buffer);
- YPotentiometerVoltage = measurement(PA0);
- memset(Buffer, '\0', 32);
- sprintf(Buffer, "Yaxis%d", YPotentiometerVoltage);
- nRF_SendDataToAir(Buffer);
- memset(Buffer, '\0', 32);
- strcpy(Buffer, "TX");
- nRF_SendDataToAir(Buffer);
- }
- // if ( RX_Flag == 1 )
- // {
- //// if ( RXFlag == 1 )
- //// {
- //// nRF_RX_Power_Up();
- //// RXFlag = 0;
- //// }
- //
- // nRF_RX_EVENT();
- // }
- // OLED_SendString(0, 35, "Voltage: " ,1, 1, 0);
- // OLED_SendInt(0, 35, IntegerPart, 1, 1, 0);
- // OLED_SendString(0, strlen("Voltage: ")+1, "." ,1, 1, 0);
- // OLED_SendInt(0, strlen("Voltage: ")+2, FractionPart, 1, 1, 0);
- // OLED_SendString(0, 35, " " ,1, 1, 0);
- // OLED_Display();
- }
- }
- void RX_function ( void * nRF_RX_buff, uint8_t len )
- {
- char BufferForNumbers[32] = "";
- char key[] = "0123456789"; //key using to searching numbers in received commands
- char *pointer;
- uint8_t BufferIndexIncrement = 0;
- //data parsing
- if (strncmp(nRF_RX_buff, "Vol", 3) == 0) // if received data contains "Xaxis"
- {
- pointer = strpbrk(nRF_RX_buff, key); // strpbrk returns pointer to the first searching character
- while( pointer != NULL )
- {
- BufferForNumbers[BufferIndexIncrement++] = *pointer;
- pointer = strpbrk(pointer+1, key);
- }
- // Voltage = atoi(BufferForNumbers) * 25 * 2;
- // IntegerPart = Voltage / 10000;
- // FractionPart = ( Voltage / 100 ) % 100;
- }
- if (strncmp(nRF_RX_buff, "TX", 2) == 0)
- {
- nRF_Flag = 0;
- _delay_ms(3);
- }
- // if (nRF_RX_buff != 0) nRF_Clear_RX();
- PORTA ^= (1<<PA3);
- }
- uint8_t measurement(uint8_t channel)
- {
- ADMUX = (ADMUX & 0xF8) | channel; // choosen ADC channel
- ADCSRA |= (1<<ADSC); // measurement
- while( ADCSRA & (1<<ADSC) ); // wait until conversion ends
- return ADCH; // result from ADCH
- }
- //uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
- //{
- // return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- //}
- // Timer using for delays
- //ISR(TIMER0_COMP_vect)
- //{
- // ms10_delay_flag = 1; // set flag in every 10ms
- //
- // if ( ++ms10_counter > 99) // if one second reached
- // {
- // seconds_flag = 1; // set second flag
- // seconds++;
- // if( seconds > 59 ) seconds = 0; // if one minute reached
- // ms10_counter = 0;
- // }
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement