Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | //***************************************************************************** |
2 | // File Name : stxetxtest.c |
||
3 | // Title : Example usage of STX/ETX packet protocol and library code |
||
4 | // Revision : 0.1 |
||
5 | // Notes : |
||
6 | // Target MCU : Atmel AVR series |
||
7 | // Editor Tabs : 4 |
||
8 | // |
||
9 | // Revision History: |
||
10 | // When Who Description of change |
||
11 | // ----------- ----------- ----------------------- |
||
12 | // 20-Nov-2002 pstang Created the program |
||
13 | //***************************************************************************** |
||
14 | |||
15 | //----- Include Files --------------------------------------------------------- |
||
16 | #include <avr/io.h> // include I/O definitions (port names, pin names, etc) |
||
17 | #include <avr/interrupt.h> // include interrupt support |
||
18 | #include <avr/pgmspace.h> |
||
19 | |||
20 | #include "global.h" // include our global settings |
||
21 | #include "uart.h" // include uart function library |
||
22 | #include "rprintf.h" // include printf function library |
||
23 | #include "timer.h" // include timer function library |
||
24 | #include "lcd.h" // include LCD support |
||
25 | #include "stxetx.h" // include STX/ETX packet support |
||
26 | |||
27 | |||
28 | // make a transmitting packet buffer area in memory |
||
29 | // where we can assemble the data we wish to transmit |
||
30 | // using stx/etx |
||
31 | unsigned char TxPacket[20]; |
||
32 | |||
33 | // function prototypes |
||
34 | void receiveStxEtx(void); |
||
35 | void transmitStxEtx(void); |
||
36 | u08 getSw(void); |
||
37 | |||
38 | // main code |
||
39 | int main(void) |
||
40 | { |
||
41 | // initialize the AVRlib libraries |
||
42 | timerInit(); // initialize the timer system |
||
43 | uartInit(); // initialize the UART (serial port) |
||
44 | lcdInit(); |
||
45 | rprintfInit(lcdDataWrite); // init rprintf |
||
46 | |||
47 | // we will be using the AVR UART to communicate the |
||
48 | // STX/ETX protocol to another AVR board or to a PC |
||
49 | |||
50 | // initialize stxetx to use the UART for sending data |
||
51 | stxetxInit(uartSendByte); |
||
52 | |||
53 | // set baud rate to 4800 (less than 5000) if communicating using |
||
54 | // the Linx LC radio modules |
||
55 | uartSetBaudRate(4800); |
||
56 | |||
57 | // choose to either transmit or receive |
||
58 | transmitStxEtx(); |
||
59 | //receiveStxEtx(); |
||
60 | |||
61 | return 0; |
||
62 | } |
||
63 | |||
64 | void transmitStxEtx(void) |
||
65 | { |
||
66 | u16 packetNum=0; |
||
67 | |||
68 | lcdClear(); |
||
69 | |||
70 | while(1) |
||
71 | { |
||
72 | // assemble the data I wish to send inside a STX/ETX packet |
||
73 | // send the string "ALOHA" |
||
74 | TxPacket[0] = 'A'; |
||
75 | TxPacket[1] = 'L'; |
||
76 | TxPacket[2] = 'O'; |
||
77 | TxPacket[3] = 'H'; |
||
78 | TxPacket[4] = 'A'; |
||
79 | // send the packet number |
||
80 | TxPacket[5] = packetNum>>8; // high byte |
||
81 | TxPacket[6] = packetNum; // low byte |
||
82 | // send the current state of PORTA |
||
83 | TxPacket[7] = inb(PINA); |
||
84 | |||
85 | // send the packet with: |
||
86 | // packet status = 0 |
||
87 | // packet type = 0x55 |
||
88 | // (the packet type and status may be, and mean, anything the user wishes) |
||
89 | // 8-bytes of user data |
||
90 | stxetxSend(0, 0x55, 8, TxPacket); |
||
91 | |||
92 | // output our packet data to the LCD |
||
93 | lcdGotoXY(0,0); rprintf("Sending..."); |
||
94 | lcdGotoXY(20,0); rprintf("PORTA: "); rprintfu08(inb(PINA)); |
||
95 | lcdGotoXY(0,1); rprintf("String: ALOHA"); |
||
96 | lcdGotoXY(20,1); rprintf("PacketNum: "); rprintfu16(packetNum); |
||
97 | |||
98 | // increment our packet number |
||
99 | packetNum++; |
||
100 | // pause for a moment |
||
101 | timerPause(10); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | void receiveStxEtx(void) |
||
106 | { |
||
107 | u16 packetNum=0; |
||
108 | u08* dataPtr; |
||
109 | |||
110 | lcdClear(); |
||
111 | |||
112 | while(1) |
||
113 | { |
||
114 | lcdGotoXY(0,0); rprintf("Receiving..."); |
||
115 | |||
116 | // here we get the UART's receive buffer and give it to the STX/ETX |
||
117 | // packet processing function. If the packet processor finds a valid |
||
118 | // packet in the buffer, it will return true. |
||
119 | if(stxetxProcess(uartGetRxBuffer())) |
||
120 | { |
||
121 | // sxtetxProcess has reported that it found a packet |
||
122 | // let's get the data... |
||
123 | |||
124 | // (NOTE: although I discard the status, type, and datalength |
||
125 | // below, it would be important if I were sending more than one |
||
126 | // kind of packet) |
||
127 | |||
128 | // get the packet's status |
||
129 | stxetxGetRxPacketStatus(); |
||
130 | // get the packet's type |
||
131 | stxetxGetRxPacketType(); |
||
132 | // get the packet's datalength |
||
133 | stxetxGetRxPacketDatalength(); |
||
134 | // get a pointer to the place where the received data is stored |
||
135 | dataPtr = stxetxGetRxPacketData(); |
||
136 | |||
137 | // decode packet number |
||
138 | packetNum = (dataPtr[5]<<8) | dataPtr[6]; |
||
139 | |||
140 | // output our packet data to the LCD |
||
141 | lcdGotoXY(20,0); rprintf("PORTA: "); rprintfu08( dataPtr[7] ); |
||
142 | lcdGotoXY(0,1); rprintf("String: "); |
||
143 | // print out the string |
||
144 | rprintfChar( dataPtr[0] ); |
||
145 | rprintfChar( dataPtr[1] ); |
||
146 | rprintfChar( dataPtr[2] ); |
||
147 | rprintfChar( dataPtr[3] ); |
||
148 | rprintfChar( dataPtr[4] ); |
||
149 | lcdGotoXY(20,1); rprintf("PacketNum: "); rprintfu16(packetNum); |
||
150 | } |
||
151 | } |
||
152 | } |
Powered by WebSVN v2.8.3