LTlib LaurTec Library
4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
Main Page
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
main.c
Go to the documentation of this file.
1
#include <xc.h>
2
3
#include "
LTlib.h
"
4
5
#include "
module_IO.h
"
6
#include "
module_IO.c
"
7
8
#include "
module_I2C.h
"
9
#include "
module_I2C.c
"
10
11
#define SLAVE_ADDRESS 0xA6
12
13
#define READ 0x01
14
#define WRITE 0x00
15
16
#define BYTE_TO_SEND 0x05
17
18
19
//*************************************
20
// Module Switch
21
//*************************************
22
23
#define I2C_MODULE_1
24
//#define I2C_MODULE_2
25
26
#ifdef I2C_MODULE_1
27
#define I2C_Module_open I2C1_open
28
#define I2C_Module_baud_rate I2C1_baud_rate
29
#define I2C_Module_wait_bus_IDLE I2C1_wait_bus_IDLE
30
#define I2C_Module_start_bit I2C1_start_bit
31
#define I2C_Module_read_byte I2C1_read_byte
32
#define I2C_Module_reset_write_collision_flag I2C1_reset_write_collision_flag
33
#define I2C_Module_write_byte I2C1_write_byte
34
#define I2C_Module_stop_bit I2C1_stop_bit
35
#define I2C_Module_set_slave_address I2C1_set_slave_address
36
#define I2C_Module_check_data_ready I2C1_check_data_ready
37
#define I2C_Module_check_read_write_operation I2C1_check_read_write_operation
38
#define I2C_Module_check_stop_bit I2C1_check_stop_bit
39
#define I2C_Module_check_start_bit I2C1_check_start_bit
40
#endif
41
42
#ifdef I2C_MODULE_2
43
#define I2C_Module_open I2C2_open
44
#define I2C_Module_baud_rate I2C2_baud_rate
45
#define I2C_Module_wait_bus_IDLE I2C2_wait_bus_IDLE
46
#define I2C_Module_start_bit I2C2_start_bit
47
#define I2C_Module_read_byte I2C2_read_byte
48
#define I2C_Module_reset_write_collision_flag I2C2_reset_write_collision_flag
49
#define I2C_Module_write_byte I2C2_write_byte
50
#define I2C_Module_stop_bit I2C2_stop_bit
51
#define I2C_Module_set_slave_address I2C2_set_slave_address
52
#define I2C_Module_check_data_ready I2C2_check_data_ready
53
#define I2C_Module_check_read_write_operation I2C2_check_read_write_operation
54
#define I2C_Module_check_stop_bit I2C2_check_stop_bit
55
#define I2C_Module_check_start_bit I2C2_check_start_bit
56
#endif
57
58
//*************************************
59
// Prototipi delle funzioni
60
//*************************************
61
void
board_initialization
(
void
);
62
63
//*************************************
64
// I2C Slave
65
//*************************************
66
int
main
(
void
) {
67
68
unsigned
char
address;
69
unsigned
char
data;
70
71
IO_set_all_ports_as_inputs
();
72
73
IO_set_port_direction
(
IO_PORTD
,
IO_ALL_PORT_OUTPUT
);
74
75
// I2C initialization
76
I2C_Module_set_slave_address
(
SLAVE_ADDRESS
);
77
I2C_Module_open
(
I2C_SLAVE_7_BIT_ADDRESS
);
78
79
while
(1) {
80
81
//Wait for the first byte (Address)
82
while
(
I2C_Module_check_data_ready
()==0);
83
address =
I2C_Module_read_byte
();
84
85
//Check Read or Write operation
86
if
(
I2C_Module_check_read_write_operation
() ==
WRITE
){
87
88
//Wait for the second byte (Data)
89
while
(
I2C_Module_check_data_ready
()==0);
90
data =
I2C_Module_read_byte
();
91
92
//Print the data out
93
IO_write_port
(
IO_PORTD
, data);
94
95
}
96
97
//wait for the stop bit
98
while
(
I2C_Module_check_start_bit
() == 0);
99
100
}
101
}
LTlib_v_4.0.3
ex
PIC18
_module_I2C
01_module_I2C_Slave.X
main.c
Generated on Wed Aug 3 2016 19:57:31 for LTlib LaurTec Library by
1.8.3.1