Dependencies:   mbed

Committer:
mitesh2patel
Date:
Tue Mar 08 10:38:53 2011 +0000
Revision:
0:a22794f9a5ce
pwm_adc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mitesh2patel 0:a22794f9a5ce 1 //////////////////////////////////////////////////
mitesh2patel 0:a22794f9a5ce 2 // Include Files
mitesh2patel 0:a22794f9a5ce 3 #include "mbed.h"
mitesh2patel 0:a22794f9a5ce 4
mitesh2patel 0:a22794f9a5ce 5 //////////////////////////////////////////////////
mitesh2patel 0:a22794f9a5ce 6 //Pin Defines for LED
mitesh2patel 0:a22794f9a5ce 7 DigitalOut sysled(LED1); //System Indicator
mitesh2patel 0:a22794f9a5ce 8 //DigitalOut ind1(LED2); //
mitesh2patel 0:a22794f9a5ce 9 DigitalOut ind2(LED3); //idle state indicator
mitesh2patel 0:a22794f9a5ce 10 DigitalOut ind3(LED4); //adc convertion indicator
mitesh2patel 0:a22794f9a5ce 11
mitesh2patel 0:a22794f9a5ce 12
mitesh2patel 0:a22794f9a5ce 13
mitesh2patel 0:a22794f9a5ce 14
mitesh2patel 0:a22794f9a5ce 15 // ADC pins
mitesh2patel 0:a22794f9a5ce 16 AnalogIn Sense1(p15); //Sensor 1
mitesh2patel 0:a22794f9a5ce 17 AnalogIn Sense2(p16); //Sensor 2
mitesh2patel 0:a22794f9a5ce 18 AnalogIn Sense3(p17); //Sensor 3
mitesh2patel 0:a22794f9a5ce 19 AnalogIn Sense4(p18); //Sensor 4
mitesh2patel 0:a22794f9a5ce 20 AnalogIn Sense5(p19); //Sensor 5
mitesh2patel 0:a22794f9a5ce 21 AnalogIn Sense6(p20); //Sensor 6
mitesh2patel 0:a22794f9a5ce 22
mitesh2patel 0:a22794f9a5ce 23
mitesh2patel 0:a22794f9a5ce 24 //PWM signals
mitesh2patel 0:a22794f9a5ce 25 PwmOut x(p21);
mitesh2patel 0:a22794f9a5ce 26 PwmOut y(p22);
mitesh2patel 0:a22794f9a5ce 27 PwmOut x1(p23);
mitesh2patel 0:a22794f9a5ce 28 PwmOut y1(p24);
mitesh2patel 0:a22794f9a5ce 29 PwmOut x2(p25);
mitesh2patel 0:a22794f9a5ce 30 PwmOut y2(p26);
mitesh2patel 0:a22794f9a5ce 31
mitesh2patel 0:a22794f9a5ce 32
mitesh2patel 0:a22794f9a5ce 33 //Configure the PC Serial Port for CDC USB
mitesh2patel 0:a22794f9a5ce 34 Serial pc(USBTX, USBRX); // tx, rx of USB
mitesh2patel 0:a22794f9a5ce 35
mitesh2patel 0:a22794f9a5ce 36
mitesh2patel 0:a22794f9a5ce 37 //////////////////////////////////////////////////
mitesh2patel 0:a22794f9a5ce 38
mitesh2patel 0:a22794f9a5ce 39 LocalFileSystem fs("fs"); //file systems
mitesh2patel 0:a22794f9a5ce 40
mitesh2patel 0:a22794f9a5ce 41 Timer timer; //timer
mitesh2patel 0:a22794f9a5ce 42
mitesh2patel 0:a22794f9a5ce 43
mitesh2patel 0:a22794f9a5ce 44 // main Program
mitesh2patel 0:a22794f9a5ce 45 int main() {
mitesh2patel 0:a22794f9a5ce 46
mitesh2patel 0:a22794f9a5ce 47 unsigned short usCurrentValue;
mitesh2patel 0:a22794f9a5ce 48 unsigned int i=0,j=0;
mitesh2patel 0:a22794f9a5ce 49 // float beg,end;
mitesh2patel 0:a22794f9a5ce 50
mitesh2patel 0:a22794f9a5ce 51 //Configure the Fastest Baud Rate
mitesh2patel 0:a22794f9a5ce 52 pc.baud(115200); // baud rate
mitesh2patel 0:a22794f9a5ce 53 printf("ADC TEST !!\n"); //print the value in the serial port
mitesh2patel 0:a22794f9a5ce 54
mitesh2patel 0:a22794f9a5ce 55
mitesh2patel 0:a22794f9a5ce 56
mitesh2patel 0:a22794f9a5ce 57 //duty cycle for pwm sigmnals
mitesh2patel 0:a22794f9a5ce 58 x =0.2;
mitesh2patel 0:a22794f9a5ce 59 y =0.3;
mitesh2patel 0:a22794f9a5ce 60 x1=0.4;
mitesh2patel 0:a22794f9a5ce 61 x2=0.5;
mitesh2patel 0:a22794f9a5ce 62 y1=0.6;
mitesh2patel 0:a22794f9a5ce 63 y2=0.8;
mitesh2patel 0:a22794f9a5ce 64
mitesh2patel 0:a22794f9a5ce 65
mitesh2patel 0:a22794f9a5ce 66 //Say the System is Ready to Run
mitesh2patel 0:a22794f9a5ce 67 sysled = 1;
mitesh2patel 0:a22794f9a5ce 68
mitesh2patel 0:a22794f9a5ce 69
mitesh2patel 0:a22794f9a5ce 70
mitesh2patel 0:a22794f9a5ce 71 char fname[20]; // charecter buffer
mitesh2patel 0:a22794f9a5ce 72
mitesh2patel 0:a22794f9a5ce 73 ////////////////// // loop counts the no: of existing files
mitesh2patel 0:a22794f9a5ce 74
mitesh2patel 0:a22794f9a5ce 75 while(1)
mitesh2patel 0:a22794f9a5ce 76 {
mitesh2patel 0:a22794f9a5ce 77 snprintf(fname, sizeof(fname), "/fs/adc%d.txt", i); //define the fine name with fanme buffer size
mitesh2patel 0:a22794f9a5ce 78 FILE *fp = fopen(fname, "r"); //open the file with read mode
mitesh2patel 0:a22794f9a5ce 79
mitesh2patel 0:a22794f9a5ce 80 if(fp==NULL) // if file name is not there exit the loop
mitesh2patel 0:a22794f9a5ce 81 break;
mitesh2patel 0:a22794f9a5ce 82
mitesh2patel 0:a22794f9a5ce 83 i++;
mitesh2patel 0:a22794f9a5ce 84 fclose(fp); // close the opened file
mitesh2patel 0:a22794f9a5ce 85 }
mitesh2patel 0:a22794f9a5ce 86
mitesh2patel 0:a22794f9a5ce 87 FILE *fp = fopen(fname, "w"); //open the file in write mode
mitesh2patel 0:a22794f9a5ce 88 fprintf(fp,"AD_15\t AD_16\t AD_17\t AD_18\t AD_19\t AD_20\n");
mitesh2patel 0:a22794f9a5ce 89
mitesh2patel 0:a22794f9a5ce 90
mitesh2patel 0:a22794f9a5ce 91
mitesh2patel 0:a22794f9a5ce 92 // timer.start(); //start the timer
mitesh2patel 0:a22794f9a5ce 93 // beg=timer.read_ms(); //note the starting time of the timer
mitesh2patel 0:a22794f9a5ce 94
mitesh2patel 0:a22794f9a5ce 95
mitesh2patel 0:a22794f9a5ce 96 for(j=0;j<2000;j++){ //loop repeats for 5 sec
mitesh2patel 0:a22794f9a5ce 97 ind3 = !ind3;
mitesh2patel 0:a22794f9a5ce 98 usCurrentValue = (Sense1.read_u16()&0xFFF); //read the adc value
mitesh2patel 0:a22794f9a5ce 99 fprintf(fp,"%fV\t",(usCurrentValue*3.3/4095)); // print the value in the opened file
mitesh2patel 0:a22794f9a5ce 100 // printf("\n ADC Val at pin15 : %f Volts = %d (Decimal Reading)",(usCurrentValue*3.3/4095),usCurrentValue); //print the value in usb terminal
mitesh2patel 0:a22794f9a5ce 101
mitesh2patel 0:a22794f9a5ce 102
mitesh2patel 0:a22794f9a5ce 103
mitesh2patel 0:a22794f9a5ce 104
mitesh2patel 0:a22794f9a5ce 105 ind3 = !ind3;
mitesh2patel 0:a22794f9a5ce 106 usCurrentValue = (Sense2.read_u16()&0xFFF); //read the adc value
mitesh2patel 0:a22794f9a5ce 107 fprintf(fp,"%fV\t",(usCurrentValue*3.3/4095));
mitesh2patel 0:a22794f9a5ce 108 // printf("\n ADC Val at pin16 : %f Volts = %d (Decimal Reading)",(usCurrentValue*3.3/4095),usCurrentValue);
mitesh2patel 0:a22794f9a5ce 109
mitesh2patel 0:a22794f9a5ce 110
mitesh2patel 0:a22794f9a5ce 111
mitesh2patel 0:a22794f9a5ce 112 ind3 = !ind3;
mitesh2patel 0:a22794f9a5ce 113 usCurrentValue = (Sense3.read_u16()&0xFFF); //read the adc value
mitesh2patel 0:a22794f9a5ce 114 fprintf(fp,"%fV\t",(usCurrentValue*3.3/4095));
mitesh2patel 0:a22794f9a5ce 115 // printf("\n ADC Val at pin17 : %f Volts = %d (Decimal Reading)",(usCurrentValue*3.3/4095),usCurrentValue);
mitesh2patel 0:a22794f9a5ce 116
mitesh2patel 0:a22794f9a5ce 117
mitesh2patel 0:a22794f9a5ce 118 ind3 = !ind3;
mitesh2patel 0:a22794f9a5ce 119 usCurrentValue = (Sense4.read_u16()&0xFFF); //read the adc value
mitesh2patel 0:a22794f9a5ce 120 fprintf(fp,"%fV\t",(usCurrentValue*3.3/4095));
mitesh2patel 0:a22794f9a5ce 121 // printf("\n ADC Val at pin18 : %f Volts = %d (Decimal Reading)",(usCurrentValue*3.3/4095),usCurrentValue);
mitesh2patel 0:a22794f9a5ce 122
mitesh2patel 0:a22794f9a5ce 123
mitesh2patel 0:a22794f9a5ce 124
mitesh2patel 0:a22794f9a5ce 125
mitesh2patel 0:a22794f9a5ce 126
mitesh2patel 0:a22794f9a5ce 127 ind3 = !ind3;
mitesh2patel 0:a22794f9a5ce 128 usCurrentValue = (Sense5.read_u16()&0xFFF); //read the adc value
mitesh2patel 0:a22794f9a5ce 129 fprintf(fp," %fV\t",(usCurrentValue*3.3/4095));
mitesh2patel 0:a22794f9a5ce 130 // printf("\n ADC Val at pin19 : %f Volts = %d (Decimal Reading)",(usCurrentValue*3.3/4095),usCurrentValue);
mitesh2patel 0:a22794f9a5ce 131
mitesh2patel 0:a22794f9a5ce 132
mitesh2patel 0:a22794f9a5ce 133
mitesh2patel 0:a22794f9a5ce 134 ind3 = !ind3;
mitesh2patel 0:a22794f9a5ce 135 usCurrentValue = (Sense6.read_u16()&0xFFF); //read the adc value
mitesh2patel 0:a22794f9a5ce 136 fprintf(fp,"%fV\t",(usCurrentValue*3.3/4095));
mitesh2patel 0:a22794f9a5ce 137 //printf("\n ADC Val at pin20 : %f Volts = %d (Decimal Reading)",(usCurrentValue*3.3/4095),usCurrentValue);
mitesh2patel 0:a22794f9a5ce 138
mitesh2patel 0:a22794f9a5ce 139 fprintf(fp,"\n");
mitesh2patel 0:a22794f9a5ce 140 //fprintf(fp,"\n");
mitesh2patel 0:a22794f9a5ce 141 //seperate the one set of valus from other
mitesh2patel 0:a22794f9a5ce 142 //fprintf(fp,"\n");
mitesh2patel 0:a22794f9a5ce 143 }
mitesh2patel 0:a22794f9a5ce 144 // end=timer.read_ms(); //note the ending time of the timer
mitesh2patel 0:a22794f9a5ce 145 // timer.stop(); //stop the timer
mitesh2patel 0:a22794f9a5ce 146 // printf("timer starts at %fms and end at %fms\n",beg,end); //print the timer starting and ending time values
mitesh2patel 0:a22794f9a5ce 147
mitesh2patel 0:a22794f9a5ce 148 fclose(fp); // close the file
mitesh2patel 0:a22794f9a5ce 149 ind2=1; //indicating idle state
mitesh2patel 0:a22794f9a5ce 150
mitesh2patel 0:a22794f9a5ce 151 while(1); // wait untill the reset
mitesh2patel 0:a22794f9a5ce 152
mitesh2patel 0:a22794f9a5ce 153
mitesh2patel 0:a22794f9a5ce 154
mitesh2patel 0:a22794f9a5ce 155 }