Hi I have found the codes and I am trying to understand the code as it is about generating sine wave but I got lost in the offset sections. I would be grateful if you offer any help me to understand the codes as shown below: In fact I would expect the PWM frequency should be 5kHz and the frequency modulation - 400Hz
// Timer 0 & 1 - Timer mode (Non-staggered) // T0I - 000 hence prescaler would be 1, period = 1.6384ms // T1I - 111 hence prescaler would be 128, period = 209.72ms CC1_T01CON = 0x0700; // Since I don't know what is the desired period for timer 0 & reload (0XF060) // Hence 61536=(DESIRED PERIOD/1.6384E-3)*65536 // DESIRED PERIOD would be 1.5384e-3 (650Hz) C1_T0 = 0xF060; CC1_T0REL = 0xF060; // CAPCOM1 : CC1_CC2/3 ARE ALLOCATED TO TIMER TO // : Mode 3 are set for CC2 and CC3 CC1_M0 = 0x7700; ALTSEL0P6 |= 0x003C; // select alternate output function DP6 = (DP6 & ~(uword)0x003C) | 0x003C; //set direction register // Enable the CAPCOM1 Timer CC1_T01CON_T0R = 1; // set CAPCOM1 timer 0 run bit // Obtain the offset but I don't understand the reason behind this method offset = (0xFFFF - CC1_T0REL)>>1; // Generating look up table for sine wave TempFloat = TABLE_SIZE; // 50 TempFloat = (360)/TempFloat; // Get the increment size for the sine wave look up table for (loopcount=0; loopcount<TABLE_SIZE; loopcount++){ SineWave[loopcount]=(offset*sin(((loopcount*TempFloat)*3.14159)/180)); } // I don't why offset is added to CC1_TOREL (Can anyone care to explain reason behind this?) CC1_CC2 = CC1_T0REL + offset; CC1_CC3 = CC1_T0REL + offset; CC1_CC2_Temp = CC1_CC2; CC1_CC3_Temp = CC1_CC3; // Can anyone please tell me what _sof_ is for? SRCP5 = _sof_(&CC1_CC2_Temp); SRCP6 = _sof_(&CC1_CC3_Temp); Interrupt routine for CC1_T0INT int TempInt; long int TempLong; pwm_ref++; if(pwm_ref >= 50){ pwm_ref = 0; } TempLong = 5000; // amplitude TempLong = SineWave[pwm_ref]*TempLong; TempInt = (TempLong)/10000; // Can someone please please tell me what is the hell is going on epecially two last codes // CC1_CC2_Temp = CC1_T0REL + (offset + TempInt); // apply sine wave CC1_CC3_Temp = CC1_T0REL + (offset - TempInt); // apply sine wave