I am working on ADXL375 and interfacing it with Arduino UNO using I2C protocol. I get the values of X, Y, Z axis as mentioned in the datasheet, i.e., when placed horizontally I get x=0g, y=0g, z=1g(approx. calibrated). I have enabled trigger mode and mapped interrupt to INT2. My Shock Threshold is set to 0x28 = 31.2g.
When I tap the module on table, interrupt triggers even though the threshold is 31.2g, but the values I get are unchanged (around x= 0, y=0, z=1). How to get the values of X, Y, Z during the shock? When I tilt the module, I can see the values change accordingly. but these values hardly go beyond 3g. what am I doing wrong?
Here is my code setup for registers:
/*START Set Shock Threshold*/
Wire.beginTransmission(Device_Address);
Wire.write(0x1D); //Shock Duration Register Address
Wire.write(0x28); //Scale Factor is 780mg/LSB, hence 0x28 = 31.2g
Wire.endTransmission();
/*END Set Shock Threshold*/
/*START Set DUR Thresh_SHOCK*/
//Used for Double Shock Detection Only**
Wire.beginTransmission(Device_Address);
Wire.write(0x21); //Shock Duration Register Address
Wire.write(0x50); //Scale Factor is 625us/LSB, hence 0x50 = 50ms
Wire.endTransmission();
/*END Set DUR Thresh_SHOCK*/
/*START Set Latency*/
Wire.beginTransmission(Device_Address);
Wire.write(0x22); //Latent Register Address
Wire.write(0x20); //Scale Factor is 1.25ms/LSB, hence 0x20 = 400ms
Wire.endTransmission();
/*END Set Latency*/
/*START Set Shock Window to 300ms*/
Wire.beginTransmission(Device_Address);
Wire.write(0x23); //Window Register Address
Wire.write(0xF0); //Scale Factor is 1.25ms/LSB, hence 0xF0 = 300ms
Wire.endTransmission();
/*END Set Shock Window to 300ms*/
/*START Enable XYZ-Axis Shock Detection START*/
Wire.beginTransmission(Device_Address);
Wire.write(0x2A); //SHOCK_AXES Register
Wire.write(0x07); //Enable SHOCK_X, SHOCK_Y, SHOCK_Z
Wire.endTransmission();
/*END Enable XYZ-Axis Shock Detection END*/
/*START Set Out-Data-Rate(ODR) to 3200Hz*/
Wire.beginTransmission(Device_Address);
Wire.write(0x2C); //BW_RATE Register Address
Wire.write(0x0F); //3200 Hz Output Data Rate
Wire.endTransmission();
/*END Set Out-Data-Rate(ODR) to 3200Hz */
/*START Enable Single Shock Interrupt*/
Wire.beginTransmission(Device_Address);
Wire.write(0x2E); //INT_Enable Register Address
Wire.write(0x40); //Enable single Shock Int
Wire.endTransmission();
/*END Enable Single Shock Interrupt*/
/*START Assign Single Shock Interrupt*/
Wire.beginTransmission(Device_Address);
Wire.write(0x2F); //INT_Map Register Address
Wire.write(0x40); //Assign single Shock Int
Wire.endTransmission();
/*END Assign Single Shock Interrupt*/
/*START Data Format*/
Wire.beginTransmission(Device_Address);
Wire.write(0x31); //DATA_FORMAT Reg
Wire.write(0x0B);
Wire.endTransmission();
/*END Data Format*/
/*START Enable Trigger Mode*/
Wire.beginTransmission(Device_Address);
Wire.write(0x38); //FIFO_CTL Register Address
Wire.write(0xEA); //Enable Trigger Mode, set samples = 10
Wire.endTransmission();
/*END Enable Trigger Mode*/
/*START Offset Calibration*/
// Scale Factor = 0.196g/MSB
Wire.beginTransmission(Device_Address);
Wire.write(0x1E); //OFSX Address
Wire.write(0xFA); //OFSX offset
Wire.endTransmission();
Wire.beginTransmission(Device_Address);
Wire.write(0x1F); //OFSY Address
Wire.write(0xFB); //OFSY offset
Wire.endTransmission();
Wire.beginTransmission(Device_Address);
Wire.write(0x20); //OFSZ Address
Wire.write(0xFF); //OFSZ offset
Wire.endTransmission();
/*END Offset Calibration*/
/*Start Enable Measuring*/
Wire.beginTransmission(Device_Address);
Wire.write(0x2D); //POWER_CTL Register
Wire.write(0x08); //Enable Measuring
Wire.endTransmission();
/*END Enable Measuring*/
/*Attach Interrupt to Digital pin 2*/
attachInterrupt(digitalPinToInterrupt(2), ISR_Func, RISING);
Here is how I am receiving the values:
int16_t data_x = 0, data_x_lsb = 0;
int16_t data_y = 0, data_y_lsb = 0;
int16_t data_z = 0, data_z_lsb = 0;
Wire.beginTransmission(Device_Address);
Wire.write(0x32); //read LSB
Wire.endTransmission();
Wire.requestFrom(Device_Address, 6);
while (Wire.available()) {
data_x_lsb = Wire.read();
data_x = Wire.read();
data_y_lsb = Wire.read();
data_y = Wire.read();
data_z_lsb = Wire.read();
data_z = Wire.read();
data_x = (data_x << 8) | (data_x_lsb);
data_y = (data_y << 8) | (data_y_lsb);
data_z = (data_z << 8) | (data_z_lsb);
}
data_x = (double)data_x*49/1000
data_y = (double)data_y*49/1000
data_z = (double)data_z*49/1000
Sample Output:
14:36:51.120 -> -0.072 -0.067 0.977
14:36:51.221 -> -0.087 -0.096 0.949
14:36:51.325 -> 0.010 -0.191 0.988
14:36:51.427 -> -0.062 -0.162 1.071
14:36:51.536 -> -0.010 -0.088 1.071
14:36:51.614 -> -0.015 -0.037 1.052
14:36:51.725 -> -0.022 -0.047 1.044
14:36:51.837 -> 0.062 -0.043 1.012
14:36:52.025 -> FIFO STATUS REG: A0
14:36:52.025 -> Shock Occured
14:36:52.062 -> ACT STATUS SHOCK REG: 1
14:36:52.062 -> INT_SOURCE: C3
14:36:52.137 -> 0.055 -0.081 0.997
14:36:52.252 -> 0.024 0.031 1.033
14:36:52.354 -> 0.011 -0.072 1.079
14:36:52.455 -> 0.022 -0.031 0.973
14:36:52.547 -> 0.014 -0.042 1.041
14:36:52.654 -> -0.062 -0.036 1.018
14:36:52.770 -> -0.080 -0.003 1.003
14:36:52.880 -> -0.081 -0.118 1.084
14:36:52.972 -> -0.080 -0.039 1.046
14:36:53.079 -> -0.109 -0.016 0
According to datasheet, it says that we need to reset trigger mode after each triggering event. I tried doing that but to no avail.
I am doing multibyte read using I2C and implemented moving average filter with the span of 4.
Update:
I am able to get the output values. All I had to do was resetting the trigger mode by entering into bypass mode in the initial setup.
and resetting again after each shock event.
This is the code section I added in my setup and calling after each shock event.
/*START Disable Trigger Mode/enable Bypass Mode*/
Wire.beginTransmission(Device_Address);
Wire.write(0x38); //FIFO_CTL Register Address
Wire.write(0x2A); //Disable Trigger Mode, set samples = 10
Wire.endTransmission();
/*END Disable Trigger Mode/enable Bypass Mode*/
/*START Enable Trigger Mode*/
Wire.beginTransmission(Device_Address);
Wire.write(0x38); //FIFO_CTL Register Address
Wire.write(0xEA); //Enable Trigger Mode, set samples = 10
Wire.endTransmission();
/*END Enable Trigger Mode*/
As for the values remaining constant even after shock, I was resetting trigger mode after reading only the output FIFO [0]), when there are 10 FIFOs to collect data, as I have configured in register 0x38(samples = 10). So, the shock values were being stored in the later stages of FIFOs and not in FIFO [0]. Reading FIFO more than 10 times after shock event and then resetting the trigger mode solved the problem.
Sample Output(X Y Z in g):
0.000 0.000 0.196
-0.490 0.098 0.000
0.098 -0.392 0.980
Shock Occured
FIFO STATUS REG: A0
ACT STATUS SHOCK REG: 1
INT_SOURCE: C3
0.490 -0.098 2.156
0.490 -0.098 2.156
0.392 0.098 1.960
0.392 0.294 2.254
0.294 0.098 1.960
-0.784 -0.882 1.470
-0.686 -0.980 1.274
10.976 13.524 59.290
27.342 17.934 36.358
-13.034 -6.566 -1.666
-0.098 0.098 1.078
0.000 0.294 0.686
0.098 0.098 1.470
Related
I use Timer 1 for creating Complimentary PWM Output. I Set all registers according to the STM32F3 Reference Manual. When i debug code, The Timer Counter counts very well but the TIM1_CH1 Output doesnt output anything.(Other Channel Outputs have the same issue)
Here is my code :
void Timer1_PWMInit()
{
//----------------Setup Timer 1 for 25khz Center Aligned PWM-----------------
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // Enable Timer 1 Clock
GPIOE->AFR[1] |= 0x20; // Set Alternate Function 2 For Timer 1 CH1 Output
TIM1->CR1 = 0; // Clear CR1 Register(Turn off Timer 1)
TIM1->CNT = 0; // Clear Timer Counter
TIM1->PSC = 0; // Set Timer 1 Prescaler to 1:1
TIM1->ARR = 320; // 40 us(25 khz) PWM Period
TIM1->CCR1 = 320; // Timer 1 Channel 1 Duty Cycle(%50)
TIM1->CCR2 = 320; // Timer 1 Channel 2 Duty Cycle(%50)
TIM1->CCR3 = 320; // Timer 1 Channel 3 Duty Cycle(%50)
TIM1->CCR5 = 0; // Timer 1 Channel 5 Duty Cycle
TIM1->CCMR1 = 0x6868; // Set Channel 1 and Channel 2 Output Compare mode to PWM Mode
TIM1->CCMR2 = 0x68; // Set Channel 3 Output Compare mode to PWM Mode
TIM1->CCMR3 = 0x10048; // Set Channel 5 Output Compare Mode to Combined PWM Mode
TIM1->CCER = 0x10555; // Enable Output Compare Channels
TIM1->CR1 = 0xE0; // Set Counting Mode to Center Aligned Mode
TIM1->EGR |= 0x1; // Set UG bit for Updating Registers
TIM1->BDTR = 0xC800; // Set MOE and OOSR bits for enabling Output
TIM1->CR1 |= 0x1; // Enable Timer 1
//-------------------------------------------------------------------------
}
Try to enable GPIOE clock using 'RCC_AHBENR_GPIOEEN' and also set alternate function for the other timer channel output pins.
I have STM32F7 Disco board with STM32F723IEK MCU. Trying to trigger a DMA request from a timer causes a DMA error, but only for timers from the APB1 group (TIM2 to TIM7 and others), that are connected to DMA1. Doing the same with TIM1 and TIM8, which are connected to DMA2, works fine. The error manifests itself as the TEIFx flag being set in the appropriate DMA LISR or HISR register and DMA immediately disabled after the first transaction. The NDTR register is decremented by one.
According to the datasheet, the TEIF error may be triggered by a "bus error". I understand this as e.g. trying to access a peripheral that is not accessible from the DMA bus. However, the same setup works well using DMA2 and TIM1/TIM8, without changing the DMA address. So the problem seems to be related to DMA request and not the data transaction itself. Given that there are lot of timer channels defined for DMA1, this should certainly work.
I have tried to vary DMA settings but this made no difference. The relevant portion of the test program is below. The full version https://github.com/ak-hard/stm32-dma-tim/blob/master/main.c is only slightly larger and have no dependencies except CMSIS and STM32 Device headers.
I wonder if someone can comment on or reproduce this problem.
const struct
{
TIM_TypeDef *tim;
DMA_TypeDef *dma;
DMA_Stream_TypeDef *stream;
unsigned channel;
} CFG = {
// uncomment the needed combination below, only TIM1 and TIM8 work
// TIM1, DMA2, DMA2_Stream5, 6
TIM8, DMA2, DMA2_Stream1, 7
// TIM2, DMA1, DMA1_Stream1, 3
// TIM2, DMA1, DMA1_Stream7, 3
// TIM3, DMA1, DMA1_Stream2, 5
// TIM4, DMA1, DMA1_Stream6, 2
// TIM5, DMA1, DMA1_Stream0, 6
// TIM5, DMA1, DMA1_Stream6, 6
// TIM6, DMA1, DMA1_Stream1, 7
// TIM7, DMA1, DMA1_Stream2, 1
// TIM7, DMA1, DMA1_Stream4, 1
};
enum
{
DMA_SxCR_DIR_P2M = 0,
DMA_SxCR_PSIZE_WORD = DMA_SxCR_PSIZE_1,
DMA_SxCR_MSIZE_WORD = DMA_SxCR_MSIZE_1,
};
#define DMA_SxCR_CHSEL_NUM(ch) ((ch) << DMA_SxCR_CHSEL_Pos)
uint32_t buf;
void start(void)
{
SysTick->LOAD = 0xffffffu;
SysTick->VAL = 0;
SysTick->CTRL = 5;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN | RCC_APB2ENR_TIM8EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN | RCC_APB1ENR_TIM3EN | RCC_APB1ENR_TIM4EN | RCC_APB1ENR_TIM5EN
| RCC_APB1ENR_TIM6EN | RCC_APB1ENR_TIM7EN;
RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN | RCC_AHB1ENR_DMA2EN;
LED_PORT->MODER |= 1 << (2 * LED_PIN);
LED_PORT->OSPEEDR |= 3 << (2 * LED_PIN); // fastest speed
CFG.tim->CR1 |= TIM_CR1_ARPE;
CFG.tim->ARR = 16;
CFG.tim->PSC = 1000;
CFG.tim->EGR = TIM_EGR_UG; // Generate Update Event to copy ARR to its shadow
CFG.tim->DIER |= TIM_DIER_UDE;
CFG.stream->CR |= DMA_SxCR_CHSEL_NUM(CFG.channel) | DMA_SxCR_DIR_P2M | DMA_SxCR_PSIZE_WORD | DMA_SxCR_MSIZE_WORD;
CFG.stream->NDTR = 16;
CFG.stream->PAR = (uint32_t) &GPIOA->IDR;
CFG.stream->M0AR = (uint32_t) &buf;
CFG.stream->CR |= DMA_SxCR_EN;
CFG.tim->CR1 |= TIM_CR1_CEN;
// wait until DMA state changes
while (CFG.dma->LISR == 0 && CFG.dma->HISR == 0)
delay_ms(1);
// check for any TEIFx bits
int error = (CFG.dma->LISR | CFG.dma->HISR) & 0x02080208;
while (1)
{
LED_PORT->ODR ^= 1 << LED_PIN;
delay_ms(error ? 100 : 500);
}
}
There used to an answer here but it got deleted for some reason. Thanks to its author though.
Looking at the bus matrix, it becomes clear that the peripheral bus of the DMA1 is only connected to APB1. It is actually not a part of the matrix at all. This probably means that DMA1 can only handle transfers from/to APB1 peripherals. Since GPIO is an AHB peripheral, it is not accessible from DMA1. This should also apply to other APB2 (e.g. SPI1) and AHB peripherals (e.g. OTGFS). Normally, it does not make sense to access AHB or APB2 peripherals from DMA1 because their requests are not routed to DMA1. However, it may be needed for convoluted cases like GPIO by a timer.
I personally think this point could be made more obvious in the documentation.
My Infrared-Receiver sends me digital data on Port P1.1. I already have my Timer configured that if theres a falling or rising edge a interrupt gets triggered. I wanna know how i get the actual time difference between the edges. Afterwards i wanna save them in an array.
Main:
// Stop watchdog timer
WDTCTL = WDTPW + WDTHOLD;
//1mhz = 0.000001
BCSCTL1 = CALBC1_1MHZ; // load calibrated data
DCOCTL = CALDCO_1MHZ;
//Define Outputs
P1DIR = green_led+red_led+IR_Send;
P2DIR = LED1+LED2+LED3;
//Define Inputs
P1DIR &= ~IR_Recv;
//Set IR_Recv as input for Timer (TA0.CCI0A)
P1SEL |= BIT1;
//Timer_A using SMCLK/8 = 0.000008s and Continuous mode
TACTL = TASSEL_2 | MC_2 | ID_3;
// falling edge and rising edge capture mode, CCI0A, enable IE
CCTL0 = CM_3 | CCIS_0 | CAP | CCIE;
//Enter LPM and enable Global Interrupts
__bis_SR_register(CPUOFF + GIE);
Interrupt Routine:
//gets called when falling or rising edge is detected on IR_Recv
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
if(edgeCount < 10){
rxData[edgeCount] = TACCR0;
TACCR0 = 0;
}
edgeCount++;
P2OUT ^= LED2;
//Clear interrupt Flag
TACCTL0 &= ~CCIFG;
//go back to LPM
__bic_SR_register_on_exit ( CPUOFF );
}
To get the time difference, in ticks, between two captured events, just let the timer keep running without resetting it, and subtract the captured time stamps:
uint16_t difference = rxData[1] - rxData[0];
This computation is correct even when the timer counter happens to overflow.
Hello i have been coding with Tm4c123gh6pm lately and i have faced a part where i want to use the edge timing mode , now i haven't went for the tivaware api's i just went for traditional register level and following the datasheet and i faced this problem
i want to know which pin to use as the one where the timer is waiting for the rising edge to come so it moves the current timer value ?
i checked the data sheet all i can find is that timer 0 is somehow related to PB6 and PF0 now i tried PB6 and it didn't work but is that even the correct approach ?
are these the correct pins the microcontroller's timer waits for the rising edge to move the current timer value ?
here is a sample of my code and please note that this is just a testing code im trying its not a final code what i did was copy the initialization section in the datasheet for the timer mode i want and followed it step by step
this code will make the counter start running from 0xFF downwards but when i place a 'high' signal i.e 3.3 volts on PB6 nothing is moved to the GPTMTnR register.
i just wonder if there is something im doing wrong and im not noticing ?
#include "tm4c123gh6pm.h"
void Timer0Init(void);
int main(void)
{
int i=0;
Timer0Init();
while(1){
for(i=0;i<100000;i++){
}
}
return 0;
}
void Timer0Init(void)
{
//initialize PORT B
volatile unsigned long delay;
SYSCTL_RCGC2_R |= 0x00000002; // 1) B clock
delay = SYSCTL_RCGC2_R; // delay to allow clock to stabilize
GPIO_PORTB_AMSEL_R &= 0x00; // 2) disable analog function
GPIO_PORTB_PCTL_R &= 0x00000000; // 3) GPIO clear bit PCTL
GPIO_PORTB_DIR_R &= 0x00; // 4.2) PB all input
GPIO_PORTB_AFSEL_R &= 0x40; // 5) no alternate function
GPIO_PORTB_DEN_R |= 0xFF; // 7) enable digital pins PF4-PF1
GPIO_PORTB_PCTL_R = 7;
//timer clock
SYSCTL_RCGCTIMER_R |=0x01;
delay = SYSCTL_RCGCTIMER_R;
//1. Ensure the timer is disabled (the TnEN bit is cleared) before making any changes.
TIMER0_CTL_R &= 0xFE;
//2. Write the GPTM Configuration (GPTMCFG) register with a value of 0x0000.0004.
TIMER0_CFG_R= 0x00000004;
//3. In the GPTM Timer Mode (GPTMTnMR) register, write the TnCMR field to 0x1 and the TnMR field to 0x3.
TIMER0_TAMR_R= TIMER0_TAMR_R | 0x007;
//4. Configure the type of event that the timer captures by writing the TnEVENT field of the GPTM Control (GPTMCTL) register.
TIMER0_CTL_R = TIMER0_CTL_R & 0xFFFFFFF3;
//5. If a prescaler is to be used, write the prescale value to the GPTM Timer n Prescale Register (GPTMTnPR).
//no prescaler for now
//6. Load the timer start value into the GPTM Timer n Interval Load (GPTMTnILR) register.
TIMER0_TAILR_R = 0xFF;
//7. If interrupts are required, set the CnEIM bit in the GPTM Interrupt Mask (GPTMIMR) register.
//no interrupts required
//8. Set the TnEN bit in the GPTM Control (GPTMCTL) register to enable the timer and start counting.
TIMER0_CTL_R= TIMER0_CTL_R & 0x00000001;
TIMER0_CTL_R |= 0x01;
//9. Poll the CnERIS bit in the GPTMRIS register or wait for the interrupt to be generated (if enabled). In both cases,
//the status flags are cleared by writing a 1 to the CnECINT bit of the GPTM Interrupt Clear (GPTMICR) register.
//The time at which the event happened can be obtained by reading the GPTM Timer n (GPTMTnR) register.
/*In Input Edge Timing mode, the timer continues running after an edge event has been detected,
but the timer interval can be changed at any time by writing the GPTMTnILR register. The change
takes effect at the next cycle after the write.*/
}
there was a problem with my portb initialization i used tivaware for that part and it worked perfectly fine now on placing high on PB6 it passes the current timer value to the other register
also note that when counting up the timer counts up to the value initialized in the TAIL_R register also if u want to reset the timer you need to write the value u want the timer to begin with in the TAV_R register
Best Of Luck everyone
I am working on project in which I need to display different colors on RGB led. I am using pwm to drive different colors on LED. My Pic is PIC24FJ64GA004 with which I am working on now. Basic concept of this project is to use switch to controls colors.
Colors on RGB led will be according to days and month in year. For that I am using 7-segment led with switch to count days and month.
Problem at the moment is following code. I am trying to change PWM values by following settings. But instead of changing it it is giving me some thing weird.
I need your help guys. Could you please help me with this thing.
for( counter=0x0000;counter<=0x4571;counter++){
//*****Timer2 starting from here*****//
PR2 = 0x4571; // Initialize PR2 with 0x4571 = 17777 as PWM cycle
IFS0bits.T2IF = 0; // Clear Output Compare interrupt flag
IEC0bits.T2IE = 1; // Enable Output Compare interrupts
T2CONbits.TON = 1; // Start Timer2 with assumed settings
//**********************************//
//*****For RED LED OC1 choosed with timer 2*****//
OC1CONbits.OCM = 0; // Output compare channel is disabled
OC1R = 0x0000 ; // Initialize Compare Register1 with 50% duty cycle
OC1RS = counter; // Initialize Secondary Compare Register1 with 50% duty cycle
OC1CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC1CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC1CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC1CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
//*****For Green Led OC2 and OC3 Choosed with timer2 as well*****//
OC2CONbits.OCM = 0; // Output compare channel is disabled
OC2R =0x0000; // Initialize Compare Register1 with 50% duty cycle
OC2RS =counter; // Initialize Secondary Compare Register1 with 50% duty cycle
OC2CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC2CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC2CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC2CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
//*****For Blue Led OC2 and OC3 Choosed with timer2 as well*****//
OC3CONbits.OCM = 0; // Output compare channel is disabled
OC3R = 0x0000; // Initialize Compare Register1 with 50% duty cycle
OC3RS = counter; // Initialize Secondary Compare Register1 with 50% duty cycle
OC3CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC3CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC3CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC3CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
}
But this code is working alright. I put different values as well. It works fine.
//*****For RED LED OC1 choosed with timer 2*****//
OC1CONbits.OCM = 0; // Output compare channel is disabled
OC1R = 0x22B8; // Initialize Compare Register1 with 50% duty cycle
OC1RS = 0x22B8; // Initialize Secondary Compare Register1 with 50% duty cycle
OC1CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC1CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC1CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC1CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
//*****For Green Led OC2 and OC3 Choosed with timer2 as well*****//
OC2CONbits.OCM = 0; // Output compare channel is disabled
OC2R =0x22B8; // Initialize Compare Register1 with 50% duty cycle
OC2RS =0x22B8;//0x22B8; // Initialize Secondary Compare Register1 with 50% duty cycle
OC2CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC2CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC2CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC2CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
//*****For Blue Led OC2 and OC3 Choosed with timer2 as well*****//
OC3CONbits.OCM = 0; // Output compare channel is disabled
OC3R = 0x22B8; // Initialize Compare Register1 with 50% duty cycle
OC3RS = 0x22B8; // Initialize Secondary Compare Register1 with 50% duty cycle
OC3CONbits.OCSIDL = 0; // Output capture will continue to operate in CPU Idle mode
OC3CONbits.OCFLT = 0; // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
OC3CONbits.OCTSEL = 0; // Timer2 is the clock source for output Compare
OC3CONbits.OCM = 0x6; // PWM mode on OC, Fault pin disabled
//*****Timer1 starting from here*****//
PR1 = 65535; // Initialize PR2 cycle
T1CONbits.TCKPS = 2; // Setting pre-scaler to 1/64
IFS0bits.T1IF = 0; // Clear Output Compare interrupt flag
IEC0bits.T1IE = 1; // Enable Output Compare interrupts
T1CONbits.TON = 1; // Start Timer1 with assumed settings
//*****Timer2 starting from here*****//
PR2 = 0x4571; // Initialize PR2 with 0x4571 = 17777 as PWM cycle
IFS0bits.T2IF = 0; // Clear Output Compare interrupt flag
IEC0bits.T2IE = 1; // Enable Output Compare interrupts
T2CONbits.TON = 1; // Start Timer2 with assumed settings
//**********************************//
You are updating your PWMs each time around the loop.
It's very unlikely that the counter has had time to expire, therefore you keep resetting it before it has chance to "do" a PWM cycle.
At the end of the loop, you need to wait for (at least one of) your PWMs to have expired.
An alternative way might be to set up the next value you want in the loop and have an interrupt service routine copy that to the PWM register when the timer expires. And once that has happened, you can set up the next next value :) You need to take care about how you transfer values between the main loop and the ISR as they are effectively different "thread" contexts.