gcht101hello/code20240328.html 202403228
Sorce Code:P6MX115serialsin
hello

    // description for embedded beginner, advise and suggestion for next steps
    // P6MX115serialsin 20240328
    // description: generate sinwave data for sinwave PWM on PIC
    // board :bread board PIC16F18115 5V INTOSC 32MHz
    // MPU; PIC16F18115 Flash:14KB, SRAM:1KB, EEPROM:128B 
    // TIMER:2, PWM16bit:2 USART2,MSSP2,1DSM;1DA8bit;2,ADC12bit:5
    // device:USB-TTL
    // language: C XC8 v2.46 C99
    // IDE: MPLAB X v6.20, PIC16F1xxx_DFP 1.24.1848
    // Debug Tool; PICkit4  Use low voltage programming mode entry
    // Serial; monitor/plotter 115200baud using Arduino IDE
    // ----------------------------------------------------
    // PINOUT: PIC16F18115 DIP 8pin
    //3V3    1 VDD         VSS 8 GND
    //LED       2 RA5         RA0/RX 7 ICSPDAT
    //       3 RA4         RA1/TX 6 ICSPCLK  USB-TTL RX
    //RST S1 4 MCR         RA2/RX 5 RA2      USB-TTL TX 
    // select parameeter
    // MAx table size |  Max sin data, range from 0 to Max
    //                |  249.0     |    255.0
    //-------------------------------------------
    //   40           | calculated |   calculated
    //   80           | calculated |  calculated
    //   180          | calculated |   calculated
    //------------------------------------------
    
    #pragma config FEXTOSC = OFF, RSTOSC = HFINTOSC_32MHz // Power-up default value for COSC bits (HFINTOSC with 2x PLL (32MHz))
    #pragma config CLKOUTEN = OFF, CSWEN = ON, FCMEN = ON    // MCLRE  
    #pragma config MCLRE = EXTMCLR, PWRTS = PWRT_OFF,WDTE = OFF, LPBOREN = OFF,BOREN = ON      
    #pragma config BORV = LO, PPS1WAY = ON, STVREN = ON,  DEBUG = OFF    
    #pragma config WRTSAF = OFF, LVP = ON    // low voltage
    #pragma config CP= OFF, CPD=OFF
    
    #include 
    #include   //for sprintf
    #include 
    #define _XTAL_FREQ 32000000 // delay_ms(x)
    #define LEDA4 LATA4  // LED4 pin
    #define LEDA5 LATA5  // LED5 pin
    #define sinMAX 249.0   // sin data range 0-249
    //#define sinMAX 255.0 // sin data range 0-255 
    #define tableMax 160 // sinwave table MAX 160
    //#define tableMax 80 // sinwave table MAX  80
    //#define tableMax 40 // sinwave table MAX 40
    //------------------------------------
    void putch(char ch) {
        while (!TX1IF);      //serial write
        TX1REG = ch;
    }
    void gpioBegin(void);
    void serialBegin(void);
    void serialString(char *string);
    char sinWave[tableMax];  // sinwave table 
    float degSin;   // calculate sin degree
    void main(void) {
        gpioBegin();
        serialBegin();
        serialString("\r\nP6MX115serialsin\r\n");       
    //----------------------------------------------------  
    while(1){  
        degSin = (2.0 * 3.141592) / (float)(tableMax);
        for(char i=0; iEUSART:RX1;   
        RA1PPS= 0x13;  //RA1->EUSART:TX1;  
    }
    void serialString(char *string){
        while(*string) putch(*string++);
    }
    //-----------------------------------------------------
    
    /*
     result
     Data: 410/1024bytes 40% 
     program: 5121/8192 words 63%
     */
    
    /* result  MAX table:160, sin data range:0-255
     127, 132, 137, 142, 147, 152, 157, 162, 166, 171,
     176, 180, 185, 189, 194, 198, 202, 206, 210, 214,
     217, 221, 224, 227, 230, 233, 236, 238, 241, 243,
     245, 247, 248, 250, 251, 252, 253, 254, 254, 254,
     255, 254, 254, 254, 253, 252, 251, 250, 248, 247,
     245, 243, 241, 238, 236, 233, 230, 227, 224, 221,
     217, 214, 210, 206, 202, 198, 194, 189, 185, 180,
     176, 171, 166, 162, 157, 152, 147, 142, 137, 132,
     127, 122, 117, 112, 107, 102,  97,  92,  88,  83,
      78,  74,  69,  65,  60,  56,  52,  48,  44,  40,
      37,  33,  30,  27,  24,  21,  18,  16,  13,  11,
       9,   7,   6,   4,   3,   2,   1,   0,   0,   0,
       0,   0,   0,   0,   1,   2,   3,   4,   6,   7,
       9,  11,  13,  16,  18,  21,  24,  27,  30,  33,
      37,  40,  44,  48,  52,  56,  60,  65,  69,  74,
      78,  83,  88,  92,  97, 102, 107, 112, 117, 122,
    ----------------------------------------------
     */
     /* 
      * result  MAX table: 80 sin data range:0-255
      * ----------------------------------------------
     127, 137, 147, 157, 166, 176, 185, 194, 202, 210,
     217, 224, 230, 236, 241, 245, 248, 251, 253, 254,
     255, 254, 253, 251, 248, 245, 241, 236, 230, 224,
     217, 210, 202, 194, 185, 176, 166, 157, 147, 137,
     127, 117, 107,  97,  88,  78,  69,  60,  52,  44,
      37,  30,  24,  18,  13,   9,   6,   3,   1,   0,
       0,   0,   1,   3,   6,   9,  13,  18,  24,  30,
      37,  44,  52,  60,  69,  78,  88,  97, 107, 117,
    ----------------------------------------------
    */
    /*result MAX table:40, sin data range:0-249
      127, 147, 166, 185, 202, 217, 230, 241, 248, 253,
     255, 253, 248, 241, 230, 217, 202, 185, 166, 147,
     127, 107,  88,  69,  52,  37,  24,  13,   6,   1,
       0,   1,   6,  13,  24,  37,  52,  69,  88, 107,
    ----------------------------------------------
    */
    /* result MAX table:40, sin data range:0-249
     124, 143, 162, 181, 197, 212, 225, 235, 242, 247,
     249, 247, 242, 235, 225, 212, 197, 181, 162, 143,
     124, 105,  86,  67,  51,  36,  23,  13,   6,   1,
       0,   1,   6,  13,  23,  36,  51,  67,  86, 105,
    */
    /* result MAX table:80, sin data range:0-249
     124, 134, 143, 153, 162, 172, 181, 189, 197, 205,
     212, 219, 225, 230, 235, 239, 242, 245, 247, 248,
     249, 248, 247, 245, 242, 239, 235, 230, 225, 219,
     212, 205, 197, 189, 181, 172, 162, 153, 143, 134,
     124, 114, 105,  95,  86,  76,  67,  59,  51,  43,
      36,  29,  23,  18,  13,   9,   6,   3,   1,   0,
       0,   0,   1,   3,   6,   9,  13,  18,  23,  29,
      36,  43,  51,  59,  67,  76,  86,  95, 105, 114,
     */
    /* result MAX table:180, sin data range:0-249
     124, 129, 134, 139, 143, 148, 153, 158, 162, 167,
     172, 176, 181, 185, 189, 193, 197, 201, 205, 209,
     212, 215, 219, 222, 225, 228, 230, 233, 235, 237,
     239, 241, 242, 244, 245, 246, 247, 248, 248, 248,
     249, 248, 248, 248, 247, 246, 245, 244, 242, 241,
     239, 237, 235, 233, 230, 228, 225, 222, 219, 215,
     212, 209, 205, 201, 197, 193, 189, 185, 181, 176,
     172, 167, 162, 158, 153, 148, 143, 139, 134, 129,
     124, 119, 114, 109, 105, 100,  95,  90,  86,  81,
      76,  72,  67,  63,  59,  55,  51,  47,  43,  39,
      36,  33,  29,  26,  23,  20,  18,  15,  13,  11,
       9,   7,   6,   4,   3,   2,   1,   0,   0,   0,
       0,   0,   0,   0,   1,   2,   3,   4,   6,   7,
       9,  11,  13,  15,  18,  20,  23,  26,  29,  33,
      36,  39,  43,  47,  51,  55,  59,  63,  67,  72,
      76,  81,  86,  90,  95, 100, 105, 109, 114, 119,
     */