Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
...trying to figure out why the variable 'andy' on line 79 is sent as 10111010 even though the shift register has set 10111011.. is this a problem with an extra bit shifting the register over? ...is this a problem with MSB/LSB? </lost>
#include <avr/io.h> //Include the headers that handles Input/Output function for AVR chips
#include "spi_shift_register.h"
#include <util/delay.h> //Include the headers that allow for a delay function
#include <avr/interrupt.h>
#include <inttypes.h>
#include <util/setbaud.h>
//#include <stdlib.h>
//#include <string.h>
#include <stdio.h>
void setup(void)
{
DDRB |= _BV(PB0); // set LED pin as output
PORTB |= _BV(PB0); // turn the LED on
// USI stuff
DDRB |= _BV(PB4); // as output (latch)
DDRB |= _BV(PB6); // as output (DO)
DDRB |= _BV(PB7); // as output (USISCK)
DDRB |= _BV(PB3); // !!! as output enable (OE on 5891) !!! NEW !!! TODO: NOT CURRENTLY CONNECTED
DDRB |= _BV(PB2); // as parallel load (PL on 165)
DDRB &= ~_BV(PB5); // as input (DI)
PORTB |= _BV(PB5); // pullup on (DI)
// UART stuff
DDRD |= 1<<PD0; /* set PD0 to output */
inituart();
// Write 0x00 to 595
transmitbyte(0x00);
}
void loop(void)
{
uint8_t receivedByte = receivebyte();
// TODO: Get 8 bits from 165
uint8_t new_bits_from_165 = receivedByte;
//int bits_to_change = old_bits_from_165 ^ new_bits_from_165; /* Which switches have been switched on the 165? */
uint8_t ending_point = 0b01011001;
uint8_t bits_to_change = old_bits_from_165 ^ ending_point;
uint8_t on_bits = ending_point & bits_to_change;
uint8_t off_bits = old_bits_from_165 & bits_to_change;
// TODO: TRANSMIT any changes from the 165 as MIDI note on/off...
//
// transmit_variable_serially("old_bits_from_165",old_bits_from_165);
// char str[16];
// sprintf(str, "%i", (int)new_bits_from_165);
//transmit_variable_serially("new_bits_from_165",new_bits_from_165);
transmit_variable_serially("ending_point",ending_point);
transmit_variable_serially("bits_to_change",bits_to_change);
transmit_variable_serially("on_bits",on_bits);
transmit_variable_serially("off_bits", off_bits);
//transmit_variable_serially("andy",~receivedByte);
//transmitbyte(receivedByte);
/*for(int i = 0; i < sizeof(on_bits); i++)
{
char a;
a = ((on_bits >> i) & 1) == 0 ? 48 : 49;
transmitbyte(a);
//transmitbyte(on_bits >> i & 1 == 0 ? "1" : "0");
}*/
// TODO: Respond with appropriate settings for 595 to update
//
// TODO: Write 8 bits for 595
// on_bits
// TODO: Output Enable for /pulse/ duration on the 595's
//
uint8_t andy;
__LATCH_LOW;
__PL_HIGH;
andy = spi_transfer(0x01); // channel 1 active (red)
//andy = USIDR;
transmit_variable_serially("BHAT", andy);
__LATCH_HIGH;
__PL_LOW;
_delay_ms(500);
PORTB ^= _BV(PB0); // toggle LED
__LATCH_LOW;
spi_transfer(0x02); // channel 2 active (green)
__LATCH_HIGH;
_delay_ms(500);
PORTB ^= _BV(PB0); // toggle LED
__LATCH_LOW;
spi_transfer(0x04); // channel 3 active (blue)
__LATCH_HIGH;
_delay_ms(500);
PORTB ^= _BV(PB0); // toggle LED
__LATCH_LOW;
spi_transfer(0x07); // channels 1,2,3 active (white)
__LATCH_HIGH;
_delay_ms(500);
PORTB ^= _BV(PB0); // toggle LED
__LATCH_LOW;
spi_transfer(0x00); // all outputs off
__LATCH_HIGH;
_delay_ms(500);
PORTB ^= _BV(PB0); // toggle LED
}
int main(void)
{
setup();
for(;;) {
loop();
}
};
/*
* Functions dealing with hardware specific jobs / settings
*/
uint8_t spi_transfer(uint8_t data) {
USIDR = data;
USISR = _BV(USIOIF); // clear flag
while((USISR & _BV(USIOIF)) == 0) {
USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
}
return USIDR;
}
void inituart(void)
{
UBRRH = UBRRH_VALUE;
UBRRL = UBRRL_VALUE;
UCSRB = (1 << RXEN) | (1 << TXEN); // Enable UART receiver and transmitter
UCSRC = (1 << UCSZ1) | (1 << UCSZ0); // set to 8 data bits, 1 stop bit
}
void transmitbyte (unsigned char data)
{
while (!(UCSRA & (1 << UDRE))); // Wait for empty transmit buffer
UDR = data; // Start transmittion
}
void transmitbytes (char data[])
{
int size = strlen(data);//sizeof(data)/sizeof(data[0]);
for(int i = 0; i < size; i++)
{
transmitbyte(data[i]);
}
}
const char *byte_to_binary(int x)
{
static char b[9];
b[0] = '\0';
int z;
for (z = 128; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
void transmitbytes_clearscreen(void)
{
transmitbyte(27); // ESC command
transmitbytes("[2J"); // clear screen command
transmitbyte(27);
transmitbytes("[H"); // cursor to home command*/
}
void transmitbyte_newline(void)
{
transmitbytes("\n\r");
}
void transmit_variable_serially(char variable_name[], uint8_t variable)
{
transmitbytes(variable_name);
transmitbyte(27);
transmitbyte("[0;30f");
transmitbytes("\t= ");
transmitbytes(byte_to_binary(variable));
transmitbyte_newline();
}
uint8_t receivebyte(void)
{
/* Wait for empty transmit buffer */
//while(!(UCSRA & (1 << RXC)));
/* Return the data */
return UDR;
}
/*
* spi_shift_register.h
*
* Created on: Aug 31, 2016
* Author: advorak
*/
#ifndef SPI_SHIFT_REGISTER_H_
#define SPI_SHIFT_REGISTER_H_
#define F_CPU 16000000UL //Define the speed the clock is running at. Used for the delay.h functions
#define BAUD 38400//19200
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1) //The formula that does all the required maths
#define __LATCH_LOW PORTB &= ~(1<< PB4)
#define __LATCH_HIGH PORTB |= (1<< PB4)
#define __PL_LOW PORTB &= ~(1<< PB2)
#define __PL_HIGH PORTB |= (1<< PB2)
// Function prototypes
void setup(void);
void loop(void);
uint8_t spi_transfer(uint8_t data);
void inituart(void);
void transmitbyte (unsigned char data);
void transmitbytes (char data[]);
const char *byte_to_binary(int x);
void transmitbytes_clearscreen(void);
void transmitbyte_newline(void);
void transmit_variable_serially(char variable_name[], uint8_t variable);
uint8_t receivebyte(void);
int main(void);
// Variables
uint8_t old_bits_from_165 = 0x00;
#endif /* SPI_SHIFT_REGISTER_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.