Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Definim LED-urile
- #define LED1 0x08
- #define LED2 0x04
- #define LED3 0x02
- #define LED4 0x01
- //Definim segmentele de la afisaj
- #define SEGA 0x80
- #define SEGB 0x40
- #define SEGC 0x20
- #define SEGD 0x10
- #define SEGE 0x08
- #define SEGF 0x04
- #define SEGG 0x02
- enum {FADE_IN, ON, FADE_OUT, OFF} LED4_STATE = FADE_IN;
- enum {I1, I2, I3} INITIALS = I1; //I1 = B, I2 = G, I3 = A
- void InitializeTimer1()
- {
- TCCR1A = 0;
- TCCR1B = 0;
- TCNT1 = 0;
- //Formula pentru OCR1A:
- //OCR1A = [16000000Hz/(1024*1Hz)]-1
- OCR1A = 15624;
- TCCR1B |= (1 << WGM12); //Mod CTC
- TCCR1B |= (1 << CS12) | (1 << CS10); //Prescaler 1024
- TIMSK1 |= (1 << OCIE1A); //Setare flag match
- }
- void InitializeTimer2()
- {
- //Resetare registru Timer2
- //OBS: Pentru TCCR2B = 0 programul se blocheaza
- TCCR2A = 0;
- TCCR2A |= (1 << COM2B1); //Activam pinul 3 ca PWM
- TCCR2A |= (1 << WGM21)|(1 << WGM20); //Mod PWM
- //1024 prescaler
- TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
- OCR2B = 0;
- }
- void InitializeUSART()
- {
- UBRR0 = 103; //BAUD 9600bps
- //Activare transmisie/receptie
- UCSR0B = (1<<RXEN0)|(1<<TXEN0);
- UCSR0C = (1<<USBS0)|(3<<UCSZ00); //8 data biti si 2 de stop
- }
- void InitializeADC()
- {
- ADMUX |= (1 << REFS0);
- ADCSRA |= (1 << ADEN);
- ADCSRA |= (1 << ADSC);
- ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
- }
- void LED4PWM()
- {
- //Teoretic TCNT1 numara pana la OCR1A (15624) intr-o secunda
- //Cum OCR2B are valoarea maxima 255 ar trebui ca tot la 61
- //(15624/256 = ~61) de incrementari ale lui TCNT1 sa
- //Incrementam/decrementam valoarea lui OCR2B, dar efectul de
- //Fade in/out se vede mai bine pentru o incrementare/
- //decrementare cu 10
- if(LED4_STATE == FADE_IN)
- {
- if(TCNT1 % 61 == 0)
- OCR2B += 10;
- }
- else if(LED4_STATE == FADE_OUT)
- {
- if(TCNT1%61 == 0)
- OCR2B-=10;
- }
- else if(LED4_STATE == ON)
- {
- OCR2B = 255;
- }
- else if(LED4_STATE == OFF)
- {
- OCR2B = 0;
- }
- }
- void SevenSegment()
- {
- if(INITIALS == I1)
- {
- PORTD &= ~(SEGB | SEGA);
- PORTB |= (SEGG | SEGF | SEGE);
- PORTD |= (SEGD | SEGC);
- }
- else if(INITIALS == I2)
- {
- PORTD &= ~(SEGB);
- PORTB &= ~(SEGG);
- PORTB |= (SEGF | SEGE);
- PORTD |= (SEGD | SEGC | SEGA);
- }
- else if(INITIALS == I3)
- {
- PORTD &= ~(SEGD);
- PORTB |= (SEGG | SEGF | SEGE);
- PORTD |= (SEGC | SEGB | SEGA);
- }
- }
- void ReadLED2()
- {
- unsigned char cRead = UDR0;
- if(cRead == 'A' || cRead == 'a')
- PORTB |= LED2;
- else if(cRead == 'S' || cRead == 's')
- PORTB &= ~(LED2);
- }
- uint16_t ReadADC(uint8_t channel)
- {
- ADMUX &= 0xF0;
- ADMUX |= channel;
- ADCSRA |= (1<<ADSC);
- while(ADCSRA & (1<<ADSC));
- return ADCW;
- }
- void Temperature()
- {
- uint16_t nADC = ReadADC(0); //Citim CAN-ul
- //Conversia in grade C
- //Conversia nu se face ideal, desi formula e verificata
- //De mai multe ori (problema poate e la simulator)
- float temp = ((nADC*5.0/1024.0)-0.5)*100;
- //Variabilele pentru transmisie
- char intPart[5], fracPart[2];
- //Conversia intreg -> char a temperaturii
- itoa((int)temp, intPart, 10);
- itoa((int)(abs((temp-(int)temp))*100), fracPart, 10);
- strcat(intPart, ".");
- strcat(intPart, fracPart);
- strcat(intPart, "\n");
- //Conditionala cu histereza de 0.5 grade
- if(temp < 34.5)
- PORTB &= ~(LED3);
- else if(temp > 35.5)
- PORTB |= LED3;
- //Transmisia temperaturii
- for(int i = 0; i < strlen(intPart); i++)
- {
- while (!(UCSR0A & (1<<UDRE0)));
- UDR0 = intPart[i];
- }
- }
- void setup()
- {
- cli(); //Intrerupem intreruperile
- //Configuratia porturilor
- DDRB = 0x3F;
- DDRD = 0xF8;
- DDRC = 0x00;
- //Functiile de initializare necesare
- InitializeTimer1();
- InitializeTimer2();
- InitializeUSART();
- InitializeADC();
- sei(); //Activam intreruperile
- }
- void loop()
- {
- ReadLED2();
- LED4PWM();
- SevenSegment();
- if(TCNT1%512 == 0) //Facem ~2 citiri/secunda
- Temperature();
- delay(1);
- }
- ISR(TIMER1_COMPA_vect)
- {
- PORTB ^= LED1;
- if(LED4_STATE == FADE_IN)
- LED4_STATE = ON;
- else if(LED4_STATE == FADE_OUT)
- LED4_STATE = OFF;
- else if(LED4_STATE == ON)
- LED4_STATE = FADE_OUT;
- else if(LED4_STATE == OFF)
- LED4_STATE = FADE_IN;
- if(INITIALS == I1)
- INITIALS = I2;
- else if(INITIALS == I2)
- INITIALS = I3;
- else if(INITIALS == I3)
- INITIALS = I1;
- }
Add Comment
Please, Sign In to add comment