diff options
Diffstat (limited to 'Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c')
-rwxr-xr-x | Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c new file mode 100755 index 0000000..701c0c0 --- /dev/null +++ b/Modelica-Arduino-MDD_Arduino_Revised/Resources/src/cmd_analog_volt.c @@ -0,0 +1,63 @@ +#include <errno.h> +#include <termios.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> /* memset */ +#include <stdio.h> +#include <math.h> +#include <stdint.h> +#include <stdlib.h> +#include "../Include/serial.h" +#include "../Include/analogv.h" + +uint16_t cmd_analog_in_volt(int h,int pin_no) +{ + char pin[5]="A",v1[2]; + int stat; + int read[3]; + sprintf(v1,"%c",pin_no+48); + //strcpy(pin,"A"); + strcat(pin,v1); + int wr=write_serial(h,pin,2); + stat=status_serial(h,read); + //printf("Read:%d\n",read[0] ); + while (read[0] < 2) + stat=status_serial(h,read); + char values[5]; + int a_rd=read_serial(h,values,2); + values[2]='\0'; + printf("%s\n",values); + int l=strlen(values); + //printf("%d\n",l ); + uint8_t temp[l+1]; + int i; + for (i = 0; i < l; ++i) + { + temp[i]=(uint8_t)(values[i]); + } + //printf("%u %u\n",temp[0],temp[1]); + uint16_t result=(uint16_t)(256*temp[1]+temp[0]); + result/=1023; + //printf("%u\n",result); + return result; +} + +int cmd_analog_out_volt(int h,int pin_no,double val) +{ + char v1[2],v2[2]; + if(val > 5) + val = 5; + else if(val < 0) + val = 0; + val=val*255/5; + //printf("%f\n",val); + char code_sent[10]; + strcpy(code_sent,"W"); + sprintf(v1,"%c",pin_no+48); + sprintf(v2,"%c",abs(ceil(val))); + strcat(code_sent,v1); + strcat(code_sent,v2); + //printf("%s\n",code_sent); + return write_serial(h,code_sent,3); + +}
\ No newline at end of file |