Microprocessor Formulas

Microprocessor Formulas

Microprocessor Formulas are a new way to define and store solutions to common software problems faced by microprocessor programmers. The most common problem is how to get various hardware peripherals working. Many of us have wasted hours and hours over a few lines of code, for example trying to get Timer 2 to interrupt properly, or getting EEPROM storage to work. The same problem is laboriously solved again and again by many people, clearly an unnecessary waste of time. There must be a better way!

The aim of Microprocessor Formulas is to build a complete library of simple code formulas (code examples). This is a huge job and you can help (and get your name as a formula author), see “How You Can Help” below.

Contents

Introduction

Microprocessor Formulas

What’s in a Microprocessor Formula?

How You Can Help

Blank Proforma

Introduction

The ability to understand and re-use the work of others is essential to modern civilization and especially technology. There is one area that is definitely a little slow in this area – hardware interface and control for microprocessors. Most manufacturers will supply a good quality data sheet that describes the registers inside a microprocessor, and what each register can do. What’s missing is the sequence of register operations and other software that will achieve a goal, for example how to get Timer 0 to cause a clock tick interrupt. Even as an experienced microprocessor programmer , I have wasted a lot of time trying to work out the register operations myself, or hunting for software on the web. Many other people have just the same experience.

What has the rest of the software world done? A lot! A software programmer typically has access to-

  • Data structures: these are ways to organize data. For example an array or a link list is a data structure. Usually algorithms (see below) operate on data structures to achieve some stated goal. There are many books and web sites that list and detail data structures, for example http://cs.stonybrook.edu/~algorith/implement/takaoka/implement.shtml.

    Illustration: In baking, a data structure equivalent is the ingredients that make up the recipe, and the arrangement of those ingredients.

  • Algorithms: an algorithm shows in code or pseudo-code how to achieve a certain goal, for example how to sort data using the bubble sort technique.
    There are vast repositories of algorithms in books or on the web, try the following as start-
    http://ww3.algorithmdesign.net/software.html and http://en.wikipedia.org/wiki/Algorithm which has good references at the end of the article.

    Illustration: In baking, an algorithm would be a recipe to cook a specific item given a set of ingredients, for example cooking a specific type of fruit cake.

  • Software Design Patterns: these represent conceptual solutions to common problems. For example the Facade pattern shows how many interfaces to several software elements can be replaced with one interface (which is more maintainable and easier to use). Patterns are not algorithms as they deal with the concept level not the code level.
    Again there are vast repositories of software patterns in books and on the web, see the references at the end of http://en.wikipedia.org/wiki/Design_pattern_(computer_science).

    Illustration: In baking, one pattern would be “whipping eggs”. This activity can be applied to a vast variety of recipes and can be scaled up or scaled down to suit the amount of a whipped egg required.

Question: where are the carefully structured repositories for microprocessor code that shows how to use each peripheral or solve problems specific to microprocessor software? Well as far as I can see there isn’t much at all. To be sure there are many good web sites and books that show a specific algorithms or applications, but there is no structured repository or even the concept of such.

Does this mean we are doomed to roam the Internet for hours on end trying to find examples of how to make Pulse Width Modulator 2 work? There must be a better way!

Microprocessor Formulas

Microprocessor Formulas” is a new term to describe the concept of a structured repository of algorithms (formulas) that solve problems specific to microprocessor software design. These algorithms will often be related to the initialization and control of hardware peripherals but may be related to generic problems faced specifically by microprocessor programmers.

Naming conventions: microprocessor formulas shall be categorized in the format “Scope:Title”-

  • Microprocessor-Name formula: Short Title.
    Scope = a particular microprocessor chip, eg ATMEGA32
    Example:
    ATMEGA32 formula: TIMER 0 Clock Tick Interrupt.

  • Microprocessor-Family formula : Short Title.
    Scope = a family of microprocessor chips, eg AVR family of microprocessors.
    Example:
    AVR formula: Interrupt Handling.

  • Generic formula : Short Title.
    Scope = most microprocessors.
    Example:
    Generic formula: Executive Loop.

