TEST EDİLMİŞTİR

Dependencies:   mbed RF24

Committer:
belkaous
Date:
Fri Jan 18 14:31:17 2019 +0000
Revision:
1:45ed2ebefd5a
Parent:
0:b643b00c78dc
Child:
2:0914762d8957
292

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hamaint 0:b643b00c78dc 1 #include "mbed.h"
hamaint 0:b643b00c78dc 2 #include <RF24.h>
hamaint 0:b643b00c78dc 3
hamaint 0:b643b00c78dc 4
belkaous 1:45ed2ebefd5a 5 RF24 device(SPI_MOSI, SPI_MISO, SPI_SCK, D9, SPI_CS);
hamaint 0:b643b00c78dc 6 DigitalIn ms(D2);
hamaint 0:b643b00c78dc 7 DigitalIn mybutton(USER_BUTTON);
hamaint 0:b643b00c78dc 8 const uint64_t pipe_address = 0xF0F0F0F0AA;
hamaint 0:b643b00c78dc 9
hamaint 0:b643b00c78dc 10 void receiver();
hamaint 0:b643b00c78dc 11 void transmitter(char* message);
hamaint 0:b643b00c78dc 12
hamaint 0:b643b00c78dc 13 int main() {
hamaint 0:b643b00c78dc 14
hamaint 0:b643b00c78dc 15 device.begin();
hamaint 0:b643b00c78dc 16
belkaous 1:45ed2ebefd5a 17 char message[32] = "vv";
belkaous 1:45ed2ebefd5a 18
belkaous 1:45ed2ebefd5a 19 if (ms){
belkaous 1:45ed2ebefd5a 20 device.openWritingPipe(pipe_address);
belkaous 1:45ed2ebefd5a 21
belkaous 1:45ed2ebefd5a 22 } else{
belkaous 1:45ed2ebefd5a 23 /*slave*/
belkaous 1:45ed2ebefd5a 24
belkaous 1:45ed2ebefd5a 25 device.openReadingPipe(1,pipe_address);
belkaous 1:45ed2ebefd5a 26 device.startListening();
belkaous 1:45ed2ebefd5a 27
belkaous 1:45ed2ebefd5a 28 }
belkaous 1:45ed2ebefd5a 29
belkaous 1:45ed2ebefd5a 30
hamaint 0:b643b00c78dc 31 while (1){
hamaint 0:b643b00c78dc 32
hamaint 0:b643b00c78dc 33
hamaint 0:b643b00c78dc 34 if (ms){
hamaint 0:b643b00c78dc 35 /*master*/
hamaint 0:b643b00c78dc 36 if (mybutton == 0) {
hamaint 0:b643b00c78dc 37
hamaint 0:b643b00c78dc 38 transmitter(message);
belkaous 1:45ed2ebefd5a 39 printf("%s => emis : %d\r\n",message,sizeof(message));
hamaint 0:b643b00c78dc 40 wait(0.2);
hamaint 0:b643b00c78dc 41
hamaint 0:b643b00c78dc 42 }
hamaint 0:b643b00c78dc 43
hamaint 0:b643b00c78dc 44 } else{
hamaint 0:b643b00c78dc 45 /*slave*/
hamaint 0:b643b00c78dc 46
belkaous 1:45ed2ebefd5a 47 receiver();
hamaint 0:b643b00c78dc 48 }
hamaint 0:b643b00c78dc 49 }
hamaint 0:b643b00c78dc 50
hamaint 0:b643b00c78dc 51 }
hamaint 0:b643b00c78dc 52
hamaint 0:b643b00c78dc 53 void transmitter(char* message){
hamaint 0:b643b00c78dc 54 device.write(message,sizeof(message));
hamaint 0:b643b00c78dc 55 }
hamaint 0:b643b00c78dc 56
hamaint 0:b643b00c78dc 57 void receiver(){
belkaous 1:45ed2ebefd5a 58
belkaous 1:45ed2ebefd5a 59
belkaous 1:45ed2ebefd5a 60 // if (device.available()){
belkaous 1:45ed2ebefd5a 61
hamaint 0:b643b00c78dc 62 char text[32];
hamaint 0:b643b00c78dc 63 device.read(&text, sizeof(text));
hamaint 0:b643b00c78dc 64 if (text[0]!='\0')
belkaous 1:45ed2ebefd5a 65 printf("%s recu : %d\n\r",text,sizeof(text));
belkaous 1:45ed2ebefd5a 66 // }
hamaint 0:b643b00c78dc 67 }
hamaint 0:b643b00c78dc 68
hamaint 0:b643b00c78dc 69