Alx09

Untitled

May 27th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | None | 0 0
  1. int Received;
  2.  
  3. int i;
  4. float temperature;
  5. bool ok;
  6. void setup(){
  7.   InitUSART();
  8.   DDRD = 0xFE;
  9.   DDRB = 0x0F;
  10.   TCCR2A |= (1<<COM2A1)| (1 << WGM20) | (1 << WGM21);
  11.   TCCR2B |= (1<<CS22) | (1<CS21) | (1<<CS20); // prescaler 1024
  12.  
  13.   TCCR1A = 0;
  14.   TCCR1B = 0;
  15.   TCNT1 = 0;
  16.   OCR1A = 15624;          //Formula pentru OCR1A:
  17.                           //OCR1A = [16000000Hz/(1024*1Hz)]-1
  18.   TCCR1B |= (1 << WGM12); //Mod CTC
  19.   TCCR1B |= (1 << CS12) | (1 << CS10); //Prescaler 1024
  20.   TIMSK1 |= (1 << OCIE1A); //Setare flag match
  21.   sei(); //Activam intreruperile
  22.    
  23. }
  24. void InitUSART()
  25. {
  26.     UBRR0 = 103; //BAUD 9600bps
  27.  
  28.     //Activare transmisie/receptie
  29.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  30.     UCSR0C = (1<<USBS0)|(3<<UCSZ00); //8 data biti si 2 de stop
  31. }
  32.  
  33. void loop(){
  34.  S_A();
  35.  
  36.  PWM();
  37.  if(TCNT1%512 == 0) //Facem ~2 citiri/secunda
  38.       Temperature();
  39.   delay(1);
  40. }
  41.  
  42. void S_A(){
  43.   unsigned char cRead = UDR0;
  44.     if(cRead == 'A' || cRead == 'a')
  45.       PORTB |= 0x04;
  46.     else if(cRead == 'S' || cRead == 's')
  47.       PORTB &= ~(0x04);
  48. }
  49.  
  50. void segment(){
  51.   PORTD = 0xEE;
  52.   delay(1000);
  53.   PORTD = 0x7A;
  54.   delay(1000);
  55. }
  56.  
  57. void PWM(){
  58.  
  59.   for (i = 0; i < 256; i++){
  60.       OCR2A = i;
  61.      _delay_ms(10);
  62.     }
  63.  
  64.    for (i = 255; i; i--){
  65.       OCR2A = i;
  66.      _delay_ms(10);
  67.     }
  68. }
  69.    
  70. uint16_t ReadADC(uint8_t channel)
  71. {
  72.     ADMUX &= 0xF0;
  73.     ADMUX |= channel;
  74.     ADCSRA |= (1<<ADSC);
  75.     while(ADCSRA & (1<<ADSC));
  76.     return ADCW;
  77. }
  78.    
  79. void Temperature(){
  80. uint16_t nADC = ReadADC(0); //Citim CAN-ul
  81.     //Conversia in grade C
  82.     //Conversia nu se face ideal, desi formula e verificata
  83.     //De mai multe ori (problema poate e la simulator)
  84.     float temp = ((nADC*5.0/1024.0)-0.5)*100;
  85.     //Variabilele pentru transmisie
  86.     char intPart[5], fracPart[2];
  87.    
  88.     //Conversia intreg -> char a temperaturii
  89.     itoa((int)temp, intPart, 10);
  90.     itoa((int)(abs((temp-(int)temp))*100), fracPart, 10);
  91.     strcat(intPart, ".");
  92.     strcat(intPart, fracPart);
  93.     strcat(intPart, "\n");
  94.    
  95.     //Conditionala cu histereza de 0.5 grade
  96.     if(temp < 34.5)
  97.       PORTB &= ~(0x02);
  98.     else if(temp > 35.5)
  99.       PORTB |= 0x02;
  100.    
  101.     //Transmisia temperaturii
  102.     for(int i = 0; i < strlen(intPart); i++)
  103.     {
  104.        while (!(UCSR0A & (1<<UDRE0)));
  105.        UDR0 = intPart[i];  
  106.     }
  107. }
  108.  
  109. void adc_init() //adc initialization
  110. {
  111.   //set division factor between system clock frequency and the input clock to the ADC- 128
  112.   ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));
  113.   ADMUX  |= (1  << REFS0); //AVcc with external capacitor at Aref pin
  114.   ADCSRA |= (1  << ADEN); //enable ADC
  115.   ADCSRA |= (1  << ADSC); //ADC start conversion
  116. }
  117.  
  118. uint16_t read_adc(uint8_t channel)
  119. {
  120.   ADMUX &= 0xF0; //set input AO to A5
  121.   ADMUX |= channel; //select chanel AO to A5
  122.   ADCSRA |= (1<<ADSC); //start conversion
  123.   while(ADCSRA & (1<<ADSC)); //wait while adc conversion are not updated
  124.   return ADCW; //read and return voltage
  125. }
  126.  
  127.  
  128.  
  129. ISR(TIMER1_COMPA_vect){
  130.   PORTB ^= 0x08;
  131.   if(ok == 0){
  132.     ok = 1;
  133.     PORTD =0xEE;
  134.   }
  135.   else{
  136.     ok = 0;
  137.     PORTD = 0x7A;
  138.  
  139.   }
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
Advertisement
Add Comment
Please, Sign In to add comment