attiny85 with mcp4921 dac challange

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

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Why do you want to go to ATtiny?

Tom... :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Why do you want to go to ATtiny?

Tom... :slight_smile:

Thanks Tom. I was making the adjust at the same time you wrote your reply :slight_smile:
The code is now in a code box.
Why a ATTiny? Well, it is a small 8 pin dil which makes the product i try to develop, very small. And it looks more professional the a Nano.
Why do you ask? Do you have a better suggestion? I am always open for them :slight_smile:

Thanks!

help......

Hi,

MCP4921 talk SPI

Have you got the 4921 working with the Nano first?

Tom... :slight_smile:

What you're trying to do looks very strange. Does it even compile?

It looks like you're trying to use a method transfer() from a class named tinySPI. But, then you implement that function yourself. Is this the tinySPI you're trying to use: tinySPI/tinySPI.h at master · JChristensen/tinySPI · GitHub?

If so, study the example code that comes with it.