Alx09

sadness sbm

May 24th, 2021
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.63 KB | None | 0 0
  1. //Definim LED-urile
  2. #define LED1 0x08
  3. #define LED2 0x04
  4. #define LED3 0x02
  5. #define LED4 0x01
  6.  
  7.  
  8. //Definim segmentele de la afisaj
  9. #define SEGA 0x80
  10. #define SEGB 0x40
  11. #define SEGC 0x20
  12. #define SEGD 0x10
  13. #define SEGE 0x08
  14. #define SEGF 0x04
  15. #define SEGG 0x02
  16.  
  17. enum {FADE_IN, ON, FADE_OUT, OFF} LED4_STATE = FADE_IN;
  18. enum {I1, I2, I3} INITIALS = I1; //I1 = B, I2 = G, I3 = A
  19.  
  20. void InitializeTimer1()
  21. {
  22.    
  23.     TCCR1A = 0;
  24.     TCCR1B = 0;
  25.     TCNT1 = 0;
  26.  
  27.     //Formula pentru OCR1A:
  28.     //OCR1A = [16000000Hz/(1024*1Hz)]-1
  29.     OCR1A = 15624;
  30.  
  31.     TCCR1B |= (1 << WGM12); //Mod CTC
  32.     TCCR1B |= (1 << CS12) | (1 << CS10); //Prescaler 1024
  33.     TIMSK1 |= (1 << OCIE1A); //Setare flag match
  34. }
  35.  
  36. void InitializeTimer2()
  37. {
  38.     //Resetare registru Timer2
  39.     //OBS: Pentru TCCR2B = 0 programul se blocheaza
  40.     TCCR2A = 0;
  41.  
  42.     TCCR2A |= (1 << COM2B1); //Activam pinul 3 ca PWM
  43.     TCCR2A |= (1 << WGM21)|(1 << WGM20); //Mod PWM
  44.  
  45.     //1024 prescaler
  46.     TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
  47.     OCR2B = 0;
  48. }
  49.  
  50. void InitializeUSART()
  51. {
  52.     UBRR0 = 103; //BAUD 9600bps
  53.  
  54.     //Activare transmisie/receptie
  55.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  56.     UCSR0C = (1<<USBS0)|(3<<UCSZ00); //8 data biti si 2 de stop
  57. }
  58.  
  59. void InitializeADC()
  60. {
  61.     ADMUX |= (1 << REFS0);
  62.     ADCSRA |= (1 << ADEN);
  63.     ADCSRA |= (1 << ADSC);
  64.     ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  65. }
  66.  
  67. void LED4PWM()
  68. {
  69.     //Teoretic TCNT1 numara pana la OCR1A (15624) intr-o secunda
  70.     //Cum OCR2B are valoarea maxima 255 ar trebui ca tot la 61
  71.     //(15624/256 = ~61) de incrementari ale lui TCNT1 sa
  72.     //Incrementam/decrementam valoarea lui OCR2B, dar efectul de
  73.     //Fade in/out se vede mai bine pentru o incrementare/
  74.     //decrementare cu 10
  75.     if(LED4_STATE == FADE_IN)
  76.     {
  77.       if(TCNT1 % 61 == 0)
  78.         OCR2B += 10;
  79.     }
  80.     else if(LED4_STATE == FADE_OUT)
  81.     {
  82.       if(TCNT1%61 == 0)
  83.         OCR2B-=10;
  84.     }
  85.     else if(LED4_STATE == ON)
  86.     {
  87.       OCR2B = 255;
  88.     }
  89.     else if(LED4_STATE == OFF)
  90.     {
  91.       OCR2B = 0;
  92.     }
  93. }
  94.  
  95. void SevenSegment()
  96. {
  97.     if(INITIALS == I1)
  98.     {
  99.       PORTD &= ~(SEGB | SEGA);
  100.  
  101.       PORTB |= (SEGG | SEGF | SEGE);
  102.       PORTD |= (SEGD | SEGC);
  103.     }
  104.     else if(INITIALS == I2)
  105.     {
  106.       PORTD &= ~(SEGB);
  107.       PORTB &= ~(SEGG);
  108.  
  109.       PORTB |= (SEGF | SEGE);
  110.       PORTD |= (SEGD | SEGC | SEGA);
  111.     }
  112.     else if(INITIALS == I3)
  113.     {
  114.       PORTD &= ~(SEGD);
  115.  
  116.       PORTB |= (SEGG | SEGF | SEGE);
  117.       PORTD |= (SEGC | SEGB | SEGA);
  118.     }
  119. }
  120.  
  121. void ReadLED2()
  122. {
  123.     unsigned char cRead = UDR0;
  124.     if(cRead == 'A' || cRead == 'a')
  125.       PORTB |= LED2;
  126.     else if(cRead == 'S' || cRead == 's')
  127.       PORTB &= ~(LED2);
  128. }
  129.  
  130. uint16_t ReadADC(uint8_t channel)
  131. {
  132.     ADMUX &= 0xF0;
  133.     ADMUX |= channel;
  134.     ADCSRA |= (1<<ADSC);
  135.     while(ADCSRA & (1<<ADSC));
  136.     return ADCW;
  137. }
  138.  
  139. void Temperature()
  140. {
  141.     uint16_t nADC = ReadADC(0); //Citim CAN-ul
  142.     //Conversia in grade C
  143.     //Conversia nu se face ideal, desi formula e verificata
  144.     //De mai multe ori (problema poate e la simulator)
  145.     float temp = ((nADC*5.0/1024.0)-0.5)*100;
  146.     //Variabilele pentru transmisie
  147.     char intPart[5], fracPart[2];
  148.    
  149.     //Conversia intreg -> char a temperaturii
  150.     itoa((int)temp, intPart, 10);
  151.     itoa((int)(abs((temp-(int)temp))*100), fracPart, 10);
  152.     strcat(intPart, ".");
  153.     strcat(intPart, fracPart);
  154.     strcat(intPart, "\n");
  155.    
  156.     //Conditionala cu histereza de 0.5 grade
  157.     if(temp < 34.5)
  158.       PORTB &= ~(LED3);
  159.     else if(temp > 35.5)
  160.       PORTB |= LED3;
  161.    
  162.     //Transmisia temperaturii
  163.     for(int i = 0; i < strlen(intPart); i++)
  164.     {
  165.        while (!(UCSR0A & (1<<UDRE0)));
  166.        UDR0 = intPart[i];  
  167.     }
  168. }
  169.  
  170. void setup()
  171. {
  172.     cli(); //Intrerupem intreruperile
  173.    
  174.     //Configuratia porturilor
  175.     DDRB = 0x3F;
  176.     DDRD = 0xF8;
  177.     DDRC = 0x00;
  178.  
  179.     //Functiile de initializare necesare
  180.     InitializeTimer1();
  181.     InitializeTimer2();
  182.     InitializeUSART();
  183.     InitializeADC();
  184.  
  185.     sei(); //Activam intreruperile
  186. }
  187.  
  188. void loop()
  189. {
  190.     ReadLED2();
  191.     LED4PWM();
  192.     SevenSegment();
  193.     if(TCNT1%512 == 0) //Facem ~2 citiri/secunda
  194.       Temperature();
  195.     delay(1);
  196. }
  197.  
  198. ISR(TIMER1_COMPA_vect)
  199. {
  200.     PORTB ^= LED1;
  201.  
  202.     if(LED4_STATE == FADE_IN)
  203.       LED4_STATE = ON;
  204.     else if(LED4_STATE == FADE_OUT)
  205.       LED4_STATE = OFF;
  206.     else if(LED4_STATE == ON)
  207.       LED4_STATE = FADE_OUT;
  208.     else if(LED4_STATE == OFF)
  209.       LED4_STATE = FADE_IN;
  210.      
  211.     if(INITIALS == I1)
  212.       INITIALS = I2;
  213.     else if(INITIALS == I2)
  214.       INITIALS = I3;
  215.     else if(INITIALS == I3)
  216.       INITIALS = I1;
  217. }
Add Comment
Please, Sign In to add comment