Advertisement
RybaSG

Untitled

Mar 8th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4.  
  5. // 1-Wire DS18B20 library
  6. #include "1Wire/ds18x20.h"
  7. //  LCD 16x2 library
  8. #include "LCD/lcd44780.h"
  9.  
  10. //map function
  11. uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max);
  12. void motor_direction(uint8_t direction); // left = 1, right = 0
  13. void motor_PWM_value(uint8_t duty);  // duty cycle of PWM ( 0-100% )
  14.  
  15. volatile uint8_t PWM;
  16.  
  17. uint8_t sensors_counter;
  18. uint8_t subzero, cel, cel_fract_bits; // sign of temperature, total part of measurement, fraction part of measurement
  19.  
  20. int main()
  21. {
  22.     //TIMER2 settings
  23.     TCCR2 |= (1<<WGM21);    // CTC
  24.     TCCR2 |= (1<<COM21);    // clear on compare match
  25.     TCCR2 |= (1<<CS22);     // prescaler = 64
  26.     TIMSK |= (1<<OCIE2);    // compare match interrupt enable
  27.     OCR2 = 5;
  28.  
  29.     //PWM, motor pinout settings
  30.     DDRA |= (1<<PA0);       // PWM output
  31.     DDRA |= (1<<PA1);       // direction output
  32.     DDRA |= (1<<PA2);       // direction output
  33.  
  34.     //LCD settings
  35.     lcd_init();
  36.  
  37.     //interrupts enable
  38.     sei();
  39.  
  40.     //variables
  41.  
  42.     sensors_counter = search_sensors();                 // Amount of sensors ( 1Wire bus )
  43.  
  44.     for(;;)
  45.     {
  46.  
  47.         DS18X20_start_meas(DS18X20_POWER_EXTERN, NULL);                                         // Read temperature, normal mode
  48.         _delay_ms(750);                                                                         // Wait for measurement
  49.  
  50.         if( DS18X20_OK == DS18X20_read_meas(gSensorIDs[0], &subzero, &cel, &cel_fract_bits) )   // Read temperature
  51.         {
  52.             lcd_locate(0,0);
  53.             if( subzero ) lcd_str("-");     // if negative temperature show "-"
  54.             else lcd_str(" ");
  55.             lcd_int(cel);                   // total part of temperature
  56.             lcd_str(".");
  57.             lcd_int(cel_fract_bits);        // fraction part of temperature
  58.             lcd_str(" C");
  59.         }
  60.         else
  61.         {
  62.             lcd_cls();
  63.             lcd_locate(1,0);
  64.             lcd_str("Error");
  65.         }
  66.     }
  67.  
  68. }
  69.  
  70. uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
  71. {
  72.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  73. }
  74.  
  75. void motor_direction(uint8_t direction)
  76. {
  77.     if(direction)
  78.     {
  79.         PORTA |= (1<<PA1);
  80.         PORTA &= ~(1<<PA2);
  81.     }
  82.     else
  83.     {
  84.         PORTA &= ~(1<<PA1);
  85.         PORTA |= (1<<PA2);
  86.     }
  87. }
  88.  
  89. void motor_PWM_value(uint8_t duty)
  90. {
  91.     PWM = map(duty, 0, 100, 0, 255);
  92. }
  93.  
  94. ISR(TIMER2_COMP_vect)
  95. {
  96.     static uint8_t counter; // counter for compare with PWM ( duty cycle )
  97.  
  98.     if ( counter >= PWM )
  99.     {
  100.         PORTA &= ~(1<<PA0);
  101.     }
  102.     else
  103.     {
  104.         PORTA |= (1<<PA0);
  105.     }
  106.  
  107.     counter++;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement