Saturday, June 11, 2011

Learning the microcontrollers

Today marked the first day where I put in some serious time with learning how to code the MCUs. I decided to work on the code for the servos in the mouth of the hood. It is a little slow going as I am learning the assembly language of the chips as well as understanding its memory architecture. Anyway, the code doesn't do much yet but it actually represents a leap in understanding. The code just loops through looking at two servos (I could expand it to more, just keeping number down for debugging purposes). It makes a decision if it is time to move the servo and if it is, sets the servo up for the move.

I have a lot of code to go but it is coming to me pretty quickly now. To give you an idea of what it looks like... Yeah, this is what I do for fun.


list p=16f887 ; list directive to define processor
#include ; processor specific variable definitions

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.

__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V

SERVOCOUNT EQU D'2'
MINTIMER EQU D'1000' ;smallest pulse for one end of servo positioning
MAXTIMER EQU D'2000' ;largest pulse for other end of servo positioning
TOTALTIMER EQU D'40000' ;max 40ms

; declare servo arrary
VARIABLES UDATA_SHR
servo_start res 0
servo1_cycle res 1
servo1_duration_high res 1
servo1_duration_low res 1
servo2_cycle res 1
servo2_duration_high res 1
servo2_duration_low res 1
loop_count res 1


;**********************************************************************
RESET_VECTOR CODE 0x0000 ; processor reset vector
nop
goto start ; go to beginning of program

MAIN_PROG CODE

start
movlw SERVOCOUNT
movwf loop_count ; init each servo...
movlw servo_start ; start of the array of servo variables.
movwf FSR ; move the address into FSR

INIT_SERVO
; if cycle is 0, means that we are done at this position and must generate new position and cycle count
decfsz INDF
goto SAME_SERVO_POSITION ;cycle isn't 0 yet, stay at same place.
NEW_SERVO_POSITION
;new cycle count
movlw d'32' ;stay here for 32 cycles of 40ms ~1.2Seconds total. TODO replace this with randomizer
movwf INDF
;new servo position
incf FSR
movlw 0x05 ;move to center position of servo high byte TODO: replace this with randomizer
movwf INDF
incf FSR
movlw 0xDC ;move to center position of servo low byte
movwf INDF
incf FSR
decfsz loop_count, 1 ; are we done?
goto INIT_SERVO
goto INIT_SERVO_END
SAME_SERVO_POSITION ;this servo does not need to be moved yet.
incf FSR ;move to next servo
incf FSR
incf FSR
decfsz loop_count, 1 ;are we done?
goto INIT_SERVO
INIT_SERVO_END
goto $

end

No comments:

Post a Comment