Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Created on : 3 Mar 2018
- Author: Maciej Ryba
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- // 1-Wire DS18B20 library
- #include "1Wire/ds18x20.h"
- // 1-Wire DHT11 library
- #include "dht11/dht11.h"
- // LCD 16x2 library
- #include "LCD/lcd44780.h"
- #define heater (1<<PD6)
- #define direction_1 (1<<PA1)
- #define direction_2 (1<<PA2)
- //map function
- uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max);
- void motor_direction(uint8_t direction); // left = 1, right = 0
- void motor_PWM_value(uint8_t duty); // duty cycle of PWM ( 0-100% )
- void motor_increase_PWM(uint8_t from, uint8_t to); // soft increase of duty cycle
- // volatile variables
- volatile uint8_t PWM; // duty cycle
- volatile uint8_t ms10_counter, ms10_delay_flag, seconds, seconds_flag; // for setting delays
- // global variables
- uint8_t sensors_counter;
- uint8_t subzero, cel, cel_fract_bits; // sign of temperature, total part of measurement, fraction part of measurement
- int8_t temperature = 0;
- volatile int8_t humidity = 0;
- int main()
- {
- //LCD settings
- lcd_init();
- lcd_locate(0,4);
- lcd_str("Loading");
- _delay_ms(2000);
- //TIMER0 (delay) settings
- TIMER0_init();
- //TIMER2 (PWM) settings
- TCCR2 |= (1<<WGM21); // CTC
- TCCR2 |= (1<<CS22); // prescaler = 64
- TIMSK |= (1<<OCIE2); // compare match interrupt enable
- OCR2 = 5;
- //PWM, motor pinout settings
- DDRA |= (1<<PA0); // PWM output
- DDRA |= direction_1; // direction output
- DDRA |= direction_2; // direction output
- //Heater
- DDRD |= heater;
- PORTD |= heater; // heater off
- //interrupts enable
- sei();
- //variables
- uint8_t i = 0; // for incrementation of loop
- uint8_t x = 0; // for incrementation of accelaration
- sensors_counter = search_sensors(); // Amount of sensors ( 1Wire bus )
- lcd_cls();
- for(;;)
- {
- motor_direction(1);
- if ( ms10_delay_flag )
- {
- /* temperature measurement */
- if ( !(ms10_counter%74) ) // wait for measurement (750ms)
- DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL); // Read temperature, normal mode
- ms10_delay_flag = 0;
- /* temperature measurement */
- humidity = dht11_gethumidity();
- lcd_locate(1,0);
- lcd_int(humidity);
- /*------------------------------------------------------------ temperature control ----------------------------------------------------------*/
- if( DS18X20_OK == DS18X20_read_meas(gSensorIDs[0], &subzero, &cel, &cel_fract_bits) ) // Read temperature
- {
- lcd_locate(0,0);
- if( subzero ) lcd_str("-"); // if negative temperature show "-"
- else lcd_str(" ");
- lcd_int(cel); // total part of temperature
- lcd_str(".");
- lcd_int(cel_fract_bits); // fraction part of temperature
- lcd_str(" C");
- /* hysteresis loop */
- if ( cel <= 22 )
- {
- PORTD &= ~heater;
- }
- if ( cel >= 24 )
- {
- PORTD |= heater;
- }
- /* hysteresis loop */
- if ( cel >= 25 && cel <= 27 )
- {
- if ( x < 25 )
- {
- if ( !(ms10_counter%9) ) // delay 100ms
- x++;
- motor_PWM_value(x);
- ms10_delay_flag = 0;
- }
- }
- else if ( cel >= 28 && cel <= 30 )
- {
- if (x < 40)
- {
- if ( !(ms10_counter%9) ) // delay 100ms
- x++;
- motor_PWM_value(x);
- ms10_delay_flag = 0;
- }
- }
- else if( cel >= 31 )
- {
- if ( x < 60 )
- {
- if ( !(ms10_counter%9) ) // delay 100ms
- x++;
- motor_PWM_value(x);
- ms10_delay_flag = 0;
- }
- }
- else
- {
- x = 0; // if temperature < 25, fan stop
- motor_PWM_value(0);
- }
- }/* end of measurement loop */
- else
- {
- lcd_cls();
- lcd_locate(1,0);
- lcd_str("Error");
- }
- }/* end of delay if */
- /*---------------------------------------------------------- temperature control ----------------------------------------------------------*/
- } /* end of infinite loop */
- }/* end of main loop */
- uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- void motor_direction(uint8_t direction)
- {
- if(direction)
- {
- PORTA |= direction_1;
- PORTA &= ~direction_2;
- }
- else
- {
- PORTA &= ~direction_1;
- PORTA |= direction_2;
- }
- }
- void motor_PWM_value(uint8_t duty)
- {
- PWM = map(duty, 0, 100, 0, 255);
- }
- void motor_increase_PWM(uint8_t from, uint8_t to)
- {
- uint8_t i;
- if ( ms10_delay_flag )
- {
- for ( i = from; i < to; i++ )
- {
- if ( !(ms10_counter%9) )
- motor_PWM_value(i);
- ms10_delay_flag = 0;
- }
- }
- }
- // PWM interrupt
- ISR(TIMER2_COMP_vect)
- {
- static uint8_t counter; // counter for compare with PWM ( duty cycle )
- if ( counter >= PWM )
- {
- PORTA &= ~(1<<PA0);
- }
- else
- {
- PORTA |= (1<<PA0);
- }
- counter++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement