Revision 86196a57 forklift/code/motor.c

View differences:

forklift/code/motor.c
1 1
#include <avr/io.h>
2 2
#include "motor.h"
3 3
#include <stdlib.h>
4

  
4 5
/*
5 6

  
6 7
motor controller
......
14 15

  
15 16
int8_t motor_speed;
16 17

  
17

  
18

  
19 18
void motor_init(void)
20 19
{
21 20
  // WGM1 0b0101 (fast PWM, 8-bit)
......
36 35
/* Speed must be between -127 and 127 */
37 36
{
38 37
  motor_speed = speed;
39
  OCR1AL = abs(speed)*2;
38
  OCR1AL = (uint8_t)abs(speed)*2;
40 39
  if(speed>0) //go forwards
41 40
  {
42
  	PORTD |= (1<<PD7);
43
	PORTD = PORTD & ~(1<<PD6);
44
  }	
41
    PORTD |= (1<<PD7);
42
    PORTD = PORTD & ~(1<<PD6);
43
  }  
45 44
  if(speed<0) //go backwards
46 45
  {
47
  	PORTD |= (1<<PD6);
48
	PORTD = PORTD & ~(1<<PD7);
46
    PORTD |= (1<<PD6);
47
    PORTD = PORTD & ~(1<<PD7);
49 48
  }
50 49
  if(speed==0) // turn motor off
51 50
  {
52
	PORTD = PORTD & ~(1<<PD6);
53
	PORTD = PORTD & ~(1<<PD7);
51
    PORTD = PORTD & ~(1<<PD6);
52
    PORTD = PORTD & ~(1<<PD7);
54 53
  }
55 54
}
56 55

  

Also available in: Unified diff