Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SD_MAX.h"
- #include "diskio.h"
- #include "integer.h"
- #include "pff.h"
- #include "pffconf.h"
- #include <avr/io.h>
- #include <util/delay.h>
- #include <inttypes.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdint.h>
- /*if ((BAUD_ERROR>10)||(BAUD_ERROR <-10))
- {
- error "Zu hohe Baudrate";
- }*/
- uint16_t highByte = 0;
- uint16_t lowByte = 0;
- uint16_t wert =0;
- uint16_t result = 0;
- int T=0;
- int H=0;
- int Z=0;
- int E=0;
- //SPI-Konfiguration MAX6675
- void SPI_MasterInitMAX(void){
- // Set MOSI and SCK output, all others input
- DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS_M);
- DDR_SPI &= ~(1<<DD_MISO); //MISO Eingang
- SPI_PORT |= (1<<DD_SS_M);
- SPI_PORT &=~(1<<DD_SCK)|(1<<DD_MISO);
- //Enable SPI, Master, set clock rate fck/8, wegen 16MHZ MC und 4MHZ Max6675
- 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)
- }
- //SPI-Uebertragung MAX
- char SPI_MasterTransmitAndReceive(char cdata)
- {
- // Start transmission
- SPDR = cdata;
- // Wait for transmission complete
- while(!(SPSR & (1<<SPIF)));
- return SPDR;
- }
- typedef struct
- {
- #define NUM_OF_SENS 5
- char out[ NUM_OF_SENS * 7 ];
- uint16_t temp[NUM_OF_SENS];
- }sd_t;
- uint8_t tempToStr( sd_t *sd )
- {
- char result[4] = "";
- memset( ( char* )sd->out , 0 , NUM_OF_SENS * 7 ); // Speicher löschen
- uint8_t x;
- for( x = 0 ; x < NUM_OF_SENS ; x++ )
- {
- if( sd->temp[x] > 300 || sd->temp[x] == 0 ) // Fehler ist aufgetreten
- {
- return 1;
- }
- strcat( sd->out , itoa( x + 1 , result , 10 ) );
- strcat( sd->out , "#" );
- itoa( sd->temp[x] , result, 10 );
- strcat( sd->out + strlen( sd->out ) , result );
- strcat( sd->out , " " );
- }
- strcat( sd->out , "\r\n" );
- return 0;
- }
- typedef enum
- {
- MAX6675_THREE_STATE = 1<<0,
- MAX6675_ID = 1<<1,
- MAX6675_THERMOCOUPLE_INPUT = 1<<2,
- }max6675_state;
- max6675_state max6675getState( uint8_t in )
- {
- return ( in & ( MAX6675_THREE_STATE | MAX6675_ID ) | MAX6675_THERMOCOUPLE_INPUT );
- }
- void calcSaveTemp( uint8_t highByte , uint8_t lowByte , uint8_t sensNum )
- {
- uint16_t tempAD = ( uint16_t ) highByte << 5 | ( uint16_t ) lowByte >> 3;
- tempAD >>= 2;
- sd_t s;
- s.temp[ sensNum ] = tempAD;
- tempToStr( &s );
- }
- int main(void)
- {
- FATFS fs;
- //FRESULT res;
- SPI_MasterInitMAX();
- disk_initialize();
- pf_mount(&fs); //mount the volume
- pf_open("Daten.txt"); //open a file
- while(1)
- {
- _delay_ms(200); //unter 200 ms sendet MAX nichts mehr
- PORTB &= ~(1<<2); // CS Max low
- _delay_ms(1);
- highByte = SPI_MasterTransmitAndReceive(0x03);
- lowByte = SPI_MasterTransmitAndReceive(0x03);
- PORTB |= (1<<2); // CS Max high
- if( max6675getState( lowByte ) & MAX6675_THERMOCOUPLE_INPUT )
- {
- // Fühler Fehler
- }
- calcSaveTemp( highByte , lowByte , 0 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement