Advertisement
m33600

spi to i2c - lado slave - post

May 16th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. // Gravado num Arduino Pro Mini
  2. // spi to i2c - lado slave
  3. // www.copacabana.io
  4. // work in progress. Editarei esse pastebin atualizando as funções
  5.  
  6. //-----------------preparacao do sensor tipo K SPI
  7. #include "max6675.h"
  8. int ktcSO = 12;
  9. int ktcCS = 10;
  10. int ktcCLK = 13;
  11. MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
  12. //_________________________________________________
  13.  
  14. #include <Wire.h>
  15. #include <SPI.h> // made no diference!
  16.  
  17. #define XSensorPin A1
  18. #define YSensorPin A2
  19.  
  20. const byte SlaveDeviceId = 1;
  21. byte LastMasterCommand = 0;
  22.  
  23. int temperatura;  // é float, mas só quero ler inteiro (1 byte)
  24.  
  25. void softReset() { asm volatile ("  jmp 0"); }
  26. void pisca(){
  27.      digitalWrite(13,HIGH);
  28.    delay(50);
  29.    digitalWrite(13,LOW);
  30. }
  31. void setup(){
  32.   pinMode(13,OUTPUT);
  33.  
  34.   Serial.begin(9600);
  35.   Serial.println("boot");
  36.   Wire.begin(SlaveDeviceId);      // join i2c bus with Slave ID
  37.   Wire.onReceive(receiveCommand); // register talk event
  38.   Wire.onRequest(slavesRespond);  // register callback event
  39.  
  40.   pinMode(XSensorPin, INPUT);
  41.   pinMode(YSensorPin, INPUT);
  42. }
  43.  
  44.  
  45. void lerSPI(){
  46.     temperatura = 0;
  47.       // basic readout test over SPI  
  48.    Serial.print("Leitura local (slave) Deg C = ");
  49.    temperatura = (int) (ktc.readCelsius());
  50.    Serial.println(temperatura);
  51.  
  52.    delay(500);
  53.  
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. void loop(){
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.   delay(100);
  77. }
  78.  
  79. void receiveCommand(int howMany){
  80.   LastMasterCommand = Wire.read(); // 1 byte (maximum 256 commands)
  81. }
  82.  
  83. void slavesRespond(){
  84.  
  85.   int returnValue = 0;
  86.  
  87.   switch(LastMasterCommand){
  88.     case 0:   // No new command was received
  89.       Wire.write("NA");
  90.                           delay(100);
  91.     break;
  92.    
  93.     case 1:   // Return X sensor value
  94.       returnValue = GetXSensorValue();
  95.    // softReset();
  96.                            delay(100);
  97.  
  98.     break;
  99.  
  100.     case 2:   // Return Y sensor value
  101.       returnValue = GetYSensorValue();
  102.                           delay(100);
  103.  
  104.     break;
  105.  
  106.    
  107.   }
  108.  
  109.   byte buffer[2];                 // split int value into two bytes buffer
  110.   buffer[0] = returnValue >> 8;
  111.   buffer[1] = returnValue & 255;
  112.   Wire.write(buffer, 2);          // return response to last command
  113.   LastMasterCommand = 0;          // null last Master's command
  114. }
  115.  
  116. int GetXSensorValue(){
  117.   int val = (int) (ktc.readCelsius()); //analogRead(XSensorPin);
  118.   Serial.print(val);
  119.   Serial.println("  leitura local");
  120.   Serial.println(ktc.readCelsius());
  121.   return val;
  122. }
  123.  
  124. int GetYSensorValue(){
  125.   int val = analogRead(YSensorPin);
  126.   return val;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement