LTlib LaurTec Library  4.0.0 Beta
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 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 
69 
70  //Enable Pull-up resistor on PORTB
72 
73  //I2C Initialization
74  //Baudrate 400kHz @20MHz
75  I2C_Module_baud_rate (400);
77 
78 
79  //Check for any pressed button
80  while (1) {
81 
82  switch (PORTB & BUTTON_MASK) {
83 
85  break;
86 
88  break;
89 
91  break;
92 
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
109 
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
117  data = I2C_Module_read_byte ();
119  }
120 
121  // Invio del secondo Byte (data)
122  while(I2C_Module_write_byte (data));
123 
125 
127  }