Certainly! To replace the runtime sine table calculation with a pre-calculated sine table, we'll remove the init_sine_table() function and define the sine_table array with precomputed values. This optimization is common in embedded systems to save computational resources and execution time.
Here's the modified code with the pre-calculated sine table:
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 16000000 // 16 MHz oscillator frequency
#define SINE_TABLE_SIZE 256
// Pre-calculated sine table values (0 to PR2)
const uint8_t sine_table[SINE_TABLE_SIZE] = {
24, 24, 25, 25, 25, 25, 25, 25,
25, 26, 26, 26, 26, 26, 26, 26,
27, 27, 27, 27, 27, 27, 27, 28,
28, 28, 28, 28, 28, 28, 28, 29,
29, 29, 29, 29, 29, 29, 29, 29,
30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 33, 33, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 33, 33, 33, 33, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 34, 34,
33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32,
31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 29, 29,
29, 29, 29, 29, 29, 29, 29, 28,
28, 28, 28, 28, 28, 28, 28, 27,
27, 27, 27, 27, 27, 27, 26, 26,
26, 26, 26, 26, 26, 25, 25, 25,
25, 25, 25, 25, 24, 24, 24, 24,
24, 24, 24, 23, 23, 23, 23, 23,
23, 23, 22, 22, 22, 22, 22, 22,
22, 21, 21, 21, 21, 21, 21, 21,
20, 20, 20, 20, 20, 20, 20, 20,
19, 19, 19, 19, 19, 19, 19, 19,
19, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15
};
uint16_t phaseA_index = 0;
uint16_t phaseB_index = SINE_TABLE_SIZE / 3;
uint16_t phaseC_index = (2 * SINE_TABLE_SIZE) / 3;
void update_pwm(void) {
uint8_t dutyA = sine_table[phaseA_index];
uint8_t dutyB = sine_table[phaseB_index];
uint8_t dutyC = sine_table[phaseC_index];
// Set PWM duty cycles
PWM1DCH = dutyA;
PWM1DCL = 0; // Assuming 8-bit duty cycle
PWM2DCH = dutyB;
PWM2DCL = 0;
PWM3DCH = dutyC;
PWM3DCL = 0;
// Increment phase indexes
phaseA_index = (phaseA_index + 1) % SINE_TABLE_SIZE;
phaseB_index = (phaseB_index + 1) % SINE_TABLE_SIZE;
phaseC_index = (phaseC_index + 1) % SINE_TABLE_SIZE;
}
void main(void) {
// Oscillator configuration
OSCCONbits.IRCF = 0b1111; // 16 MHz internal oscillator
OSCCONbits.SCS = 0b10; // Internal oscillator
// Configure Timer2 for PWM
T2CONbits.T2CKPS = 0b01; // Prescaler = 4
PR2 = 49; // PR2 value for 20 kHz PWM frequency
T2CONbits.TMR2ON = 1; // Enable Timer2
// Configure PWM Modules
PWM1CONbits.PWM1POL = 0; // Active-high
PWM1CONbits.PWM1EN = 1; // Enable PWM1
PWM2CONbits.PWM2POL = 0;
PWM2CONbits.PWM2EN = 1;
PWM3CONbits.PWM3POL = 0;
PWM3CONbits.PWM3EN = 1;
// Assign PWM outputs to pins (PPS configuration)
// Adjust these based on your microcontroller and desired pins
RC5PPS = 0x0C; // PWM1 output to RC5
RC4PPS = 0x0D; // PWM2 output to RC4
RC3PPS = 0x0E; // PWM3 output to RC3
// Set PWM pins as outputs
TRISCbits.TRISC5 = 0;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC3 = 0;
// Configure Timer0 for updates
OPTION_REGbits.T0CS = 0; // Timer0 Clock Source: Internal
OPTION_REGbits.PSA = 0; // Prescaler assigned to Timer0
OPTION_REGbits.PS = 0b100; // Prescaler = 1:16
TMR0 = 256 - 20; // Preload TMR0
// Enable Timer0 interrupt
INTCONbits.TMR0IE = 1; // Enable Timer0 interrupt
INTCONbits.GIE = 1; // Enable Global Interrupts
while (1) {
// Main loop
// Implement V/F control logic here if needed
}
}
void __interrupt() ISR(void) {
if (INTCONbits.TMR0IF) {
INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag
// Reload TMR0
TMR0 = 256 - 20;
// Update PWM outputs
update_pwm();
}
}
Explanation of Changes:
-
Removed init_sine_table() Function:
- Since we're using a pre-calculated sine table, we no longer need to compute the sine values at runtime.
-
Defined a Pre-calculated sine_table[]:
- The
sine_table is defined as a const uint8_t array with 256 pre-computed values.
- The values range from approximately 0 to
PR2 (which is 49 in this case).
-
Consistency with PR2:
- The sine table values were calculated based on
PR2 = 49. If you change PR2, you should regenerate the sine table to match the new PR2 value.
-
Removed math.h Include:
- Since we're not performing any runtime mathematical calculations, the
math.h header is no longer needed.
Notes:
Advantages of Using a Pre-calculated Sine Table:
-
Performance Improvement:
- Eliminates the need for floating-point calculations at runtime, which can be slow on microcontrollers without hardware floating-point units.
-
Memory Usage:
- Saves the overhead of including the math library and reduces code size.
-
Deterministic Timing:
- Ensures consistent execution time for each update cycle, which is important for real-time applications like PWM signal generation.
Regenerating the Sine Table:
If you need to change the PR2 value or the SINE_TABLE_SIZE, you should regenerate the sine table accordingly. You can use a script (e.g., in Python) to generate the array:
import math
SINE_TABLE_SIZE = 256
PR2 = 49
sine_table = []
for i in range(SINE_TABLE_SIZE):
value = int( ( math.sin( (2 * math.pi * i) / SINE_TABLE_SIZE ) + 1 ) * (PR2 / 2) )
sine_table.append(value)
# Now print the values in C array format
print("const uint8_t sine_table[{}] = {{".format(SINE_TABLE_SIZE))
for idx, val in enumerate(sine_table):
print(f" {val},", end='')
if (idx + 1) % 8 == 0:
print()
print("};")
Conclusion:
By replacing the runtime calculation with a pre-calculated sine table, the code becomes more efficient and suitable for real-time embedded applications where computational resources are limited.