Archive

Posts Tagged ‘avr-libc’

Servo Control with an XMega

I’m not going to go into a long treatise on PWM servo signals – there are plenty of references for that already.  I’d like to focus on a simple way to do a bunch of servos with fairly accurate timings on an XMega.  Also, I know this isn’t an optimal implementation – there are efficiency improvement that could be made on almost every front.  But it took me almost no time to come up with, and that says a lot.  I’m more of an idoit than I let on…

The method I’m going to line out below can drive up to 7 servos completely in interrupts.  It’ll use up your timers, but it’s easy peasy and will save you time if you don’t need all your timers for other tasks.

So, we’ve got a standard servo signal that’s composed of basically three stages:

  1. A twenty millisecond low period.
  2. A one millisecond high period.
  3. A variable high period:  To turn the servo to one extreme, force the signal low immediately.  To turn the servo to the opposite extreme, force the signal low after a full millisecond.  To turn it to some point in between, just vary the signal at this point between zero and one milliseconds.

With the advent of the XMega, the AVR series of microcontrollers now have an plethora of peripherals, notably for this article eight 16-bit timers.  EIGHT!  That’s power, baby.  For this example, I’ll only do two servos, which will require three timers.  That seems like a lot because with most ATMegas, that was about it – you only had 4 timers, and only two of those were 16-bit (and 8-bit timers == headaches

I started from App Note AVR1306, which describes the timers.  BTW, app notes are the way to become familiar with the XMegas, especially if you’re coming up from the previous AVR families.  Download the drivers along with the PDF, they’re pretty useful.  Include these in your project.  I’ve used these with only a few modifications, and they’ve worked pretty well for me so far.

Set up a global array that can be used for servo position variables:

volatile uint16_t servo[3];

Note it’s declared volatile – this is necessary for all variables you’ll be using between the main loop and an interrupt service routine.  Also note that I declared 3 spaces for two servos – on this particular one, I liked labeling the servos “Servo1”, “Servo2”, etc. instead of base zero.  Personal preference, but adding the extra byte keeps the numbering consistent between labels and array index.

Next up is the timer initialization routine.  Let’s look at it first.  The comments are fairly explanatory, and I’ll go through it a little more afterwards:

/*! \brief This function initializes timers for use as servo PWM signal drivers.
 *
 *    This function initializes timers for use as servo PWM signal drivers.
 *    It probably wouldn't hurt to improve this by making it modular at some point...
 *
 *    - TimerD0: 20ms servo timer.  This timer triggers the rest of the servo position timers.
 *    - TimerD1: 1-2ms Servo1 position timer.
 *    - TimerE0: 1-2ms Servo2 position timer.
 *
 */
void init_timers()
{
    //! TimerD0: 20ms Servo Timer
    TC_SetPeriod( &TCD0, 2500 );                            // Set period (2500 ticks = 20ms)
    TC0_SetOverflowIntLevel( &TCD0, TC_OVFINTLVL_MED_gc );  // Set Interrupt on Overflow
    TC0_ConfigClockSource( &TCD0, TC_CLKSEL_DIV256_gc );    // Set clock and prescaler (8us per tick)

    //! TimerD1: Servo1 Position Timer
    TC1_SetOverflowIntLevel( &TCD1, TC_OVFINTLVL_OFF_gc );  // Set Interrupt on Overflow, but not yet
    TC1_ConfigClockSource( &TCD1, TC_CLKSEL_DIV256_gc );    // Set clock and prescaler (8us per tick)

    //! TimerE0: Servo2 Position Timer
    TC0_SetOverflowIntLevel( &TCE0, TC_OVFINTLVL_OFF_gc );  // Set Interrupt on Overflow, but not yet
    TC0_ConfigClockSource( &TCE0, TC_CLKSEL_DIV256_gc );    // Set clock and prescaler (8us per tick)

    PMIC.CTRL |= PMIC_MEDLVLEN_bm;
    sei();
}

So, as the above code lines out, TimerD0 is the 20 millisecond timer.  Every twenty milliseconds, it fires.  When it does, it raises the servo signal and then enables TimerD1 and TimerE0, which subsequently time out the 1ms period + position offset.  These functions all come from the application note mentioned above.  The three functions used to set up TimerD0 sets the period, enables the overflow interrupt at the medium tier, and then sets the prescaler.   Pretty straightforward if you use the driver functions.

Here’s the ISR for TimerD0:

/*! \brief Timer/CounterD0 Overflow interrupt service routine
 *
 *  TimerD0 overflow interrupt service routine.
 *  Set for a 20ms period.  Sets servo output port pin, enables TCD1 which resets the pin.
 */
ISR(TCD0_OVF_vect)
{
    uint16_t period;

    /// SERVO1 Timer Enable
    period = 63 + SERVO1_OFFSET + servo[1];                // Calculate period
    SET_SERVO1;                                            // Raise the servo pin
    TC1_SetOverflowIntLevel( &TCD1, TC_OVFINTLVL_MED_gc ); // Set Interrupt on Overflow
    TC_SetPeriodBuffered( &TCD1, period);                  // Start TCD1, set period
    TC1_ConfigClockSource( &TCD1, TC_CLKSEL_DIV256_gc );   // Set clock and prescaler (8us per tick)

    /// SERVO2 Timer Enable
    period = 63 + SERVO2_OFFSET + servo[2];                // Calculate period
    SET_SERVO2;                                            // Raise the servo pin
    TC0_SetOverflowIntLevel( &TCE0, TC_OVFINTLVL_MED_gc ); // Enable timer overflow interrupt
    TC_SetPeriodBuffered( &TCE0, period);                  // Start timer, set period
    TC0_ConfigClockSource( &TCE0, TC_CLKSEL_DIV256_gc );   // Set clock and prescaler (8us per tick)
}

So, it does the following for both servos:

  • Calculates the period.  If everything were a perfect world, the constant 63 should be 125 (and a #define ;) which, with the prescaler given and a 32MHz clock, should be 1ms.  However, between natural delays in the code and the particular servos I was using, I calibrated that offset to be 63 to get the maximum range.  YMMV.
  • Raises the servo signal pin.  This macro is defined in my header file, and is pretty simple: #define SET_SERVO1 PORTD.OUTSET = PIN5_bm
  • Enables the interrupt
  • Sets the period.  You must use the TC_SetPeriodBuffered for this particular application (can’t remember why off the top of my head).
  • Set the clock source to enable the timer.

Did you notice a subtle difference between the TimerD1 and TimerE0 code?  There are two types of timers in the XMegas – Timer0’s and Timer1’s.  Hence, there are TC0_ prefixes and TC1_ prefixes that must be used for appropriate timers.  I’ll have to refer you to the datasheet, because once again I can’t remember what the difference is – it’s not relevant for this example.

Last, let’s look at the position timer ISR code:

/*! \brief Timer/CounterD1 Overflow interrupt service routine
 *
 *  Timer D1 overflow interrupt service routine.
 *  Ends the Servo1 position pulse and disables itself.
 */
ISR(TCD1_OVF_vect)
{
    CLR_SERVO1;                                            // Reset the servo pin low
    TC1_SetOverflowIntLevel( &TCD1, TC_OVFINTLVL_OFF_gc ); // Clear Interrupt on Overflow
    TC1_ConfigClockSource( &TCD1, TC_CLKSEL_OFF_gc );      // Stop clock
}

There’s not much to it:  Reset the signal low, disable the timer interrupt, and disable the timer clock source.  This all gets re-enabled in about 18-19ms!

Now, from just about anywhere in your program, when you want to update the servo position, you just have to change the value of the appropriate servo[] index.  Here’s an example main loop:

int main(void) {
    init_clock();
    init_io();
    init_timers();

    int i;
    for (i=0 ; i<20 ; i+=10)
    {
        servo[1] = i;
        servo[2] = i;
        _delay_ms(1000);
    }
}

I should mention that you need to include “pmic_driver.h” from the PMIC app note AVR1305, and “TC_driver.h” from AVR1306.  Also copy in the matching .c files into the same folder.

That’s really about it.  And it’s easy to add on to this and control another servo:

  1. Add an extra index into the “servos[]” array.
  2. Pick an unused timer and add it to the “timer_init()” function.
  3. Cut, paste, and modify timer code for the new timer in the 20ms timer ISR.
  4. Add an ISR for the new timer, again cutting, pasting, and modifying for the new servo.

That about does it.  It seems to work pretty well for my uses.  Make sure and calibrate the limits on your servo variables so that you maximize your reach (for the servos I tested it with, it was between 0 and 270) .  One problem I had was erratic behavior when it collided with another timer occasionally – this was fixed by adding lifting this up from the “LO” level interrupts to the “MED” level interrupts.  It could also be moved even higher to avoid problems even further.  This is an issue with the XMega, where it wouldn’t necessarily have been with an ATMega, because XMega interrupts have changed and a higher level interrupt can interrupt a lower level interrupt.  Good to keep in the back of your mind as you code…

As always, let me know in the comments if you need any help setting this up.

Advertisements
Categories: How-To Tags: , , , ,

ZephyrEye: LCD functions

11 February 2010 Leave a comment

So, how do I get all these different components to play nicely together?  Lots of time in the datasheets, browsing forums for tips, and a perpetual lack of anything better to do.  That’s because of the inherent coolness of the project, obviously, and not from lack what “y’all call da social skills“…

I’ll be going through each of the components in the chip and talk about how I interfaced them to the ATMega128 AVR microcontroller.  I’ll try to give enough detail so that someone could rewrite this in a different language if they felt like it.  Once the component interfaces have been described, I’ll go through and describe the remaining functions (menuing, GPS, games, etc.).

This handy little LCD display is pretty easy to use.  SparkFun sells them standalone as well as with a sweet little breakout board, which handles the hard to solder connector and funky backlight voltage nicely, allowing just about anybody with a spare micro lying around to use this display in their projects.  Here are some simplified pin descriptions (but remember the datasheet is the final reference) for those setting up their own display.

  • 1) V_digital: Supply voltage for internal logic circuitry.  Should be separate from V_display.
  • 2) LCD_reset: Resets the LCD.  Necessary for communications.
  • 3) DIO: Data input pin, connect to the MOSI line of your MCU’s SPI hardware.
  • 4) SCK: Serial clock input pin, connect SCK line of your MCU’s SPI hardware.
  • 5) CS: Connected to the chip select line of your MCU’s SPI hardware
  • 6) V_DSP: Display voltage.  This supply voltage is the one that actually drivers the liquid crystals, and therefore needs to be very solid.  The datasheet recommends it has its own voltage regulator, which is not always necessary (it can be tied to Vcc) but I have conceded as it reduces flickering.
  • 8 & 9) GND & LEDGND: Connect to ground.
  • 10) V_LED: White LED Backlight voltage.  +6.8V required to drive these little suckers.  I’ve used the TPS61040 boost regulator to do this, it works nicely.

Note that pin 7 is NC.  It’s also worth pointing out, since this is a battery driven project, that it’s useful to have enable lines connected to GPIO pins on your MCU.  In particular, I’ve been pleased that the +6.8V regulator can simply be turned off when the display is dormant – this saves between 40 and 60 milliamps, which can extend battery life a LOT.

BASCOM-AVR has some very nice routines to use knock-off cell phone displays with the Phillips and Epson chipsets.  However, it didn’t quite work with this particular display from SparkFun so well.  Luckily, I’d already been using this display for a while before Bascom added a library for it, and by using my init routine after calling theirs, it seemed to work out OK.  Here’s the relevant pieces of code (remember, the code can be found on the Google Code repository).

If this LCD worked directly with BASCOM, setting it up would be easy peasy:

'*******************************************************************************
  'Configure Display
$lib "LCD-epson.LBX"                                        'Library for LCD screen
Config Graphlcd = Color , Controlport = Porte , Cs = 5 , Rs = 6 , Scl = 3 , Sda = 4
'*******************************************************************************

I had to add on my custom init routine to get things to work right, as well as a few helper functions:

'************************************************************
'Sends initialization data to LCD screen
Sub Init_lcd()
   Lcd_cs = 0

   Waitms 10

   Snd_cmd Disctl
   Snd_data &H03
   Snd_data 32
   Snd_data 12
   Snd_data &H00

   Waitms 10

   Snd_cmd Comscn
   Snd_data &H01

   Snd_cmd Oscon
   Snd_cmd Slpout

   Snd_cmd Volctr
   Snd_data 5
   Snd_data &H01

   Snd_cmd Pwrctr
   Snd_data &H0F

   Waitms 100

   Snd_cmd Disinv

   Snd_cmd Datctl
   Snd_data &H00
   Snd_data 0
   Snd_data &H01
   Snd_data &H00

   Snd_cmd Rgbset8                                          'Set up the color pallette
   'RED
   Snd_data 0
   Snd_data 2
   Snd_data 4
   Snd_data 6
   Snd_data 8
   Snd_data 10
   Snd_data 12
   Snd_data 15
   'GREEN
   Snd_data 0
   Snd_data 2
   Snd_data 4
   Snd_data 6
   Snd_data 8
   Snd_data 10
   Snd_data 12
   Snd_data 15
   'BLUE
   Snd_data 0
   Snd_data 4
   Snd_data 9
   Snd_data 15

   Snd_cmd No_op

   Snd_cmd Paset
   Snd_data 2
   Snd_data 131

   Snd_cmd Caset
   Snd_data 0
   Snd_data 131

   Snd_cmd Ramwr

   Clr_scr 255

   Snd_cmd Dison

   Waitms 200

   For B = 0 To 140
     Snd_cmd Volup
     Waitms 2
   Next I
