Attachment 'ADCtiny85r.ino'

Download

   1 // ADCtiny85r.ino, A/D converter, I2C library from rambo
   2 // 2013-12-14 RR
   3 // Get this from https://github.com/rambo/TinyWire
   4 #include <TinyWireS.h>
   5 // http://playground.arduino.cc/Main/WireLibraryDetailedReference
   6 
   7 // The default buffer size, Can't recall the scope of defines right now
   8 #ifndef TWI_RX_BUFFER_SIZE
   9 #define TWI_RX_BUFFER_SIZE ( 16 )
  10 #endif
  11 
  12 // Pin 13 has an LED connected on most Arduino boards.
  13 int led = 1;             // on ATtiny85 (D)1, pin 6
  14 
  15 int analogPin = 3;       // AD8307
  16 
  17 int aval_i = 0;          // analog value input AD8307
  18 int aval_s = 0;          // analog sum
  19 int aval   = 0;          //  spare
  20 int achanin = analogPin;         // analog channel in
  21 #define avg_num 8        // average number, max 30
  22 int avg_cnt = 0;         // average count
  23 uint8_t rdy_n = 0x80;    // analog value not ready
  24 
  25 //Variables used in getting and parsing data.
  26 char rxChrData;               //Receives the data.
  27 char rxString[12];            //Varbiable for holding one string of data.
  28 int rxIndex = 0;              //Used to index rxString.
  29 #define I2C_SLAVE_ADDRESS 0x5 // the I2C/TWI 7-bit address 
  30 
  31 // I2C Master starts a fetch request, fill tx buffer with a 3 byte sequence
  32 void requestEvent() {  
  33     TinyWireS.send( aval >> 8);    // high byte
  34     TinyWireS.send( aval & 0xFF);  // low byte
  35     TinyWireS.send( rdy_n);        // send by ISR
  36     rdy_n = 0x80;                  // set NOT ready
  37 }
  38 
  39 /**
  40  * The I2C data received -handler
  41  *
  42  * This needs to complete before the next incoming transaction (start, data, restart/stop) on the bus does
  43  * so be quick, set flags for long running tasks to be called from the mainloop instead of running them directly,
  44  */
  45 void receiveEvent(uint8_t howMany) {
  46     if (TinyWireS.available()) {  
  47       if (howMany < 1) {
  48           // Sanity-check
  49           return;
  50       }
  51       if (howMany > TWI_RX_BUFFER_SIZE) {
  52           // Also insane number
  53           return;
  54       }
  55   
  56       //rxChrData = TinyWireS.receive();
  57       howMany--;
  58       if (!howMany) {
  59           // This write was only to set the buffer for next read
  60           return;
  61       }
  62       while(howMany--) {
  63           rxChrData = TinyWireS.receive();
  64           achanin = rxChrData;
  65           rxString[rxIndex] = char(rxChrData);
  66           rxIndex++;  
  67           // just set the analog channel 2 or 3
  68           if (achanin == 2 || achanin ==3) {
  69               analogPin = achanin; 
  70           }
  71           
  72       }
  73     }
  74 }
  75 
  76 void setup()
  77 {
  78     //Serial.begin(9600);
  79     pinMode(1, OUTPUT); // OC1A, also The only HW-PWM -pin supported by the tiny core analogWrite
  80     TinyWireS.begin(I2C_SLAVE_ADDRESS);
  81     TinyWireS.onReceive(receiveEvent);
  82     TinyWireS.onRequest(requestEvent);
  83 }
  84 
  85 void loop()
  86 {
  87 /**
  88  * This is the only way we can detect stop condition 
  89    (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=984716&sid=82e9dc7299a8243b86cf7969dd41b5b5#984716)
  90  * it needs to be called in a very tight loop in order not to miss any (REMINDER: Do *not* use delay() anywhere,
  91     use tws_delay() instead).
  92  * It will call the function registered via TinyWireS.onReceive(); if there is data in the buffer on stop.
  93  */
  94     TinyWireS_stop_check();
  95     
  96     // average voltage
  97     aval_s = 0;
  98     for (avg_cnt = 0; avg_cnt < avg_num; avg_cnt++) {
  99         aval_i = analogRead(analogPin);    // read the input pin
 100         aval_s += aval_i;
 101     }
 102     aval = aval_s / avg_num;
 103     rdy_n = 0;                 // conversion ready to get fetched
 104     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage)
 105     digitalWrite(led, LOW);    // EOC for convewrsion time measurement
 106 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-12-15 10:07:00, 3.0 KB) [[attachment:ADCNano3.ino]]
  • [get | view] (2013-12-15 09:17:00, 3.5 KB) [[attachment:ADCtiny85r.ino]]
  • [get | view] (2013-12-15 23:44:00, 89.1 KB) [[attachment:ATmega_I2C_0A03_IMAG0189.jpg]]
  • [get | view] (2013-12-15 10:41:00, 129.5 KB) [[attachment:ATtiny_I2C_0A03_IMAG0188.jpg]]
  • [get | view] (2014-01-21 16:36:00, 122.5 KB) [[attachment:PHSNA_PLX_DAQ_10_7_5V.PNG]]
  • [get | view] (2014-08-08 17:25:00, 38.7 KB) [[attachment:Wobbulator_Front_DSC05423.jpg]]
  • [get | view] (2014-03-18 08:46:00, 99.6 KB) [[attachment:Wobbulator_Prototyp2_DSC05176.jpg]]
  • [get | view] (2013-12-19 15:02:00, 65.1 KB) [[attachment:Wobbulator_Prototyp_DSC05047.jpg]]
  • [get | view] (2014-08-08 17:07:00, 83.8 KB) [[attachment:Wobbulator_oben_DSC05421.jpg]]
  • [get | view] (2013-12-15 10:18:00, 0.8 KB) [[attachment:test_adc.py]]
  • [get | view] (2013-12-15 09:32:00, 0.8 KB) [[attachment:test_adcr.py]]
  • [get | view] (2014-08-09 08:23:00, 55.6 KB) [[attachment:wob_logamp.brd]]
  • [get | view] (2014-08-09 08:23:00, 58.8 KB) [[attachment:wob_logamp.eps]]
  • [get | view] (2014-08-09 08:23:00, 322.8 KB) [[attachment:wob_logamp.sch]]
  • [get | view] (2014-08-09 08:22:00, 33.0 KB) [[attachment:wob_logamp_sch.pdf]]
  • [get | view] (2014-08-09 08:25:00, 119.2 KB) [[attachment:wob_logamp_sch.png]]
  • [get | view] (2014-08-08 17:54:00, 25.2 KB) [[attachment:wobbulator2.6.3.py]]
  • [get | view] (2014-04-05 19:10:00, 271.0 KB) [[attachment:wobbulatorM.sch]]
  • [get | view] (2014-04-05 19:09:00, 28.9 KB) [[attachment:wobbulatorM_sch.pdf]]
  • [get | view] (2014-04-05 19:09:00, 74.7 KB) [[attachment:wobbulatorM_sch.png]]
  • [get | view] (2014-08-08 14:27:00, 31.8 KB) [[attachment:wobbulator_10.7MA_limits.png]]
  • [get | view] (2014-08-08 17:06:00, 83.8 KB) [[attachment:wobbulator_DSC05418.jpg]]
  • [get | view] (2014-04-05 18:28:00, 29.5 KB) [[attachment:wobbulator_limits_0-60dB_5V.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.