Date: April 2019. Version: 1.0.2. Can Library RX/TX, IDadress and data position. 3 independent timers for sending

Dependents:   CAN_Library_Example Ekran CAN-library

Committer:
renemagrit
Date:
Thu Mar 07 23:15:11 2019 +0000
Revision:
0:9cbaaaf1768f
Child:
1:dade5cf64d0e
Verzija 1.0.0.; CAN library RX/TX protokol. ; CANadressID.h za macro IDrx/IDtx i pozicije podataka.; Februar 2019.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
renemagrit 0:9cbaaaf1768f 1 #include "CANlibrary.h"
renemagrit 0:9cbaaaf1768f 2
renemagrit 0:9cbaaaf1768f 3
renemagrit 0:9cbaaaf1768f 4 extern CAN can;
renemagrit 0:9cbaaaf1768f 5 Ticker tick; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!! nije hteo da se prevede kao extern
renemagrit 0:9cbaaaf1768f 6
renemagrit 0:9cbaaaf1768f 7 /******************************************
renemagrit 0:9cbaaaf1768f 8 - - - - - VAZNO! - - - - -
renemagrit 0:9cbaaaf1768f 9 IDjevi se tumace u decimalnom brojnom sistemu
renemagrit 0:9cbaaaf1768f 10 - can_flag niz treba namestiti adrese da idu u u intervalu od 0-15 celobrojno
renemagrit 0:9cbaaaf1768f 11 - tj, mogle bi da se i koriste one od 2000h ali tipa da to se oduzima
renemagrit 0:9cbaaaf1768f 12 nekom celobrojom konstantom koja ce davati oseg adresa od 0 do 15
renemagrit 0:9cbaaaf1768f 13 */
renemagrit 0:9cbaaaf1768f 14
renemagrit 0:9cbaaaf1768f 15
renemagrit 0:9cbaaaf1768f 16 bool can_flag[16];
renemagrit 0:9cbaaaf1768f 17 CANMessage msgs[16];
renemagrit 0:9cbaaaf1768f 18 char data[8];
renemagrit 0:9cbaaaf1768f 19 char idTX;
renemagrit 0:9cbaaaf1768f 20 float msgInterval=0;
renemagrit 0:9cbaaaf1768f 21
renemagrit 0:9cbaaaf1768f 22 void can_initRX(){
renemagrit 0:9cbaaaf1768f 23 can.attach(can_msg_receive, CAN::RxIrq);
renemagrit 0:9cbaaaf1768f 24 }
renemagrit 0:9cbaaaf1768f 25 void can_msg_receive(){ // CAN RX Interrupt Function
renemagrit 0:9cbaaaf1768f 26 CANMessage tmpMsg;
renemagrit 0:9cbaaaf1768f 27 if (can.read(tmpMsg)) { //Detect message
renemagrit 0:9cbaaaf1768f 28 can_flag[tmpMsg.id]=true;
renemagrit 0:9cbaaaf1768f 29 msgs[tmpMsg.id]=tmpMsg;
renemagrit 0:9cbaaaf1768f 30 }
renemagrit 0:9cbaaaf1768f 31 };
renemagrit 0:9cbaaaf1768f 32
renemagrit 0:9cbaaaf1768f 33 uint16_t get_data(char id, char indeks){
renemagrit 0:9cbaaaf1768f 34 return msgs[id].data[indeks<<1] | (msgs[id].data[(indeks << 1) + 1] << 8);
renemagrit 0:9cbaaaf1768f 35 }
renemagrit 0:9cbaaaf1768f 36
renemagrit 0:9cbaaaf1768f 37 bool check_flag(char id){
renemagrit 0:9cbaaaf1768f 38 bool ret = can_flag[id];
renemagrit 0:9cbaaaf1768f 39 can_flag[id]=false;
renemagrit 0:9cbaaaf1768f 40 return ret;
renemagrit 0:9cbaaaf1768f 41 }
renemagrit 0:9cbaaaf1768f 42
renemagrit 0:9cbaaaf1768f 43
renemagrit 0:9cbaaaf1768f 44 //-------------------------------------------------------------------
renemagrit 0:9cbaaaf1768f 45 //-------------------------------------------------------------------
renemagrit 0:9cbaaaf1768f 46 // Funkcije slanja poruka:
renemagrit 0:9cbaaaf1768f 47
renemagrit 0:9cbaaaf1768f 48 void canTX_set_Interval(float inter){
renemagrit 0:9cbaaaf1768f 49
renemagrit 0:9cbaaaf1768f 50 //Zadavanje fiksnog intervala slanja CAN poruke
renemagrit 0:9cbaaaf1768f 51 //Vrednost je realna. Jedinica zadavanja je u sekundama.
renemagrit 0:9cbaaaf1768f 52 msgInterval=inter;
renemagrit 0:9cbaaaf1768f 53 }
renemagrit 0:9cbaaaf1768f 54
renemagrit 0:9cbaaaf1768f 55 void can_initTX(char idtx){
renemagrit 0:9cbaaaf1768f 56
renemagrit 0:9cbaaaf1768f 57 //Fukcija koja zadaje fiksnu adresu slanja poruke
renemagrit 0:9cbaaaf1768f 58 idTX=idtx;
renemagrit 0:9cbaaaf1768f 59 }
renemagrit 0:9cbaaaf1768f 60 void can_msg_send(){
renemagrit 0:9cbaaaf1768f 61
renemagrit 0:9cbaaaf1768f 62 /*
renemagrit 0:9cbaaaf1768f 63 * Fukcija koja salje podatke na CAN mrezu po zadatom,setovanom, IDju.
renemagrit 0:9cbaaaf1768f 64 * Duzina poruke podesena je fiksno na 8B.
renemagrit 0:9cbaaaf1768f 65 */
renemagrit 0:9cbaaaf1768f 66 can.write(CANMessage((uint16_t)idTX, data, 8));
renemagrit 0:9cbaaaf1768f 67 }
renemagrit 0:9cbaaaf1768f 68 void can_msg_send_tick(){
renemagrit 0:9cbaaaf1768f 69
renemagrit 0:9cbaaaf1768f 70 //Funkcija koja se poziva za slanje poruka u okviru fiksnog intervala
renemagrit 0:9cbaaaf1768f 71 tick.attach(can_msg_send, msgInterval);
renemagrit 0:9cbaaaf1768f 72 }
renemagrit 0:9cbaaaf1768f 73 //----------------------------------------------------------------------------
renemagrit 0:9cbaaaf1768f 74 /* Pomocne funkcije za pakovanje podataka prilikom slanja:
renemagrit 0:9cbaaaf1768f 75 * Ove funkcije omogućavaju da automatsko pakovanje u razlicite velicine poruke
renemagrit 0:9cbaaaf1768f 76 * u zavisnosti od tipa i pozicije na kojoj treba poslati podatak.
renemagrit 0:9cbaaaf1768f 77 *
renemagrit 0:9cbaaaf1768f 78 * Realizovane su za dva tipa:
renemagrit 0:9cbaaaf1768f 79 * -unit16_t (automatsko pakovanje u dva podatka od po 1B)
renemagrit 0:9cbaaaf1768f 80 * -char
renemagrit 0:9cbaaaf1768f 81 *
renemagrit 0:9cbaaaf1768f 82 * Format: dta = vrednost koju saljemo kao podatak CANom, pos = pozicija u CAN
renemagrit 0:9cbaaaf1768f 83 * poruci.
renemagrit 0:9cbaaaf1768f 84 *
renemagrit 0:9cbaaaf1768f 85 * CAN poruka: (IDslanja, DD7, DD6, DD5, DD4, DD3,DD2, DD1, DD0)
renemagrit 0:9cbaaaf1768f 86 * Velicina podataka (DD7...DD0) je od po 1Byte.
renemagrit 0:9cbaaaf1768f 87 * Maximalno se mogu poslati 4 podatka od po 2B (pos polje u opsegu od 0 do 3)
renemagrit 0:9cbaaaf1768f 88 * ili 8 od po 1B (pos polje u opsegu od 0 do 7).
renemagrit 0:9cbaaaf1768f 89 */
renemagrit 0:9cbaaaf1768f 90
renemagrit 0:9cbaaaf1768f 91 void pack_data(uint16_t dta, char pos){
renemagrit 0:9cbaaaf1768f 92
renemagrit 0:9cbaaaf1768f 93 //Pakuje unit16_t u dva podatka od po 1B, na poziciji pos u CAN poruci
renemagrit 0:9cbaaaf1768f 94 data[pos<<1] = dta;
renemagrit 0:9cbaaaf1768f 95 data[(pos<<1)+1] = dta >> 8;
renemagrit 0:9cbaaaf1768f 96 }
renemagrit 0:9cbaaaf1768f 97
renemagrit 0:9cbaaaf1768f 98 void pack_data(char dta, char pos){
renemagrit 0:9cbaaaf1768f 99
renemagrit 0:9cbaaaf1768f 100 //Pakuje char u jedan podatka od 1B, na poziciji pos u CAN poruci
renemagrit 0:9cbaaaf1768f 101 data[pos<<1] = dta;
renemagrit 0:9cbaaaf1768f 102 }
renemagrit 0:9cbaaaf1768f 103
renemagrit 0:9cbaaaf1768f 104
renemagrit 0:9cbaaaf1768f 105