arduino pwm dac behaving very strange


hi,
i have java code sends pcm recorded sample esp8266 via wifi, in turn sends data arduino via serial plays sample using pwm dac. using timer1 fast pwm carrier frequency whereas timer2 used set sample rate. please see below code snippets.
1. arduino code
code: [select]
#include "arduino.h"
#include <avr/interrupt.h> // use timer interrupt library


void setup() {
 serial.begin(400000);
 //serial.begin(266666);
 /****set timer1 8-bit fast pwm output ****/
 pinmode(9, output); // make timer's pwm pin output
 tccr1b = (1 << cs10); // set prescaler full 16mhz,
 tccr1a |= (1 << com1a1); // pin low when tcnt1=ocr1a
 tccr1a |= (1 << wgm10); // use 8-bit fast pwm mode, count 0-255, , duty cycle set ocr1al(ocr1a lower byte)
 tccr1b |= (1 << wgm12);
 //freq calculation: @ 16mhz, time taken count till 255 (1,000,000/16,000,000)*256usec=16usec
 //freq 16,000,000/256=62500hz=62.5khz,

 /******** set timer2 call isr ********/
 tccr2a = 0; // no options in control register a
 tccr2b = (1 << cs21); // set prescaler divide 8,
 //i.e., 16,000,000/8=2,000,000 clock tick/sec,
 //so every tick @ 500ns (.5us), spend 32 usec needs count upto 64, 64*.5=32
 timsk2 = (1 << ocie2a); // call isr when tcnt2 = ocra2
 ocr2a = 64; // set frequency of generated wave=2,000,000/64=31250hz
 sei(); // enable interrupts generate waveform!
}

void loop() { // nothing do!
}
uint32_t stime=millis();
uint32_t counter=0;
uint32_t etime=0;
/******** called every time tcnt2 = ocr2a ********/
isr(timer2_compa_vect) { // called when tcnt2 == ocr2a
 tcnt2 = 6; // timing compensate isr run time
 if(serial.available()){
 uint8_t byte=serial.read();
 ocr1al=byte;
 }//else{
 //ocr1al=0;// duty cycle zero, no output
 //}
 /*counter++;
 if(counter>=31250){
 counter=0;
 etime=millis()-stime;
 stime=millis();
 serial.println(etime);
 }*/
 //serial.write(100);
}

2. esp 8266 code:
code: [select]
/*
 *  sketch demonstrates how scan wifi networks.
 *  api same wifi shield library,
 *  obvious difference being different file need include:
 */
#include "esp8266wifi.h"
const char* ssid = "debojit-dlink";
const char* password = "india@123";
uint8_t outbuffer[2920];
uint8_t inbuffer[2920];

const char* host="192.168.0.105";
wificlient client;
void setup(){
 serial.begin(400000);
 //serial.begin(266666);
 setup_connect_as_station();
}
void setup_connect_as_station(){
 serial.println();
 serial.print("connecting ");
 serial.println(ssid);
 serial.print("configuring access point...");
 /* can remove password parameter if want ap open. */
 wifi.softap("espap");
 ipaddress myip = wifi.softapip();
 serial.print("ap ip address: ");
 serial.println(myip);

 wifi.begin(ssid, password);

 while (wifi.status() != wl_connected) {
 delay(500);
 serial.print(".");
 }
 serial.println("");
 serial.println("wifi connected ip is");
 serial.println(wifi.localip());
 serial.println("#################");
 wifi.printdiag(serial);
 wifi.hostname("esp8266");
 wifi.printdiag(serial);
}

void loop(){
 const int httpport = 8086;
 if (!client.connect(host, httpport)) {
 serial.println("connection failed");
 delay(2000);
 return;
 }
 serial.println("connected, receiving...");
 int cnt=0;
 long readbytestotal=0;
 int writebytestoserialtotal=0;
 uint32_t stime=millis();
 while(true){
 cnt++;
 int readbytes=client.read(inbuffer,2920);
 if(readbytes>0){
 int writebytestoserial=serial.write(inbuffer,readbytes);
 //serial.read();
 //readbytestotal+=readbytes;
 //writebytestoserialtotal+=writebytestoserial;
 //serial.print(readbytes);
 }
 if(cnt>=10){
 //stime=millis()-stime;
 //serial.print(stime);serial.println("-------------------");
 cnt=0;
 yield();
 }
 /*if(readbytestotal>=29200){
 uint32_t etime=millis()-stime;
 stime=millis();
 serial.println("-----------------------------------------");
 serial.println("-----------------------------------------");
 serial.println("-----------------------------------------");
 serial.println("-----------------------------------------");
 serial.print("time taken: ");serial.println(etime);
 serial.print("read bytes: ");serial.println(readbytestotal);
 serial.print("write bytes: ");serial.println(writebytestoserialtotal);
 readbytestotal=0;
 writebytestoserialtotal=0;

 }*/

 }
}



void setup_as_station() {

 // set wifi station mode , disconnect ap if connected
 wifi.mode(wifi_sta);
 wifi.disconnect();
 delay(100);
 wifi.printdiag(serial);
 serial.println("setup done");
}

void loop_wifi_scanner() {
 serial.println("scan start");

 // wifi.scannetworks return number of networks found
 int n = wifi.scannetworks();
 serial.println("scan done");
 if (n == 0)
 serial.println("no networks found");
 else
 {
 serial.print(n);
 serial.println(" networks found");
 for (int = 0; < n; ++i)
 {
 // print ssid , rssi each network found
 serial.print(i + 1);
 serial.print(": ");
 serial.print(wifi.ssid(i));
 serial.print(" (");
 serial.print(wifi.rssi(i));
 serial.print(")");
 serial.println((wifi.encryptiontype(i) == enc_type_none)?" ":"*");
 delay(10);
 }
 }
 serial.println("");

 // wait bit before scanning again
 delay(5000);
}


3. java code
code: [select]
package com.test.socket;

import java.io.datainputstream;
import java.io.dataoutputstream;
import java.io.fileinputstream;
import java.io.ioexception;
import java.net.serversocket;
import java.net.socket;
import java.net.socketaddress;
import java.net.sockettimeoutexception;

import javax.sound.sampled.audioformat;
import javax.sound.sampled.audiosystem;
import javax.sound.sampled.dataline;
import javax.sound.sampled.sourcedataline;

public class datatransceiversocket extends thread {
 private serversocket serversocket;
 private socketaddress remotesocketaddress;

 public datatransceiversocket(serversocket serversocket, string name) throws ioexception {
 this.setname(name);
 this.serversocket = serversocket;
 // serversocket.setsotimeout(10000);
 }

 public void run() {
 while(true){
 system.out.println(getname() + "->waiting client on port " + serversocket.getlocalport() + "...");
 socket server = null;
 try {
 server = serversocket.accept();
 remotesocketaddress = server.getremotesocketaddress();
 system.out.println(getname() + "->just connected " + remotesocketaddress);
 } catch (ioexception e1) {
 // todo auto-generated catch block
 e1.printstacktrace();
 return;
 }
 sender sender=new sender(server);
 sender.start();

 }
 }

 public static void main(string[] args) {
 try {
 serversocket serversocket = new serversocket(8086);
 thread t = new datatransceiversocket(serversocket, "t1");
 t.start();
 } catch (ioexception e) {
 e.printstacktrace();
 }
 }

}

class sender extends thread{
 socket socket;
 byte buffer[] = new byte[2920];
 private dataoutputstream out;
 public sender(socket socket) {
 this.socket=socket;
 try {
 out = new dataoutputstream(this.socket.getoutputstream());
 } catch (ioexception e) {
 }
 }
 @override
 public void run() {
 while(true){
 try{
 fileinputstream fis=new fileinputstream("c:\\users\\debojitk\\desktop\\esp\\samples\\test40000_1.raw");
 
 int readbytes=0;
 while((readbytes=fis.read(buffer, 0, buffer.length))>-1){
 out.write(buffer,0,readbytes);
 //thread.sleep(40);
 }
 fis.close();
 }catch(exception ex){
 ex.printstacktrace();
 }
 }
 }
}


i have set arduino timer2 generate clock frequency of 31250hz, , sending sample of same rate. strangely speed matching serail speed , not timer speed. in code serial speed 400000 baud, i.e., can send 50000 bytes/second so, if send sample of 40000hz play on pwm ratio of baud rate , sample rate 50000:40000=1.25. same things happen when set serial speed @ 500000 baud , sample rate 50000hz, here serial speed 62500 (16usec per byte), , the ratio 62500:50000=1.25.
more strangely if decrease timer interrupt frequency increasing ocr2a (say 64 128) audio quality degrades, i.e., effective sampling rate gets halved. if make 255, quality gets lowered speed remains same, i.e., timer speed not controlling play speed rather driven serial speed.
can explain phenomenon.
is occuring due serail buffer overflow issue because else in data path did flow control done @ arduino end only. should try putting timer @ esp end synchronize things.
thanks advance.
debojit

i expecting reply time.


kindly help.

thanks,
debojit


Arduino Forum > Using Arduino > Audio > arduino pwm dac behaving very strange


arduino

Comments

Popular posts from this blog

Valutazione Template - Joomla! Forum - community, help and support

SD Datastring Convention

First use of Arduino Uno : avrdude error on Blink uploading