LTlib LaurTec Library
4.0.0 Beta
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
2
#include <xc.h>
3
4
#include "
LTlib.h
"
5
6
#include "
module_IO.h
"
7
#include "
module_IO.c
"
8
9
#include "
module_I2C.h
"
10
#include "
module_I2C.c
"
11
12
#define SLAVE_ADDRESS 0xA6
13
#define READ 0x01
14
#define WRITE 0x00
15
16
#define BUTTON_MASK 0xF0
17
#define BUTTON_1 0x01
18
#define BUTTON_2 0x02
19
#define BUTTON_3 0x04
20
#define BUTTON_4 0x08
21
22
#define BUTTON_1_PRESSED 0b11100000
23
#define BUTTON_2_PRESSED 0b11010000
24
#define BUTTON_3_PRESSED 0b10110000
25
#define BUTTON_4_PRESSED 0b01110000
26
27
28
//*************************************
29
// Module Switch
30
//*************************************
31
32
#define I2C_MODULE_1
33
//#define I2C_MODULE_2
34
35
#ifdef I2C_MODULE_1
36
#define I2C_Module_open I2C1_open
37
#define I2C_Module_baud_rate I2C1_baud_rate
38
#define I2C_Module_wait_bus_IDLE I2C1_wait_bus_IDLE
39
#define I2C_Module_start_bit I2C1_start_bit
40
#define I2C_Module_read_byte I2C1_read_byte
41
#define I2C_Module_reset_write_collision_flag I2C1_reset_write_collision_flag
42
#define I2C_Module_write_byte I2C1_write_byte
43
#define I2C_Module_stop_bit I2C1_stop_bit
44
#endif
45
46
#ifdef I2C_MODULE_2
47
#define I2C_Module_open I2C2_open
48
#define I2C_Module_baud_rate I2C2_baud_rate
49
#define I2C_Module_wait_bus_IDLE I2C2_wait_bus_IDLE
50
#define I2C_Module_start_bit I2C2_start_bit
51
#define I2C_Module_read_byte I2C2_read_byte
52
#define I2C_Module_reset_write_collision_flag I2C2_reset_write_collision_flag
53
#define I2C_Module_write_byte I2C2_write_byte
54
#define I2C_Module_stop_bit I2C2_stop_bit
55
#endif
56
57
//*************************************
58
// Prototipi delle funzioni
59
//*************************************
60
void
write_data
(
unsigned
char
data);
61
62
//*************************************
63
// I2C Master
64
//*************************************
65
66
int
main
(
void
) {
67
68
IO_set_all_ports_as_inputs
();
69
70
//Enable Pull-up resistor on PORTB
71
IO_enable_pull_up_resistors
(
IO_PORTB
,
IO_BIT4
+
IO_BIT5
+
IO_BIT6
+
IO_BIT7
);
72
73
//I2C Initialization
74
//Baudrate 400kHz @20MHz
75
I2C_Module_baud_rate
(400);
76
I2C_Module_open
(
I2C_MASTER
);
77
78
79
//Check for any pressed button
80
while
(1) {
81
82
switch
(PORTB &
BUTTON_MASK
) {
83
84
case
BUTTON_1_PRESSED
:
write_data
(
BUTTON_1
);
85
break
;
86
87
case
BUTTON_2_PRESSED
:
write_data
(
BUTTON_2
);
88
break
;
89
90
case
BUTTON_3_PRESSED
:
write_data
(
BUTTON_3
);
91
break
;
92
93
case
BUTTON_4_PRESSED
:
write_data
(
BUTTON_4
);
94
break
;
95
}
96
}
97
98
}
99
100
//*************************************
101
// write_data implementation
102
//*************************************
103
void
write_data
(
unsigned
char
data){
104
105
unsigned
char
dummy_read;
106
107
//check if the bus is Idle, before continuing
108
I2C_Module_wait_bus_IDLE
;
109
110
I2C_Module_start_bit
();
111
112
//Lettura fittizia per cancellare il buffer in ingresso
113
dummy_read =
I2C_Module_read_byte
();
114
115
// Invio del primo Byte (indirizzo) - Scrittura
116
while
(
I2C_Module_write_byte
(
SLAVE_ADDRESS
|
WRITE
)) {
117
data =
I2C_Module_read_byte
();
118
I2C_Module_reset_write_collision_flag
();
119
}
120
121
// Invio del secondo Byte (data)
122
while
(
I2C_Module_write_byte
(data));
123
124
I2C_Module_wait_bus_IDLE
();
125
126
I2C_Module_stop_bit
();
127
}
LTlib_v_4.0.0
ex
PIC18
_module_I2C
01_module_I2C_Master.X
main.c
Generated on Sun Feb 21 2016 13:52:58 for LTlib LaurTec Library by
1.8.3.1