Keil Logo

C166: How to Load PEC Registers of XC16X Devices

Information in this article applies to:

  • C166 all Versions


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 split 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 "intrins.h"

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

const char string1 [] = "0123456789\n";

#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)


  • Refer to _sof_ in the C166 User's Guide.


Last Reviewed: Thursday, February 25, 2021

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.