/********************************************* This program was produced by the CodeWizardAVR V1.23.8d Standard Automatic Program Generator © Copyright 1998-2003 HP InfoTech s.r.l. http://www.hpinfotech.ro e-mail:office@hpinfotech.ro Project : MEGAbitty Motor Test Version : 1.0 Date : 2/8/2005 Author : Monty Goodson Company : BittyBot Comments: Test H-Bridge by sweeping PWM ratio. Cycles motor A & B from zero to full speed fwd, back to zero, then full speed reverse, and then back to zero. Mot A starts out at zero; Mot B starts out at 1/2 speed reverse. MotA is disabled during every other fwd-rev cycle MotB is disabled during every other rev-fwd cycle Chip type : ATmega8 Program type : Application Clock frequency : 16.000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 256 *********************************************/ #include #include #define FULL_PERIOD 2000 //in ms #define FULL_VALUE 256 #define GREEN_LED PORTB.3 #define BLUE_LED PORTB.4 #define MOTA_EN_N PORTB.0 #define MOTB_EN_N PORTB.5 bit motorAFwd = 1; bit motorBFwd = 1; void main(void) { unsigned char motorAVal = FULL_VALUE >> 1; //motor stopped unsigned char motorBVal = FULL_VALUE >> 2; //motor 1/2 speed rev // Input/Output Ports initialization // Port B initialization // Func0=Out Func1=Out Func2=Out Func3=Out Func4=Out Func5=Out Func6=In Func7=Out // State0=1 State1=1 State2=1 State3=1 State4=1 State5=1 State6=T State7=0 PORTB=0x3F; DDRB=0xBF; // Port C initialization // Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In // State0=T State1=T State2=T State3=T State4=T State5=T State6=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In // State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T PORTD=0x00; DDRD=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 16000.000 kHz // Mode: Fast PWM top=00FFh // OC1A output: Non-Inv. // OC1B output: Inverted // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0xB1; TCCR1B=0x09; TCNT1H=0x00; TCNT1L=0x00; OCR1AH=0x00; OCR1AL=0x7F; OCR1BH=0x00; OCR1BL=0x7F; MOTA_EN_N = 0; //Enable motor A MOTB_EN_N = 0; //Enable motor B while (1) { OCR1AL = motorAVal; OCR1BL = motorBVal; if(motorAFwd) { if(++motorAVal == (FULL_VALUE-1)) { motorAFwd = 0; } if(motorAVal == (FULL_VALUE >> 1)) { MOTA_EN_N ^= 1; //Disable motor every other fwd/rev cycle GREEN_LED ^= 1; //Turn off LED when disable motor } } else { if(--motorAVal == 1) motorAFwd = 1; } if(motorBFwd) { if(++motorBVal == (FULL_VALUE-1)) motorBFwd = 0; } else { if(--motorBVal == 1) { motorBFwd = 1; } if(motorBVal == (FULL_VALUE >> 1)) { MOTB_EN_N ^= 1; //Disable motor every other fwd/rev cycle BLUE_LED ^= 1; //Turn off LED when disable motor } } delay_ms(FULL_PERIOD/FULL_VALUE); }; }