RetroBSD

2.11BSD operating system for microcontrollers
It is currently Mon Oct 14, 2019 10:32 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Dec 12, 2013 11:39 am 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Hi, this is a simple adc.h lib for simple adc() handling with uecide.
Based on adcdemo.c
Code:
// ADC demo
// Pito 12/2013
// based on adcdemo, but simpler

#include "adc.h"

int main()
{
   int i, numb;

   for (i=0; i<6; i++){
      if ( adcinit(i) < 0 ) return (66);
      numb = adcread(i);
      printf("adc%d %4d  \n", i, numb);
      printf("adc%d %4d  \n", i, adc[i].value);
      adcclose(i);
   }
   return 0;
}


adc.h
Code:
// adc.h
// small lib for adcinit(), adcread(), adcclose()
// based on adcdemo.c
// Pito 12/2013

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/adc.h>
#include <unistd.h>

struct adc_channel {
   int fd;
   int value;
};

struct adc_channel adc[16];

int adcinit( int i) {
   char temp[100];
   long flags;
   sprintf(temp,"/dev/adc%d",i);
   adc[i].fd = open(temp,O_RDWR);
   if(!adc[i].fd){
      printf("Error: unable to open %s\n",temp);
      return (-1);
   }
   fcntl(adc[i].fd,F_GETFD,&flags);
   flags |= O_NONBLOCK;
   fcntl(adc[i].fd,F_SETFD,&flags);
   return 0;
}

void adcclose( int i){
   close(adc[i].fd);
}

int adcread( int i) {
   char temp[10];
   int val = -1;
   if(read(adc[i].fd,temp,20)){
      if(sscanf(temp,"%d",&val)){
         adc[i].value = val;
      }
   }
   return val;
}

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron




Powered by phpBB® Forum Software © phpBB Group

BSD Daemon used with permission