Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] C code for atmegat328p - nRF24l01+

Status
Not open for further replies.

Romdata

Newbie level 2
Joined
May 22, 2017
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
46
Hello there.

I´m new in this forum, so please be patient with me if i make mistakes or anything.

I do have this project on my education, where i have to send some values from a sensor througha atmega 328P via a nRF24l01+ to a Resparry Pi, I have the transmision up and runing, but now i´m stuck, and i have been for the last 20 days now

I want to send a static value for the sensors ID, and some dynamic value for the mesurement that the sensor has mesuret, at this moment i can only send a static value OR a dynamic value, i hope that some of you would like to give me some ideas hov to combine my static and dynamic data., some of the code i will show belov has danish words in, dont get confused about thet

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "nrf24l01.h"
#include "nrf24l01-mnemonics.h"
#include "STDIO_UART.h"
void setup_timer(void);
nRF24L01 *setup_rf(void);
volatile bool rf_interrupt = false;
volatile bool send_message = false;
int main(void) {
    uint8_t to_address[5] = { 0xc2, 0xc2, 0xc2, 0xc2, 0xc2 };
    bool on = false;
    sei();
    nRF24L01 *rf = setup_rf();
    setup_timer();
    
    char text_string[] = "1001:";
    void *text_send;
    text_send=&text_string;
    
    int tal=1;
    int *tal_done;
    tal_done=&tal;
    
    while (1) {                
                                        if (rf_interrupt)
                                        {
                                            rf_interrupt = false;
                                            int success = nRF24L01_transmit_success(rf);
                                            if (success != 0)
                                            nRF24L01_flush_transmit_message(rf);
                                        }
                                        
                                        if (send_message)
                                        {                                        
                                        send_message = false;
                                        on = !on;
                                        nRF24L01Message msg;
                                        memcpy(msg.data, ("%s" ,&text_string ), 10);
                                        msg.length = (strlen((char *)msg.data))+1;
                                        nRF24L01_transmit(rf, to_address, &msg);
                                        (*tal_done)++;
                                        }
                                                                                
                    }
    return 0;
}
nRF24L01 *setup_rf(void) {
    nRF24L01 *rf = nRF24L01_init();
    rf->ss.port = &PORTB;
    rf->ss.pin = PB2;
    rf->ce.port = &PORTB;
    rf->ce.pin = PB1;
    rf->sck.port = &PORTB;
    rf->sck.pin = PB5;
    rf->mosi.port = &PORTB;
    rf->mosi.pin = PB3;
    rf->miso.port = &PORTB;
    rf->miso.pin = PB4;
    // interrupt on falling edge of INT0 (PD2)
    EICRA |= _BV(ISC01);
    EIMSK |= _BV(INT0);
    nRF24L01_begin(rf);
    return rf;
}
// setup timer to trigger interrupt every second when at 1MHz
void setup_timer(void) {
    TCCR1B |= _BV(WGM12);
    TIMSK1 |= _BV(OCIE1A);
    //OCR1A = 15624;
    OCR1A = 0xFFFF;
    TCCR1B |= _BV(CS10) | _BV(CS11);
}
// each one second interrupt
ISR(TIMER1_COMPA_vect) {
    send_message = true;
}
// nRF24L01 interrupt
ISR(INT0_vect) {
    rf_interrupt = true;
}
 
*msg are referring to this struct
 
typedef struct {
    int pipe_number;
    uint8_t data[32];
    uint8_t length;
} nRF24L01Message;



And it all have to be set in one*msg vareiable for this function

void nRF24L01_transmit(nRF24L01 *rf, void *address, nRF24L01Message *msg);

As the code is now it sends the static ID to the respbarry ( 1001: )
and i just for a start want to have the ( tal ) to be incrementet and sendt with the string for each cycle like

1001:1
1001:2
1001:3
1001:4
1001:5

And so on.

I admit that the startcode is some i had modifyed so far.

I realy hope that some one can see what i want, and how to do it, feel free to ask if there is something i havent covered.

Best regard

Ricky
 
Last edited by a moderator:


Code C - [expand]
1
2
3
4
5
6
7
char text_string[] = "1001:";
    void *text_send;
    text_send=&text_string;
    
    int tal=1;
    int *tal_done;
    tal_done=&tal;


What is the purpose of pointers? tal_done is used only in one place and doesn't need pointer. text_send can be assigned without ampersand
text_send=text_string;
As an array name is an address itself. Besides that, I saw no use of text_send at all.


memcpy(msg.data, ("%s" ,&text_string ), 10);
Not the best syntax, although it works. Again, no ampersand sign is needed. This line can be written like this:
memcpy(msg.data, text_string, sizeof(text_string));


As for your demand for combining data there are several ways of doing that.
1. The simplest, but the most consuming way is sprintf function:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
if (send_message)
    {                                        
        send_message = false;
        on = !on;
        nRF24L01Message msg;
        sprintf(msg.data, "%s:%d", text_string, tal);
        msg.length = (strlen((char *)msg.data))+1;
        nRF24L01_transmit(rf, to_address, &msg);
        tal++;
    }



2. A little bit more complex:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
if (send_message)
    {                                        
        send_message = false;
        on = !on;
        nRF24L01Message msg;
        memcpy(msg.data, text_string, sizeof(text_string));  // I assume that text_string will not be changed
        itoa(tal, msg.data + sizeof(text_string) - 1);  // Place dynamic data after the static text
        msg.length = (strlen((char *)msg.data))+1;
        nRF24L01_transmit(rf, to_address, &msg);
        tal++;
    }

 

Thank you a lot, i did not know how to do it, so i made it more complicated than it was, so i tried using pointers, and any thing, but the sprintf function did it for me, and i´m so happy for your time and help, it solved my problem, so thank you one more time :)
I will clean my code now for pointers.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top