User Tools

Site Tools


attiny_avr-gcc

ATTiny44 Example

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
 
#define datapin 5
#define latchpin 0
#define enablepin 1
#define clockpin 4
 
int SB_CommandMode = 0;
int SB_RedCommand = 0;
int SB_GreenCommand = 0;
int SB_BlueCommand = 0;
 
#define NumLEDs 2
 
int LEDChannels[NumLEDs][3];
int newred = 0;
int newgreen = 0;
int newblue = 0;
int oldred = 0;
int oldgreen = 0;
int oldblue = 0;
 
 
void USI_ShiftOut(unsigned char SendByte) {
 
	USIDR = SendByte;
 
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USITC);
	USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
 
}
 
void SB_SendPacket() {
 
   if (SB_CommandMode == 1) {
    SB_RedCommand = 127;
    SB_GreenCommand = 115;
    SB_BlueCommand = 115;
   }
 
   USI_ShiftOut(SB_CommandMode << 6 | SB_BlueCommand>>4);
 
   USI_ShiftOut(SB_BlueCommand<<4 | SB_RedCommand>>6);
 
   USI_ShiftOut(SB_RedCommand << 2 | SB_GreenCommand>>8);
 
   USI_ShiftOut(SB_GreenCommand);
 
 
}
 
 
 
void WriteLEDArray() {
 
   SB_CommandMode = 0; // Write to PWM control registers
   for (int h = 0;h<NumLEDs;h++) {
       SB_RedCommand = LEDChannels[h][0];
       SB_GreenCommand = LEDChannels[h][1];
       SB_BlueCommand = LEDChannels[h][2];
       SB_SendPacket();
   }
 
		_delay_ms(.1);
		PORTA |= (1 << latchpin);
   		PORTA |= (1 << enablepin);
		_delay_ms(.1);
		PORTA &= ~(1 << enablepin);
		PORTA &= ~(1 << latchpin);
 
   SB_CommandMode = 1; // Write to current control registers
   for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
		_delay_ms(.1);
		PORTA |= (1 << latchpin);
   		_delay_ms(.1);
   		PORTA &= ~(1 << latchpin);
 
}
 
 
void fadeall(int rate, int steps, int fromred, int fromgreen, int fromblue, int tored, int togreen, int toblue) {
 
 for (int i = 0; i < steps; i++) {
 
   for (int j = 0; j < NumLEDs; j++) {
 
     LEDChannels[j][0] = ((fromred * (steps - i - 1) + tored * i)/(steps-1))*4;
     LEDChannels[j][1] = ((fromgreen * (steps - i - 1) + togreen * i)/(steps-1))*4;
     LEDChannels[j][2] = ((fromblue * (steps - i - 1) + toblue * i)/(steps-1))*4;
 
   }
 
  WriteLEDArray();
  _delay_ms(rate);
 
 } 
 
 
}
 
 
int main(void) {
 
	DDRA |= (1 << datapin)|(1 << latchpin)|(1 << enablepin)|(1 << clockpin);
 
	PORTA &= ~((1 << latchpin)|(1 << enablepin));
 
	while(1) {
 
		oldred = newred;
		oldgreen = newgreen;
		oldblue = newblue;
 
		newred = (rand()/2048)*16;
		newgreen = (rand()/2048)*16;
		newblue = (rand()/2048)*16;
 
   		fadeall(8, 65, 0, 0, 0, newred, newgreen, newblue);
		_delay_ms(500);
		fadeall(8, 65, newred, newgreen, newblue, 0, 0, 0);
		_delay_ms(500);
 
	}
 
}
/home/macetec/public_html/docs/data/pages/attiny_avr-gcc.txt · Last modified: 2010/08/28 20:33 by macegr