Use FRDM-KL46Z as an analog output source

Dependencies:   mbed

Committer:
allonq
Date:
Thu Feb 13 02:04:12 2014 +0000
Revision:
0:94185708309f
Use FRDM-KL46Z as a analog output source. Parameter adjustable via GUI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
allonq 0:94185708309f 1 /* This is a mbed program for EE202A, hm1. written by Yujing Qian's team
allonq 0:94185708309f 2 Main idea:
allonq 0:94185708309f 3 Use interrupt to change the default configuration of the analog output.
allonq 0:94185708309f 4 The corresponding GUI is Analog_outpt_control.py
allonq 0:94185708309f 5 All the commented-out code is for debugging use, please ignore them
allonq 0:94185708309f 6 Author: Yujing Qian, Tianlei Tang Feb.12/2014
allonq 0:94185708309f 7 */
allonq 0:94185708309f 8 #include "mbed.h"
allonq 0:94185708309f 9 #include "math.h"
allonq 0:94185708309f 10 #define BUFFER_SIZE 20
allonq 0:94185708309f 11 #define PI 3.14159265359
allonq 0:94185708309f 12
allonq 0:94185708309f 13 AnalogOut analogout(PTE30);
allonq 0:94185708309f 14 Serial pc(USBTX,USBRX);
allonq 0:94185708309f 15
allonq 0:94185708309f 16 bool received;
allonq 0:94185708309f 17 int buff = 0;
allonq 0:94185708309f 18 char rx_buffer[BUFFER_SIZE];
allonq 0:94185708309f 19 void receive_handler(){
allonq 0:94185708309f 20 while (pc.readable() && buff< BUFFER_SIZE){
allonq 0:94185708309f 21 rx_buffer[buff] = pc.getc();
allonq 0:94185708309f 22 if (rx_buffer[buff] == '#'){
allonq 0:94185708309f 23 rx_buffer[buff] = '\0';
allonq 0:94185708309f 24 /*
allonq 0:94185708309f 25 rx_buffer[12] = '\0';
allonq 0:94185708309f 26 buff=13;
allonq 0:94185708309f 27 rx_buffer[0]=0;
allonq 0:94185708309f 28 rx_buffer[1]=0;
allonq 0:94185708309f 29 int tag=16;
allonq 0:94185708309f 30 char* testx=(char*)&(tag);
allonq 0:94185708309f 31 rx_buffer[2]=(*testx);
allonq 0:94185708309f 32 rx_buffer[3]=0;
allonq 0:94185708309f 33 rx_buffer[4]=0;
allonq 0:94185708309f 34 rx_buffer[5]=0;
allonq 0:94185708309f 35 tag=32;
allonq 0:94185708309f 36 testx=(char*)&(tag);
allonq 0:94185708309f 37 rx_buffer[6]=(*testx);
allonq 0:94185708309f 38 rx_buffer[7]=0;
allonq 0:94185708309f 39 rx_buffer[8]=0;
allonq 0:94185708309f 40 rx_buffer[9]=0;
allonq 0:94185708309f 41 tag=16;
allonq 0:94185708309f 42 testx=(char*)&(tag);
allonq 0:94185708309f 43 rx_buffer[10]=(*testx);
allonq 0:94185708309f 44 rx_buffer[11]=0;
allonq 0:94185708309f 45 */
allonq 0:94185708309f 46 buff++;
allonq 0:94185708309f 47 received = true;
allonq 0:94185708309f 48 //pc.printf("done!");
allonq 0:94185708309f 49 break;
allonq 0:94185708309f 50 }
allonq 0:94185708309f 51 buff++;
allonq 0:94185708309f 52 }
allonq 0:94185708309f 53 return;
allonq 0:94185708309f 54 }
allonq 0:94185708309f 55
allonq 0:94185708309f 56
allonq 0:94185708309f 57
allonq 0:94185708309f 58
allonq 0:94185708309f 59
allonq 0:94185708309f 60 int main() {
allonq 0:94185708309f 61 pc.baud(9600);
allonq 0:94185708309f 62 pc.attach(&receive_handler,Serial::RxIrq);
allonq 0:94185708309f 63 //set interrupt
allonq 0:94185708309f 64 float freq=0.25;
allonq 0:94185708309f 65 float amp=0.5;
allonq 0:94185708309f 66 float bias=0.5;
allonq 0:94185708309f 67 int *read;
allonq 0:94185708309f 68 char* select;
allonq 0:94185708309f 69 int count=0;
allonq 0:94185708309f 70 while (true) {
allonq 0:94185708309f 71 if (received) {
allonq 0:94185708309f 72 //pc.printf("yeah");
allonq 0:94185708309f 73 select=rx_buffer;
allonq 0:94185708309f 74 read=(int*)select;
allonq 0:94185708309f 75 amp=read[0]/10000.0;
allonq 0:94185708309f 76 freq=read[1]/10000.0;
allonq 0:94185708309f 77 bias=read[2]/10000.0;
allonq 0:94185708309f 78 buff=0;
allonq 0:94185708309f 79 received=false;}
allonq 0:94185708309f 80 //float tmp;
allonq 0:94185708309f 81 for(count=0; count<120; count++){
allonq 0:94185708309f 82 analogout.write(amp*(sin(2*PI*count/120.0))+bias);
allonq 0:94185708309f 83 // tmp=amp*(sin(2*PI*count/120.0))+bias;
allonq 0:94185708309f 84 // pc.printf("ans=%f\n",tmp);
allonq 0:94185708309f 85 wait((1/(120.0*freq)));
allonq 0:94185708309f 86 }//one cirle
allonq 0:94185708309f 87
allonq 0:94185708309f 88 }
allonq 0:94185708309f 89 }
allonq 0:94185708309f 90
allonq 0:94185708309f 91