Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- // 1-Wire DS18B20 library
- #include "1Wire/ds18x20.h"
- // LCD 16x2 library
- #include "LCD/lcd44780.h"
- //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% )
- volatile uint8_t PWM;
- uint8_t sensors_counter;
- uint8_t subzero, cel, cel_fract_bits; // sign of temperature, total part of measurement, fraction part of measurement
- int main()
- {
- //TIMER2 settings
- TCCR2 |= (1<<WGM21); // CTC
- TCCR2 |= (1<<COM21); // clear on compare match
- TCCR2 |= (1<<CS22); // prescaler = 64
- TIMSK |= (1<<OCIE2); // compare match interrupt enable
- OCR2 = 5;
- //PWM, motor pinout settings
- DDRA |= (1<<PA0); // PWM output
- DDRA |= (1<<PA1); // direction output
- DDRA |= (1<<PA2); // direction output
- //LCD settings
- lcd_init();
- //interrupts enable
- sei();
- //variables
- sensors_counter = search_sensors(); // Amount of sensors ( 1Wire bus )
- for(;;)
- {
- DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL); // Read temperature, normal mode
- _delay_ms(750); // Wait for measurement
- 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");
- }
- else
- {
- lcd_cls();
- lcd_locate(1,0);
- lcd_str("Error");
- }
- }
- }
- 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 |= (1<<PA1);
- PORTA &= ~(1<<PA2);
- }
- else
- {
- PORTA &= ~(1<<PA1);
- PORTA |= (1<<PA2);
- }
- }
- void motor_PWM_value(uint8_t duty)
- {
- PWM = map(duty, 0, 100, 0, 255);
- }
- 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