Attachment 'ADCNano3.ino'

Download

   1 /*
   2   ADCNano3
   3   Reads on TWI/I2C request analog value
   4   For Nano3, clock @ 16 MHz
   5   Mac OS X device: /dev/tty.usbserial-A900FYDY
   6 
   7   This example code is in the public domain.
   8    2013-12-06 RR
   9  */
  10 
  11 #include <Wire.h>
  12 #include "stdio.h"
  13 //#include "stdlib.h"
  14  
  15 // Pin 13 has an LED connected on most Arduino boards.
  16 int led = 13;            // on ATtiny25 (D)4, pin 3
  17 
  18 int analogPin = 3;       // AD8307
  19 
  20 int aval_i = 0;          // analog value input
  21 int aval_s = 0;          // analog sum
  22 int aval = 0;            // analog value
  23 int achanin = 0;         // analog channel in
  24 char buff[30];           // for debug sprintf
  25 
  26 #define avg_num 8        // average number, max 30
  27 int avg_cnt = 0;         // average count
  28 
  29 //*********************   send debug string to UART1
  30 void com_print(char *daten) {
  31     while( *daten) { Serial.write(*daten++);         }
  32 }
  33 
  34 // function that executes whenever data is received from master
  35 // this function is registered as an event, see setup()
  36 void receiveEvent(int howMany) {
  37     while(1 < Wire.available()) {  // loop through all but the last
  38         char c = Wire.read();      // receive byte as a character
  39         //Serial.println(c);         // print the character
  40     }
  41     achanin = Wire.read();      // receive byte as an integer
  42     //Serial.println(achanin, DEC);    // print the integer
  43 }
  44 
  45 // function that executes whenever data is requested by master
  46 // this function is registered as an event, see setup()
  47 // if you do not use the "length" parameter, you could not trasmit "00".
  48 void requestEvent() {
  49     byte bytes[2];
  50     bytes[0] = aval >> 8;    // upper byte
  51     bytes[1] = aval & 0xFF;  // lower byte
  52     Wire.write(bytes, 2); 
  53 }
  54     
  55 // the setup routine runs once when you press reset:
  56 void setup() { 
  57     Serial.begin(38400);       // for debug only
  58     Wire.begin(5);                 // slave address
  59     Wire.onReceive(receiveEvent);  // register event
  60     Wire.onRequest(requestEvent);  // register event
  61     pinMode(led, OUTPUT);      // initialize the digital pin as an output.
  62     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  63     delay(200);                // wait for 100 ms
  64     digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  65     delay(100); 
  66     //Serial.println("Hello");
  67 }
  68 
  69 // the loop routine runs over and over again forever:
  70 void loop() {
  71     //int pause_ms = 1000;
  72 
  73     // start A/D convertion with a I2C master request
  74     if (achanin == 2 || achanin ==3) {
  75         analogPin = achanin;
  76         achanin = 0;          // ACK master command
  77         // average voltage
  78         aval_s = 0;
  79         for (avg_cnt = 0; avg_cnt < avg_num; avg_cnt++) {
  80             aval_i = analogRead(analogPin);    // read the input pin
  81             aval_s += aval_i;
  82         }
  83         aval = aval_s / avg_num;
  84         digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage)
  85         digitalWrite(led, LOW);    // EOC for convewrsion time measurement
  86         //Serial.println(aval_3, DEC); 
  87         //sprintf(buff,"2 %d, 3 %d\r\n", aval_2, aval_3);
  88         //com_print(buff);
  89     }
  90     //delay(pause_ms);
  91 }

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.