1
0
mirror of synced 2024-11-23 09:44:25 +01:00

Use Triple-A shifting library instead.

This commit is contained in:
Mika Tuupola 2012-01-15 20:04:13 +02:00
parent 6f11fafbef
commit d0df80b533
5 changed files with 104 additions and 42 deletions

View File

@ -51,7 +51,7 @@ FORMAT = ihex
TARGET = main TARGET = main
# List C source files here. (C dependencies are automatically generated.) # List C source files here. (C dependencies are automatically generated.)
SRC = $(TARGET).c uart/uart_async.c SRC = $(TARGET).c uart/uart_async.c shift/shift.c
# List Assembler source files here. # List Assembler source files here.
# Make them always end in a capital .S. Files ending in a lowercase .s # Make them always end in a capital .S. Files ending in a lowercase .s

View File

@ -23,7 +23,7 @@
* *
* To compile and upload run: make clean; make; make program; * To compile and upload run: make clean; make; make program;
* *
* Copyright 2011 Mika Tuupola * Copyright 2011-2012 Mika Tuupola
* *
* Licensed under the MIT license: * Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php * http://www.opensource.org/licenses/mit-license.php
@ -36,42 +36,11 @@
#include "main.h" #include "main.h"
#include "uart/uart.h" #include "uart/uart.h"
#include "pins/digital.h" #include "shift/shift.h"
//#define LATCH 10 /* RCK */
//#define DATA 11 /* SER IN */
//#define CLOCK 13 /* SRCK */
#define LATCH B0 /* RCK */
#define DATA B2 /* SER IN */
#define CLOCK B1 /* SRCK */
static void init(void) {
pin_mode(LATCH, OUTPUT);
pin_mode(CLOCK, OUTPUT);
pin_mode(DATA, OUTPUT);
}
/* Assumes MSB first. */
void shift_out(uint8_t data) {
for(uint8_t i = 0; i < 8; i++) {
/* Write bit to data port. */
if (0 == (data & _BV(7 - i))) {
digital_write(DATA, LOW);
} else {
digital_write(DATA, HIGH);
}
/* Pulse clock input to write next bit. */
digital_write(CLOCK, LOW);
digital_write(CLOCK, HIGH);
}
}
int main(void) { int main(void) {
init(); shift_out_init();
uart_init(); uart_init();
stdout = &uart_output; stdout = &uart_output;
stdin = &uart_input; stdin = &uart_input;
@ -80,8 +49,7 @@ int main(void) {
/* Show pattern for 5 seconds. */ /* Show pattern for 5 seconds. */
shift_out(0b10101010); shift_out(0b10101010);
shift_out(0b11110000); shift_out(0b11110000);
digital_write(LATCH, LOW); shift_out_latch();
digital_write(LATCH, HIGH);
_delay_ms(5000); _delay_ms(5000);
while (1) { while (1) {
@ -96,9 +64,7 @@ int main(void) {
shift_out(i & 0xff); shift_out(i & 0xff);
/* Pulse latch to transfer data from shift registers */ /* Pulse latch to transfer data from shift registers */
/* to storage registers (should this be inside shift_out()?). */ shift_out_latch();
digital_write(LATCH, LOW);
digital_write(LATCH, HIGH);
_delay_ms(50); _delay_ms(50);
} }

View File

@ -1,3 +1 @@
static void init(void);
void shift_out(uint8_t data);
int main(void); int main(void);

View File

@ -0,0 +1,68 @@
/*
* shift.c
*
* Copyright 2011-2012 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
#include "pins/digital.h"
#include "shift/shift.h"
/* Assumes MSB first. */
uint8_t shift_in(void) {
uint8_t byte = 0;
uint8_t pin_value;
for(int i=0; i<8; i++) {
pin_value = digital_read(SHIFT_IN_DATA);
byte |= (pin_value << ((8 - 1) - i));
/* printf("%d = %d \n", ((8 - 1) - i), pin_value); */
/* Pulse clock to write next bit. */
digital_write(SHIFT_IN_CLOCK, LOW);
digital_write(SHIFT_IN_CLOCK, HIGH);
}
return byte;
}
void shift_in_latch(void) {
digital_write(SHIFT_IN_LATCH, LOW);
digital_write(SHIFT_IN_LATCH, HIGH);
}
void shift_in_init(void) {
pin_mode(SHIFT_OUT_LATCH, OUTPUT);
pin_mode(SHIFT_OUT_CLOCK, OUTPUT);
pin_mode(SHIFT_OUT_DATA, INPUT);
}
/* Assumes MSB first. */
void shift_out(uint8_t data) {
for(uint8_t i = 0; i < 8; i++) {
/* Write bit to data port. */
if (0 == (data & _BV(7 - i))) {
digital_write(SHIFT_OUT_DATA, LOW);
} else {
digital_write(SHIFT_OUT_DATA, HIGH);
}
/* Pulse clock to write next bit. */
digital_write(SHIFT_OUT_CLOCK, LOW);
digital_write(SHIFT_OUT_CLOCK, HIGH);
}
}
void shift_out_latch(void) {
digital_write(SHIFT_OUT_LATCH, LOW);
digital_write(SHIFT_OUT_LATCH, HIGH);
}
void shift_out_init(void) {
pin_mode(SHIFT_OUT_LATCH, OUTPUT);
pin_mode(SHIFT_OUT_CLOCK, OUTPUT);
pin_mode(SHIFT_OUT_DATA, OUTPUT);
}

View File

@ -0,0 +1,30 @@
/*
* shift.h
*
* Copyright 2011-2012 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
*/
#ifndef SHIFT_H
#define SHIFT_H
/* Configure ports here. */
#define SHIFT_IN_LATCH B0
#define SHIFT_IN_DATA B2
#define SHIFT_IN_CLOCK B1
#define SHIFT_OUT_LATCH B0
#define SHIFT_OUT_DATA B3
#define SHIFT_OUT_CLOCK B1
uint8_t shift_in(void);
void shift_in_latch(void);
void shift_in_init(void);
void shift_out(uint8_t data);
void shift_out_latch(void);
void shift_out_init(void);
#endif /* SHIFT_H */