Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Delete bitsToArray() from Mega2560.c/.h
Add 74HC595 pinout in shift_register.c
  • Loading branch information
mur4ik18 committed Jul 28, 2023
1 parent 8046130 commit 14cf7b7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
4 changes: 3 additions & 1 deletion examples/blink.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
void setup() {
pinMode(13, OUTPUT);
USART_Init(MYUBRR);
USART_Transmit("Started");
print("Started");
}

int main(void) {
setup();
while(1) {
turn(LED, ON);
print("LED On\n");

_delay_ms(1000);
turn(LED, OFF);

print("LED OFF\n");
_delay_ms(1000);
}
Expand Down
12 changes: 12 additions & 0 deletions examples/shift_register.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#include "../lib/Mega2560.h"
#include <util/delay.h>

// ------- 74HC595 Pinout -------
// Q0 = 15
// Q1-Q7 = 1-7
// GND = 8
// Vcc = 16 (5V)
// Q0 = 15
// Ds = 14 (dataPin)
// OE = 13
// STcp = 12 (latchPin)
// SHcp = 11 (clockPin)
// MR = 10
// ------------------------------
const uint8_t dataPin = 2;
const uint8_t latchPin = 3;
const uint8_t clockPin = 4;
Expand Down
21 changes: 0 additions & 21 deletions lib/Mega2560.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@ void turn(uint8_t port, bool onoff) {
}
}

void bitsToArray(uint8_t num, uint8_t *bitters){
unsigned int size = sizeof(unsigned char);
unsigned int maxPow = 1<<(size*8-1);
int bittsArray[7] = {0,0,0,0,0,0,0};
int i=0,j;
for(;i<size;++i){
for(j=0;j<8;++j){
for(;i<size*8;++i){
bittsArray[i] = num&maxPow ? 1 : 0;
num = num<<1;
}
}
}
int k = 0;
for (int i=7; i>=0;i--) {
bitters[k++] = bittsArray[i];
}
}

void pinMode(uint8_t num, bool mode) {
uint8_t port = digital_to_port(num);
uint8_t bit = getBit(num);
Expand All @@ -48,13 +29,11 @@ void pinMode(uint8_t num, bool mode) {

if (mode == INPUT) {
uint8_t saved_SREG = SREG;
// cli();
*reg &= ~bit;
*pin &= ~bit;
SREG = saved_SREG;
} else {
uint8_t saved_SREG = SREG;
// cli();
*reg |= bit;
SREG = saved_SREG;
}
Expand Down
1 change: 0 additions & 1 deletion lib/Mega2560.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ static const uint8_t PROGMEM digital_ports[] = {
#define getPort(x) ((volatile uint8_t *) pgm_read_byte( portx + x ))

void turn(uint8_t port, bool onoff);
void bitsToArray(uint8_t num, uint8_t *bitters);
void pinMode(uint8_t num, bool mode);

#endif /* MEGA_2560_H */
2 changes: 1 addition & 1 deletion lib/lib_74HC595.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

void shiftOut(int8_t dataPin, int8_t clockPin, uint8_t data) {
for(int8_t i = 0; i < 8; i++) {
if ((data & 128) != 0 ) {
if ((data & 0b10000000) != 0 ) {
turn(dataPin, ON);
} else {
turn(dataPin, OFF);
Expand Down

0 comments on commit 14cf7b7

Please sign in to comment.