Line No. | Rev | Author | Line |
---|---|---|---|
1 | 1 | kaklik | //////////////////////////////////////////////////////////////////////////////////////////// |
2 | // knihovna pro bezdratovou komunikaci pomoci protokolu AX.25 |
||
3 | //////////////////////////////////////////////////////////////////////////////////////////// |
||
4 | // |
||
5 | // porty: |
||
6 | // #define PTT PIN_A2 // PTT control |
||
7 | // #define TXo PIN_C0 // To the transmitter modulator |
||
8 | // |
||
9 | // |
||
10 | //////////////////////////////////////////////////////////////////////////////////////////// |
||
11 | |||
12 | #define PERIODAH delay_us(222) // Halfperiod H 222;78/1200 500;430/500 |
||
13 | #define TAILH delay_us(78) |
||
14 | #define PERIODAL delay_us(412) // Halfperiod L 412;345/1200 1000;880/500 |
||
15 | #define TAILL delay_us(345) |
||
16 | #byte STATUS = 3 // CPUs status register |
||
17 | |||
18 | byte SendData[16] = {'A'<<1, 'L'<<1, 'L'<<1, ' '<<1, ' '<<1, ' '<<1, 0x60, |
||
19 | 'C'<<1, 'Z'<<1, '0'<<1, 'R'<<1, 'R'<<1, 'R'<<1, 0x61, |
||
20 | 0x03, 0xF0}; |
||
21 | |||
22 | boolean bit; |
||
23 | int fcslo, fcshi; // variabloes for calculating FCS (CRC) |
||
24 | int stuff; // stuff counter for extra 0 |
||
25 | int flag_flag; // if it is sending flag (7E) |
||
26 | int fcs_flag; // if it is sending Frame Check Sequence |
||
27 | int i; // for for |
||
28 | |||
29 | void flipout() //flips the state of output pin a_1 |
||
30 | { |
||
31 | stuff = 0; //since this is a 0, reset the stuff counter |
||
32 | if (bit) |
||
33 | { |
||
34 | bit=FALSE; //if the state of the pin was low, make it high. |
||
35 | } |
||
36 | else |
||
37 | { |
||
38 | bit=TRUE; //if the state of the pin was high make it low |
||
39 | } |
||
40 | } |
||
41 | |||
42 | void fcsbit(byte tbyte) |
||
43 | { |
||
44 | #asm |
||
45 | BCF STATUS,0 |
||
46 | RRF fcshi,F // rotates the entire 16 bits |
||
47 | RRF fcslo,F // to the right |
||
48 | #endasm |
||
49 | if (((STATUS & 0x01)^(tbyte)) ==0x01) |
||
50 | { |
||
51 | fcshi = fcshi^0x84; |
||
52 | fcslo = fcslo^0x08; |
||
53 | } |
||
54 | } |
||
55 | |||
56 | void SendBit () |
||
57 | { |
||
58 | if (bit) |
||
59 | { |
||
60 | output_high(TXo); |
||
61 | PERIODAH; |
||
62 | output_low(TXo); |
||
63 | PERIODAH; |
||
64 | output_high(TXo); |
||
65 | PERIODAH; |
||
66 | output_low(TXo); |
||
67 | TAILH; |
||
68 | } |
||
69 | else |
||
70 | { |
||
71 | output_high(TXo); |
||
72 | PERIODAL; |
||
73 | output_low(TXo); |
||
74 | TAILL; |
||
75 | }; |
||
76 | } |
||
77 | |||
78 | void SendByte (byte inbyte) |
||
79 | { |
||
80 | int k, bt; |
||
81 | |||
82 | for (k=0;k<8;k++) //do the following for each of the 8 bits in the byte |
||
83 | { |
||
84 | bt = inbyte & 0x01; //strip off the rightmost bit of the byte to be sent (inbyte) |
||
85 | if ((fcs_flag == FALSE) & (flag_flag == FALSE)) fcsbit(bt); //do FCS calc, but only if this |
||
86 | //is not a flag or fcs byte |
||
87 | if (bt == 0) |
||
88 | { |
||
89 | flipout(); |
||
90 | } // if this bit is a zero, flip the output state |
||
91 | else |
||
92 | { //otherwise if it is a 1, do the following: |
||
93 | if (flag_flag == FALSE) stuff++; //increment the count of consequtive 1's |
||
94 | if ((flag_flag == FALSE) & (stuff == 5)) |
||
95 | { //stuff an extra 0, if 5 1's in a row |
||
96 | SendBit(); |
||
97 | flipout(); //flip the output state to stuff a 0 |
||
98 | }//end of if |
||
99 | }//end of else |
||
100 | // delay_us(850); //introduces a delay that creates 1200 baud |
||
101 | SendBit(); |
||
102 | inbyte = inbyte>>1; //go to the next bit in the byte |
||
103 | }//end of for |
||
104 | }//end of SendByte |
||
105 | |||
106 | void SendPacket(char *data) |
||
107 | { |
||
108 | bit=FALSE; |
||
109 | |||
110 | fcslo=fcshi=0xFF; //The 2 FCS Bytes are initialized to FF |
||
111 | stuff = 0; //The variable stuff counts the number of 1's in a row. When it gets to 5 |
||
112 | // it is time to stuff a 0. |
||
113 | |||
114 | // output_low(PTT); // Blinking LED |
||
115 | // delay_ms(1000); |
||
116 | // output_high(PTT); |
||
117 | |||
118 | flag_flag = TRUE; //The variable flag is true if you are transmitted flags (7E's) false otherwise. |
||
119 | fcs_flag = FALSE; //The variable fcsflag is true if you are transmitting FCS bytes, false otherwise. |
||
120 | |||
121 | for(i=0; i<10; i++) SendByte(0x7E); //Sends flag bytes. Adjust length for txdelay |
||
122 | //each flag takes approx 6.7 ms |
||
123 | flag_flag = FALSE; //done sending flags |
||
124 | |||
125 | for(i=0; i<16; i++) SendByte(SendData[i]); //send the packet bytes |
||
126 | |||
127 | for(i=0; 0 != *data; i++) |
||
128 | { |
||
129 | SendByte(*data); //send the packet bytes |
||
130 | data++; |
||
131 | }; |
||
132 | |||
133 | fcs_flag = TRUE; //about to send the FCS bytes |
||
134 | fcslo =fcslo^0xff; //must XOR them with FF before sending |
||
135 | fcshi = fcshi^0xff; |
||
136 | SendByte(fcslo); //send the low byte of fcs |
||
137 | SendByte(fcshi); //send the high byte of fcs |
||
138 | fcs_flag = FALSE; //done sending FCS |
||
139 | flag_flag = TRUE; //about to send flags |
||
140 | SendByte(0x7e); // Send a flag to end packet |
||
141 | } |
||
142 | |||
143 | |||
144 |
Powered by WebSVN v2.8.3