End Sub
'*******************************************************************************

Wow, I should have commented that a LOT better … bad Brad! (hitting myself with rolled up newspaper)

Here’s the helper functions.  Normally I’d just refer you to the code, but for those of you trying to get this LCD to work, using the 9-bit frame is a little tricky.  I will refer you to the code for pin definitions and constants, however.  I ended up bit-banging it – YMMV.  Be careful with the polarity and phase of the SPI signal, it’s very particular.  Lastly, note the syntax of BASCOM-AVR for addressing a bit in a byte is the dot operator (e.g. Dab.7 returns the value of the 7th bit of the byte variable Dab).

'*******************************************************************************
Sub Snd_data(byval Lcddata As Byte)
  Lcd_sck = 0
  Lcd_dio = 1                                               'Data = 1
  Lcd_sck = 1

  Shiftbits Lcddata
End Sub
'************************************************************

'*******************************************************************************
Sub Snd_cmd(byval Lcdcmd As Byte)
  Lcd_sck = 0
  Lcd_dio = 0                                               'Commands = 0
  Lcd_sck = 1

  Shiftbits Lcdcmd
End Sub
'*******************************************************************************

'*******************************************************************************
Sub Shiftbits(byval Dab As Byte)
   Lcd_sck = 0
   Lcd_dio = Dab.7
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.6
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.5
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.4
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.3
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.2
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.1
   Lcd_sck = 1
   Lcd_sck = 0
   Lcd_dio = Dab.0
   Lcd_sck = 1
