ATmega328 8 bit counter external clock and UP/DOWN count

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have spent some time reading into the aymega328 8 bit timer counter trying to understand how to implement

a) external clock input
b) UP / DOWN count direction based on external conditions

I am having some difficulty with the second point. I can not see if it is possible to do that; namely change direction of count based on external ( to the counter control logic ) conditions such that the counter acts as a dynamic hardware register.

The idea being that a direction and distance information is used to establish a servo motor ( not the aircraft model servo motor) where the content of the counter represents the the position of the motor.

Any and all comments apropos the topic welcome.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is called an incremental encoder. AFAIK only AT90PWM avrs have this feature.
If you would like to emulate that on AVR, then I suggest some external chip or (tricky)software.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brute,

No need in this instance as I am not using an incremental encoder; no quadrature signals only a single pulse train and up/down flag.
What I want to do is update counter content using these two signals.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is your uC so busy you cannot do it in software?
If that is the case then you could try using two counters and clock first one with every edge and the second one only when counting backwards. Then:

position = first-2*second; //and some overflows

Or first timer counting forwards and second counting backwards etc.
And if that is super fast then pick 16-bit counter and ICP every direction change.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AFAIK Xmega have UPDOWN (search Xmega family docs for that).

I have a couple AVR apps that do the up/down position counting as you described. Both are simulators/"test rigs" for testing "real" encoders. I simply look at the DIR signal when the pulse occurs.

(It depends on data rates and whether the signal(s) are "clean" or not whether I interrupt on the trigger edge or fast-poll and debounce.)

In one instance I have separate UP and DOWN pins.

The end result is essentially the same as x1 quadrature (if only looking at one edge of the trigger signal) or x2 quadrature (if looking at both edges of the trigger signal).

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The intention here is to drive a DC motor with a simple single channel transducer as feedback.
The hardware counter is to log the transducer pulses.
Up and down flags to retain track of shaft position.
Range of rotation ( maximum shaft excursion ) is less than the maximum counter range.
No interrupts, no sftware overheads other than flag setting and interrogation of counter content.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atmel confirm ATmega do not support the function but Xmega do.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The intention here is to drive a DC motor with a simple single channel transducer as feedback.

I wonder how (from technical point of view) are you going to make the signal of sign of first time derivative of position (that you call a "direction") and synchronize it with transducer..
That looks like a challenge if you do not plan to loose steps when the shaft is ~stalled and vibrating.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brute,

No need to worry about that.
The motor is a brushed DC motor operating through a worm gear transmission.

The servo is operating as a positional loop.
Current position is in the counter register.
UP/DOWN flag determined by comparison between current position in the counter register and newly desired position.
Motor drive derived from difference between the counter register content and newly desired position and used to generate a PWM output analogue much as UP DOWN flag. Counter input provided by a single hall sensor sensing rotation of permanent magnet on the motor shaft.

Not a new idea readily implemented on a 16F84 micro from memory.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am totally confused.
I thought that your hardware gives:

    a) signal from "single channel transducer" (like optical interrupter) b) direction of revolution of the shaft
and you wanted to interface those two binary signals into an AVR to implement a closed loop position controller.

But now you write that:

Quote:
Current position is in the counter register

then what is the point of this:
Quote:
The intention here is to drive a DC motor with a simple single channel transducer as feedback.

when you already have the exact position in some counter register?

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On point a) that is so.
On point b) A new position instruction is issued and the controller drives the motor accordingly.
Advantage of using the timer counter register with external signal is o intermediate interrupts, the content of the register always contains position information.
It seems a shame to let that bit of hardware go to waste.
The UP/DOWN signal would be generated by positional error signal to increment or decrement the content as the motor shaft rotation generates a single pulse train.

A for instance....

Let us refer to shaft position as percentage of maximum.

The register says the shaft is at 60% position.
An instruction word comes in to send the shaft to 35% position.

An error of -25% is generated. It will then cause the counter DOWN flag to be activated and the 25% error will be applied to a PWM signal to drive the motor in the DOWN direction.
Over time the error will reduce and overshoot and return back and forth which is not the topic of this discussion.
Simply by having access to the counter register and external clocking and UPDOWN flag the control problem is made simpler.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
the content of the register always contains position information
Except when power goes off? ie a QLD storm :wink:

If it's related to our discussion a while ago how will you know the position of the motor after the system restarts?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The motor has a current overload supervisory circuit.
It is driven to the point where the overload takes place and the overload condition is sensed.
This position plus a small offset represents 0% setting.
The 100% setting is in E prom, squirreled away during commissioning.

This is kind of tangential. The customer has become potential..possibly... has decided to go with a low cost clone of a Siemens smart relay kind of thing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I do not understand a thing - I give up :cry:

No RSTDISBL, no fun!