Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.59 KB | None | 0 0
  1. #include "config.h"
  2.  
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5. #include <avr/interrupt.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9. #include "OneWire.h"
  10. #include "LCD.h"
  11.  
  12. //#define F_CPU 8000000UL
  13.  
  14. #define MOSI 3
  15. #define MISO 4
  16. #define SCK 5
  17. #define SS 2
  18.  
  19. #define SPE 6
  20. #define DORD 5
  21. #define MSTR 4
  22. #define CPOL 3
  23. #define CPHA 2
  24. #define SPR1 1
  25. #define SPR0 0
  26. #define SPIF 7
  27.  
  28. inline void explodeDoubleNumber(int* numbers, double flt) {
  29.     numbers[0] = abs((int) flt);
  30.     numbers[1] = abs((int) ((flt - ((int) flt)) * 10));
  31. }
  32.  
  33. inline void printTemp(double d, uint8_t i) {
  34.     char text[17] = "T[";
  35.     int fs[2];
  36.     char num[5];
  37.    
  38.     itoa(i, num, 10);
  39.     strcat(text, num);
  40.     strcat(text, "]=");
  41.    
  42.     explodeDoubleNumber(fs, d);
  43.     if (d < 0) {
  44.         strcat(text, "-");
  45.     }
  46.     itoa(fs[0], num, 10);
  47.     strcat(text, num);
  48.     strcat(text, ".");
  49.     itoa(fs[1], num, 10);
  50.     strcat(text, num);
  51.     strcat(text, "'C");
  52.     lcdClear();
  53.     lcdGotoXY(0, 0);
  54.     lcdPuts(text);
  55. }
  56.  
  57. double getTemp(uint64_t ds18b20s) {
  58.     uint8_t temperatureL;
  59.     uint8_t temperatureH;
  60.     double retd = 0;
  61.     setDevice(ds18b20s);
  62.     writeByte(CMD_CONVERTTEMP);
  63.     _delay_ms(750);
  64.     setDevice(ds18b20s);
  65.     writeByte(CMD_RSCRATCHPAD);
  66.     temperatureL = readByte();
  67.     temperatureH = readByte();
  68.     retd = ((temperatureH << 8) + temperatureL) * 0.0625;
  69.     return retd;
  70. }
  71.  
  72. uint8_t getTemp8(char LH) {    //uint8_t getTemp8(uint64_t ds18b20s, char LH) {
  73.     uint8_t temperatureL;
  74.     uint8_t temperatureH;
  75.     //setDevice(ds18b20s);
  76.     skipRom();
  77.     writeByte(CMD_CONVERTTEMP);
  78.     _delay_ms(750);
  79.     //setDevice(ds18b20s);
  80.     skipRom();
  81.     writeByte(CMD_RSCRATCHPAD);
  82.     temperatureL = readByte();
  83.     temperatureH = readByte();
  84.     if(LH == 'l'){
  85.         return temperatureL;
  86.         } else if (LH == 'h'){
  87.         return temperatureH;
  88.     }
  89.     return 0;
  90. }
  91.  
  92. void SPI_SLave_init(void)
  93. {
  94.     DDRB &= ~(1<<SCK)|~(1<<MOSI)|~(1<<SS);//MISO как вход
  95.     DDRB |=(1<<MISO);
  96.     SPCR=0;//Обнулить регистр SPCR
  97.     SPSR=0;//Обнулить регистр SPSR
  98.     SPCR &= ~(1<<MSTR); //режим слейв?
  99.     SPCR |= (1<<SPIE);
  100.     //SPCR |= (1<<SPI2X);
  101.     SPCR |= (1<<CPOL);
  102.     SPCR |= (1<<CPHA);
  103.     SPCR |= (1<<SPR0);//не надо
  104.     SPCR |= (1<<SPR1);//не надо
  105.     //SPCR |= (1<<DORD);
  106.     SPCR &=~ (1<<DORD);
  107.     SPCR |=(1<<SPE);
  108. }
  109.  
  110. uint8_t SPI_Slave_Receive(void)
  111. {
  112.     while(!(SPSR&(1<<SPIF)));/* Ожидание завершения приема: */
  113.     return SPDR;/* Чтение принятых данных и возврат: */
  114. }
  115.  
  116. uint8_t SPI_Slave_Transmit(uint8_t cData)
  117. {
  118.     uint8_t report;
  119.     PORTB &=~(1<<SS);   //Установить "0" на линии SS
  120.     SPDR = cData;   /* Запуск передачи данных: */
  121.     while(!(SPSR & (1<<SPIF))); /* Ожидание завершения передачи: */
  122.     report = SPDR;
  123.     PORTB |=(1<<SS);    //Установить "1" на линии SS
  124.     return report;
  125. }
  126.  
  127.  
  128. uint8_t command;
  129. uint8_t temp_cache0;
  130. uint8_t temp_cache1;
  131. uint8_t numb;
  132. uint8_t temp_cache3;
  133. uint8_t temp_cache4;
  134. uint8_t temp_cache5;
  135.  
  136. const int N = 1000;
  137. double retd = 0;
  138.  
  139. ISR(SPI_STC_vect)//прерывание SPI прием байта
  140. {
  141.     command=SPDR;
  142.     //  uint8_t n = 8;
  143.     //      uint64_t roms[n];
  144.     //      searchRom(roms, n);
  145.     if (command == 0) {
  146.         temp_cache0 = getTemp8('l');
  147.         SPDR = temp_cache0;
  148.         }
  149.     else if (command == 1) {
  150.         temp_cache1 = getTemp8('h');
  151.         SPDR = temp_cache1;
  152.     }
  153.     else if (command == 2) {
  154.         PORTC&=~(1<<PC0);
  155.     } else if (command == 3){
  156.         PORTC|=(1<<PC0);
  157.     }
  158.    
  159. }
  160.  
  161. int main(void) {
  162.     _delay_ms(100);
  163.     SPI_SLave_init();
  164.     sei();
  165.    
  166.     DDRC|=(1<<PC0);
  167.     PORTC=0;
  168.    
  169.    
  170.     oneWireInit(PIND7);
  171.     while (1) {
  172.        
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement