Colin Gribben

Program help NEEDED

Discussion created by Colin Gribben on Apr 26, 2011
Latest reply on Apr 26, 2011 by Colin Gribben

Hi all

 

I am trying to creat a program for a window cleaning gondola system. Using the Freescale HCS12 microcontroller and the Metroworks CodeWarrior Intergrated Development Environment. I will also be using the HCS12E128 target board.

 

I have written the following code and my program works but i am also trying to add in an interrupt so that an LED will blink every 0.5 seconds when the prgram is running. Here is my program below.

 

// Author:- C G

// Window cleaning gondola system

 

#include <hidef.h>                  /* common defines and macros */

 

#include <mc9s12e128.h>      /* derivative information */

 

#pragma LINK_INFO DERIVATIVE “SampleS12”

 

 

Static int setpoint = 0;

 

//---------------------------------------------------------------------------------------------------------------

// Puts the following code into NOT BANKED FLASH MEMORY

//---------------------------------------------------------------------------------------------------------------

 

#pragma TRAP_PROC

 

#pragma CODE_SEG_NEAR_SEG NON_BANKED

 

 

//---------------------------------------------------------------------------------------------------------------

// Initialise peripherals

//---------------------------------------------------------------------------------------------------------------

 

 

#define PANIC            0x01    // Switch 0 PANIC button

#define GROUND       0x02    // Switch 1 Ground reached switch

#define DOWN_L       0x04    // Switch 2 DOWN LOW speed

#define DOWN_H       0x08    // Switch 3 DOWN HIGH speed

#define UP_L              0x10    // Switch 4 UP LOW speed

#define UP_H             0x20    // Switch 5 UP HIGH speed

#define PROXIMITY 0x40   // Switch 6 Ground proximity detector

 

#define ALARM          0x01    // Led0 PANIC alarm signal

#define ALARMOFF   0xFE 

#define DOWN             0x02   // Led1 DOWN control output

#define DOWNOFF   0xFD

#define SPEED            0x04    // Led2  SPEED ratio control signal

#define SPEEDOFF  0xFB

#define LED3 0x08

#define LED4 0x10

#define LED5 0x20

#define LED6 0x40

#define OPERATIONAL 0x80   // Led7 System heatbeat

#define OPERATIONAL 0x80

 

 

//---------------------------------------------------------------------------------------------------------------

// Real Time interrupt routine

//---------------------------------------------------------------------------------------------------------------

 

Void RTIHan(void){/*periodic interrupt */

 

 

setpoint++;   // Increment setpoint each time there is an interrupt

 

PTT^= 0x01;  // Toggle Bit 0 of Port T

 

CRGFLG = 0x80; // Acknowledge by clearing RTIF (by writing a ‘1’// to the bit position)

 

#pragma CODE _SEG DEFAULT

 

#pragma CODE_SEG DEFAULT

 

//---------------------------------------------------------------------------------------------------------------

// Peripheral Initialization routine

//---------------------------------------------------------------------------------------------------------------

 

Void PeriphInit(void)

 

{

    // Set up digital output

    PTT = 0x00;                        // Clears port T[7..0]

    DDRT = 0xFF;                    // Configures port T[7..0] as output

   

    // Set up digital input                               (using ADT pins for digital input)

    ATDDIEN0 = 0x00;                // turn off pins 8 to 15

    ATDDIEN1 = 0xff;                          // turn on pins 0-7

    PERAD = 0xff;                     // turn on pullups 0-7  

   

//  Use port U as digital input from switches

    DDRU = 0x00;                        // turn off pins 8 to 15

    PERU = 0xff;                                                           // turn on pullups 0-7

 

}void RTIinit(void){

 

COPCTL = 0x00; // disable COP

 

DDRT = 0x80; // Port T bit 7 is an output to LED //

 

RTICTL = 0x7F; //Arm and set RTI //

 

CRGINT = 0x80; // enable RTI interrupt by setting RTIE bit //

 

  }

 

//---------------------------------------------------------------------------------------------------------------

// Main Program:

//---------------------------------------------------------------------------------------------------------------

 

void main(void)             //Main function

{

int swdata =0;                         // temporary storage of input data

int outdata=0;                         // temporary storage of output data

 

     

PeriphInit();                               // Microcontroller initialization

EnableInterrupts;                    // Enables interrupts

 

 

    do                                                    // loop while...

    {                              

                                                            // get data for next cycle                       

       swdata = PTU;                              // Read switches into temp variable

                       

                    

      // if (panic AND !ground) OR                     // note !  means NOT

      // if (alarm AND !ground) THEN set alarm ELSE clear alarm ;

 

      if (

            ( (swdata & PANIC) && !(swdata & GROUND) )

            ||           // logical OR

            ( (outdata & ALARM)  && !(swdata & GROUND) )

          )

      //then

            (outdata = outdata |  ALARM);                                    // Turn single bit ON

            else  (outdata = outdata & ALARMOFF);                    // Turn single bit OFF

         

             

       PTT = outdata;                               //  show LEDs as set.

       outdata=outdata;          // does nothing - just for BREAKPOINT.

      

      // if (down_l AND !ground) OR                  // note !  means NOT

      // if (down_h AND !ground) OR

      // if (alarm  AND !ground) THEN set down ELSE clear down ;

            

               if (

                    ( (swdata & DOWN_L) && !(swdata & GROUND) )

                    || // logical OR

                    ( (swdata & DOWN_H) && !(swdata & GROUND) )

                    || // logical OR

                    ( (outdata & ALARM) && !(swdata & GROUND) )

                   )

                //then

                   (outdata = outdata |  DOWN);       // Turn single bit ON

                   else (outdata = outdata & ALARMOFF & DOWNOFF);

                   

                   PTT = outdata;                               //  show LEDs as set.

                   

              // if (up_h AND !up_l AND !proximity AND !alarm) OR                   

      // if (down_h AND !down_l AND !proximity AND !alarm) THEN set speed ELSE clear speed ;

 

      if (

            ( (swdata & UP_H) && !(swdata & UP_L) && !(swdata & PROXIMITY) && !(outdata & ALARM) )

            ||           // logical OR

            ( (swdata & DOWN_H)  && !(swdata & DOWN_L) && !(swdata & PROXIMITY) && !(outdata & ALARM) )

          )

      //then

            (outdata = outdata |  SPEED);                          // Turn single bit ON

            else  (outdata = outdata & SPEEDOFF);                      // Turn single bit OFF

           

            PTT = outdata;                          //  show LEDs as set.

                   

               

                       

  

    } while (       1 );                               // loop FOREVER - PUT IN A BREAKPOINT!!   

}

 

Summary program works but interrupt doesn't work. Can some have a look at my code and tell me where i am going wrong of fix it for me.

 

thanks in advance

 

CG

Outcomes