00001
00002
00003
00004
00005
00006
00007 #include <avr/io.h>
00008 #include "motor.h"
00009
00010 #define ENABLEA PD6
00011 #define PWMA PD5
00012 #define DIRA PB4
00013 #define MOTADIRPORT PORTB
00014 #define MOTAENPORT PORTD
00015
00016 #define ENABLEB PC6
00017 #define PWMB PD4
00018 #define DIRB PC7
00019 #define MOTBDIRPORT PORTC
00020 #define MOTBENPORT PORTC
00021
00022 #define PWM1_PORT_DIR DDRD
00023
00024
00025 void pwm_timer1_init(void)
00026 {
00027
00028
00029
00030 TCCR1A=((1<<COM1A1) | (1<<COM1B1) | (1<<WGM10));
00031
00032
00033
00034
00035 TCCR1B=(1<<CS10);
00036
00037 OCR1AL=0x00;
00038 OCR1BL=0x00;
00039
00040
00041 PWM1_PORT_DIR|=(1<<PWMA);
00042 PWM1_PORT_DIR|=(1<<PWMB);
00043 }
00044
00045
00046 void pwm_timer1A_set(unsigned char value)
00047 {
00048 OCR1AL=value;
00049 }
00050
00051
00052 void pwm_timer1B_set(unsigned char value)
00053 {
00054 OCR1BL=value;
00055 }
00056
00057 void initDCMotorA(void)
00058 {
00059
00060
00061 pwm_timer1_init();
00062
00063
00064 DDRB|=(1<<DIRA);
00065 PORTB|=(1<<DIRA);
00066
00067
00068 DDRD|=(1<<ENABLEA);
00069 PORTD|=(1<<ENABLEA);
00070 }
00071
00072
00073 void initDCMotorB(void)
00074 {
00075
00076
00077 pwm_timer1_init();
00078
00079
00080 DDRC|=(1<<ENABLEB);
00081 PORTC|=(1<<ENABLEB);
00082
00083
00084 DDRC|=(1<<DIRB);
00085 PORTC|=(1<<DIRB);
00086 }
00087
00088
00089 void motorBplus(unsigned char pwmValue)
00090 {
00091 uint8_t pwm = 255-pwmValue;
00092 MOTBDIRPORT|=(1<<DIRB);
00093 MOTBENPORT|=(1<<ENABLEB);
00094 pwm_timer1B_set(pwm);
00095 }
00096
00097 void motorBminus(unsigned char pwmValue)
00098 {
00099 MOTBDIRPORT&=!(1<<DIRB);
00100 MOTBENPORT|=(1<<ENABLEB);
00101 pwm_timer1B_set(pwmValue);
00102 }
00103
00104 void motorAplus(unsigned char pwmValue)
00105 {
00106 uint8_t pwm = 255-pwmValue;
00107 MOTADIRPORT|=(1<<DIRA);
00108 MOTAENPORT|=(1<<ENABLEA);
00109 pwm_timer1A_set(pwm);
00110 }
00111
00112 void motorAminus(unsigned char pwmValue)
00113 {
00114 MOTADIRPORT&=!(1<<DIRA);
00115 MOTAENPORT|=(1<<ENABLEA);
00116 pwm_timer1A_set(pwmValue);
00117 }