?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 6

Line No. Rev Author Line
1 6 kaklik //*****************************************************************************
2 // File Name : i2ctest.c
3 //
4 // Title : example usage of I2C library functions
5 // Revision : 1.0
6 // Notes :
7 // Target MCU : Atmel AVR series
8 // Editor Tabs : 4
9 //
10 // Revision History:
11 // When Who Description of change
12 // ----------- ----------- -----------------------
13 // 03-Feb-2003 pstang Created the program
14 //*****************************************************************************
15  
16  
17 //----- Include Files ---------------------------------------------------------
18 #include <avr/io.h> // include I/O definitions (port names, pin names, etc)
19 #include <avr/interrupt.h> // include interrupt support
20  
21 #include "global.h" // include our global settings
22 #include "uart.h" // include uart function library
23 #include "rprintf.h" // include printf function library
24 #include "a2d.h" // include A/D converter function library
25 #include "timer.h" // include timer function library (timing, PWM, etc)
26 #include "vt100.h" // include VT100 terminal support
27 #include "i2c.h" // include i2c support
28 #include "debug.h" // include debug functions
29  
30  
31 #define LOCAL_ADDR 0xA0
32 #define TARGET_ADDR 0xA0
33  
34 // local data buffer
35 unsigned char localBuffer[] = "Pascal is cool!!Pascal is Cool!!";
36 unsigned char localBufferLength = 0x20;
37  
38 void i2cTest(void);
39 void i2cSlaveReceiveService(u08 receiveDataLength, u08* receiveData);
40 u08 i2cSlaveTransmitService(u08 transmitDataLengthMax, u08* transmitData);
41 void i2cMasterSendDiag(u08 deviceAddr, u08 length, u08* data);
42 void testI2cMemory(void);
43 void showByte(u08 byte);
44  
45 //----- Begin Code ------------------------------------------------------------
46 int main(void)
47 {
48 // initialize our libraries
49 // initialize the UART (serial port)
50 uartInit();
51 // make all rprintf statements use uart for output
52 rprintfInit(uartSendByte);
53 // initialize the timer system
54 timerInit();
55 // clear terminal screen
56 vt100ClearScreen();
57 vt100SetCursorPos(1,1);
58 // print a little intro message so we know things are working
59 rprintf("\r\nWelcome to i2c test!\r\n");
60  
61 // run tests
62  
63 i2cTest();
64  
65 return 0;
66 }
67  
68  
69 void i2cTest(void)
70 {
71 u08 c=0;
72 showByte(0x55);
73  
74 // initialize i2c function library
75 i2cInit();
76 // set local device address and allow response to general call
77 i2cSetLocalDeviceAddr(LOCAL_ADDR, TRUE);
78 // set the Slave Receive Handler function
79 // (this function will run whenever a master somewhere else on the bus
80 // writes data to us as a slave)
81 i2cSetSlaveReceiveHandler( i2cSlaveReceiveService );
82 // set the Slave Transmit Handler function
83 // (this function will run whenever a master somewhere else on the bus
84 // attempts to read data from us as a slave)
85 i2cSetSlaveTransmitHandler( i2cSlaveTransmitService );
86  
87 timerPause(500);
88 showByte(0xAA);
89  
90 while(1)
91 {
92 rprintf("Command>");
93 while(!c) uartReceiveByte(&c);
94  
95 switch(c)
96 {
97 case 's':
98 rprintf("Send: ");
99 // get string from terminal
100 localBufferLength = 0;
101 c = 0;
102 while((c != 0x0D) && (localBufferLength < 0x20))
103 {
104 while(!uartReceiveByte(&c));
105 localBuffer[localBufferLength++] = c;
106 uartSendByte(c);
107 }
108 // switch CR to NULL to terminate string
109 localBuffer[localBufferLength-1] = 0;
110 // send string over I2C
111 i2cMasterSend(TARGET_ADDR, localBufferLength, localBuffer);
112 //i2cMasterSendNI(TARGET_ADDR, localBufferLength, localBuffer);
113 rprintfCRLF();
114 break;
115 case 'r':
116 rprintf("Receive: ");
117 // receive string over I2C
118 i2cMasterReceive(TARGET_ADDR, 0x10, localBuffer);
119 //i2cMasterReceiveNI(TARGET_ADDR, 0x10, localBuffer);
120 // format buffer
121 localBuffer[0x10] = 0;
122 rprintfStr(localBuffer);
123 rprintfCRLF();
124 break;
125 case 'm':
126 testI2cMemory();
127 break;
128 default:
129 rprintf("Unknown Command!");
130 break;
131 }
132 c = 0;
133 rprintfCRLF();
134 }
135  
136 }
137  
138  
139 // slave operations
140 void i2cSlaveReceiveService(u08 receiveDataLength, u08* receiveData)
141 {
142 u08 i;
143  
144 // this function will run when a master somewhere else on the bus
145 // addresses us and wishes to write data to us
146  
147 showByte(*receiveData);
148 cbi(PORTB, PB6);
149  
150 // copy the received data to a local buffer
151 for(i=0; i<receiveDataLength; i++)
152 {
153 localBuffer[i] = *receiveData++;
154 }
155 localBufferLength = receiveDataLength;
156 }
157  
158 u08 i2cSlaveTransmitService(u08 transmitDataLengthMax, u08* transmitData)
159 {
160 u08 i;
161  
162 // this function will run when a master somewhere else on the bus
163 // addresses us and wishes to read data from us
164  
165 showByte(*transmitData);
166 cbi(PORTB, PB7);
167  
168 // copy the local buffer to the transmit buffer
169 for(i=0; i<localBufferLength; i++)
170 {
171 *transmitData++ = localBuffer[i];
172 }
173  
174 localBuffer[0]++;
175  
176 return localBufferLength;
177 }
178  
179 void i2cMasterSendDiag(u08 deviceAddr, u08 length, u08* data)
180 {
181 // this function is equivalent to the i2cMasterSendNI() in the I2C library
182 // except it will print information about transmission progress to the terminal
183  
184 // disable TWI interrupt
185 cbi(TWCR, TWIE);
186  
187 // send start condition
188 i2cSendStart();
189 i2cWaitForComplete();
190 rprintf("STA-");
191  
192 // send device address with write
193 i2cSendByte( deviceAddr&0xFE );
194 i2cWaitForComplete();
195 rprintf("SLA+W-");
196  
197 // send data
198 while(length)
199 {
200 i2cSendByte( *data++ );
201 i2cWaitForComplete();
202 rprintf("DATA-");
203 length--;
204 }
205  
206 // transmit stop condition
207 // leave with TWEA on for slave receiving
208 i2cSendStop();
209 while( !(inb(TWCR) & BV(TWSTO)) );
210 rprintf("STO");
211  
212 // enable TWI interrupt
213 sbi(TWCR, TWIE);
214 }
215  
216 void testI2cMemory(void)
217 {
218 u08 i;
219 u08 txdata[66];
220 u08 rxdata[66];
221  
222 rprintfProgStrM("\r\nRunning I2C memory test (24xxyy devices)\r\n");
223  
224 // compose address
225 txdata[0] = 0;
226 txdata[1] = 0;
227 // compose data
228 for(i=0; i<16; i++)
229 txdata[2+i] = localBuffer[i];
230 // send address and data
231 i2cMasterSendNI(TARGET_ADDR, 18, txdata);
232  
233 rprintfProgStrM("Stored data: ");
234 // null-terminate data
235 txdata[18] = 0;
236 rprintfStr(&txdata[2]);
237 rprintfCRLF();
238  
239 timerPause(100);
240  
241 // send address
242 i2cMasterSendNI(TARGET_ADDR, 2, txdata);
243 // get data
244 i2cMasterReceiveNI(TARGET_ADDR, 16, rxdata);
245 // null-terminate received string
246 rxdata[16] = 0;
247  
248 rprintfProgStrM("Received data: ");
249 rprintfStr(rxdata);
250 rprintfCRLF();
251 /*
252 u08 c;
253 u16 addr=0;
254  
255 while(1)
256 {
257 while(!uartReceiveByte(&c));
258  
259 switch(c)
260 {
261 case '+':
262 addr+=64;
263 break;
264 case '-':
265 addr-=64;
266 break;
267 }
268 c = 0;
269 txdata[0] = (addr>>8);
270 txdata[1] = (addr&0x00FF);
271 i2cMasterSendNI(TARGET_ADDR, 2, txdata);
272 i2cMasterReceiveNI(TARGET_ADDR, 64, rxdata);
273 rprintfProgStrM("Received data at ");
274 rprintfu16(addr);
275 rprintfProgStrM(":\r\n");
276 debugPrintHexTable(64, rxdata);
277 }
278 */
279 }
280  
281 void showByte(u08 byte)
282 {
283 // set PORTB to output
284 outb(DDRB, 0xFF);
285 // output byte to LEDs
286 outb(PORTB, ~byte);
287 }
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3