#include <xc.h> #include <stdint.h> #define XTALFREQ 16000000 // 16 MHz oscillator…
- User
#include <xc.h> #include <stdint.h>
#define _XTAL_FREQ 16000000 // 16 MHz oscillator frequency
#define SINE_TABLE_SIZE 256
// Sine table with values from 0 to 1023 for 10-bit PWM const uint16_t sine_table[SINE_TABLE_SIZE] = {384, 393, 403, 412, 422, 431, 440, 450, 459, 468, 477, 486, 495, 504, 513, 522, 531, 540, 548, 557, 565, 573, 581, 589, 597, 605, 613, 620, 628, 635, 642, 649, 656, 662, 669, 675, 681, 687, 692, 698, 703, 708, 713, 718, 723, 727, 731, 735, 739, 742, 746, 749, 751, 754, 756, 759, 761, 762, 764, 765, 766, 767, 768, 768, 768, 768, 768, 767, 766, 765, 764, 762, 761, 759, 756, 754, 751, 749, 746, 742, 739, 735, 731, 727, 723, 718, 713, 708, 703, 698, 692, 687, 681, 675, 669, 662, 656, 649, 642, 635, 628, 620, 613, 605, 597, 589, 581, 573, 565, 557, 548, 540, 531, 522, 513, 504, 495, 486, 477, 468, 459, 450, 440, 431, 422, 412, 403, 393, 384, 375, 365, 356, 346, 337, 328, 318, 309, 300, 291, 282, 273, 264, 255, 246, 237, 228, 220, 211, 203, 195, 187, 179, 171, 163, 155, 148, 140, 133, 126, 119, 112, 106, 99, 93, 87, 81, 76, 70, 65, 60, 55, 50, 45, 41, 37, 33, 29, 26, 22, 19, 17, 14, 12, 9, 7, 6, 4, 3, 2, 1, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 9, 12, 14, 17, 19, 22, 26, 29, 33, 37, 41, 45, 50, 55, 60, 65, 70, 76, 81, 87, 93, 99, 106, 112, 119, 126, 133, 140, 148, 155, 163, 171, 179, 187, 195, 203, 211, 220, 228, 237, 246, 255, 264, 273, 282, 291, 300, 309, 318, 328, 337, 346, 356, 365, 375,};
volatile uint16_t phaseA_index = 0; volatile uint16_t phaseB_index = SINE_TABLE_SIZE / 3; volatile uint16_t phaseC_index = (2 * SINE_TABLE_SIZE) / 3;
// Variables for V/F control volatile uint16_t amplitude_scaling = 784; // Default to maximum amplitude volatile uint16_t frequency_scaling = 1; // Not used in this version
// Timer0 variables volatile uint8_t tmr0_reload_value = 0;
void update_pwm(void); void init_ADC(void); uint16_t read_ADC(uint8_t channel); void init_PWM(void); void init_Timer0(uint16_t freq_adc); void init_Oscillator(void); void init_PPS(void);
void update_pwm(void) { // Get the raw sine values uint16_t raw_dutyA = sine_table[phaseA_index]; uint16_t raw_dutyB = sine_table[phaseB_index]; uint16_t raw_dutyC = sine_table[phaseC_index];
code// Scale the amplitude based on the amplitude_scaling factor (0 to 1023) uint16_t dutyA = (raw_dutyA * amplitude_scaling) / 784; uint16_t dutyB = (raw_dutyB * amplitude_scaling) / 784; uint16_t dutyC = (raw_dutyC * amplitude_scaling) / 784; // Set PWM duty cycles (10-bit values) PWM1DCH = (dutyA >> 2) & 0xFF; PWM1DCL = (dutyA & 0x0003) << 6; PWM2DCH = (dutyB >> 2) & 0xFF; PWM2DCL = (dutyB & 0x0003) << 6; PWM3DCH = (dutyC >> 2) & 0xFF; PWM3DCL = (dutyC & 0x0003) << 6; // Increment phase indexes by 1 phaseA_index = (phaseA_index + 1) % SINE_TABLE_SIZE; phaseB_index = (phaseB_index + 1) % SINE_TABLE_SIZE; phaseC_index = (phaseC_index + 1) % SINE_TABLE_SIZE; - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.