gcht101hello/code20240324.html 202403224
Sorce Code:P2MX683pwmsinsimple
hello

    /* description for embedded beginner and feedback and suggestion for next steps
    * P2MX683pwmsinsimple  20240322
    * description:generates sin wave 50Hz using  PWM(CCP)
    * LED brightness is weakly, normal and strongly. 
    * board: Curiosity development with PIC12F683 5V INTOSC 8MHz
    * MPU; PIC12F683 DIP8P INTOSC8MHz
    * Flash:2KW, RAM:128B,EEPROM 256Byte
    * language: C XC8 v2.46 C99
    * IDE: MPLABX v6.20, XC8 v2.46
    * debug tool: PICkit3/PICkit4 
   //------------------------------------------------
    * PIC12F683 DIP 8Pin on Curiosity development board 	
   5V      1 VDD           VSS 8 GND
           2 GP5           GP0 7 ICSPDAT
           3 GP4           GP1 6 ICSPCLK
   RST S2  4 GP3/MCLR GP2/CCP1 5 LED PWM OUT
   */
   //config:INTOSCIO  8 MHz, Disable watchdog timer, Enable master clear on reset
   #pragma config FOSC=INTOSCIO, WDTE=OFF, PWRTE=OFF, MCLRE=ON    
   #pragma config CP= OFF, CPD=OFF, BOREN=ON, IESO=ON, FCMEN=ON    
   #include 
   #define _XTAL_FREQ 8000000
   #define LED2 GP2  //Defined  LED2  contrilled by PWM
   unsigned int dutyValue=0;
   void gpioBegin(); //initialize I/O configuration.
   void pwmBegin(void);//Sets up the CCP1 module for PWM mode (active-high).
   void pwmDutyWrite(unsigned int duty);//PWM duty cycle based on the provided.
   //-------------------------------------------------
   const unsigned char sinValue[] ={   //  sin wave data each 9 degree
       125,145,164,182,198,213,226,236,244,248,
       250,248,244,236,226,213,198,182,164,145,
       125,105, 86, 68, 52, 37, 24, 14,  6,  2,
         0,  2,  6, 14, 24, 37, 52, 68, 86,105
       };
   unsigned char count = 0;
   void main(void) {
       gpioBegin(); //initialization
       pwmBegin();  //initialization
   while(1){              //loop 
       while(!TMR2IF);
       TMR2IF=0;    
       count++;
       CCPR1L = sinValue[count];   
       if(count >= 40)count = 0;  
   }// end while 
   }// end main
   
   void gpioBegin(void){
       OSCCON = 0x70;			// Sets internal oscillator to 8 MHz (OSCCON)
       ANSEL =  0b00000000;    // Sets all pins to digital inputs
       GPIO   = 0b000000;		// GPIO clear
       TRISIO = 0b011011;		// GP0,1,3,4:INPUT,GP2,GP5;OUTPUT
       CMCON0 = 0x07; // Enables comparator (CMCON0) (potentially for future use)
   }
   void pwmBegin(void){
       TRISIO2 = 0;           //  GP2:PWM OUTPUT
       CCP1CON = 0b00001100;  //  PWM mode active-high
       CCPR1L = 0;            // duty 0  
       PR2 = 249 ;            //  set PWM period  approx. 2KHz
       T2CON =0b00000101;     //  set TMR2ON, Prescaler 1:4
       while(!TMR2IF);        //  wait overflow
     
   }
   void pwmDutyWrite(unsigned int duty){
       while(!TMR2IF);  // wait timer2 overflow
       TMR2IF=0;        // flag clear
       count++;         // index count up
       CCPR1L = sinValue[count];   // set duty according sin table
       if(count >= 40)count = 0;    // 1 cycle 40 sampling
   }
   /* result
    Data: 5/128bytes 6%
    Program: 108/2048words 4%
    */
   /*
   PWM Period Calculation:
   T2CON = 0b00000101
    * TMR2ON=1 * prescale:  1/4
   Timer2_Clock = FOSC / 4 / 4 (prescale)= 8 MHz / 4 /4 = 0.5 MHz
   PR2 Register Value: The code sets PR2 = 249
   Calculating PWM Period:
   The PWM period (in seconds)
   PWM_Period = (PR2 + 1) * Timer2_Clock_Period
   PWM_Period = (249 + 1) * (1 /0.5 MHz) = 250 * 2 us= 500us
    * approximately 2kHz
    */
   /*
    CCP1CON: CCP1 CONTROL REGISTER
   ? ? DC1B1 DC1B0 CCP1M3 CCP1M2 CCP1M1 CCP1M0
   bit 7-6 Unimplemented: Read as ?0?
   bit 5-4 DC1B<1:0>: PWM Duty Cycle Least Significant bits
   Capture mode:
   Unused.
   Compare mode:
   Unused.
   PWM mode:
   These bits are the two LSbs of the PWM duty cycle. The eight MSbs are found in CCPR1L.
   bit 3-0 CCP1M<3:0>: CCP Mode Select bits
   0000 = Capture/Compare/PWM off (resets CCP module)
   0001 = Unused (reserved)
   0010 = Unused (reserved)
   0011 = Unused (reserved)
   0100 = Capture mode, every falling edge
   0101 = Capture mode, every rising edge
   0110 = Capture mode, every 4th rising edge
   0111 = Capture mode, every 16th rising edge
   1000 = Compare mode, set output on match (CCP1IF bit is set)
   1001 = Compare mode, clear output on match (CCP1IF bit is set)
   1010 = Compare mode, generate software interrupt on match (CCP1IF bit is set, CCP1 pin
   is unaffected)
   1011 = Compare mode, trigger special event (CCP1IF bit is set, TMR1 is reset and A/D
   conversion is started if the ADC module is enabled. CCP1 pin is unaffected.)
   110x = PWM mode active-high
   111x = PWM mode active-low
   */
   
   
   /*
   T2CON: TIMER 2 CONTROL REGISTER
   ? TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
   bit 6-3 TOUTPS<3:0>: Timer2 Output Postscaler Select bits
   0000 = 1:1 Postscaler
   0001 = 1:2 Postscaler
   0010 = 1:3 Postscaler
   0011 = 1:4 Postscaler
   0100 = 1:5 Postscaler
   0101 = 1:6 Postscaler
   0110 = 1:7 Postscaler
   0111 = 1:8 Postscaler
   1000 = 1:9 Postscaler
   1001 = 1:10 Postscaler
   1010 = 1:11 Postscaler
   1011 = 1:12 Postscaler
   1100 = 1:13 Postscaler
   1101 = 1:14 Postscaler
   1110 = 1:15 Postscaler
   1111 = 1:16 Postscaler
   bit 2 TMR2ON: Timer2 On bit
   1 = Timer2 is on
   0 = Timer2 is off
   bit 1-0 T2CKPS<1:0>: Timer2 Clock Prescale Select bits
   00 = Prescaler is 1
   01 = Prescaler is 4
   1x = Prescaler is 16
   */