-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
171 lines (151 loc) · 4.14 KB
/
main.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//#include <SendOnlySoftwareSerial.h>
//
//SendOnlySoftwareSerial uart(3);
#define SYS_CLK 1000.0 // [kHz]
#define T2 (((SYS_CLK *4700) /1000000) +1) /4 // >4,7us
#define T4 (((SYS_CLK *4000) /1000000) +1) /4 // >4,0us
// PCF8574A
#define TWIADDR 0x38
#define PP0 0b11111110
#define PP1 0b11111101
#define PP2 0b11111011
#define PP3 0b11110111
#define PP4 0b11101111
#define PP5 0b11011111
#define PP6 0b10111111
#define PP7 0b01111111
// MCU Pin
#define SDA PB0
#define SCL PB2
#define DDR DDRB
#define PORT PORTB
#define InterruptPIN PCINT1
#define LEDTEST PB4
// Define Modes
#define SEND 0
#define GET 1
void Start(void){
//Starting Communication
PORT |= (1<<SCL);
while(!(PINB & (1<<SCL)));
_delay_us(T2);
PORT &= ~(1<<SDA);
_delay_us(T4);
PORT &= ~(1<<SCL);
PORT |= (1<<SDA);
}
void Stop(void){
//End of Communication
PORT &= ~(1<<SDA);
PORT |= (1<<SCL);
while(!(PINB & (1<<SCL))); // Wait for SCL to go high.
_delay_us(T4);
PORT |= (1<<SDA);
_delay_us(T2);
}
//void Transmit(uint8_t mode) {
void Transmit(void) {
USISR |= (1<<USIOIF);
while (!(USISR & (1<<USIOIF))) {
_delay_us(T2);
USICR |= (1<<USITC);
while(!(PINB & (1<<SCL)));
_delay_us(T4);
USICR |= (1<<USITC);
}
USISR |= (1<<USIOIF);
_delay_us(T2);
// USIDR = 0xFF;
// if(!mode)
DDR |= (1<<SDA);
}
unsigned char sendGetData(unsigned char data, unsigned char mode){
Start();
// Send SLA-ADDR and Write Bit
PORT &= ~(1<<SCL);
if(mode)USIDR = (TWIADDR << 1 | mode); //read
else USIDR = (TWIADDR << 1); //write
USISR |= (1<<USISIF)|(1<<USIOIF)|(1<<USIPF);
Transmit();
//Reply ACK first, then follow by send
DDR &= ~(1<<SDA); // SDA in received ACK
USIDR = 0x00; //ACK ContinueSession
USISR |= (1<<USICNT3) | (1<<USICNT2) | (1<<USICNT1);
Transmit();
// Engage Send / Receive
if(mode == SEND) {
//Send Byte Data
PORT &= ~(1<<SCL);
USIDR = data;
USISR |= (1<<USISIF)|(1<<USIOIF)|(1<<USIPF); //8bit
Transmit();
} else if(mode == GET) {
//Receive Byte Data
DDR &= ~(1 << SDA); // Enable SDA as input
USISR |= (1<<USISIF)|(1<<USIOIF)|(1<<USIPF); //8bit
Transmit();
data = USIDR;
}
// Send NACK=FF End
DDR &= ~(1<<SDA);
USIDR = 0xFF; // NACK EndSession
USISR |= (1<<USICNT3) | (1<<USICNT2) | (1<<USICNT1);//1bit
Transmit();
// Stop Send / Get Data Register
Stop();
return data;
}
uint8_t DDRPP=0x0F,count;
int main(void){
unsigned char DDRPPRes=0;
//unsigned char c=0;
//OSCCAL -= 10;
//uart.begin(9600);
//USI TWI/I2C Init and enable Pin Change Interrupt init
PORT |= (1<<SDA) | (1<<SCL);
DDR |= (1<<SDA) | (1<<SCL);
USICR = (1<<USIWM1) | (1<<USICS1) | (1<<USICLK);
PCMSK |= 1<<InterruptPIN;
DDR |= 1<<LEDTEST;
sendGetData(DDRPP,SEND);
while (1) {
if (GIFR & (1<<PCIF)) {
DDRPP = sendGetData(0,GET); //get col val
GIFR |= 1<<PCIF;
}
if(DDRPP > 0 && DDRPP < 0x0F){
DDRPPRes = DDRPP; //store col val
DDRPP = 0xF0; sendGetData(DDRPP,SEND); //change to row
DDRPP = sendGetData(0,GET); //get row val
if(DDRPP > 0x0F && DDRPP < 0xF0) DDRPPRes |= DDRPP; //store col+row val
/* do something here */
/* switch (DDRPPRes){
case 119: c=1;break;
case 123: c=2;break;
case 125: c=3;break;
case 183: c=4;break;
case 187: c=5;break;
case 189: c=6;break;
case 215: c=7;break;
case 219: c=8;break;
case 221: c=9;break;
case 235: c=0;break;
case 231: c='*';break;
case 237: c='#';break;
case 126: c='A';break;
case 190: c='B';break;
case 222: c='C';break;
case 238: c='D';break;
default: break;
}
*/
//debounce
PORT ^= 1<<PB4;
DDRPP = 0x0F; sendGetData(DDRPP,SEND); //back
_delay_ms(200);
}
}
}