Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

#include <xc.h> #include <math.h> #define XTALFREQ 16000000 // 16 MHz oscillator…

  1. User

    #include <xc.h> #include <math.h>

    #define _XTAL_FREQ 16000000 // 16 MHz oscillator frequency

    #define SINE_TABLE_SIZE 256

    uint8_t phaseA_index = 0; uint8_t phaseB_index = SINE_TABLE_SIZE / 3; uint8_t phaseC_index = (2 * SINE_TABLE_SIZE) / 3;

    const uint16_t sine_table[256]= {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,};

    void update_pwm(void) { uint16_t dutyA = sine_table[phaseA_index]; uint16_t dutyB = sine_table[phaseB_index]; uint16_t dutyC = sine_table[phaseC_index];

    // Set PWM duty cycles
    PWM1DCH = ((dutyA & 0x03FCu) >> 2);
    PWM1DCL = ((dutyA & 0x0003u) << 6);     // Assuming 8-bit duty cycle
    
    PWM2DCH = ((dutyB & 0x03FCu) >> 2);
    PWM2DCL = ((dutyB & 0x0003u) << 6);
    
    PWM3DCH = ((dutyC & 0x03FCu) >> 2);
    PWM3DCL = ((dutyC & 0x0003u) << 6);
    
    
    // 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;
    

    }

    volatile uint8_t tmr0_reload_value = 20; // Initial TMR0 reload value

    void init_adc(void) { // Configure ADC ADCON1bits.ADFM = 1; // Right justified ADCON1bits.ADCS = 0b110; // FOSC/64 ADCON1bits.ADPREF = 0; // VREF+ = VDD ADCON0bits.CHS = 0b00000; // Select AN0 ADCON0bits.ADON = 1; // Enable ADC __delay_us(10); // Acquisition time }

    void main(void) { // Oscillator configuration OSCCONbits.IRCF = 0b1111; // 16 MHz internal oscillator OSCCONbits.SCS = 0b10; // Internal oscillator

    // Configure Timer2 for PWM
    T2CONbits.T2CKPS = 0b00;  // Prescaler = 1
    PR2 = 199;                 // PR2 value for 20 kHz PWM frequency
    T2CONbits.TMR2ON = 1;     // Enable Timer2
    
    // Configure PWM Modules
    
    PWM1CON = 0xC0;
    PWM2CON = 0xC0;
    PWM3CON = 0xC0;
    
    // Set PWM pins as outputs
    TRISCbits.TRISC5 = 0;
    TRISCbits.TRISC3 = 0;
    TRISAbits.TRISA2 = 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
    

    init_adc();

    while (1) {
        // Start ADC conversion
        ADCON0bits.GO_nDONE = 1;    
        while (ADCON0bits.GO_nDONE); // Wait for conversion to complete
    
        // Read ADC result
        uint16_t adc_value = ((uint16_t)ADRESH << 8) | ADRESL;
    
        // Map ADC value to TMR0 reload value
        tmr0_reload_value = 16 + ((uint32_t)adc_value * 33) / 1023;
    
        // Clamp the reload value to the valid range
        if (tmr0_reload_value < 16) tmr0_reload_value = 16;
        if (tmr0_reload_value > 49) tmr0_reload_value = 49;
    
        // Optional: Additional logic or delay
        __delay_ms(10);
    }
    

    }

    void __interrupt() ISR(void) { if (INTCONbits.TMR0IF) { INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag

        // Reload TMR0
        TMR0 = 256 - tmr0_reload_value;
    
        // Update PWM outputs
        update_pwm();
    }
    

    } add a amplitude scaling through another adc

  2. Assistant

    Certainly! To add amplitude scaling through another ADC channel to your existing code, we'll need to perform the following steps:

    1. Configure AN1 as Analog Input: We'll set up AN1 (RA1 pin) as an analog input for amplitude control.

    2. Modify the Main Loop: We'll read the ADC value from AN1 and map it to an amplitude scaling factor.

    3. Update the update_pwm Function: We'll adjust the PWM duty cycles based on the amplitude scaling factor.

    Here's the modified code with the necessary changes:

    #include <xc.h>
    #include <math.h>
    
    #define _XTAL_FREQ 16000000  // 16 MHz oscillator frequency
    
    #define SINE_TABLE_SIZE 256
    
    uint8_t phaseA_index = 0;
    uint8_t phaseB_index = SINE_TABLE_SIZE / 3;
    uint8_t phaseC_index = (2 * SINE_TABLE_SIZE) / 3;
    
    const uint16_t sine_table[256]=
    {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 uint8_t amplitude_scaling = 255;  // Amplitude scaling factor (0-255)
    
    void update_pwm(void) 
    {
        uint16_t dutyA = sine_table[phaseA_index];
        uint16_t dutyB = sine_table[phaseB_index];
        uint16_t dutyC = sine_table[phaseC_index];
    
        // Scale the duty cycles by amplitude_scaling
        uint16_t temp_dutyA = ((uint32_t)dutyA * amplitude_scaling) >> 8;
        uint16_t temp_dutyB = ((uint32_t)dutyB * amplitude_scaling) >> 8;
        uint16_t temp_dutyC = ((uint32_t)dutyC * amplitude_scaling) >> 8;
    
        // Set PWM duty cycles (10-bit duty cycle)
        PWM1DCH = ((temp_dutyA & 0x03FCu) >> 2);
        PWM1DCL = ((temp_dutyA & 0x0003u) << 6);
    
        PWM2DCH = ((temp_dutyB & 0x03FCu) >> 2);
        PWM2DCL = ((temp_dutyB & 0x0003u) << 6);
    
        PWM3DCH = ((temp_dutyC & 0x03FCu) >> 2);
        PWM3DCL = ((temp_dutyC & 0x0003u) << 6);
    
        // 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;
    }
    
    volatile uint8_t tmr0_reload_value = 20;  // Initial TMR0 reload value
    
    void init_adc(void)
    {
        // Configure ADC
        ADCON1bits.ADFM = 1;       // Right justified
        ADCON1bits.ADCS = 0b110;   // FOSC/64
        ADCON1bits.ADPREF = 0;     // VREF+ = VDD
        ADCON0bits.CHS = 0b00000;  // Select AN0
        ADCON0bits.ADON = 1;       // Enable ADC
        __delay_us(10);            // Acquisition time
    
        // Set AN0 and AN1 as analog inputs
        ANSELAbits.ANSA0 = 1;      // RA0/AN0 as analog input for frequency control
        ANSELAbits.ANSA1 = 1;      // RA1/AN1 as analog input for amplitude control
    
        // Set TRIS bits for inputs
        TRISAbits.TRISA0 = 1;      // RA0 as input
        TRISAbits.TRISA1 = 1;      // RA1 as input
    }
    
    void main(void) {
        // Oscillator configuration
        OSCCONbits.IRCF = 0b1111; // 16 MHz internal oscillator
        OSCCONbits.SCS = 0b10;    // Internal oscillator
    
        // Configure Timer2 for PWM
        T2CONbits.T2CKPS = 0b00;  // Prescaler = 1
        PR2 = 199;                // PR2 value for 20 kHz PWM frequency
        T2CONbits.TMR2ON = 1;     // Enable Timer2
    
        // Configure PWM Modules
        PWM1CON = 0xC0;
        PWM2CON = 0xC0;
        PWM3CON = 0xC0;
    
        // Set PWM pins as outputs
        TRISCbits.TRISC5 = 0;
        TRISCbits.TRISC3 = 0;
        TRISAbits.TRISA2 = 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
    
        init_adc();
    
        while (1) {
            // Start ADC conversion on AN0 (Frequency control)
            ADCON0bits.CHS = 0b00000;  // Select AN0
            __delay_us(10);            // Acquisition time
            ADCON0bits.GO_nDONE = 1;    
            while (ADCON0bits.GO_nDONE); // Wait for conversion to complete
    
            // Read ADC result for frequency control
            uint16_t adc_value = ((uint16_t)ADRESH << 8) | ADRESL;
    
            // Map ADC value to TMR0 reload value
            tmr0_reload_value = 16 + ((uint32_t)adc_value * 33) / 1023;
    
            // Clamp the reload value to the valid range
            if (tmr0_reload_value < 16) tmr0_reload_value = 16;
            if (tmr0_reload_value > 49) tmr0_reload_value = 49;
    
            // Start ADC conversion on AN1 (Amplitude control)
            ADCON0bits.CHS = 0b00001;  // Select AN1
            __delay_us(10);            // Acquisition time
            ADCON0bits.GO_nDONE = 1;   
            while (ADCON0bits.GO_nDONE); // Wait for conversion to complete
    
            // Read ADC result for amplitude control
            uint16_t amplitude_adc_value = ((uint16_t)ADRESH << 8) | ADRESL;
    
            // Map ADC value to amplitude scaling factor (0-255)
            amplitude_scaling = amplitude_adc_value >> 2;
    
            // Optional: Additional logic or delay
            __delay_ms(10);
        }
    }
    
    void __interrupt() ISR(void) {
        if (INTCONbits.TMR0IF) {
            INTCONbits.TMR0IF = 0; // Clear Timer0 interrupt flag
    
            // Reload TMR0
            TMR0 = 256 - tmr0_reload_value;
    
            // Update PWM outputs
            update_pwm();
        }
    }
    

    Explanation of Changes:

    • Global Variable for Amplitude Scaling:

      • Added a global volatile uint8_t amplitude_scaling variable to store the amplitude scaling factor.
    • ADC Initialization (init_adc Function):

      • Configured ANSELAbits.ANSA1 to set AN1 (RA1) as an analog input.
      • Set TRISAbits.TRISA1 as input for RA1.
    • Main Loop Modifications:

      • After reading the ADC value from AN0 (frequency control), we switch the ADC channel to AN1 for amplitude control.
      • Mapped the 10-bit ADC value from AN1 to an 8-bit amplitude scaling factor by right-shifting by 2 bits (amplitude_scaling = amplitude_adc_value >> 2;).
    • Updated update_pwm Function:

      • Scaled the duty cycles (dutyA, dutyB, dutyC) by the amplitude_scaling factor using the formula:
        temp_dutyA = ((uint32_t)dutyA * amplitude_scaling) >> 8;
        
        This effectively scales the amplitude of the sine wave according to the ADC input on AN1.
      • Adjusted the PWM duty cycle registers (PWM1DCH, PWM1DCL, etc.) to use the scaled duty cycles (temp_dutyA, etc.).

    With these changes, your code now reads a second ADC channel (AN1) to control the amplitude of the output waveform. By adjusting the input voltage on AN1, you can vary the amplitude of the generated sine waves in real-time.

    Note: Ensure that the hardware connections match the code modifications, specifically connecting your amplitude control voltage to the AN1 (RA1) pin of your microcontroller.