It will take a lot of time and effort to generate a near complete set of microprocessor formulas, later in this web page you will read how you can contribute.

How to store Microprocessor formulas: there are many variations on microprocessors and languages used. Some guidelines are needed to maintain order and make a formula easy to find, or know where to place a new one. The following may seem boring, but it is essential.

  • Key word list: maintain a list of key words to use in descriptions. The microprocessor data sheet, and the names used in the compiler are a good place to start.
    For example timer 0 could be indicated in a title with “TIMER0” “timer0” “timer 0” “TMR0” and other variants. Searching for anything to do with timer 0 could become a nightmare.
    The solution is to have an alphabetically sorted list of agreed key words which people should use when creating a formula name, or when searching for a formula.

  • A storage hierarchy of folders is required. In general 3 levels are required as illustrated in the example below. For reading convenience only, the directories are shown in upper case and the files are shown in lower case:

 --+--keywords.txt
   +--GENERIC +------ executive_loop.c
   |          '------ warm_cold_start.c
   |
   '--AVR-+-----------ATMEGA32_AVRGCC--+--atmega32_timer0_overflow_intr.c
          |                            +--atmega32_timer0_PWM.c
          |                            '--aaa_readme.html
          |
          +-----------ATMEGA32_STUDASM-+--atmega32_timer0_overflow_intr.asm
          |                            +--atmega32_timer0_PWM.asm
          |
          +-----------TINY26_AVRGCC----+--tiny26_timer0_overflow_intr.c
          |                            '--atmega32_keywords.txt
          |
          '-----------atmega32_keywords.txt
  • Rules for the hierarchy are as follows:
    1. Top level: file of keywords called keywords.txt, folders “generic” and any microprocessor families (eg AVR).

    2. Microprocessor and Language: within each microprocessor family folder, one folder for each microprocessor and language/compiler.
      Example folder names: ATMEGA32_avrgcc ATMEGA32_studioasm
      A microprocessor family or chip keyword list may be stored here.
      Example
      ATMEGA32_keywords.txt

    3. Formula files: within each specific microprocessor folder there are formula files which contain compilable code. Normally there is one file per formula.
      The naming shall be of form
      micro_name_description
      For example atmega32_timer0_overflow_intr.c
      This folder may also contain readme file specific to the microprocessor and other related files.
      Example files:
      ATMEGA32_avrgcc_make_file.txt, ATMEGA32_readme.html
      A microprocessor specific keyword list may stored be here.

What’s in a Microprocessor Formula?

A formula will normally contain more text than code because a formula must explain such things as when the formula is useful, the resources it consumes, and its limitations.
Code must be as simple as possible, but result in an output which is physically observable.

The example below should be used as a proforma and all headings must be kept even if unused.

/*------------------ timer0_overflow_intr --------------------------

ATMEGA32_AVRGCC FORMULA : TIMER0 clock tick interrupt.

PURPOSE : show how Timer 0 overflow can be used to cause an regular
clock tick style interrupt on an ATMEGA32.

RESOURCES USED : Timer 0

RESOURCES BLOCKED : The PWM related to Timer0 is not usable.

LANGUAGE AND COMPILER : C compiled by AVR-GCC 

ADVANTAGES, DISADVANTAGES, and COMMENTS
  * Stable period with little jitter.
  * Counter increments only at fixed frequencies,
      at Ck, Ck/8, Ck/64, Ck/256, Ck/1028.
      See register TCCR0 for how to choose the frequency.
  * Interrupt occurs every 256 counts.
  * If the global interrupt flag is disabled in SREG, perhaps
    temporarily by another interrupt, then this interrupt
    will be delayed until the flag is enabled.
  * Timing example,  12 MHz crystal,  Ck/1024,
      PB0 changes at 12,000,000 MHz / 1024 /256 = 45.8 Hz
      PB0 frequency (2 changes per cycle) = 22.8 Hz
      You can see an LED flicker at this frequency.

EXTENSIONS and VARIATIONS
  * The period could be shortened by setting TCNT0 count
    register on interrupt.
      TCNT0=0   : same frequency as above, eg 22.8 Hz
      TCNT0=128 : double frequency above,  eg 45 Hz.
      TCNT0=255 : 256/(256-255) times frequency above,  eg 5859Hz.
  * Period of square wave = 2* (256 - START_CNT) * X / xtal_frequency
    X is divisor (1, 8, 64, 256, 1028).

RELATED FORMULA
  * See any pattern with the key word "timer".

OBSERVABLE RESULTS: in the code below, PB0 (pin 1) toggles at 22.8 Hz
for a 12 MHz crystal.  If this drives an LED then the flicker is visible.

AUTHORS & DATES : Dr. Pj Radcliffe 15/09/2009

LICENSE: GPL (always GPL!).

*/

//--------------- HEADER FILES----------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>

//--------------- GLOBALS --------------------------------------

// Start counting value for timer 0 up counter.
#define START_CNT 0
// Double frequency
//#define START_CNT 128 

/*-------------- Timer0 Overflow Interrupt --------------------

  PURPOSE - This is the interrupt service routine for timer 0 overflow.

     NOTE - The CPU automatically calls this routine when TIMER0 overflows.
          - The ISR automatically handles the saving and restoring
            of registers.
*/
ISR(TIMER0_OVF_vect)
{ TCNT0 = START_CNT ;
  PORTB ^= 0b00000001  ; // toggle PB0
}

//-------------- MAIN FUNCTION-----------------------------------

int main()
{//----- Port initialization, just to enable PB0 to toggle.
   DDRB  |= 0b00000001 ;     // set PB0, pin 1, to an output.
   PORTB &= 0b11111110 ;     // set PB0 to zero.

 //------ Timer 0 initialization.
   TCNT0  = START_CNT ;      // Initialize Counter 0.
   TCCR0  = 0b00000101 ;     // Prescaler = CPU clock/1024
   TIMSK |= (1<<TOIE0);      // Enable Overflow Interrupt Enable

 //------ Enable Global Interrupts.
   sei();

 //------ forever loop
   for (;;)
     {
     }

   return(0) ;
  }

How You Can Help

A microprocessor such as the Atmel ATMEGA32 will require at least a hundred Microprocessor Formulas to fully describe its hardware functionality. This would be very useful to people BUT it’s a HUGE amount of work. This is just the type of project that is well suited to the Open Source community.

If you can, please generate an ATMEGA32 Microprocessor algorithm based on your projects and email it to me at pjr@rmit.edu.au. It will place it on the web site and of course your name as author will be kept. Please take care with your text and example code, ideally show it to another programmer before sending it to me.

What of other microprocessors? Hmmm …..

Blank Proforma

Below is a blank proforma you can cut and paste to create your own Microprocessor formula.

/*------------------ formula_name.c --------------------------

ATMEGA32_AVRGCC FORMULA : very short title (replace ATMEGA32_AVRGCC if needed)

PURPOSE : explain the basic idea.

RESOURCES USED : 

RESOURCES BLOCKED : 

LANGUAGE AND COMPILER : C compiled by AVR-GCC 

ADVANTAGES, DISADVANTAGES, and COMMENTS
  * 

EXTENSIONS and VARIATIONS
  * 

RELATED FORMULA
  * 

OBSERVABLE RESULTS: what can be observed on pins?

AUTHORS & DATES : Your name, date completed

LICENSE: GPL (always GPL!).
*/

//--------------- HEADER FILES----------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>

//--------------- GLOBALS --------------------------------------

// #defines, global variables. 

/*-------------- Function Name ---------------------------------

  PURPOSE - 

     NOTE -
*/
void function_name
{
}

//-------------- MAIN FUNCTION-----------------------------------

int main()
{//----- Port initialization, just to enable observation of effects.

 //------ Initialization of formula.

 //------ forever loop, if required.
   for (;;)
     {
     }

   return(0) ;
  }
%d bloggers like this: