Dear readers,
I need help. I have a project. In this project i have the wish to read analog voltage with the attiny85, do a correction on the reading and then send an analog voltage out, with help of a dac mcp4921.
As i am pretty new to Arduino, i depend on the internet. I got a Nano and the mcp47xx working. That combi uses I2C. The Attiny85 and MCP4921 talk SPI. And thats where i got stuck.
I would really appreciate your help......Please do.
Here is the script i have sofar.
//
// +-\/-+
// RESET Ain0 (D 5) PB5 1| |8 Vcc
// CLK1 Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1 SCK / USCK / SCL
// CLK0 Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1 MISO / DO
// GND 4| |5 PB0 (D 0) pwm0 MOSI / DI / SDA
// +----+
namespace tinySPI
{
const byte DI = 0; // D0, pin 5 Data In
const byte DO = 1; // D1, pin 6 Data Out (this is *not* MOSI)
const byte USCK = 2; // D2, pin 7 Universal Serial Interface clock
const byte SS = 3; // D3, pin 2 Slave Select
void begin ()
{
digitalWrite (SS, HIGH); // ensure SS stays high until needed
pinMode (USCK, OUTPUT);
pinMode (DO, OUTPUT);
pinMode (SS, OUTPUT);
pinMode (DI, INPUT);
USICR = bit (USIWM0); // 3-wire mode
} // end of tinySPI_begin
// What is happening here is that the loop executes 16 times.
// This is because the 4-bit counter in USISR is initially zero, and then
// toggles 16 times until it overflows, thus counting out 8 bits (16 toggles).
// The data is valid on the clock leading edge (equivalent to CPHA == 0).
byte transfer (const byte b)
{
USIDR = b; // byte to output
USISR = bit (USIOIF); // clear Counter Overflow Interrupt Flag, set count to zero
do
{
USICR = bit (USIWM0) // 3-wire mode
| bit (USICS1) | bit (USICLK) // Software clock strobe
| bit (USITC); // Toggle Clock Port Pin
} while ((USISR & bit (USIOIF)) == 0); // until Counter Overflow Interrupt Flag set
return USIDR; // return read data
} // end of tinySPI_transfer
}; // end of namespace tinySPI
// en dan nu doen we iets ermee
void setup (void)
{
tinySPI::begin ();
} // end of setup
void loop (void)
{
int value = 0;
for (int i=0; i<4; i++) value = value + analogRead(A2);
// enable Slave Select
digitalWrite(tinySPI::SS, LOW);
tinySPI::transfer (value);
digitalWrite(tinySPI::SS, HIGH);
// delay (1);
} // end of loop