Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:44 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Looking for code for MCP3208 8 channel ADC with SPI interface

Author Message
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 172
Posted: 07:29pm 10 Dec 2023
Copy link to clipboard 
Print this post

Dear all,
Does someone have code to access the MCP3208 chip via SPI from MMBasic?
Thanks!
 
oh3gdo
Regular Member

Joined: 18/11/2021
Location: Finland
Posts: 47
Posted: 08:33am 11 Dec 2023
Copy link to clipboard 
Print this post

karleich
I found CCS c-language code for MCP3208

// olympic++  adc de-noising routine for 10 or 12 bit ADC values
// NOTE: you must have selected the desired ADC channel before calling
// reads 16x , sorts and tosses low4 && high 4 readings
// then averages the middle 8

unsigned int16 adchlx(void){  // read 16 - sort, keep middle 8 average

unsigned int8 i;  unsigned int16 accum=0;
unsigned int16 s, b[16]; int1 didswap=1;

  for ( i = 0 ; i < 16;  i++ ) {
          b[i]= read_adc(ADC_start_and_read); // ADC set for 10 or 12 bits
          delay_us(8);
    } //    end of for loop for multi sample
  while(didswap){  // bubble sort
    didswap=0;
    for (i=0; i<15; i++){ // i 0-15
     if(b[(i)]>b[(i+1)]){ // if low element greater than next  -do  swap
          s=b[i]; // hold upper
          b[i]=b[(1+i)];
          b[(1+i)]=s;
          didswap=1;
     } // ~if
    } // ~for
   } // ~while
  // now sort and keep middle 8 values
  for (i=4; i<12; i++){  accum +=b[i];  }
  return(accum>>3);
}

I think it is not too difficult to change it to MMBASIC

The place is https://www.ccsinfo.com/forum/viewtopic.php?t=50320&highlight=mcp3208

Pekka Ritamaki
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 482
Posted: 09:03am 11 Dec 2023
Copy link to clipboard 
Print this post

maybe tweak the Basic examples here

( https://microcontrollershop.com/download/adc_examples.zip?osCsid=abnttkhk7ndtssls53d5vimaf4 )
Edited 2023-12-11 19:04 by zeitfest
 
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 172
Posted: 10:58pm 11 Dec 2023
Copy link to clipboard 
Print this post

Thanks @oh3gdo - I did find C code, too, but for some reason failed to adapt it to MMBasic, specifically the SPI communication.

Thanks @zeitfest - I’ll have a closer look.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024