Advertisement
jh_elec

Untitled

Jul 6th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. #include "SD_MAX.h"
  2. #include "diskio.h"
  3. #include "integer.h"
  4. #include "pff.h"
  5. #include "pffconf.h"
  6.  
  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #include <inttypes.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdint.h>
  14.  
  15.  
  16. /*if ((BAUD_ERROR>10)||(BAUD_ERROR <-10))
  17. {
  18. error "Zu hohe Baudrate";
  19. }*/
  20.  
  21. uint16_t highByte = 0;
  22. uint16_t lowByte = 0;
  23. uint16_t wert =0;
  24. uint16_t result = 0;
  25.  
  26. int T=0;
  27. int H=0;
  28. int Z=0;
  29. int E=0;
  30.  
  31.  
  32. //SPI-Konfiguration MAX6675
  33. void SPI_MasterInitMAX(void){
  34.     // Set MOSI and SCK output, all others input
  35.     DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS_M);
  36.     DDR_SPI &= ~(1<<DD_MISO); //MISO Eingang
  37.     SPI_PORT |= (1<<DD_SS_M);
  38.     SPI_PORT &=~(1<<DD_SCK)|(1<<DD_MISO);
  39.     //Enable SPI, Master, set clock rate fck/8, wegen 16MHZ MC und 4MHZ Max6675
  40.     SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0)|(1<<CPHA); //DORD Bit = 0 -> MSB wird zuerst übertragen; SPI Modus 1 (CPHA=1, CPOL=0)
  41.    
  42. }
  43.  
  44. //SPI-Uebertragung MAX
  45. char SPI_MasterTransmitAndReceive(char cdata)
  46. {
  47.     // Start transmission
  48.     SPDR = cdata;
  49.    
  50.     // Wait for transmission complete
  51.     while(!(SPSR & (1<<SPIF)));
  52.     return SPDR;
  53. }
  54.  
  55.  
  56. typedef struct
  57. {
  58.     #define NUM_OF_SENS     5
  59.    
  60.     char out[ NUM_OF_SENS * 7 ];
  61.     uint16_t temp[NUM_OF_SENS];
  62.    
  63. }sd_t;
  64.  
  65.  
  66. uint8_t tempToStr( sd_t *sd )
  67. {
  68.     char result[4] = "";
  69.    
  70.     memset( ( char* )sd->out , 0 , NUM_OF_SENS * 7 ); // Speicher löschen
  71.    
  72.     uint8_t x;
  73.     for( x = 0 ; x < NUM_OF_SENS ; x++ )
  74.     {
  75.         if( sd->temp[x] > 300 || sd->temp[x] == 0 ) // Fehler ist aufgetreten
  76.         {
  77.             return 1;
  78.         }
  79.        
  80.         strcat( sd->out , itoa( x + 1 , result , 10 ) );
  81.         strcat( sd->out , "#" );
  82.        
  83.         itoa( sd->temp[x] , result, 10 );
  84.         strcat( sd->out + strlen( sd->out ) , result );
  85.         strcat( sd->out , " " );      
  86.     }
  87.    
  88.     strcat( sd->out , "\r\n" );
  89.    
  90.     return 0;
  91. }
  92.  
  93. typedef enum
  94. {
  95.         MAX6675_THREE_STATE         = 1<<0,
  96.         MAX6675_ID                  = 1<<1,
  97.         MAX6675_THERMOCOUPLE_INPUT  = 1<<2,
  98. }max6675_state;
  99.  
  100. max6675_state max6675getState( uint8_t in )
  101. {
  102.     return ( in & ( MAX6675_THREE_STATE | MAX6675_ID ) | MAX6675_THERMOCOUPLE_INPUT );
  103. }
  104.  
  105. void calcSaveTemp( uint8_t highByte , uint8_t lowByte , uint8_t sensNum )
  106. {
  107.     uint16_t tempAD = ( uint16_t ) highByte << 5 | ( uint16_t ) lowByte >> 3;
  108.     tempAD >>= 2;
  109.    
  110.    
  111.     sd_t s;
  112.     s.temp[ sensNum ] = tempAD;
  113.        
  114.     tempToStr( &s );
  115. }
  116.  
  117. int main(void)
  118. {
  119.     FATFS fs;
  120.     //FRESULT res;
  121.     SPI_MasterInitMAX();
  122.  
  123.     disk_initialize();  
  124.     pf_mount(&fs); //mount the volume
  125.     pf_open("Daten.txt"); //open a file    
  126.    
  127.     while(1)
  128.     {
  129.         _delay_ms(200); //unter 200 ms sendet MAX nichts mehr
  130.         PORTB &= ~(1<<2); // CS Max low
  131.         _delay_ms(1);
  132.         highByte = SPI_MasterTransmitAndReceive(0x03);
  133.         lowByte = SPI_MasterTransmitAndReceive(0x03);
  134.         PORTB |= (1<<2); // CS Max high
  135.        
  136.         if( max6675getState( lowByte ) & MAX6675_THERMOCOUPLE_INPUT )
  137.         {
  138.             // Fühler Fehler
  139.         }
  140.  
  141.         calcSaveTemp( highByte , lowByte , 0 );
  142.        
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement