i2c_test.c


1
#include <mega128.h>
2
3
// Alphanumeric LCD Module functions
4
#asm
5
   .equ __lcd_port=0x1B
6
#endasm
7
#include <lcd.h>
8
9
// I2C Bus functions
10
#asm
11
   .equ __i2c_port=0x12
12
   .equ __sda_bit=1
13
   .equ __scl_bit=0
14
#endasm
15
#include <i2c.h>
16
17
// Standard Input/Output functions
18
#include <stdio.h>
19
20
// 2 Wire bus interrupt service routine
21
interrupt [TWI] void twi_isr(void)
22
{
23
// Place your code here
24
25
}
26
27
// Declare your global variables here
28
29
void main(void)
30
{ 
31
char in;
32
// Declare your local variables here
33
34
// Input/Output Ports initialization
35
// Port A initialization
36
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
37
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
38
PORTA=0x00;
39
DDRA=0x00;
40
41
// Port B initialization
42
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
43
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
44
PORTB=0x00;
45
DDRB=0x00;
46
47
// Port C initialization
48
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
49
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
50
PORTC=0x00;
51
DDRC=0x00;
52
53
// Port D initialization
54
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
55
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
56
PORTD=0x00;
57
DDRD=0x00;
58
59
// Port E initialization
60
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
61
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
62
PORTE=0x00;
63
DDRE=0x00;
64
65
// Port F initialization
66
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In
67
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T
68
PORTF=0x00;
69
DDRF=0x00;
70
71
// Port G initialization
72
// Func0=In Func1=In Func2=In Func3=In Func4=In
73
// State0=T State1=T State2=T State3=T State4=T
74
PORTG=0x00;
75
DDRG=0x00;
76
77
// Timer/Counter 0 initialization
78
// Clock source: System Clock
79
// Clock value: Timer 0 Stopped
80
// Mode: Normal top=FFh
81
// OC0 output: Disconnected
82
ASSR=0x00;
83
TCCR0=0x00;
84
TCNT0=0x00;
85
OCR0=0x00;
86
87
// Timer/Counter 1 initialization
88
// Clock source: System Clock
89
// Clock value: Timer 1 Stopped
90
// Mode: Normal top=FFFFh
91
// OC1A output: Discon.
92
// OC1B output: Discon.
93
// OC1C output: Discon.
94
// Noise Canceler: Off
95
// Input Capture on Falling Edge
96
TCCR1A=0x00;
97
TCCR1B=0x00;
98
TCNT1H=0x00;
99
TCNT1L=0x00;
100
OCR1AH=0x00;
101
OCR1AL=0x00;
102
OCR1BH=0x00;
103
OCR1BL=0x00;
104
OCR1CH=0x00;
105
OCR1CL=0x00;
106
107
// Timer/Counter 2 initialization
108
// Clock source: System Clock
109
// Clock value: Timer 2 Stopped
110
// Mode: Normal top=FFh
111
// OC2 output: Disconnected
112
TCCR2=0x00;
113
TCNT2=0x00;
114
OCR2=0x00;
115
116
// Timer/Counter 3 initialization
117
// Clock source: System Clock
118
// Clock value: Timer 3 Stopped
119
// Mode: Normal top=FFFFh
120
// OC3A output: Discon.
121
// OC3B output: Discon.
122
// OC3C output: Discon.
123
TCCR3A=0x00;
124
TCCR3B=0x00;
125
TCNT3H=0x00;
126
TCNT3L=0x00;
127
OCR3AH=0x00;
128
OCR3AL=0x00;
129
OCR3BH=0x00;
130
OCR3BL=0x00;
131
OCR3CH=0x00;
132
OCR3CL=0x00;
133
134
// External Interrupt(s) initialization
135
// INT0: Off
136
// INT1: Off
137
// INT2: Off
138
// INT3: Off
139
// INT4: Off
140
// INT5: Off
141
// INT6: Off
142
// INT7: Off
143
EICRA=0x00;
144
EICRB=0x00;
145
EIMSK=0x00;
146
147
// Timer(s)/Counter(s) Interrupt(s) initialization
148
TIMSK=0x00;
149
ETIMSK=0x00;
150
151
// USART0 initialization
152
// Communication Parameters: 8 Data, 1 Stop, No Parity
153
// USART0 Receiver: Off
154
// USART0 Transmitter: On
155
// USART0 Mode: Asynchronous
156
// USART0 Baud rate: 9600
157
UCSR0A=0x00;
158
UCSR0B=0x08;
159
UCSR0C=0x06;
160
UBRR0H=0x00;
161
UBRR0L=0x67;
162
163
// Analog Comparator initialization
164
// Analog Comparator: Off
165
// Analog Comparator Input Capture by Timer/Counter 1: Off
166
// Analog Comparator Output: Off
167
ACSR=0x80;
168
SFIOR=0x00;
169
170
// 2 Wire Bus initialization
171
// Generate Acknowledge Pulse: On
172
// 2 Wire Bus Slave Address: 01h
173
// General Call Recognition: Off
174
// Bit Rate: 400,000 kHz
175
TWBR=0x0C;
176
TWAR=0x02;
177
TWCR=0x45;
178
179
// LCD module initialization
180
lcd_init(16);
181
_lcd_ready();
182
lcd_clear();
183
184
i2c_init();
185
i2c_start();
186
i2c_write(0b10010001);
187
in = i2c_read(1);
188
lcd_putchar(in);
189
lcd_gotoxy(0,1);
190
in = i2c_read(0);
191
lcd_putchar(in);
192
193
while (1)
194
      {
195
      // Place your code here
196
197
      };
198
}