1 | /*******************************************************
|
2 | Author: Manfred Langemann
|
3 | mailto: Manfred.Langemann ät t-online.de
|
4 | Begin of project: 04.01.2008
|
5 | Latest version generated: 04.01.2008
|
6 | Filename: Main.c
|
7 | Description: Main routine for testing
|
8 | TWI_Master_main.c
|
9 | ********************************************************/
|
10 | #include <stdio.h>
|
11 | #include <avr/interrupt.h>
|
12 |
|
13 | #include "General.h"
|
14 | #include "RS232.h"
|
15 | #include "Delay.h"
|
16 | #include "TWI_Master.h"
|
17 | /*
|
18 | ** This main programm demonstrates how to use the
|
19 | ** implemented TWI master functions. These are:
|
20 | ** TWIM_Init
|
21 | ** TWIM_ReadAck
|
22 | ** TWIM_ReadNack
|
23 | ** TWIM_Write
|
24 | ** TWIM_Stop
|
25 | **
|
26 | ** For testing this program, use the program
|
27 | ** TWI_Slave_main.c in the slave uC and connect the
|
28 | ** two TWI lines properly (don't forget to also
|
29 | ** connect GND between Master and Slave!)
|
30 | **
|
31 | ** Used uC for Master is ATMega32
|
32 | */
|
33 | int main (void)
|
34 | {
|
35 | uint8_t i;
|
36 | uint8_t j=0;
|
37 | uint8_t Data[8];
|
38 | uint8_t SlaveAddress = 15;
|
39 | /*
|
40 | ** Clear any interrupt
|
41 | */
|
42 | cli ();
|
43 | /*
|
44 | ** Wait 1 second for POR
|
45 | */
|
46 | Delay_ms (1000);
|
47 | /*
|
48 | ** Initiate RS232
|
49 | */
|
50 | RS232_Init ();
|
51 | printf ("Hello world...\n");
|
52 | /*
|
53 | ** Initiate TWI Master Interface with bitrate of 100000 Hz
|
54 | */
|
55 | if (!TWIM_Init (100000))
|
56 | {
|
57 | printf ("Error in initiating TWI interface\n");
|
58 | while (1);
|
59 | }
|
60 | /*
|
61 | ** Endless loop
|
62 | */
|
63 | while (1)
|
64 | {
|
65 | /*
|
66 | ** Read byte(s) from the slave.
|
67 | ** It is implicitely assumed, that the slave will send
|
68 | ** 8 bytes
|
69 | */
|
70 | if (!TWIM_Start (SlaveAddress, TWIM_READ))
|
71 | {
|
72 | TWIM_Stop ();
|
73 | printf ("Could not start TWI Bus for READ\n");
|
74 | }
|
75 | else
|
76 | {
|
77 | for (i=0;i<7;i++)
|
78 | {
|
79 | Data[i] = TWIM_ReadAck ();
|
80 | printf ("Reading Byte %d: %d\n", i, Data[i]);
|
81 | }
|
82 | Data[7] = TWIM_ReadNack ();
|
83 | printf ("Reading Byte %d: %d\n", i, Data[7]);
|
84 | TWIM_Stop ();
|
85 | Delay_ms (1000);
|
86 | }
|
87 | /*
|
88 | ** Write byte(s) to the slave.
|
89 | ** It is implicitely assumed, that the slave will
|
90 | ** accepts 8 bytes
|
91 | */
|
92 | if (!TWIM_Start (SlaveAddress, TWIM_WRITE))
|
93 | {
|
94 | TWIM_Stop ();
|
95 | printf ("Could not start TWI Bus for WRITE\n");
|
96 | }
|
97 | else
|
98 | {
|
99 | for (i=0;i<8;i++)
|
100 | {
|
101 | TWIM_Write (j++);
|
102 | printf ("Byte %d sent: %d\n", i, j);
|
103 | }
|
104 | TWIM_Stop ();
|
105 | Delay_ms (1000);
|
106 | }
|
107 | /*
|
108 | ** Do something else
|
109 | */
|
110 | i++;
|
111 | }
|
112 |
|
113 | return 0;
|
114 | }
|