Jump to content
  • 0

Register Manipulation in ChipKIT Core


JayWeeks

Question

I'm playing around with timer interrupts on the DP32 using Arduino and the ChipKIT Core. I was surprised to find out that this involves register manipulation. So I can set up timers using register manipulation in Arduino, but I can't seem to do anything else with register manipulation.

For example, here is some code of mine which should set up a PWM signal on pin 13 (OC4) of the DP32 using register manipulation, instead of analogWrite() function. Problem is that it doesn't work and I can't figure out why. This problem is exacerbated by the apparent lack of documentation on any of the register manipulation functions(?) that I use to set my timers up previously. I know they work, but I only found them in a tutorial that doesn't explain where they come from, or much about them at all.

So I have the following questions:

  • Am I allowed to do register manipulation for more than just timers?
  • Where can I find more information about how ChipKIT Core uses and defines these things?
  • What am I doing wrong with my code?
/********************************
   Constants
 ********************************/
#define T3CON_ENABLE_BIT 0x8000
#define T3CON_PRESCALER_BITS 0x0070
#define T3_SOURCE_INT 0

#define OC4_ENABLE_BIT 0x8000
#define OC4_TIMER_SEL_BIT 0x0008
#define OC4_MODE_BITS 0x0007
#define OC4_MODE_PWM_SET 0x0006

// Prescaler values
// Don't change these. Set the prescaler below using these.
#define T3_PRESCALE_1_1 0
#define T3_PRESCALE_1_2 1
#define T3_PRESCALE_1_4 2
#define T3_PRESCALE_1_8 3
#define T3_PRESCALE_1_16 4
#define T3_PRESCALE_1_32 5
#define T3_PRESCALE_1_64 6
#define T3_PRESCALE_1_256 7
// Set the prescaler value we want to use
#define PRESCALE T3_PRESCALE_1_256    // Current it's set to 1:256

// The DP32 runs at 40 MHz
// The uC32 and WF32 run at 80 MHz
#define CLOCK_FREQ 40000000   // Right now we're set for the DP32

// Set our target frequency
// This is the frequency that our interrupt will run at in Hz
#define T3_FREQUENCY 5

void setup()
{
  uint32_t period;
  uint32_t cycle;
  uint32_t mask;
  
  // Disable everything
  T3CONCLR = T3CON_ENABLE_BIT;            // Turn the timer off
  OC4CONCLR = OC4_ENABLE_BIT;             // Turn OC4 off

  RPB2R = 0b0101;

  // Calculate the period we need for our given frequency
  if (PRESCALE == 7) period = 256; // 1:256 is a special case
  else period = 1 << PRESCALE;
  period = period * T3_FREQUENCY;
  period = CLOCK_FREQ / period;
  
  // Set up our timer
  T3CONCLR = T3CON_PRESCALER_BITS;        // Clear the old prescaler
  mask = PRESCALE << 4;                   // Shift our new prescaler
  mask = mask | T3CON;                    // Mask our prescaler
  T3CON = mask;                           // Set the prescaler
  TMR3 = 0;                               // Clear the counter
  PR3 = period;                           // Set the period
  
  // Calculate our cycle
  cycle = period /2;              // We want a 50% duty cycle
  
  // Set up our output compare
  OC4CONSET = OC4_TIMER_SEL_BIT;  // Select timer 3 for source
  OC4CONCLR = OC4_MODE_BITS;      // Clear our mode pins
  OC4CONSET = OC4_MODE_PWM_SET;   // Set our mode to PWM w/out fault pin
  OC4R = cycle;                   // Set our cycle

  // Enable everything
  T3CONSET = T3CON_ENABLE_BIT;            // Turn the timer on
  OC4CONSET = OC4_ENABLE_BIT;             // Turn OC4 on
}

void loop()
{
  
}

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...