chipKIT® Development Platform

Inspired by Arduino™

Compare Match Hexadecimal Question

Created Wed, 28 Nov 2012 18:15:53 +0000 by RoboNegro


RoboNegro

Wed, 28 Nov 2012 18:15:53 +0000

I have the following code to perform a compare match interrupt:

/The following code illustrates how to define initial OC1 pin state for the
//output compare toggle mode of operation in 16-bit mode

#include <plib.h>

int i = 0;

void setup()
{ 
  Serial.begin(9600);
  pinMode(4, OUTPUT);
  
  //configure module for OC1 pin low, toggle high
  OC1CON = 0x0001;
  //enable OC1 module
  OC1CONSET = 0x8000;
  
  //configure timer2 for prescaler of 2
  T2CON = 0x0010;
  //turn off OC1 while doing setup
  OC1CON = 0x0000;
  //configure for compare toggle mode
  OC1CON = 0x0003;
  
  //initialize compare register 1
  OC1R = 0x0500;
  //set period
  PR2 = 0x0500;
  
  //configure interrupt
  
  //clear the OC1 interrupt flag
  IFS0CLR = 0x0040;
  //enable OC1 interrupt
  IEC0SET =0x040;
  //set OC1 interrupt priority to 7, the highest level
  IPC1SET = 0x001C0000;
  
  //set subpriority to 3, maximum
  IPC1SET = 0x00030000;
  
  //enable timer2
  T2CONSET = 0x8000;
  //enable OC1
  OC1CONSET = 0x8000;
  	 
  delay(1);  // Slight delay avoids false trigger at start.
}

void loop()
{
}

extern "C"
{

void __ISR(_OUTPUT_COMPARE_1_VECTOR,ipl7) OC1_IntHandler(void)	
  {	 
    // Clear interrupt flag
    IFS0CLR = 0x0040;  
    digitalWrite(4, HIGH);
    digitalWrite(4, LOW); 
    
  }

}

The code was taken, verbatim, from the Microchip reference manual and configures the Pic32 in 'Compare Mode Toggle Setup and Interrupt Servicing (16-Bit Mode)'. The code can be found on page 16-11 of the reference manual. By hooking up a scope to digital pin 4 I can see that a pulse is generated at a frequency of 31.22 kHz.

2 Questions:

  1. In the example code from the reference manual, I configure timer2 for a prescaler of 2 by writing:

T2CON = 0x0010;

So to me this means that 0x0010 in hex = 2 in decimal, however when I plug this value into my hex to dec converter I see that 0x0010 = 16. Is this a typo in the manual or am I misunderstanding something?

  1. How do I change the compare match register so that I can generate pulses at any frequency I want, other than 31.22 kHz? I tried changing the value of OC1R to values other than 0x0500, yet the frequency of the interrupt remains unchanged.

When I change the period, by setting PR2 = to values other than 0x0200 NO interrupts are generated. What's going on here?

Thanks in advance.


majenko

Wed, 28 Nov 2012 18:25:46 +0000

The data sheet will detail what the different bits of a register do. 0x0010 will be setting a sub-set of the register to 2, not the entire register.


RoboNegro

Wed, 28 Nov 2012 19:11:55 +0000

Thanks majenko, I see my error now.