' darc2_test.bas ' Author: Kerwin Lumpkins ' Rev date: 2/2/04 ' This file tests the DARC Board and is used to verify good operation. ' It drives all the major interfaces and prompts user to take actions ' where appropriate. ' Configuration info: This program to be run on a DARC Board with: ' ATMega8535 fuse bits set for 8 MHz internal oscillator ' Serial port cable connected, Baud Rate 9600 recommended ' put in the cal value for your part here (marked on the 8535 case ' most are hex 9F, some 91's and some in between Osccal = &HA3 $crystal = 8000000 ' servo related declarations here Declare Sub Servos12_init Declare Sub Servo1_set(byval Hset As Byte , Byval Lset As Byte) Declare Sub Servo2_set(byval Hset As Byte , Byval Lset As Byte) Declare Sub Servo3_init Declare Sub Servo3_set(byval Setting As Byte) Dim Servos12_active As Bit Dim Setting As Byte ' Analog to Digital Conversion related items Declare Sub Adc_init Declare Function Adc_get_value(byval Channel As Byte) As Integer Dim Adc_value As Integer Dim Channel As Byte ' DIP / LED test items Dim Invalue As Byte Dim Outvalue As Byte Dim A As Byte 'LCD stuff ' set up LCD pins for the DARC board interface, 16 x 2 LCD Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.1 , Rs = Portb.0 Config Lcd = 16 * 2 'Main ' # # # # # # # # # # # # # ' SERVO test ' init the servos Call Servos12_init Call Servo3_init Print "" Print "" Print "Test: " Print "Servos 1,2,3 run 10 secs" ' set servos to run for 10 seconds Call Servo1_set(0 , 60) Call Servo2_set(0 , 20) Call Servo3_set(&H09) Wait 10 ' then turn off the servos Call Servo1_set(&H03 , &HFF) Call Servo2_set(&H03 , &HFF) Call Servo3_set(&Hff) ' # # # # # # # # # # # # # ' ADC test Call Adc_init Print "" Print "" Print "Test: " Print "ADC:" For Channel = 0 To 7 Adc_value = Adc_get_value(channel) Print "Ch:" ; Channel ; " - " ; Adc_value Wait 1 Next ' # # # # # # # # # # # # # ' DIP / LED test 'High bits are outputs (LEDs), low bits are inputs (DIP) Config Portc = &B11110000 'set the data direction register to be safe Ddrc = &B11110000 Print "" Print "" Print "Test: " Print "Set DIP to 1100 to exit" ' continuous loop Invalue = Pinc And &B00001111 While Invalue <> &B00001100 'read inputs at pins of portc Invalue = Pinc And &B00001111 'set outvalue = invalue and then shift this left to the high 4 bits Outvalue = Invalue Shift Outvalue , Left , 4 'set the low 4 to be sure that the internal pullups are enabled Outvalue = Outvalue Or &B00001111 ' load the outvalue to the port Portc = Outvalue Wend Print "" Print "" Print "Test: " Print "LCD" Do Cls 'clear the LCD display Lcd "DARC Board." 'display this at the top line Wait 1 Lowerline 'select the lower line Wait 1 Lcd "Kill FRRG!" 'display this at the lower line Wait 1 For A = 1 To 10 Shiftlcd Right 'shift the text to the right Wait 1 'wait a moment Next For A = 1 To 10 Shiftlcd Left 'shift the text to the left Wait 1 'wait a moment Next Loop End ' ********************************************************* ' servo control and init subroutines Sub Servos12_init: ' set bits 5 and 4 for output Ddrd = Ddrd Or &H30 ' set timer 1 for pwm output, prescaler 64 ' these settings are for 1 MHz clock Tccr1a = &HA3 Tccr1b = &H0C 'OCR1 is a 16 bit register that sets the on time or duty 'cycle for servos 1 and 2. The setting is a 16 bit value that 'consists of the Hset being the hi byte, and LSet the low. 'The servos12 device is a 10 bit resolution PWM output so only 'the low 2 bits of Hset are valid. The Highest value for Hset = hex 3. 'Pulse width = (setting + 1) * 35 us. So a setting of '0 will give a 35 us pulse. Setting = 1 gives a 70 us 'pulse. A setting of 46 will give about 1.5 ms pulse. 'Hset = &H03 and Lset = &HFF is the OFF setting. ' Note that OCR1A sets servo 2 and B sets servo1 ' Here the servos are init'd to be at an "OFF" setting ' hex 03FF gives a 100 % duty cycle pulse. Ocr1ah = &H03 Ocr1al = &HFF Ocr1bh = &H03 Ocr1bl = &HFF Servos12_active = 1 End Sub Servos12_init Sub Servo1_set(byval Hset As Byte , Byval Lset As Byte) 'OCR1 is a 16 bit register that sets the on time or duty 'cycle for servos 1 and 2. The setting is a 16 bit value that 'consists of the Hset being the hi byte, and LSet the low. 'The servos12 device is a 10 bit resolution PWM output so only 'the low 2 bits of Hset are valid. The Highest value for Hset = hex 3. 'Pulse width = (setting + 1) * 35 us. So a setting of '0 will give a 35 us pulse. Setting = 1 gives a 70 us 'pulse. A setting of 46 will give about 1.5 ms pulse. 'Hset = &H03 and Lset = &HFF is the OFF setting. ' Note that OCR1A sets servo 2 and B sets servo1 Ocr1bh = Hset Ocr1bl = Lset End Sub Servo1_set(byval Hset As Byte , Byval Lset As Byte) Sub Servo2_set(byval Hset As Byte , Byval Lset As Byte) 'OCR1 is a 16 bit register that sets the on time or duty 'cycle for servos 1 and 2. The setting is a 16 bit value that 'consists of the Hset being the hi byte, and LSet the low. 'The servos12 device is a 10 bit resolution PWM output so only 'the low 2 bits of Hset are valid. The Highest value for Hset = hex 3. 'Pulse width = (setting + 1) * 35 us. So a setting of '0 will give a 35 us pulse. Setting = 1 gives a 70 us 'pulse. A setting of 46 will give about 1.5 ms pulse. 'Hset = &H03 and Lset = &HFF is the OFF setting. ' Note that OCR1A sets servo 2 and B sets servo1 Ocr1ah = Hset Ocr1al = Lset End Sub Servo1_set(byval Hset As Byte , Byval Lset As Byte) Sub Servo3_set(setting As Byte) 'OCR2 is an 8 bit value that sets the on time or duty cycle. 'Pulse width = (setting + 1) * 130 us. So a setting of '0 will give a 130 us pulse. Setting = 1 gives a 260 us 'pulse. A setting of 10 will give about 1.5 ms pulse. 'The OFF setting is &HFF. Ocr2 = Setting End Sub Servo3_set(setting As Byte) Sub Servo3_init: ' set bit 7 for output Ddrd = Ddrd Or &H80 ' set timer 2 for pwm output, prescaler 1024 ' these settings are for 8 MHz clock Tccr2 = &H6F 'OCR2 is an 8 bit value that sets the on time or duty cycle. 'Pulse width = (setting + 1) * 130 us. So a setting of '0 will give a 130 us pulse. Setting = 1 gives a 260 us 'pulse. A setting of 10 will give about 1.5 ms pulse. 'The OFF setting is &HFF. Ocr2 = &HFF End Sub Servo3_init ' ****************************************************************** ' ADC sub and functions Sub Adc_init ' set for AVCC reference voltage,Right justified,channel 0 Admux = &H40 'set ad status and control register for conversion disabled, conversion 'not started,auto trigger disabled,interrupts disabled,and divide 'by 64 prescaler Adcsra = &H06 End Sub Adc_init Function Adc_get_value(byval Channel As Byte) As Integer Dim Flag As Byte Dim Adcnotdone As Bit 'init the admux for channel 0 Admux = Admux And &HE0 Admux = Admux + Channel 'enable the adc Adcsra = &H86 'start the conversion Adcsra = &HC6 Adcnotdone = 1 While Adcnotdone = 1 Flag = Adcsra And &H40 'wait until the adc interrupt flag is set If Flag = 0 Then Adcnotdone = 0 Wend Dim Adcvalh As Integer Dim Adcvall As Integer Dim Adcval As Integer Adcvalh = 0 Adcvall = 0 Adcval = 0 'get the 2 adc hi and lo bytes and combine into one integer value Adcvall = Adcl Adcvalh = Adch Shift Adcvalh , Left , 8 Adcval = Adcvalh + Adcvall 'disable conversion Adcsra = &H06 Adc_get_value = Adcval End Function Adc_get_value(byval Channel As Byte)