Advertisement
paulogp

ATmega128: T4 - Controlo de um motor

Jul 13th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. /*
  2. manualni PWM
  3. */
  4. #include <avr\io.h>
  5. #include <avr/iom128.h>
  6.  
  7.  
  8. int main(void) {
  9.     // PORTD pins as input
  10.     DDRD = 0x00;
  11.  
  12.     // enable internal pull ups
  13.     PORTD = 0xFF;
  14.  
  15.     // set PORTB1 pin as output
  16.     DDRB = 0xFF; // output pro PWM port PB5, PB6, PB7
  17.  
  18.     // vyuzivaji se PB5(OC1A), PB6(OC1B)
  19.     // TOP = ICR1;
  20.     // output compare OC1A 8 bit non inverted PWM
  21.  
  22.     // clear OC1A on Compare Match, set OC1A at TOP
  23.     // fast PWM
  24.     // ICR1 = 20000 definuje 50Hz PWM DDRB 6 23 DDRB 5 23
  25.     ICR1 = 20000;
  26.  
  27.     TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
  28.  
  29.     TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS10);//(1<<WGM13)|(1<<WGM12)|(1<<CS10)
  30.  
  31.     // start timeru s divisor de freq 8
  32.     while (1) {
  33.         if(bit_is_clear(PIND, 0)){
  34.             // increase duty cycle
  35.             OCR1A += 10;
  36.             loop_until_bit_is_set(PIND, 0);
  37.         }
  38.  
  39.         if (bit_is_clear(PIND, 1)) {
  40.             // decease duty cycle
  41.             OCR1A -= 10;
  42.             loop_until_bit_is_set(PIND, 1);
  43.         }
  44.  
  45.         if (bit_is_clear(PIND, 2)) {
  46.             // increase duty cycle
  47.             OCR1B += 10;
  48.             loop_until_bit_is_set(PIND, 2);
  49.         }
  50.  
  51.         if (bit_is_clear(PIND, 3)) {
  52.             // decease duty cycle
  53.             OCR1B -= 10;
  54.             loop_until_bit_is_set(PIND, 3);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement