Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Connecting Servo to 68HC11 using Assembly Language

Status
Not open for further replies.

arfin

New Member
Hi all, i'm really interested in interfacing my 68HC11 MCU with a servo. I've used the mcu for small programs, and only 1 program that interfaces with hardware. It was just a led blinking program using XIRQ and IRQ to change the speed of the blinks.

I'd really appreciate it if someone could give me a little help with this. I'd like the language to be in assembly as that is the only language i understand.

From what i understand, i just need a PWM signal, but i have no idea how to create this signal. I have no experience with the timer system, but from what i have read this is information i need for this program.

I appreciate the help!
 
arfin said:
Hi all, i'm really interested in interfacing my 68HC11 MCU with a servo.
Tom Dickens to the rescue! :D
**broken link removed**

Top of the 68HC11 section of his site:
**broken link removed**

Other links:
**broken link removed**
**broken link removed**
 
Last edited:
Thanks for the help futz. I've been to that website, but i wasn't sure which pin ont he MCU board sends out the PWM signal. Basically i don't know how to wire the servo to the mcu.
 
arfin said:
Thanks for the help futz. I've been to that website, but i wasn't sure which pin ont he MCU board sends out the PWM signal. Basically i don't know how to wire the servo to the mcu.
Look thru the code. I assume it should be pretty obvious. I'll look now. BRB...
 
To connect a servo to a micro, you connect the servo's red wire to 5V, the black wire to ground and the white wire to signal, or the micro pin. Then you just have to write software to send a variable pulse train to the signal pin to tell the servo where to rotate to.

Those wire colors are typical - not every servo follows this standard exactly. Here's another link with more info and a chart of various servo wire colors: https://wolfstone.halloweenhost.com/Motors/svoint_RCServos.html

Botboard servos are on PA3, PA4, PA5 and PA6, which are the Timer Output Compare (TOC) pins. Tom's code there uses two of those, I think PA5 and PA6.

Here's a link to a Botboard PDF with schematic and other details.
https://www.electro-tech-online.com/custompdfs/2007/12/botboard1.pdf
 
Last edited:
Thanks for helping futz, i hate to make you do so much work. It's getting late so i can't try the program today, but as soon as i get it working i'll report back.
 
One more piece of code that may help:
View attachment SERVO8_A.asm

And pages 47 and 48 of Karl Lunt's book, Build Your Own Robot have some good info.

You may find that the servos draw too much power (they can be greedy, especially when loaded), causing power sags and causing your 68HC11 to reboot when they move. In this case you need to power the servos from a separate power supply. Connect the servo power and ground to the separate power and leave the signal wire on the 68HC11. Be sure to tie the grounds of the 68HC11 and the servo power supply together. It won't work if you don't do that.
 
Last edited:
arfin said:
From what i understand, i just need a PWM signal, but i have no idea how to create this signal. I have no experience with the timer system, but from what i have read this is information i need for this program.
Have no fear. Timers are really a lot simpler than they at first appear to be. When you first read the datasheet timer chapter they seem VERY intimidating. Once you dive in and think and calculate a bit you'll quickly realize that they're not so tough. There's just a lot of options.
 
I've been tinkering with the program, and you are right futz, PA6 and PA5 are the servo connections. So far when the servo's signal wire is connected to PA6 it moves to one direction, and PA5 to another.

I was wondering, is it possible to make the servo move to the left, then to the right, then stop in the middle, using only one of the signals PA6? I'll give it a shot tonight and let you know, i just want to know if that is possible before i attempt it. I don't see why it wouldn't be.
 
arfin said:
I've been tinkering with the program, and you are right futz, PA6 and PA5 are the servo connections. So far when the servo's signal wire is connected to PA6 it moves to one direction, and PA5 to another.
That's because he set one timer to one value and the other to another value to demonstrate the thing. In this piece of the code he's setting the duty cycle for the two timers that generate pulses on PA5 and PA6. I don't know off hand which is which - would have to dig out my reference manual. Fiddle with it and you'll soon figure it out.
Code:
	ldx	#$0A50
	stx	$1018	 ;Set Timer 2
	ldx	#$07D0
	stx	$101a	 ;Set Timer 3
	bra Main

Chances are good that you're using a different model of HC11 than his code, so you'll probably have to change the org address to suit your memory map. Also if your clock speed is different you may have to adjust the timer values.

I have it all connected up with a Techno Arts MicroStamp11 32K Turbo board and of course it won't run as is. I'm working now on getting it to move servos properly.

I was wondering, is it possible to make the servo move to the left, then to the right, then stop in the middle, using only one of the signals PA6? I'll give it a shot tonight and let you know, i just want to know if that is possible before i attempt it. I don't see why it wouldn't be.
Yes. One signal pin per servo. You only need to change the one variable to lengthen or shorten the duty cycle on the timer interrupt that's generating the pulse train for that pin.
 
Last edited:
Hey Arfin. I finally got a servo working on my Microstamp11.

I'm going to post the code here for you, but you should read the following notes if you plan to port the code to your micro. Your 68HC11 is very likely to be an E model or other, and not a D. Therefore the code won't work without some small changes.

The MS11 runs a 68HC11D0 (no internal program memory) and gives up two of its ports as address/data bus for 32K of EEPROM in expanded mode. It has only Ports A and D available to the user.

MS11 program memory starts at $8000. Yours is probably $b600.

MS11 is clocked at 9.8304MHz. Yours is probably 8MHz.

The MS11 register block is mapped at $00 to $3f. Yours is probably mapped at $1000 to $103f.

MS11 RAM is mapped from $40 to $ff. 192 bytes. Can't remember if other HC11's have the same or more. The location is almost certainly different though.

This was Tom Dickens code, but I have heavily modified it to suit. I'm only using OC2 (PA6) to control one servo. Put an LED with current limiting resistor on PD4 - anode to PD4. It will light when the servo is rotated CCW and turn off when servo is rotated CW. It just goes back and forth forever. Does nothing useful at present.

My servo may be different than yours. Listen to it carefully when it's at the ends of its travel. If it's stalling at either end you'll have to modify the numbers I'm stuffing into TOC2 in main:. Mine stops a bit before the end stops at each end.

If you want the include file just ask. You'd have to modify it to suit your register block mapping (no biggy).

Here's a couple pics. The MS11 and breadboard are mounted on an old robot base:
**broken link removed**
**broken link removed**

Code:
#include	register.inc

	ORG 	$8000
start:	ldaa	#4		;disable COP in CONFIG register
	staa	CONFIG
	lds	#$00ff		;stack pointer = top of ram
	ldaa	#$ff		;PORTD all outs
	staa	DDRD

	ldaa	#$00		;init interrupts
	staa	OC1M		;Out compares off
	staa	TCTL2		;In captures off
	staa	TMSK1		;Timer interrupts
	ldaa	#$FF
	staa	TFLG1		;Clear interrupts
	ldaa	#$F0
	staa	TCTL1		;T2 T3, set to 1
	ldaa	#$60
	staa	CFORC		;Force T2 T3 to 1
	ldaa	#$50
	staa	TCTL1		;T2 T3 set toggle
	ldaa	#$80
	staa	TMSK2		;Timer o-flow int
	staa	TFLG2		;Clear interrupt
	cli			;Enable interrupts

main:	ldx	#$14ff		;servo full CCW
	stx	TOC2	 	;Set Timer 2
	bset	PORTD,4		;LED on
	ldy	#$00ff		;delay 
	bsr	delay

	ldx	#$0580		;servo full CW
	stx	TOC2 
	bclr	PORTD,4		;LED off
	ldy	#$00ff		;delay again 
	bsr	delay
	bra 	main

delay:	ldx	#$0fff		;inner delay loop 
delay2:	dex
	bne	delay2 
	dey
	bne	delay 
	rts 

isr:	ldaa	#$00
	staa	TCTL2		;In captures off
	ldaa	#$F0
	staa	TCTL1		;T2 T3, set to 1
	ldaa	#$60
	staa	CFORC		;Force T2 T3 to 1
	ldaa	#$50
	staa	TCTL1		;T2 T3 set toggle
	ldaa	#$80
	staa	TFLG2		;Clear interrupt flag
	rti

*  Interrupt and reset vectors.
		org	$FFD6
SCI_VECT	fdb	start		;ffd6
SPI_VECT	fdb	start		;ffd8
PAI_VECT	fdb	start		;ffda
PAO_VECT	fdb	start		;ffdc
TOF_VECT	fdb	isr		;ffde

TOC5_VECT	fdb	start		;ffe0
TOC4_VECT	fdb	start		;ffe2
TOC3_VECT	fdb	start		;ffe4
TOC2_VECT	fdb	isr		;ffe6
TOC1_VECT	fdb	start		;ffe8
TIC3_VECT	fdb	start		;ffea
TIC2_VECT	fdb	start		;ffec
TIC1_VECT	fdb	start		;ffee

RTI_VECT	fdb	start		;fff0
IRQ_VECT	fdb	start		;fff2
XIRQ_VECT	fdb	start		;fff4
SWI_VECT	fdb	start		;fff6
TRAP_VECT	fdb	start		;fff8
COP_FAIL_VECT	fdb	start		;fffa
COP_CMF_VECT	fdb	start		;fffc
RESET_VECT	fdb	start		;fffe
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top