Trap conflict from ledpwm code.

15 views
Skip to first unread message

Vic Baz

unread,
Apr 20, 2018, 7:36:21 AM4/20/18
to PIC24 Assembly-to-C Book
Hi I am using the ledpwm code (slightly modified) to control a dc motor with a sn754410 half-H-bridge driver. I've got the program to build successfully and programed my pic24hj128502.
But when the program is running I am constantly getting "reset cause: trap conflict" in Tera Term. I've looked around a bit online and can not find a solution. Hoping someone could shed some light on the cause. Any help is extremely appreciated. Thanks.

Below I have added the code.

// *************************************************************************************************************************************************
// ledpwm.c - Demonstrates pulse width modulation by controlling the intensity of an LED. The ADC input value on AN0 is used to vary the PWM period.
// *************************************************************************************************************************************************
#include <stdio.h>
#include <pic24_all.h>
#include <dataXfer.h>
///////////////////////////////////////////////////////////////////////////////
#define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
#define LED1 (_LATB14)     //led1 state
#define CONFIG_LED2() CONFIG_RB15_AS_DIG_OUTPUT()
#define LED2 (_LATB15)     //led1 state
///////////////////////////////////////////////////////////////////////////////
#ifndef PWM_PERIOD
#define PWM_PERIOD 20000  // desired period, in us
#endif
void  configTimer2(void) {
  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
          | T2_32BIT_MODE_OFF
          | T2_SOURCE_INT
          | T2_PS_1_64;
  PR2 = usToU16Ticks(PWM_PERIOD, getTimerPrescale(T2CONbits)) - 1;
  TMR2  = 0;       //clear timer2 value
  _T2IF = 0;
  _T2IP = 1;
  _T2IE = 1;    //enable the Timer2 interrupt
}
///////////////////////////////////////////////////////////////////////////////
void config_pb()  {
  CONFIG_RB13_AS_DIG_INPUT();
  ENABLE_RB13_PULLUP();
  // Give the pullup some time to take effect.
  DELAY_US(1);
}
#if (HARDWARE_PLATFORM == EMBEDDED_C1)
# define PB_PRESSED()   (_RB7 == 0)
# define PB_RELEASED()  (_RB7 == 1)
#else
# define PB_PRESSED()   (_RB13 == 0)
# define PB_RELEASED()  (_RB13 == 1)
#endif
typedef enum  {
    S1,
            S2,
            S3,
            S4,
            S5,
            S6,
            S7,
            S8,
            S9,
            S10,
            S11,
            S12,
            S13,
            S14,
         
} state_t;
const char* apsz_state_names[] = {
    "STATE_S1",
    "STATE_S2",
    "STATE_S3",
    "STATE_S4",
    "STATE_S5",
    "STATE_S6",
    "STATE_S7",
    "STATE_S8",
    "STATE_S9",
    "STATE_S10",
    "STATE_S11",
    "STATE_S12",
    "STATE_S13"
            "STATE_S14"
  
};
void update_state(void) {
  static state_t e_state = S1;
  // The number of times the LED was toggled in the blink state
  switch (e_state) {
      case S1:
          LED1=0;
          LED2=0;
          if(PB_PRESSED())
          {           
              e_state=S2;
          }
          break;
      case S2:
          if(PB_RELEASED())
      {
              e_state= S3;
      }
          break;
      case S3:
          LED1=0;
          LED2=1;
          if(PB_PRESSED())
          {
              e_state= S4;    
          }
          break;
      case S4:       
          if(PB_RELEASED())
          {
              e_state=S5;
          }
          break;
      case S5:
          LED1=0;
          LED2=0;
          if(PB_PRESSED())
          {
          e_state= S6;
          }
          break;
      case S6:       
          if(PB_RELEASED())
          {
              e_state= S7;    
          }
          break;
      case S7:     
          LED1=1;
          LED2=0;
          if(PB_PRESSED())
          {
              e_state= S8;    
          }
          break;
      case S8:        
          if(PB_RELEASED())
          {
              e_state= S1;    
          }
          break;
         
     
    default:
      ASSERT(0);
  }
}
///////////////////////////////////////////////////////////////////////////////

void configOutputCompare1(void) {
  T2CONbits.TON = 0;          //disable Timer when configuring Output compare
  CONFIG_OC1_TO_RP(RB4_RP);   //map OC1 to RB4
  OC1RS = 0;  //clear both registers
  OC1R = 0;
#ifdef OC1CON1
//turn on the compare toggle mode using Timer2
  OC1CON1 = OC_TIMER2_SRC |     //Timer2 source
            OC_PWM_CENTER_ALIGN;  //PWM
  OC1CON2 = OC_SYNCSEL_TIMER2;   //synchronize to timer2
#else
//older families, this PWM mode is compatible with center-aligned, OC1R=0
//as writes to OC1RS sets the pulse widith.
  OC1CON = OC_TIMER2_SRC |     //Timer2 source
           OC_PWM_FAULT_PIN_DISABLE;  //PWM, no fault detection
#endif
}
void _ISR _T2Interrupt(void) {
  uint32_t u32_temp;
  _T2IF = 0;    //clear the timer interrupt bit
  //update the PWM duty cycle from the ADC value
  u32_temp = ADC1BUF0;  //use 32-bit value for range
  //compute new pulse width that is 0 to 99% of PR2
  // pulse width (PR2) * ADC/1024
  u32_temp = (u32_temp * (PR2))>> 10 ;  // >>10 is same as divide/1024
  OC1RS = u32_temp;  //update pulse width value
  SET_SAMP_BIT_ADC1();      //start sampling and conversion
}
int main(void) {
  uint32_t u32_pw;
  // Initialize
   configBasic(HELLO_MSG);
  // Configure PWM
 
  config_pb();
  CONFIG_LED1();
  CONFIG_LED2();
 
  configTimer2();
  configOutputCompare1();
  CONFIG_RA1_AS_ANALOG();
  configADC1_ManualCH0(RA1_AN, 31, 0);
  SET_SAMP_BIT_ADC1();      //start sampling and conversion
  T2CONbits.TON = 1;       //turn on Timer2 to start PWM
  // Report results only
  while (1) {
    update_state();
    u32_pw = ticksToUs(OC1RS, getTimerPrescale(T2CONbits));
    printf("PWM PW (us): %ld \n", u32_pw);
    DELAY_MS(100);
    doHeartbeat();
  }
}

 


Jones, Bryan

unread,
Apr 20, 2018, 10:33:38 AM4/20/18
to pic24-assemb...@googlegroups.com
I don't see anything obvious. I'd suggesting commenting out code until you isolate what's causing this.

--
You received this message because you are subscribed to the Google Groups "PIC24 Assembly-to-C Book" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pic24-assembly-to-c-book+unsub...@googlegroups.com.
To post to this group, send email to pic24-assembly-to-c-book@googlegroups.com.
Visit this group at https://groups.google.com/group/pic24-assembly-to-c-book.
For more options, visit https://groups.google.com/d/optout.



--
Bryan A. Jones, Ph.D.
Associate Professor
Department of Electrical and Computer Engineering
231 Simrall / PO Box 9571
Mississippi State University
Mississippi State, MS 39762
http://www.ece.msstate.edu/~bjones
bjones AT ece DOT msstate DOT edu
voice 662-325-3149
fax 662-325-2298

Our Master, Jesus Christ, is on his way. He'll show up right on
time, his arrival guaranteed by the Blessed and Undisputed Ruler,
High King, High God.
- 1 Tim. 6:14b-15 (The Message)
Reply all
Reply to author
Forward
0 new messages