A to D Code.

All about Sub-20 Multi Interface USB Adapter USB to I2C, SPI, GPIO, RS232, RS485, Ir, LCD

Moderator: serg

Post Reply
kovelan
Posts: 3
Joined: Sun Oct 11, 2009 10:41 am

A to D Code.

Post by kovelan »

Maybe this should be posted in "Application" forum? --kovelan

Code: Select all

/*---------------------------------------------------------------------------
 *   adn.c -- sub20 A to D ncurses __HACK__                                  
 *                                                                           
 *   by kovelan (kovelan@yahoo.com)                                          
 *                                                                           
 *   GROUND ALL UNUSED ANALOG INPUTS via 10k pulldown resistor.              
 *   ADC inputs are positive only and up to VCC or Vref if less              
 *   than VCC.  Analog scalers are needed if higher voltages are needed.     
 *                                                                           
 *   NOTE: very ALPHA code, no warranty or libality if damage occurs         
 *   to your systems.  Your on your own...                                   
 *                                                                           
 *   gcc -o adn adn.o -lsub -lncurses -L../lib -L/usr/local/lib -lusb        
 *   This should get the job done. Adjust to your build env.                 
 *--------------------------------------------------------------------------*/
/*                                                                            
 * ---------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 4.3):                                    
 * <kovelan@yahoo.com> wrote this file. As long as you retain this notice you 
 * can do whatever you want with this code. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer (or more) in return.         
 * ---------------------------------------------------------------------------
 */                                                                           
#include <stdio.h>                                                            
#include <stdlib.h>                                                           
#include <string.h>                                                           
#include <ctype.h>                                                            
#include <ncurses.h>                                                          
#include <unistd.h>                                                           

#include <libsub.h>

#include "cmd_pars.h"
#include "sub_app.h" 

/* make everything global */
sub_handle       *fd;       
int              rc=0;      
struct           usb_device *dev;

int              gain=1,scale=1023;
int              adc;              
double           Vref = 5.00;  /* normally used */
//double           Vref = 3.30;                   

/* ncurses globals */
int              current_getch;
int              doloop = 1;   
static           WINDOW *mainwnd;
static           WINDOW *screen; 

void screen_init(void) 
{                      
    mainwnd = initscr();
    noecho();           
    cbreak();           
    nodelay(mainwnd, TRUE);
    refresh();             
    wrefresh(mainwnd);     
    screen = newwin(16, 30, 1, 25);
    box(screen, ACS_VLINE, ACS_HLINE);
}                                     

/* only using 3 channels, easly expandable to more.  this was for use
 * with an accelerometer, so the X,Y and Z axis.                     
 */                                                                  
static void update_display(void)                                     
{                                                                    
    curs_set(0);                                                     
    mvwprintw(screen,1,1,"---------- AtoD ----------");              

    sub_adc_single( fd, &adc, ADC_S2 );
    mvwprintw(screen, 3,6,"Ch2(X):   %1.5fV",  Vref * adc/scale/gain );

    sub_adc_single( fd, &adc, ADC_S1 );
    mvwprintw(screen, 4,6,"Ch1(Y):   %1.5fV",  Vref * adc/scale/gain );

    sub_adc_single( fd, &adc, ADC_S0 );
    mvwprintw(screen, 5,6,"Ch0(Z):   %1.5fV",  Vref * adc/scale/gain );

    mvwprintw(screen,10,6,"PRESS q TO END");
    mvwprintw(screen,12,1,"---------- AtoD ----------");

    wrefresh(screen);
    refresh();       
}                    

void screen_end(void) 
{                     
    endwin();         
}                     

/*--------------------------------------------------------------------
 *   main function()                                                  
 *------------------------------------------------------------------*/
int main( int argc, char* argv[] )                                    
{                                                                     
/*--------------------------------------------------------------------
 *    find USB functions()                                            
 *------------------------------------------------------------------*/
    /* Find USB device */                                             
    dev = sub_find_devices( 0 );                                      
    if( !dev )                                                        
    {                                                                 
        printf("\nError: sub_find_devices()\n");                      
        exit(0);                                                      
    }                                                                 

    while( sub_find_devices(dev) );

    /* Open USB device */
    fd = sub_open( 0 );  
    if( !fd )
    {
        printf("\nError: sub_open()\n");
        exit(0);
    }
/*--------------------------------------------------------------------
 * adc functions()
 *------------------------------------------------------------------*/
    config.adc_cfg|=ADC_ENABLE;
    rc = sub_adc_config( fd, config.adc_cfg );
    if( !fd )
    {
        //endwin();
        printf("\nError: sub_adc_config(ADC_ENABLE)\n");
        exit(0);
    }

    config.adc_cfg|=ADC_REF_VCC;
    rc = sub_adc_config( fd, config.adc_cfg );
    if( !fd )
    {
        //endwin();
        printf("\nError: sub_adc_config(ADC_REF_VCC)\n");
        exit(0);
    }
/*--------------------------------------------------------------------
 * ncurses functions()
 *------------------------------------------------------------------*/
    screen_init();

    while (doloop) {
      current_getch = getch();
      if (current_getch == 113) {
         doloop = 0;
      }
      update_display();
      //usleep(500000);
        usleep(50000);
   }
   screen_end();
   printf("\nTEST ENDS\n");

   return 0;
} /************************* end of main() **************************/


xol
Site Admin
Posts: 241
Joined: Sat Aug 29, 2009 8:04 am

Re: A to D Code.

Post by xol »

HI,
Can you tell us please what exactly this code is doing? Maybe a screen shoot.
Thanks.

kovelan
Posts: 3
Joined: Sun Oct 11, 2009 10:41 am

Re: A to D Code.

Post by kovelan »

In a nutshell, it as to read the 3 AtoD channels from an accelerometer (x, y and z), sleep() and start over using NCURSES
style display. (Ncurses makes for a nice text display. So, you must have ncurses installed on your system.)

Code is very adaptable to a more generic "read as needed" by adding or subtracting how many channels you need by
changing the update_display() function. Found out it is _important_ to have all unused analog inputs grounded with
a 10k pull-down resistor.

here is some non-NCURSES AtoD code. This allows one to choose which channel to read from the command line, but only
one channel at a time. (ie: "adc ADC_S0" for the first channel, "adc ADC_S7" for the last channel. (again alpha/beer code)

Code: Select all

/*-------------------------------------------------------------------   
*   adc.c -- test adc                                                  
*                                                                       
*                                                                       
*-------------------------------------------------------------------*/  
#include <stdio.h>                                                      
#include <stdlib.h>                                                     
#include <string.h>                                                     
#include <ctype.h>                                                      

#include <libsub.h>

#include "cmd_pars.h"
#include "sub_app.h" 

sub_handle  *fd;

/*--------------------------------------------------------------------
 *   main function()                                                  
 *------------------------------------------------------------------*/
int main( int argc, char* argv[] )                                    
{                                                                     
    int         adc, gain=1,scale=1023;                               
    int         rc=0;                                                 
    struct      usb_device *dev;                                      

    if ( argc != 2 ) 
    {                
        printf("\nADC: must have 2 command line args.");
        printf("\nExample:  \"adc x\"   x = ADC_S0 (0 to 7).\n\n");
        exit (0);                                                  
    }                                                              


/*--------------------------------------------------------------------
 *    find USB functions()                                            
 *------------------------------------------------------------------*/
    /* Open USB device */                                             
    dev = sub_find_devices( 0 );                                      
    if( !dev )
    {
        printf("sub_find_devices: %s\n", sub_strerror(sub_errno));
        return -1;
    }

    while( sub_find_devices(dev) );

    /* Open USB device */
    fd = sub_open( 0 );
    if( !fd )
    {
        printf("sub_open: %s\n", sub_strerror(sub_errno));
        return -1;
    }

/*--------------------------------------------------------------------
 * adc functions()
 *------------------------------------------------------------------*/

    config.adc_cfg|=ADC_ENABLE;
    rc = sub_adc_config( fd, config.adc_cfg );
    if( !fd )
    {
        printf("sub_open: %s\n", sub_strerror(sub_errno));
        return -1;
    }

    config.adc_cfg|=ADC_REF_2_56;  /* NOTE: 2.56v reference */
    rc = sub_adc_config( fd, config.adc_cfg );
    if( !fd )
    {
        printf("sub_open: %s\n", sub_strerror(sub_errno));
        return -1;
    }

    sub_adc_single( fd, &adc, argv[1] );
    printf( "\n%s:      %1.4fV\n\n", argv[1], 2.56 * adc/scale/gain );

    return 0;

}  /************************* end of main() **************************/


Post Reply