End Sub
'************************************************************

After that, it’s smooth sailing as all of BASCOM’s internal drawing and text display functions work just fine.  Here’s all it takes to draw the setup screen.  The “splash” picture was included in the program, and is loaded from flash program space.  Pixels are set using the Pset command.  Drawing text at certain coordinates and with a defined forecolor and background is done using the Lcdat command.

'Display Picture
W1 = 0
For Y = 2 To 80
  For X = 0 To 127
    B1 = Lookup(w1 , Splash)
    Pset X , Y , B1
    Incr W1
  Next X
Next Y

Setfont Color8x8

Lcdat 88 , 20 , "Start Game" , Black , Gray
Lcdat 96 , 20 , "Join Game" , Black , White
Lcdat 104 , 20 , "Options" , Black , White
Lcdat 112 , 20 , "Settings" , Black , White
Lcdat 120 , 20 , Unit_name , Gray , White

... (near bottom of program)

'Extra include files - Fonts
$include "color16x16.font"
$include "color8x8.font"
$include "smallfont.font"

$include "zephyreyepic.txt"

So there you have it, how to use the SparkFun LCD display.  Hopefully that helps somebody understand how to use it.

As I’ve stated before, I also hope the lengthy explanation helps get all this converted into AVR-GCC.  As a side note, I would like to develop the component libraries into a more useful place: the avr-libc-corelib project.  This would make libraries for all the components readily available for others to use.

As always, if you have any questions about using this display, feel free to leave comments or email me!