Alx09

Untitled

Oct 17th, 2021
1,125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2.  
  3. const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
  4. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  5. unsigned short secunde=0, miliSecunde=0, minute=40, ore=15;
  6.  
  7.  
  8. unsigned int numar_CAN;
  9. int voltageValue, temperatureRead, temperatureReadOld = -999;
  10. unsigned int temp;
  11. void setup()
  12. {
  13.   DDRD |= 0xFF;
  14.   DDRB|=0x03;
  15.   lcd.begin(0, 2);
  16.  
  17.   TCCR1A = 0;
  18.     TCCR1B = 0;
  19.     TCNT1 = 0;
  20.  
  21.     //Formula pentru OCR1A:
  22.     //OCR1A = [16000000Hz/(1024*1Hz)]-1
  23.     OCR1A = 15624;
  24.  
  25.     TCCR1B |= (1 << WGM12); //Mod CTC
  26.     TCCR1B |= (1 << CS12) | (1 << CS10); //Prescaler 1024
  27.     TIMSK1 |= (1 << OCIE1A); //Setare flag match
  28.   DDRC&=~0x10;
  29.   DDRD=B00001111;
  30.   adc_init();
  31.   lcd.print("Ora:");
  32.   lcd.setCursor(0,1);
  33.   lcd.print("Temp = ");
  34.   Afis();
  35.  
  36.  
  37. }
  38. void Print0(int nr){
  39.  if(nr < 10 && nr > 0) lcd.print('0');
  40. }
  41. void Afis(){
  42.    
  43.   lcd.setCursor(4,0);
  44.   Print0(ore);
  45.   lcd.print(ore);
  46.   lcd.print(':');
  47.   Print0(minute);
  48.   lcd.print(minute);
  49.   lcd.print(':');
  50.   Print0(secunde);
  51.   lcd.print(secunde);
  52.      numar_CAN=read_adc(4);
  53.      temp=read_adc(5);
  54.      voltageValue=(temp*5000.0)/1024.0;
  55.      temperatureRead=(voltageValue-500)/10;
  56.   if( temperatureRead == temperatureReadOld) return;
  57.     else lcd.setCursor(7,1);
  58.      Print0(temperatureRead);
  59.      lcd.print(temperatureRead);
  60.      lcd.print("'C");
  61. }
  62. void loop(){
  63.  
  64.  
  65.    
  66. }
  67. ISR(TIMER1_COMPA_vect){
  68.  
  69.      
  70.    ++secunde;
  71.  
  72.   if(secunde ==  60){
  73.     ++minute;
  74.     secunde = 0;
  75.   }
  76.   if(minute == 60){
  77.    ++ore;
  78.     minute = 0;
  79.   }
  80.   if(ore == 24)
  81.     ore = 0;
  82.     Afis();
  83.    
  84. }
  85. void adc_init() //adc initialization
  86. {
  87.   //set division factor between system clock frequency and the input clock to the ADC- 128
  88.   ADCSRA |= ((1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0));
  89.   ADMUX|=(1<<REFS0); //AVcc with external capacitor at Aref pin
  90.   ADCSRA|=(1<<ADEN); //enable ADC
  91.   ADCSRA|=(1<<ADSC); //ADC start conversion
  92. }
  93.  
  94. uint16_t read_adc(uint8_t channel)
  95. {
  96.   ADMUX&=0xF0; //set input AO to A5
  97.   ADMUX|=channel; //select chanel AO to A5
  98.   ADCSRA|=(1<<ADSC); //start conversion
  99.   while(ADCSRA & (1<<ADSC)); //wait while adc conversion are not updated
  100.   return ADCW; //read and return voltage
  101. }
RAW Paste Data