ATTiny2313 Arduino Libraries

From ProjectPages
Jump to: navigation, search

Introduction

In order to use an ATTiny as an Arduino you need to follow the guidelines here

Another source

Go here for ATTiny85

Source for the Arduino Tiny cores. With the latest Arduino platform, 1.6.12 at time of writing, you need to follow these instructions Programming ATtinys with Arduino 1.6.8

ATTiny2313 Serial Interface Library

/*
  USART2313.h - Arduino Library for the ATTiny2313 USART.
  Created by Anthony K. Norman, March 25, 2013.
  anthony.k.norman@gmail.com
  Released into the public domain.
*/

#ifndef USART2313_h
#define USART2313_h

#include "Arduino.h"

class USART2313
{
	public:
		USART2313();
		void begin();
		void write( unsigned char data );
		void printHexByte( unsigned char data );
		void printBinByte( unsigned char data );
		void print (char *string);
		void printLn (char *string);
};

extern USART2313 Serial;

#endif
/*
  USART2313.cpp - Arduino Library for the ATTiny2313 USART.
  Created by Anthony K. Norman, March 25, 2013.
  anthony.k.norman@gmail.com
  Released into the public domain.
*/

#include "USART2313.h"


//*****************************************************
//*****************************************************
//************* USART2313 ROUTINES ************************
//*****************************************************
//*****************************************************

USART2313::USART2313(){
}

void USART2313::begin()
{
	const unsigned int UBRR = (F_CPU / (16UL * 9600)) - 1;
	UBRRH = (unsigned char) (UBRR >> 8); 
	UBRRL = (unsigned char) UBRR; 
	UCSRB = (1 << TXEN);   //enable the serial 
	// Set frame format: 8data, 2stop bit
	UCSRC = (1<<USBS)|(3<<UCSZ0);
}


void USART2313::write( unsigned char data )
{
	// Wait for empty transmit buffer 
	while ( !( UCSRA & (1<<UDRE)) )
	;
	// Put data into buffer, sends the data 
	UDR = data;
}

void USART2313::printHexByte( unsigned char data ){
	char nibble = (data >> 4);
	nibble += 0x30;
	if (nibble>0x39) nibble += 7;
	write( nibble );

	nibble = (data & 0x0F);
	nibble += 0x30;
	if (nibble>0x39) nibble += 7;
	write( nibble );
}

void USART2313::printBinByte( unsigned char data ){
	for (byte i=7; i<255; i--){
		char nibble = (data >> i);
		nibble = nibble % 2;
		nibble += 0x30;
		write( nibble );
	}
}

void USART2313::print (char *string)
{
   int count = 0;
   while (string[count] != 0)
   {
		write(string[count]);
		count++;
   }
}

void USART2313::printLn (char *string)
{
	print(string);
	write(0x0D);
	write(0x0A);
}

ATTiny2313 SPI Library

/* SPI2313.h
 * Copyright (c) 2013 Anthony K. Norman <anthony.k.norman@gmail.com>
 * SPI2313 Master library for arduino ATTiny 2313.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of either the GNU General Public License version 2
 * or the GNU Lesser General Public License version 2.1, both as
 * published by the Free Software Foundation.
 */

#ifndef _SPI2313_H_INCLUDED
#define _SPI2313_H_INCLUDED

#include <Arduino.h>


class SPI2313Class {
public:
	static byte transfer(byte _data);
	static void begin(); // Default
	static void end();

};

extern SPI2313Class SPI;

#endif	// _SPI2313_H_INCLUDED

/*
 * Copyright (c) 2013 Anthony K. Norman <anthony.k.norman@gmail.com>
 * SPI2313 Master library for arduino ATTiny 2313.
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of either the GNU General Public License version 2
 * or the GNU Lesser General Public License version 2.1, both as
 * published by the Free Software Foundation.
 */

#include "SPI2313.h"

SPI2313Class SPI2313;

byte SPI2313Class::transfer(byte _data) {
	// sends _data via by hardware USI port and returns received data
	uint8_t value = 0;

	USIDR = _data;
	USISR = (1<<USIOIF);     // Set Counter Overflow Interrupt Flag
	do
	{
		USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
	} while((USISR & _BV(USIOIF)) == 0);
	return USIDR;
}


void SPI2313Class::begin() {
	// setup 2313 SPI pins
	pinMode(SCK, OUTPUT);
	pinMode(MISO, OUTPUT);	// Be careful - when the ATTiny2313 is master then this is effectively MOSI also labelled D0
}

void SPI2313Class::end() {
}