The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 864 - File: showthread.php PHP 7.2.23 (Linux)
File Line Function
/showthread.php 864 errorHandler->error




Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mi first Pinguino program 16Ch 12 Bit AD to Serial
27-03-2012, 05:30 PM, (This post was last modified: 27-03-2012, 06:03 PM by moreno.)
#1
Mi first Pinguino program 16Ch 12 Bit AD to Serial
Hi

This is my first program for PIC32-PINGUINO-MICRO

Code:
/*
  Read16ChannelADC
  
  Read 2x8 12bit AD channels on 2 MCP3208
  Average on NUMSAMPLEAVG (now 4) lectures for each channel
  Create an ASCII HEX frame for the 16 channel
  Transmit continuously frame on Serial port (if DEBUG is defined even on CDC channel)
  27/03/2012 Ver. 1 Rev. 0 c. Moreno Manzini moreno at mediacom dot it
  
  Licence Creative Commons 3.0 http://creativecommons.org/licenses/by-sa/3.0/
  
  SPI routine inspired to http://www.arduino.cc/playground/Code/MCP3208
*/


//NOTE

// CDC.printf("%s",Str); go crazy if the length of the string is more than 64 Bytes

// Serial.xxx are alias of serial1xxx Es. Serial.begin serial1init@include <pinguinoserial1.c>

// Serial.printf is a blocking function



// Maximum transmission rate in mS
#define    TxRate_mS        100
#define    DEBUG            1
#define   NUMSAMPLEAVG    4

// the 2 MCP3208 share SELPIN, SPICLOCK and DATAOUT in this manner I read 2 channel at the same time
// These are only digital IO can be placed everywhere
#define SELPIN             16 //Selection Pin
#define SPICLOCK          17 //Clock
#define DATAOUT         18 //MOSI
#define DATAIN1          19 //MISO 1
#define DATAIN2          20 //MISO 2

// Only a remind of serial1 pinout
#define SERIAL1RX        11 //RX of Serial 1 (RD2)
#define SERIAL1TX        12 //TX of Serial 1 (RD3)

//PIC32-PINGUINO-MICRO GREEN LED
#define ledPinG            32 // the number of the Green LED pin


int Count = 0;
int CtnAVG = 0;
int TimemS0,TimemS1;
int Data[16][NUMSAMPLEAVG],Avg[16],CRCSUM;
char Str[200],Str1[20];

void setup()
{                
  memset(&Data,0,sizeof(Data));
//set pin modes
pinMode(ledPinG, OUTPUT);
pinMode(SELPIN, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN1, INPUT);
pinMode(DATAIN2, INPUT);
pinMode(SPICLOCK, OUTPUT);
//disable device to start with
digitalWrite(SELPIN,HIGH);
digitalWrite(DATAOUT,LOW);
digitalWrite(SPICLOCK,LOW);
digitalWrite(ledPinG,LOW);
//Initialize serial port to 9600Baud
Serial.begin(9600);
}

//Read 2 Channel of AD Converter uno for each MCP3208
void ReadChannels(int Ch, int * ValAD1, int * ValAD2)
{
int adcvalue1 = 0;
int adcvalue2 = 0;
int i;
byte commandbits = 0B11000000; //command bits - start, mode, chn (3), dont care (3)

//allow channel selection
commandbits |= ((Ch-1)<<3);

digitalWrite(SELPIN,LOW); //Select adc
// setup bits to be written
for (i=7; i>=3; i--)
  {
   digitalWrite(DATAOUT,commandbits&1<<i);
   //cycle clock
   digitalWrite(SPICLOCK,HIGH);
   digitalWrite(SPICLOCK,LOW);    
  }

digitalWrite(SPICLOCK,HIGH);    //ignores 2 null bits
digitalWrite(SPICLOCK,LOW);
digitalWrite(SPICLOCK,HIGH);  
digitalWrite(SPICLOCK,LOW);

//read bits from adc
for (i=11; i>=0; i--)
  {
   adcvalue1+=digitalRead(DATAIN1)<<i;
   adcvalue2+=digitalRead(DATAIN2)<<i;
   //cycle clock
   digitalWrite(SPICLOCK,HIGH);
   digitalWrite(SPICLOCK,LOW);
  }
digitalWrite(SELPIN, HIGH); //turn off device
*ValAD1 = adcvalue1;
*ValAD2 = adcvalue2;
}

void loop()
{
TimemS0 = millis();

//Read AD Converters
for (Count = 0 ; Count < 8 ; Count++)
  ReadChannels(Count,&Data[Count][CtnAVG],&Data[Count+8][CtnAVG]);
if (++CtnAVG >= NUMSAMPLEAVG)
  CtnAVG = 0;

//Calculate Average Data
for (Count = 0 ; Count < 16 ; Count++)
  Avg[Count] = (Data[Count][0]+Data[Count][1]+Data[Count][2]+Data[Count][3])/4;
  
//Generate and transmit Frame
CRCSUM = 0;
sprintf(Str,"#01-%4.4X,%4.4X,%4.4X,%4.4X,",Avg[0],Avg[1],Avg[2],Avg[3]);
for (Count = 0 ; Count < strlen(Str) ; Count++)    
  CRCSUM += Str[Count];
#ifdef DEBUG
CDC.printf("%s",Str);
#endif
Serial.printf("%s",Str);
sprintf(Str,"%4.4X,%4.4X,%4.4X,%4.4X,",Avg[4],Avg[5],Avg[6],Avg[7]);
for (Count = 0 ; Count < strlen(Str) ; Count++)    
  CRCSUM += Str[Count];
#ifdef DEBUG
CDC.printf("%s",Str);
#endif
Serial.printf("%s",Str);
sprintf(Str,"%4.4X,%4.4X,%4.4X,%4.4X,",Avg[8],Avg[9],Avg[10],Avg[11]);
for (Count = 0 ; Count < strlen(Str) ; Count++)    
  CRCSUM += Str[Count];
#ifdef DEBUG
CDC.printf("%s",Str);
#endif
Serial.printf("%s",Str);
sprintf(Str,"%4.4X,%4.4X,%4.4X,%4.4X",Avg[12],Avg[13],Avg[14],Avg[15]);
for (Count = 0 ; Count < strlen(Str) ; Count++)    
  CRCSUM += Str[Count];
#ifdef DEBUG
CDC.printf("%s",Str);
#endif
Serial.printf("%s",Str);
sprintf(Str1,"=%4.4X\r\n",CRCSUM);
#ifdef DEBUG
CDC.printf("%s",Str1);
#endif
Serial.printf("%s",Str1);

//Toggle Green LED
digitalwrite(ledPinG, digitalread(ledPinG) ^ 1);

//Total delay to TxRate_mS
#ifdef DEBUG
TimemS1 = millis() - TimemS0;
sprintf(Str1,"%4.4X\r\n",TimemS1);
CDC.printf("%s",Str1);
#endif
  
//If necessary delay to TxRate_mS
TimemS1 = millis() - TimemS0;
if (TimemS1 < TxRate_mS)
  delay(TxRate_mS - TimemS1);
}

I have not tried it yet because I just ordered the material for my new electronics lab.
When I will realize I will send the final version

Is not so important but I think it's a good idea sharing our projects.


Bye Bye, Moreno

Reply


Messages In This Thread
Mi first Pinguino program 16Ch 12 Bit AD to Serial - by moreno - 27-03-2012, 05:30 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)