Quantcast
Channel: Data converters
Viewing all articles
Browse latest Browse all 88365

Forum Post: working C code for LDC1000 EVM

$
0
0

I thought maybe this would be instructive, I coded a few routines that access the LDC1000 EVM.

I'm not fluent in C but that also means it is very simple & straightforward coding. improve at will ....

please note that some registers need to be written in sequence which means that writing the first register will  report an error (because the value is only written when the next register is changed). e.g. register 6 and 7.

this code compiled on ubuntu linux with "gcc ldcreader.c", the output is an a.out file that can be run. you might need extra privileges to access /dev/ttyACM0. I ran the program under sudo (which is generally a bad idea, I know..).

#include <stdio.h>  #define LDC_READ 3 #define LDC_WRITE 2 #define LDC_FIRMWARE 9 #define LDC_STREAM_ON 6 #define LDC_STREAM_OFF 7  FILE *fp; char hex[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};   int getval(c) char c; {  int result = -1;         int i ;  for (i = 0; i<16; i++) {  if (c == hex[i]) result = i ;  }  return result; }  write_ldc_cmd(comm,lngth) unsigned char *comm; int lngth; {  if ( fp != NULL) {  fwrite(comm,1,lngth,fp);  } }  read_ldc_resp(res,lngth) unsigned char *res; {  if ( fp != NULL) {  fread(res,1,lngth,fp);  } }  ldc_cmd(cm,reg,val,resp) char cm,reg,val; unsigned char *resp; {         int i = 0 ;  unsigned char  cmd[8];  cmd[0]= hex[cm/16];  cmd[1]= hex[cm%16];  cmd[2]= hex[reg/16];  cmd[3]= hex[reg%16];  cmd[4]= hex[val/16];  cmd[5]= hex[val%16];         cmd[6]= '0';  cmd[7]= '0';   for (i = 0; i<32; i++) resp[i] = '0';   write_ldc_cmd(cmd,8);  read_ldc_resp(resp,32); }  unsigned int ldc_get_firmware() {   unsigned char resp[32];   unsigned int i = 0;   printf("%d \n",ldc_cmd(LDC_FIRMWARE,0,0,resp)) ;   for (i=0; i<32; i++) {  printf(" %d",resp[i]);   }   printf("\n");   i  = resp[8];   i *= 256;   i += resp[9];   i *= 256;   i += resp[10];   i *= 256;   i += resp[11];     return (i); }  unsigned char ldc_read_reg(reg) unsigned char reg; {   unsigned char resp[32];   unsigned int i = 0;   ldc_cmd(LDC_READ,reg,0,resp) ;   for (i=0; i<32; i++) {  printf(" %d",resp[i]);   }   printf("\n");   i  = resp[8];   return (i); }  ldc_write_reg(reg,val) unsigned char reg, val; {   unsigned char resp[32];   unsigned int i = 0;   ldc_cmd(LDC_WRITE,reg,val,resp);   for (i=0; i<32; i++) {  printf(" %d",resp[i]);   }   printf("\n");   i  = resp[8];   if (i != val) printf(" return %d is not same as %d \n", i,val); }  ldc_stream_start() {   unsigned char resp[32];   unsigned int i = 0;   ldc_cmd(LDC_STREAM_ON,0,0,resp);    for (i=0; i<32; i++) {  printf(" %d",resp[i]);   }   printf("\n"); }  ldc_stream_stop() {   unsigned char resp[32];   unsigned int i = 0;   ldc_cmd(LDC_STREAM_OFF,0,0,resp);    for (i=0; i<32; i++) {  printf(" %d",resp[i]);   }   printf("\n"); }  ldc_stream_read() {   unsigned char data[2048];   unsigned int i = 0;   unsigned long sum = 0;   float avg = 0;   read_ldc_resp(data,2048);   for (i=0; i<2048; i++) {     if (i%2 == 0 && i < 1024) sum += (data[i]*256+data[i+1]);     //if (i%32 == 0) printf("\n %d >",i);     //printf(" %d",data[i]);   }   printf("%f ", sum/512.0);   printf("\n"); }  main() {  int i = 0 ;         unsigned char result = 0;   fp = fopen("/dev/ttyACM0","r+");  if (fp == NULL) printf("open failed\n"); else {    printf("read regs\n");    ldc_stream_start();           for (i=0; i<1000; i++) {            ldc_stream_read();           }           ldc_stream_stop();  }  fclose(fp);  printf("done\n"); }


Viewing all articles
Browse latest Browse all 88365

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>