I was tinkering with BASCOM AVR and my 3PI robot… finally got it all working really well. Why BASCOM? Well… I’ve been programming BASIC since 1978 or so… yeah… I’m old. Anyway… it is much easer to set up than AVR Studio, and I’m still learning C so this way I can jump right in with programming.
If you haven’t seen my other thread… the current version of BASCOM may have issues with the USB programmer… so I loaded AVR Studio and use it to load the HEX files.
Anyway… this program will speed up and slow down the motors with a very simple routine so you can modify it as you wish.
'======================================= '==== PWM Test Program '==== September 25, 2008 / J Rutherford '======================================= $regfile = "m168def.dat" ' Set the chip as a Mega 168 $crystal = 20000000 ' Clock speed 20 MHZ '=== Generic Port Setup === Config Portb = Output Config Portc = Output Config Portd = Output Grn_led Alias Portd.7 ' Green User LED Red_led Alias Portd.1 ' Red User LED '=== LCD SETUP === Config Lcdpin = Pin , Db4 = Portb.1 , Db5 = Portb.4 , Db6 = Portb.5 , Db7 = Portd.7 , E = Portd.4 , Rs = Portd.2 Config Lcd = 16 * 2 ' Actually 8x2 but that is not recognized by BASCOM. Rw Alias Portb.0 : Rw = 0 ' Set to Zero to write to the LCD Cursor = Off '=== PWM SETUP === Config Timer0 = Pwm , Prescale = 1 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down Config Timer2 = Pwm , Prescale = 1 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down '======================================= ' Program Start Here '======================================= Dim X As Word ' Set up a simple variable Cls Lcd "START" Locate 2 , 1 For X = 1 To 5 Lcd "*" Wait 1 Next X Cls Lcd "Speeding" ' Speed up to max For X = 1 To 254 Gosub Robot_forward Waitms 40 Next X Cls Lcd "Slowing" ' Slow down to zero For X = 254 To 0 Step -1 Gosub Robot_forward Waitms 40 Next X ' Be sure motors are stopped Cls Lcd "Stopped" Locate 2 , 1 Lcd " -DONE- " Gosub Robot_stop '======================================= Do : Loop ' Just waste time at the end End 'end program '======================================= ' Subroutines Here '======================================= Robot_forward: Pwm0a = X : Pwm0b = 0 : Pwm2a = X : Pwm2b = 0 Return Robot_stop: Pwm0a = 0 : Pwm0b = 0 : Pwm2a = 0 : Pwm2b = 0 Return Robot_reverse: Pwm0a = 0 : Pwm0b = X : Pwm2a = 0 : Pwm2b = X Return Robot_right: Pwm0a = 0 : Pwm0b = X : Pwm2a = X : Pwm2b = 0 Return Robot_left: Pwm0a = X : Pwm0b = 0 : Pwm2a = 0 : Pwm2b = X Return
I hope this helps you.