-
Notifications
You must be signed in to change notification settings - Fork 19
/
l74hc165.c
52 lines (45 loc) · 1.24 KB
/
l74hc165.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
/*
l74hc165 lib 0x01
copyright (c) Davide Gironi, 2011
Released under GPLv3.
Please refer to LICENSE file for licensing information.
*/
#include <stdio.h>
#include <avr/io.h>
#include <string.h>
#include <util/delay.h>
#include "l74hc165.h"
/*
* init the shift register
*/
void l74hc165_init(void) {
//output
L74HC165_DDR |= (1 << L74HC165_CLOCKPIN);
L74HC165_DDR |= (1 << L74HC165_LOADPIN);
//input
L74HC165_DDR &= ~(1 << L74HC165_DATAPIN);
//low
L74HC165_PORT &= ~(1 << L74HC165_CLOCKPIN);
L74HC165_PORT &= ~(1 << L74HC165_LOADPIN);
}
/*
* shift in data
*/
void l74hc165_shiftin(uint8_t *bytearray) {
//parallel load to freeze the state of the data lines
L74HC165_PORT &= ~(1 << L74HC165_LOADPIN);
_delay_us(5);
L74HC165_PORT |= (1 << L74HC165_LOADPIN);
for(uint8_t i = 0; i < L74HC165_ICNUMBER; i++){
//iterate through the bits in each registers
uint8_t currentbyte = 0;
for(uint8_t j = 0; j < 8; j++){
currentbyte |= ((L74HC165_PIN & (1 << L74HC165_DATAPIN))>>L74HC165_DATAPIN)<<(7-j);
//get next
L74HC165_PORT |= (1 << L74HC165_CLOCKPIN);
_delay_us(5);
L74HC165_PORT &= ~(1 << L74HC165_CLOCKPIN);
}
memcpy(&bytearray[i], ¤tbyte, 1);
}
}