Up: Hands On AVR-GCC programming


Analog Inputs. Using (OSC )

The last program shows how to interconnect Wiring or Arduino to other programs like Pd, using a communication scheme protocol called OSC.

Open Sound Control (OSC), developed by Matt Wright and Adrian Freed at CNMAT, is a protocol for communication among computers, sound synthesizers, and other multimedia devices that are optimized for modern networking technology. OSC's advantages include interoperability, accuracy, flexibility, and enhanced organization and documentation.

OSC is not limited to musical applications. Other programming environments like Processing, can communicate through OSC using oscP5, which is an extension library that enables processing to communicate with other hardware and software using the OSC protocol. Many implementations and application areas can be found at OSC implementations and OSCapplication areas.

The OSCimplementation in AVRLIB includes the functions used in this program like oscInit(), oscSendMessageIntInt() and oscSendMessageString(). oscInit(), initializes the OSClibrary. oscSendMessageIntInt(), writes a OSCpacket header and 8 Bytes arguments. In this case is used to send normalized voltages values from two sensors connected to the analog INPUTS of PORTF on Wiring board. The arguments in oscSendMessageIntInt(), are a 'PSTR' heading argument and two u32 variables. The arguments in oscSendMessageIntInt() are the 'PSTR' heading argument and a character type ('char') argument. In this program oscSendMessageIntInt() is being used to send a 'bang' signal, which is understood by Pd.

In order to compile-and-load this program, make sure you have edited its 'makefile', and you are working on the same directory probably called: 'sensosc'. Copy-and-paste the code below and save it as 'senseosc.c'. in the 'sensosc' directory. While editing it 'makefile' make sure the 'TRG = ' and 'SRC = ' lines should look like,


 		 
   TRG = sensosc


 		 
   SRC = $(AVRLIB)/buffer.c $(AVRLIB)/timer128.c $(AVRLIB)/ccrma/osc.c $(AVRLIB)/uart.c $(AVRLIB)/a2d.c $(TRG).c

As you can see, here we are including a2d, timer128, uart and OSC libraries as well as other standard libraries. Read the code and follow instructions on its description and try to understand what is going on. The code on 'sensosc.c' uses an array called 'a2dvalues' of two elements to store values coming from the sensors. The function 'a2dConvert10bit(ch)', reads values coming from the analog pins (ch), in PORTF.

On the breadboard, hook up a circuit for two parallel sensors (maybe a photo-resistor), and a potentiometer (POT). Illustrations for this circuits can be found at: POT and PhotoResistor. Use pin0 and pin1 of PORTF (analog port) on Wiring boards to connect wires from sensors. Make sure USB connections are fine, the board gets powered and if needed make sure serial port devices ('/dev/ttyUSBx') are working and there is communication between the board and the computer. If ``all-systems-go'', go ahead compile and load.


 		 
 

//------------------------------------------------------
//------------------------------------------------------
//   
//  AVRLIB-DEMO
//  For avrlib and Wiring development board.
//  
//
//  File:     sensosc.c
//  Author:   Juan Reyes    juanig-at-ccrma-stanford_edu
//            based on Michael Gurevich and Wendy Ju demo
//  Date:     July 1, 2008
//
//  Notes:
//-----------------------------------------------------
//  USING THE WIRING DEVELOPMENT BOARD, ANALOG INPUTS 
//  PORTF IN ATMEGA128 TO SEND SENSOR DATA USING OSC
//
//-----------------------------------------------------
//  Layout of ports in Wiring board correspond with
//  ATMega128 as follows:
//
//  ATMega128                WIRING
//  =========                ======
//  
//  PORTD                    PORT-0
//  PORTC                    PORT-1
//  PORTF                    ANALOG IN
//  PORTG                    BUILT-IN LED
//-----------------------------------------------------
//  Layout of analog pins in Wiring board correspond with
//  ATMega128 as follows:
//
//  Ch0                   W-APIN0
//  Ch1                   W-APIN1
//   .                      .
//  Ch7                   W-APIN7
//
//   Function a2dConvert10bit(Ch) reads and converts analog
//   values.
//
//-----------------------------------------------------
//  More information about Wiring board:
//
//  http://wiring.org.co/hardware/index.html
//  
//-----------------------------------------------------
//  Information about functions used here and avrlib:
// 
//  ~/avrlib/examples/a2d/a2dtest.c
//  ~/avrlib/ccrma/osc.c
//

//  
//*********************************************************
//   Description:
//   ============
//
//   This program will send sensor data values using OSC.
//   Sensors are connected to analog in (0) and (1) on 
//   Wiring board. For example you can try a photoresistor 
//   and a POT, one to control Frequencies and the other to
//   control amplitude (volume). 
//
//   Use with Pd can be seen on the accompanying patches.
//   Make sure you have OSC library and SerialIO objects in Pd. 
//
//   Information about analog sensors and Wiring:
//   http://wiring.org.co/learning/examples/photoresistor.html
//
//   Information about OSC:
//   http://opensoundcontrol.org/introduction-osc
//
//*********************************************************


#include <avr/io.h>
#include <avr/pgmspace.h>

#include "global.h"
#include "uart.h"
#include "ccrma/osc.h"
#include "timer128.h"    // use timer128.h for ATMega128
#include "a2d.h"

int main(void)
{
     u16 count;
     u16 a2dvalues[2];

     // Initialize libraries
     uartInit();
     oscInit();
     timerInit();

     // Set baud rate 115200 works on FTDI and Wiring
     uartSetBaudRate(115200);

     // ATOD STUFF
     a2dInit();
  
     // set PORTF pins to input (for ADC) 
     outb(DDRF, 0x00);        
     // turn pull-ups off 
     outb(PORTF, 0x00);   
     count = 0;          // counter variable
     
     while(1) {
       
       // Write OSC packet header, argument length = 4bytes
       oscSendMessageInt(PSTR("/counter"), count++);


       // Read sensor values and store in array a2dvalues[]
       a2dvalues[0] = a2dConvert10bit(0);
       a2dvalues[1] = a2dConvert10bit(1);

       // write OSC packet header, argument length = 8bytes
       oscSendMessageIntInt(PSTR("/a2dvalues"), a2dvalues[0], a2dvalues[1]);
       
       // Here we send a bang every 100 iterations
       if (!(count % 100)) {
	 oscSendMessageString(PSTR("/bbang"),"bang");
       }
       
       if (count > 2048) {
	 count = 0;
       }
       timerPause(10);
     }

     return 0;
}

On the 'sensosc' directory follow these steps:

  1. Compile:

    
     		 [host] make        
    
  2. Press the '<reset>' button.

  3. Load to ATMega:

    
     		 [host] make load       
    
  4. Press the '<reset>' button. If you see the progress output, and everything goes fine, press the '<reset>' button again, and you should proceed to the Pdpatch to find out if there is some communication.


next up previous
Next: Reading OSC data on Pd Up: Hands On AVR-GCC programming Previous: Analog Inputs

© Copyright 2001-2008 CCRMA, Stanford University. All rights reserved.
Created and Mantained by Juan Reyes