Advertisement
RybaSG

Untitled

Mar 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.90 KB | None | 0 0
  1. /*
  2.  Created on :   3 Mar 2018
  3.  Author:        Maciej Ryba
  4.  */
  5.  
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include <util/delay.h>
  9.  
  10. // 1-Wire DS18B20 library
  11. #include "1Wire/ds18x20.h"
  12. // 1-Wire DHT11 library
  13. #include "dht11/dht11.h"
  14. //  LCD 16x2 library
  15. #include "LCD/lcd44780.h"
  16.  
  17. #define heater      (1<<PD6)
  18. #define direction_1 (1<<PA1)
  19. #define direction_2 (1<<PA2)
  20.  
  21. //map function
  22. uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max);
  23. void motor_direction(uint8_t direction);            // left = 1, right = 0
  24. void motor_PWM_value(uint8_t duty);                 // duty cycle of PWM ( 0-100% )
  25. void motor_increase_PWM(uint8_t from, uint8_t to);  // soft increase of duty cycle
  26.  
  27. // volatile variables
  28. volatile uint8_t PWM;                                                   // duty cycle
  29. volatile uint8_t ms10_counter, ms10_delay_flag, seconds, seconds_flag;  // for setting delays
  30.  
  31. // global variables
  32. uint8_t sensors_counter;
  33. uint8_t subzero, cel, cel_fract_bits; // sign of temperature, total part of measurement, fraction part of measurement
  34. int8_t temperature = 0;
  35. volatile int8_t humidity = 0;
  36.  
  37. int main()
  38. {
  39.     //LCD settings
  40.     lcd_init();
  41.  
  42.     lcd_locate(0,4);
  43.     lcd_str("Loading");
  44.     _delay_ms(2000);
  45.  
  46.  
  47.     //TIMER0 (delay) settings
  48.     TIMER0_init();
  49.  
  50.     //TIMER2 (PWM) settings
  51.     TCCR2 |= (1<<WGM21);            // CTC
  52.     TCCR2 |= (1<<CS22);             // prescaler = 64
  53.     TIMSK |= (1<<OCIE2);            // compare match interrupt enable
  54.     OCR2 = 5;
  55.  
  56.     //PWM, motor pinout settings
  57.     DDRA |= (1<<PA0);       // PWM output
  58.     DDRA |= direction_1;    // direction output
  59.     DDRA |= direction_2;    // direction output
  60.  
  61.     //Heater
  62.     DDRD |= heater;
  63.     PORTD |= heater;        // heater off
  64.  
  65.     //interrupts enable
  66.     sei();
  67.  
  68.     //variables
  69.     uint8_t i = 0;  // for incrementation of loop
  70.     uint8_t x = 0;  // for incrementation of accelaration
  71.  
  72.     sensors_counter = search_sensors();                 // Amount of sensors ( 1Wire bus )
  73.  
  74.     lcd_cls();
  75.  
  76.     for(;;)
  77.     {
  78.         motor_direction(1);
  79.  
  80.         if ( ms10_delay_flag )
  81.         {
  82.         /* temperature measurement */
  83.  
  84.             if (  !(ms10_counter%74) )                      // wait for measurement (750ms)
  85.             DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL); // Read temperature, normal mode
  86.             ms10_delay_flag = 0;
  87.  
  88.         /* temperature measurement */
  89.  
  90.             humidity = dht11_gethumidity();
  91.             lcd_locate(1,0);
  92.             lcd_int(humidity);
  93.  
  94. /*------------------------------------------------------------ temperature control ----------------------------------------------------------*/
  95.         if( DS18X20_OK == DS18X20_read_meas(gSensorIDs[0], &subzero, &cel, &cel_fract_bits) )   // Read temperature
  96.         {
  97.             lcd_locate(0,0);
  98.             if( subzero ) lcd_str("-");     // if negative temperature show "-"
  99.             else lcd_str(" ");
  100.             lcd_int(cel);                   // total part of temperature
  101.             lcd_str(".");
  102.             lcd_int(cel_fract_bits);        // fraction part of temperature
  103.             lcd_str(" C");
  104.  
  105.             /*      hysteresis loop         */
  106.                 if ( cel <= 22  )
  107.                 {
  108.                     PORTD &= ~heater;
  109.                 }
  110.  
  111.                 if ( cel >= 24 )
  112.                 {
  113.                     PORTD |= heater;
  114.                 }
  115.             /*      hysteresis loop         */
  116.  
  117.  
  118.                     if ( cel >= 25 && cel <= 27 )
  119.                     {
  120.                         if ( x < 25 )
  121.                         {
  122.                                 if ( !(ms10_counter%9) )    // delay 100ms
  123.                                 x++;
  124.                                 motor_PWM_value(x);
  125.                                 ms10_delay_flag = 0;
  126.                         }
  127.                     }
  128.                     else if ( cel >= 28 && cel <= 30 )
  129.                     {
  130.                         if (x < 40)
  131.                         {
  132.                                 if ( !(ms10_counter%9) )    // delay 100ms
  133.                                 x++;
  134.                                 motor_PWM_value(x);
  135.                                 ms10_delay_flag = 0;
  136.                         }
  137.                     }
  138.                     else if( cel >= 31 )
  139.                     {
  140.                         if ( x < 60 )
  141.                         {
  142.                                 if ( !(ms10_counter%9) )    // delay 100ms
  143.                                 x++;
  144.                                 motor_PWM_value(x);
  145.                                 ms10_delay_flag = 0;
  146.                         }
  147.                     }
  148.                     else
  149.                     {
  150.                         x = 0;              // if temperature < 25, fan stop
  151.                         motor_PWM_value(0);
  152.                     }
  153.                 }/* end of measurement loop */
  154.             else
  155.             {
  156.                 lcd_cls();
  157.                 lcd_locate(1,0);
  158.                 lcd_str("Error");
  159.             }
  160.         }/* end of delay if  */
  161. /*---------------------------------------------------------- temperature control ----------------------------------------------------------*/
  162.     } /* end of infinite loop */
  163. }/* end of main loop */
  164.  
  165.  
  166.  
  167.  
  168. uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
  169. {
  170.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  171. }
  172.  
  173. void motor_direction(uint8_t direction)
  174. {
  175.     if(direction)
  176.     {
  177.         PORTA |= direction_1;
  178.         PORTA &= ~direction_2;
  179.     }
  180.     else
  181.     {
  182.         PORTA &= ~direction_1;
  183.         PORTA |= direction_2;
  184.     }
  185. }
  186.  
  187. void motor_PWM_value(uint8_t duty)
  188. {
  189.     PWM = map(duty, 0, 100, 0, 255);
  190. }
  191.  
  192. void motor_increase_PWM(uint8_t from, uint8_t to)
  193. {
  194.     uint8_t i;
  195.  
  196.     if ( ms10_delay_flag )
  197.     {
  198.         for ( i = from; i < to; i++ )
  199.         {
  200.             if ( !(ms10_counter%9) )
  201.             motor_PWM_value(i);
  202.             ms10_delay_flag = 0;
  203.         }
  204.     }
  205. }
  206.  
  207. // PWM interrupt
  208. ISR(TIMER2_COMP_vect)
  209. {
  210.     static uint8_t counter; // counter for compare with PWM ( duty cycle )
  211.  
  212.     if ( counter >= PWM )
  213.     {
  214.         PORTA &= ~(1<<PA0);
  215.     }
  216.     else
  217.     {
  218.         PORTA |= (1<<PA0);
  219.     }
  220.  
  221.     counter++;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement