Go Down

Topic: Read pressure and temperature on SPI (Read 52 times) previous topic - next topic

Baptiste_39

Hello everyone,



I am a beginner and wonder how can I solve this problem.
It is in the context of a TP at the university:

"
Write the program that reads the atmospheric pressure and the temperature on the pmodNav SPI module. This program will send these values on the serial link."

that's what I did :

Code: [Select]



#include <Arduino.h>
#include <SPI.h>

//Déclaration des différentes variables pour des types entiers non signé de longueur 8,16 et 32
uint8_t octet1;
uint8_t octet2;
uint8_t octet3;
uint16_t mot16;
uint32_t mot24;

void setup()
{
 //Définition du débit de données en bits par seconde (bauds)
 Serial.begin(115200);
 //Configuration de la broche 15 en sortie
 pinMode(15,OUTPUT);
 //Initialisation du bus SPI
 SPI.begin();
 //Ecris une valeur LOW sur la broche 15
 digitalWrite(15, LOW);
 //Envoi des données sur la broche 0x21 et 0x08
 SPI.transfer(0X21);
 SPI.transfer(0X08);
 //Ecris une valeur HIGH sur la broche 15
 digitalWrite(15,HIGH);
 //indiquer dans le code dans quel sens seront envoyés les bits de données de chaque octet, le bit de poids fort (MSB) ou celui de poids faible (LSB) en premier
 digitalWrite(15, LOW);
 SPI.transfer(0X10);
 SPI.transfer(0X0A);
 digitalWrite(15,HIGH);
 
 digitalWrite(15, LOW);
 SPI.transfer(0X20);
 SPI.transfer(0X80);
 digitalWrite(15,HIGH);
}




void loop() {


  //Ecris une valeur LOW sur la broche 15
  digitalWrite(15,LOW);
  //Dis au SPI que nous demandons des données au registre 0x28
  SPI.transfer(0X28);
  //le SPI renvoie les données et les stocke dans l'octet octet1
  octet1 = SPI.transfer(0);
  
  //Dis au SPI que nous demandons des données au registre 0x29
  SPI.transfer(0X29);
  //le SPI renvoie les données et les stocke dans l'octet octet1
  octet2 = SPI.transfer(0);
  
   //Dis au SPI que nous demandons des données au registre 0x2A
  SPI.transfer(0X2A);
   //le SPI renvoie les données et les stocke dans l'octet octet3
  octet3 = SPI.transfer(0);
  //Désélection du SPI lors de la finalisation des séquences
  digitalWrite(15,HIGH);
  
  //Affectaction des trois octets dans la variable mot24
  mot24 = (octet1 << 16) | (octet2 << 8) | octet3;
  int pression = mot24/4096;
  //Affichage de la pression
  Serial.println(pression);

  digitalWrite(15,LOW);
  SPI.transfer(0X2B);
  octet1 = SPI.transfer(0);
  SPI.transfer(0X2C);
  octet2 = SPI.transfer(0);
  digitalWrite(15,HIGH);
  mot16 = (octet1 << 8) | octet2;
  //Affichage de la température à l'aide de la formule
  int temp = (mot16/480) + 42.5;
  Serial.println(temp);
  delay(3000);



}
/*
//------------------------Empreinte mémoire---------------------
   DATA:     [    ] 4.0%   (used 13096 bytes from 327680 bytes)
   PROGRAM:  [==  ] 15.6%  (used 204976 bytes from 1310720 bytes)
*/




unfortunately he does not show me bad values and I do not understand why ... would I miss a loop or an instruction?

Klaus_K

Could you please let us know what hardware you are using and what you know about the affected hardware?

- What Arduino board are you using?
- Do you have a link to the data sheet of the module?
- Do you know what SPI is, how it is connected?
- Did you already test some signals? ...

First, we need to make sure you wired the system correctly. Maybe, this was already done for you?

What did you see in the Serial Monitor? Did you try 9600 baud in case you did not get any output?

The software does not seem to have any obvious issues. Which is a good start. :)


Regarding the software, you added a lot of comments in French. We might know what your code is doing but we do not know what you think it's doing. I personally use only English for programing, even tough its not my native language, but it makes it easier for people to read when I ask for help. :)

Go Up