Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int Received;
- int i;
- float temperature;
- bool ok;
- void setup(){
- InitUSART();
- DDRD = 0xFE;
- DDRB = 0x0F;
- TCCR2A |= (1<<COM2A1)| (1 << WGM20) | (1 << WGM21);
- TCCR2B |= (1<<CS22) | (1<CS21) | (1<<CS20); // prescaler 1024
- TCCR1A = 0;
- TCCR1B = 0;
- TCNT1 = 0;
- OCR1A = 15624; //Formula pentru OCR1A:
- //OCR1A = [16000000Hz/(1024*1Hz)]-1
- TCCR1B |= (1 << WGM12); //Mod CTC
- TCCR1B |= (1 << CS12) | (1 << CS10); //Prescaler 1024
- TIMSK1 |= (1 << OCIE1A); //Setare flag match
- sei(); //Activam intreruperile
- }
- void InitUSART()
- {
- 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 loop(){
- S_A();
- PWM();
- if(TCNT1%512 == 0) //Facem ~2 citiri/secunda
- Temperature();
- delay(1);
- }
- void S_A(){
- unsigned char cRead = UDR0;
- if(cRead == 'A' || cRead == 'a')
- PORTB |= 0x04;
- else if(cRead == 'S' || cRead == 's')
- PORTB &= ~(0x04);
- }
- void segment(){
- PORTD = 0xEE;
- delay(1000);
- PORTD = 0x7A;
- delay(1000);
- }
- void PWM(){
- for (i = 0; i < 256; i++){
- OCR2A = i;
- _delay_ms(10);
- }
- for (i = 255; i; i--){
- OCR2A = i;
- _delay_ms(10);
- }
- }
- 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 &= ~(0x02);
- else if(temp > 35.5)
- PORTB |= 0x02;
- //Transmisia temperaturii
- for(int i = 0; i < strlen(intPart); i++)
- {
- while (!(UCSR0A & (1<<UDRE0)));
- UDR0 = intPart[i];
- }
- }
- void adc_init() //adc initialization
- {
- //set division factor between system clock frequency and the input clock to the ADC- 128
- ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));
- ADMUX |= (1 << REFS0); //AVcc with external capacitor at Aref pin
- ADCSRA |= (1 << ADEN); //enable ADC
- ADCSRA |= (1 << ADSC); //ADC start conversion
- }
- uint16_t read_adc(uint8_t channel)
- {
- ADMUX &= 0xF0; //set input AO to A5
- ADMUX |= channel; //select chanel AO to A5
- ADCSRA |= (1<<ADSC); //start conversion
- while(ADCSRA & (1<<ADSC)); //wait while adc conversion are not updated
- return ADCW; //read and return voltage
- }
- ISR(TIMER1_COMPA_vect){
- PORTB ^= 0x08;
- if(ok == 0){
- ok = 1;
- PORTD =0xEE;
- }
- else{
- ok = 0;
- PORTD = 0x7A;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment