fork download
  1. #include <xc.h>
  2. #include <math.h>
  3.  
  4. #define _XTAL_FREQ 8000000
  5. #define SINE_TABLE_SIZE 32
  6.  
  7. unsigned char sineTable[SINE_TABLE_SIZE];
  8.  
  9. void generateSineTable() {
  10. for (int i = 0; i < SINE_TABLE_SIZE; i++) {
  11. sineTable[i] = (unsigned char)((sin(2 * M_PI * i / SINE_TABLE_SIZE) + 1) * 127.5);
  12. }
  13. }
  14.  
  15. void setPWMDuty(unsigned char dutyCycle) {
  16. CCPR1L = dutyCycle >> 2;
  17. CCP1CONbits.DC1B = dutyCycle & 3;
  18. }
  19.  
  20. void setupPWM() {
  21. TRISCbits.TRISC2 = 0;
  22. T2CONbits.T2CKPS = 0b11;
  23. PR2 = 199;
  24. CCP1CONbits.CCP1M = 0b1100;
  25. CCPR1L = 0;
  26. CCP1CONbits.DC1B = 0;
  27. T2CONbits.TMR2ON = 1;
  28. }
  29.  
  30. void main() {
  31. unsigned char index = 0;
  32. generateSineTable();
  33. setupPWM();
  34.  
  35. while (1) {
  36. setPWMDuty(sineTable[index]);
  37. index = (index + 1) % SINE_TABLE_SIZE;
  38. __delay_ms(5);
  39. }
  40. }
  41.  
Success #stdin #stdout 0.02s 25684KB
stdin
Standard input is empty
stdout
#include <xc.h>
#include <math.h>

#define _XTAL_FREQ 8000000
#define SINE_TABLE_SIZE 32

unsigned char sineTable[SINE_TABLE_SIZE];

void generateSineTable() {
    for (int i = 0; i < SINE_TABLE_SIZE; i++) {
        sineTable[i] = (unsigned char)((sin(2 * M_PI * i / SINE_TABLE_SIZE) + 1) * 127.5);
    }
}

void setPWMDuty(unsigned char dutyCycle) {
    CCPR1L = dutyCycle >> 2;
    CCP1CONbits.DC1B = dutyCycle & 3;
}

void setupPWM() {
    TRISCbits.TRISC2 = 0;
    T2CONbits.T2CKPS = 0b11;
    PR2 = 199;
    CCP1CONbits.CCP1M = 0b1100;
    CCPR1L = 0;
    CCP1CONbits.DC1B = 0;
    T2CONbits.TMR2ON = 1;
}

void main() {
    unsigned char index = 0;
    generateSineTable();
    setupPWM();

    while (1) {
        setPWMDuty(sineTable[index]);
        index = (index + 1) % SINE_TABLE_SIZE;
        __delay_ms(5);
    }
}