User Tools

Site Tools


octobrite_on_attiny84_with_hw_spi

This code is a basic example of using hardware SPI on the ATTiny24/44/84. The inline coding of software-triggered clock pulses allows the bit rate to approach F/2 (F = AVR clock frequency), or 4MHz with an internal 8MHz clock.

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
 
#define datapin 5
#define latchpin 0
#define enablepin 1
#define clockpin 4
 
#define NumLEDs 8
int LEDChannels[NumLEDs][3];
 
void USI_ShiftOut(int SendInt) {
 
	USIDR = SendInt >> 4;
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 11
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 10
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 9
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 8
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USIDR = SendInt;
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 7
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 6
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 5
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 4
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 3
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 2
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 1
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC); // 0
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
}
 
void Octo_SendPacket(int OB_RedCommand, int OB_GreenCommand, int OB_BlueCommand) {
 
   USI_ShiftOut(OB_BlueCommand);
 
   USI_ShiftOut(OB_GreenCommand);
 
   USI_ShiftOut(OB_RedCommand);
 
}
 
void WriteLEDArray() {
 
   for (int h = 0;h<NumLEDs;h++) {
       Octo_SendPacket(LEDChannels[7-h][0], LEDChannels[7-h][1], LEDChannels[7-h][2]);
   }
 
		//_delay_ms(0.1);
		PORTA |= (1 << latchpin);
   		PORTA |= (1 << enablepin);
		//_delay_ms(0.1);
		PORTA &= ~(1 << enablepin);
		PORTA &= ~(1 << latchpin);
 
}
 
void paintall (int red, int green, int blue) {
 
	for (int LEDindex = 0; LEDindex < NumLEDs; LEDindex++) {
		LEDChannels[LEDindex][0] = blue;
		LEDChannels[LEDindex][1] = green;
		LEDChannels[LEDindex][2] = red;
	}
 
	WriteLEDArray();
 
}
 
int main(void) {
 
	DDRA |= (1 << datapin)|(1 << latchpin)|(1 << enablepin)|(1 << clockpin);
 
	PORTA &= ~((1 << latchpin)|(1 << enablepin)|(1 << clockpin));
 
	while(1) {
 
		paintall(4095,0,0);
 
		__delay_ms(100);
 
		paintall(0,4095,0);
 
		__delay_ms(100);
 
		paintall(0,0,4095);
 
		__delay_ms(100);
 
	}
 
}
/home/macetec/public_html/docs/data/pages/octobrite_on_attiny84_with_hw_spi.txt · Last modified: 2010/10/22 07:04 by macegr