ARM Technical Support Knowledge Articles


Applies to: C166 C Compiler


Information in this article applies to:


The PEC registers of Infineon XC16x devices need to be loaded with addresses of variables and SFR's. How can I separate an address into a segment number and a 16-bit offset to load these PEC source and destination registers?


The library function _sof_ and a cast can be used for this. These expressions convert any address (near,far,idata,sdata etc.) into a linear address and splits this up to a segment number and 16-bit segment-offset.

The following example program should demonstrate this. It transmits a string out the serial port using PEC channel 0.

#include <XC167.H>
#include "intrins.h"

#define CLOCK 20000000UL // CPU clock when PLL is enabled

const char string1 [] = "0123456789

#define _seg_(adr) (unsigned int)(((unsigned long)(void huge *)adr) >> 16)

// This function initializes PEC Channel 0 to move the data from
// 'string1' to the serial transmit buffer.

void serial_PEC0_setup (void)
  PECC0   = 0x0500 |                  // Move Bytes, Inc Src Ptr
            ((sizeof (string1) / sizeof (string1 [0])) - 1);
  SRCP0   = _sof_ (string1);          // Source is 'string1'
  DSTP0   = _sof_ (&ASC0_TBUF);       // Destination is ASC transmit buffer
  PECSEG0 = (_seg_ (string1) << 8) |  // high byte = Segment of Source
            (_seg_ (&ASC0_TBUF));     // low byte  = Segment of Destination

// The Serial TX interrupt just reloads PEC 0 and starts the transfer of
// another copy of 'string1'.

void serial_TX_irq (void) interrupt S0TINT = 42
  serial_PEC0_setup ();

// The setup routine for the serial port also initialized the PEC 0
// transfer and sets a TX interrupt request

void serial_setup (unsigned int baudrate)
  DP3       |=  0x0400;   // Set TXD for output
  DP3       &= ~0x0800;   // Set RXD for input
  P3        |=  0x0400;   // Set TXD high
  ALTSEL0P3 |= 0x0C00;    // Configure port pins for ASC0
  ASC0_CON   = 0x8011;    // 8 bit async mode
  ASC0_TIC   = 0x00F8;    // Serial TX IRQ = Level 14, Priority 0 (PEC 0)

  ASC0_BG  = (unsigned int)(CLOCK / (32UL * (unsigned long) baudrate));

  serial_PEC0_setup ();

  PSW_IEN = 1;            // Enable interrupts

void main (void)
  serial_setup (19200);   // setup serial interface for 19200 baud

  while (1)



Article last edited on: 2007-03-05 07:55:51

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential