-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
94 lines (86 loc) · 2.92 KB
/
main.c
File metadata and controls
94 lines (86 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Sorry for my poor English (LOL)
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#define SCL 100000UL
#define LCD_ADDRESS 0x20
#define RFID_ADDRESS 0x48
#define BUTTON_UP 0x08
#define BUTTON_DOWN 0x04
#define BUTTON_LEFT 0x10
#define BUTTON_RIGHT 0x02
#define BUTTON_SELECT 0x01
// up : mask to check which button was pushed :)
ISR(TWI_vect) { //CPU jumps here when TWINT = 0
uint8_t status = TWSR & (~0x7);
if (status != 0x08 || status != 0x18 || status != 0x28 || status != 0x30) {
/*
* Normally check if there is no error.
* Normally doing a sort of routine but I did not decide what to do yet.
* So I'm screwed xD
*/
}
TWCR |= (1 << TWINT); // put the interrupt flag off (not always clean by hardware)
//also I think TWDR (so data register) is writable only when TWINT = 0, not sure xD
}
void twi_init(void) {
DDRC &= ~((1 << PC5) | (1 << PC4)); //force it to be an input
PORTC |= (1 << PC5) | (1 << PC4); //internal pull-up activated
TWBR = ((F_CPU / SCL) - 16) / 2; //clock frequency
TWCR |= (1 << TWIE) | (1 << TWEN) | (1 << TWEA) | (1 << TWINT);
/*
* Enable TWI, enable TWI interrupt and set to 1 (so unused)
* Enable ACK control when in master receiver mode,
* but disable it when the last byte is received, master have to put a NACK bit
* but I don't know how to do it (so I'm screwed, for the second time LOL.)
*/
sei();
//set global interrupt enable in SREG register
}
void twi_start(void) {
TWCR |= (1 << TWSTA); // put a start condition on the line
while ((TWCR & (1 << TWINT)) == 0)
//wait to see if start condition is sent correctly
// then it check in the interrupt function the status (TWSR)
;
TWCR &= ~(1 << TWSTA); // Have to do it or start condition will be sent again
}
void twi_write(uint8_t slave_address, uint8_t data) { // write = 0, read = 1
TWDR = (slave_address << 1) & 0xFE; //put slave address and w/r bit next to it :)
while ((TWCR & (1 << TWINT)) == 0)
// wait until the flag is equal to 0 (so it is on for the TWI)
;
TWDR = data; //put data to transmit into buffer
while ((TWCR & (1 << TWINT)) == 0)
//wait until the flag is == 0
;
}
void twi_read(uint8_t slave_address) { //didn't finish this
TWDR = (slave_address << 1) | 1; /* have to scroll it left 'cause
it's a seven bit address and the bit 0 is for R/W order
*/
while ((TWCR & (1 << TWINT)) == 0)
// wait for status again and blablabla
;
}
void twi_stop(void) { //stops communication but not I2C function on the two pins
TWCR |= (1 << TWSTO);
while ((TWCR & (1 << TWINT)) == 0)
;
TWCR &= ~(1 << TWSTO); //same reason as the start condition
}
int main(void) {
/*
* To use the LCD, you have to ask him (address) something,
* so a command and then you can read or write to thing
* you wanted to change
*/
twi_init();
twi_start();
char salut[] = { "Hello" };
for (int8_t i = 0; i < 6; i++) {
twi_write(LCD_ADDRESS, salut[i]); //doesn't work :'(
}
for (;;) { //infinite loop :D
}
}