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
27-03-2012, 05:54 PM,
#2
RE: Mi first Pinguino program 16Ch 12 Bit AD to Serial
I think
byte commandbits = B11000000;

fails because I'm guessing you haven't defined B11000000

Note: if you meant a binary constant (as I think you do) it could not start B because that makes it an identifier name.

I can't recall if 0B11000000 is legal. Try it Smile

John
Reply
27-03-2012, 06:06 PM,
#3
RE: Mi first Pinguino program 16Ch 12 Bit AD to Serial
Hi

OK problem solved, I took the sample from a Arduino page and I not thought at the 0.

Thanks, now I corrected the program.


Bye Bye, Moreno

Reply
29-01-2013, 09:33 PM,
#4
RE: Mi first Pinguino program 16Ch 12 Bit AD to Serial
Hi

DONE!!!!

The prototype work perfectly, now is ready for production.

After a lot of years, I have restart design printed circuit boards with KiCAD (before I had worked with Orcad for DOS).

Thanks to all PINGUINO peoples and thanks to OLIMEX for the great PINGUIINO32-MICRO, I am reborn in a new age.


Bye Bye, Moreno


Attached Files Thumbnail(s)
   
Reply
30-01-2013, 12:26 PM,
#5
RE: Mi first Pinguino program 16Ch 12 Bit AD to Serial
Congratulation !
What is your board for ?


(29-01-2013, 09:33 PM)moreno Wrote: Hi

DONE!!!!

The prototype work perfectly, now is ready for production.

After a lot of years, I have restart design printed circuit boards with KiCAD (before I had worked with Orcad for DOS).

Thanks to all PINGUINO peoples and thanks to OLIMEX for the great PINGUIINO32-MICRO, I am reborn in a new age.


Bye Bye, Moreno
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
30-01-2013, 01:34 PM,
#6
RE: Mi first Pinguino program 16Ch 12 Bit AD to Serial
Hi

Is a network of boards connected trough a RS485 bus to a PLC WAGO which collect the level signal in tanks.
Do this directly with WAGO modules is too expensive (about 720€ for 16 channels, I need minimum 30 channels), with PINGUINO I do all with 60€.


Bye Bye, Moreno
Reply
05-09-2013, 11:04 AM, (This post was last modified: 05-09-2013, 11:05 AM by moreno.)
#7
RE: Mi first Pinguino program 16Ch 12 Bit AD to Serial
Hi

After one month working 24/24 hour, I can say PASSED.
The customer is very happy and no problems are happened.


This is a picture of the plant, are more expensive the boxes than the boards.


Bye Bye, Moreno


Attached Files Thumbnail(s)
   
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)