Use Triple-A shifting library instead.
This commit is contained in:
parent
6f11fafbef
commit
d0df80b533
@ -51,7 +51,7 @@ FORMAT = ihex
|
||||
TARGET = main
|
||||
|
||||
# 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.
|
||||
# Make them always end in a capital .S. Files ending in a lowercase .s
|
||||
|
@ -23,7 +23,7 @@
|
||||
*
|
||||
* To compile and upload run: make clean; make; make program;
|
||||
*
|
||||
* Copyright 2011 Mika Tuupola
|
||||
* Copyright 2011-2012 Mika Tuupola
|
||||
*
|
||||
* Licensed under the MIT license:
|
||||
* http://www.opensource.org/licenses/mit-license.php
|
||||
@ -36,42 +36,11 @@
|
||||
|
||||
#include "main.h"
|
||||
#include "uart/uart.h"
|
||||
#include "pins/digital.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);
|
||||
}
|
||||
}
|
||||
#include "shift/shift.h"
|
||||
|
||||
int main(void) {
|
||||
|
||||
init();
|
||||
shift_out_init();
|
||||
uart_init();
|
||||
stdout = &uart_output;
|
||||
stdin = &uart_input;
|
||||
@ -80,8 +49,7 @@ int main(void) {
|
||||
/* Show pattern for 5 seconds. */
|
||||
shift_out(0b10101010);
|
||||
shift_out(0b11110000);
|
||||
digital_write(LATCH, LOW);
|
||||
digital_write(LATCH, HIGH);
|
||||
shift_out_latch();
|
||||
_delay_ms(5000);
|
||||
|
||||
while (1) {
|
||||
@ -96,9 +64,7 @@ int main(void) {
|
||||
shift_out(i & 0xff);
|
||||
|
||||
/* Pulse latch to transfer data from shift registers */
|
||||
/* to storage registers (should this be inside shift_out()?). */
|
||||
digital_write(LATCH, LOW);
|
||||
digital_write(LATCH, HIGH);
|
||||
shift_out_latch();
|
||||
|
||||
_delay_ms(50);
|
||||
}
|
||||
|
@ -1,3 +1 @@
|
||||
static void init(void);
|
||||
void shift_out(uint8_t data);
|
||||
int main(void);
|
||||
|
68
tpic6b595_shiftout/shift/shift.c
Normal file
68
tpic6b595_shiftout/shift/shift.c
Normal 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);
|
||||
}
|
30
tpic6b595_shiftout/shift/shift.h
Normal file
30
tpic6b595_shiftout/shift/shift.h
Normal 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 */
|
||||
|
Loading…
Reference in New Issue
Block a user