AnsweredAssumed Answered

ADuC845 P0 odd behavior with Timer2 interrupt

Question asked by jwanja on Mar 25, 2012
Latest reply on Mar 26, 2012 by jwanja

I am trying to create a square wave at P0.0 using Timer2 in auto-reload mode. My issue is that it isn't outputting anything near the frequency I was assuming it would, AND when I observe both P0.0 and P0.1 using the Logic Analyzer Window in debug mode, I can see both bits fluctuating over time. Can anyone help me with this? I will post my code below (sorry for all the comments. I put them there when trying to debug this myself.)

 

/* main.c */
#include "aduc845.h"

#define VREF_REAL    5
#define VREF_DES    5
#define FREQ_START    100
#define FREQ_END    10100 

#define VOLTAGE 0
#define CURRENT 1

unsigned int currentOrVoltage=0; //Tells ISR to output to P0.0 or P0.1
                                                             //0 - Voltage
                                                             //1 - Current

/************* INTERRUPT ISRs *************/
void freqInterrupt(void) interrupt 5
{
    if(currentOrVoltage == 0)
        P0 ^= 0x01;
    else P0 ^= 0x02;
    
    //Clear Interrupt Flag
    TF2 = 0;
}
/**************** END ISRs ****************/


void outputVoltageFrequency(unsigned int freq)
{
    unsigned int numberOfCycles;  //holds the number of clock cycles needed to
                                                              //  generate the frequency


    //Clock speed divided by desired frequency
    numberOfCycles = 1572864 / freq;
     //Autoreload value for Timer2
    numberOfCycles = 65536 - numberOfCycles; 

    //Tell Timer2 ISR output Voltage
    currentOrVoltage = 1;


    /****** TIMER2 CONFIGURATION ******/
    //By default Timer2 is configured as 16-bit autoreload timer
    //Enable Timer2 overflow interrupts
    ET2 = 1;
    //Load correct starting value into Timer2
    TL2 = numberOfCycles & 0x0F;
    TH2 = numberOfCycles >> 8;
    //Set the Autoreload value
    RCAP2L = numberOfCycles & 0x0F;
    RCAP2H = numberOfCycles >> 8;

    //Enable global interrupts
    EA = 1;
    
    //Begin Timer2
    T2CON = 0x04;
}


main ()
{
    outputVoltageFrequency(5000);    
}

Outcomes