/************************************************************************
*
* Test of the Pmod
*
*************************************************************************
* Description: Pmod_DA2
* The output voltage of the Pmod is chosen by the user from the
* Serial monitor
*
*
* Material
* 1. Arduino Uno
* 2. Pmod DA2
*
************************************************************************/
#define CS 10 // affectation of CS pin
#include <SPI.h> // Include library
char tableau[4] = {0,0,0,0};
int i = 0;
long valeur;
long consigne;
int consigne_basse;
int consigne_haute;
int val=0;
float tension;
void setup()
{
Serial.begin(115200); // initialization of serial communication
SPI.begin(); // initialization of SPI port
SPI.setDataMode(SPI_MODE3); // configuration of SPI communication in mode 3
SPI.setClockDivider(SPI_CLOCK_DIV16); // configuration of clock at 1MHz
pinMode(CS, OUTPUT);
}
void loop()
{
Serial.println("Enter the voltage in mV on 4 digits then Send");
while (Serial.available()==0); // wait for data
for(i = 0;i<4;i++) // Loop to write data in a table
{
tableau[i]=Serial.read();
delay(1);
}
delay(100);
// recomposition du nombre tension
valeur=1000*(tableau[0]-48)+100*(tableau[1]-48)+10*(tableau[2]-48)+(tableau[3]-48);
consigne=4095*valeur;
consigne=consigne/5000;
consigne_haute=consigne>>8; // order high get 4 high bits order
consigne_basse=consigne&0XFF; // order low get 8 low bits order
digitalWrite(CS, LOW); // activation of the CS line
SPI.transfer(consigne_haute); // Send order
SPI.transfer(consigne_basse);
delay(5);
digitalWrite(CS, HIGH); // deactivation of CS line
delay(100);
Serial.print("La tension de consigne est : ");
Serial.print(valeur);
Serial.println(" mV");
}
Comments
Please log in or sign up to comment.