Showing posts with label DP 32. Show all posts
Showing posts with label DP 32. Show all posts

Wednesday, 29 April 2015

chipKIT DP32 Simple dub Siren Code



Sampling Rate 22.050 kHz, with 12 bit DAC MCP4725 ( breakout board)
Using button 2(Pin 17) on DP32 and Variable pot (A2).
For the signal out, the ac coupling circuit from previous post is necessary.
*Check also I2C Speed post HERE





















#include <sys/attribs.h>
#include <Wire.h>
#define MCP4725_DID 96 // define device id - see datasheet
//
#define SYS_FREQ            (40000000L)
#define PB_DIV              4
#define PRESCALE            2
#define FRQ                 22050 //21504
#define T1_TICK             (SYS_FREQ/PB_DIV/PRESCALE/FRQ)

#define WAVE_SAMPLES 256
const int buttonPin = 17; 
// variables will change:
int buttonState = 0;         // variable to read the pushbutton status
uint8_t playOn=0;            // Variable of play state/button state
volatile uint16_t a;
uint16_t b;
uint16_t d;
uint16_t e=0;
uint16_t inc;
uint8_t statedown=0;
boolean c;
boolean togg;
uint16_t sineData [WAVE_SAMPLES];
//


void setup()
{
  // Serial.begin(57600);
  initTmr();
  pinMode(A2, INPUT); //
  pinMode(13, OUTPUT); //
  //set pins to output because they are addressed in the main loop
  sine ();  
  Wire.begin() ;
}

void loop()
{
  uint16_t aIn =analogRead(A2);
  inc=aIn >>4;
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {  
    playOn=1;   
    // play is On    
  } 
  else{
    playOn=0;
    // play is Off

  }
  if(c !=togg){

    if(playOn==1){

      a = a++  ;
      a+= (inc+(e >>4));
      if(a>=WAVE_SAMPLES){
        a-=WAVE_SAMPLES;
        // ramp up or down variables
        if (statedown==0){
          e++;
          if(e>=1023){
            statedown=1;
            e--;
          }
        }
        else{
          e--;
          if (e==0){
            statedown=0;
            e++;
          }
        }
      }
      //
      Wire.beginTransmission(MCP4725_DID); //device adress
      Wire.send(64);                     // cmd to update the DAC
      Wire.send(sineData[a] >> 4);        // the 8 most significant bits...
      Wire.send((sineData[a] & 15) << 4); /* the 4 least significant bis...*/
      Wire.endTransmission();
      /*
       */
    } 
  }//
  c=togg;
}

// call interrupt handling vector
extern "C" {

  void __ISR(16, ipl6) int1Handler(void) 
  {
    togg= !togg;
    //
    //
    IFS0CLR = 0x80000;// Clear the T4 interrupt flag Bit 19
    //
  }
}

void initTmr(){
  // 
  T4CON=0x0; //Stop timer and clear registers
  T4CONSET = 0x0010; // set prescalar 1:2 ox60 to experiment
  TMR4 = 0x0; //Clear Timer 4 register
  PR4 = T1_TICK ; //0x17; // set timer to 23 
  IFS0CLR = 0x80000;// Clear the T4 interrupt flag Bit 19
  IPC4SET = 0x00000016;// Interrupt priority 5, 2
  IEC0SET = 0x80000;// Enable T4 interrupt Bit 19
  T4CONSET = 0x8000;// Enable Timer4  
}
void sine (){
  uint16_t i;
  for(i=0;i<WAVE_SAMPLES;i++){
    b = 2047*sin((2*PI/WAVE_SAMPLES)*i);
    b+=2047;
    sineData [i]=b;
  }
}