gcht101hello/code20240329.html 20240329
Sorce Code:P2MX683pulsegenerate
hello

    /* description for embedded beginner and feedback and suggestion for next steps
    * P2MX683pulsegenerate  20240329
    * description:generates pulse using  10bitPWM(CCP)
    * Frequency: 8KHz, 10kHz, 20KHz,40KHz
    * 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  LED monitor
   RST S2  4 GP3/MCLR GP2/CCP1 5 LED PWM OUT
   */
   //INTOSCIO 8MHz 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  // LED2  signal generator output
   #define LED1 GP1  // LED1  signal generator monitof  toggle: singal/1000
   unsigned int dutyValue=0;
   void gpioBegin(); //initialize I/O configuration.
   void pwmBegin(void);//Sets up the CCP1 module for PWM mode (active-high).
   //-------------------------------------------------
   unsigned int count = 0;
   void main(void) {
       gpioBegin(); // Initializes General Purpose Input/Output (GPIO) pins.
       pwmBegin();  // CCP1 for PWM mode with a period of approximately 8KHz.
       PEIE   = 1;  // Enables all peripheral interrupts
       GIE = 1;     // Enable global interrupts
   while(1){              //Enters an infinite loop
       PR2 = 249 ;         // PWM period  approx. 8KHz  0.5*250=125[us]
       CCPR1L = 249/2;     //   duty 50%  MSB8bit
       CCP1CONbits.DC1B=0x01; // duty 50% LSB2bit
       __delay_ms(2000);
         PR2 = 200 ;       // PWM period  approx  10kHz  0.5*200=100[us]
       CCPR1L = 200/2;     //   duty 50%  MSB8bit
       CCP1CONbits.DC1B=0x00; // duty 50% LSB2bit          
       __delay_ms(2000);
       PR2 =    100;       //PWM period  approx.  20KHz   0.5*100=50[us]
       CCPR1L = 100/2;     //   duty 50%  MSB8bit
       CCP1CONbits.DC1B=0x00; // duty 50% LSB2bit
       __delay_ms(2000);
       PR2 = 50;       //PWM period  approx.   40KHz     0.5+50= 25[us]  
       CCPR1L = 50/2;     //   duty 50%  MSB8bit
       CCP1CONbits.DC1B=0x00; // duty 50% LSB2bit
       __delay_ms(2000);
   }// end while    
   }// end main
   //-----------------------------------------------
   void __interrupt() T2ISR(void) {
     if (PIR1bits.TMR2IF) {  // Check if Timer2 overflow interrupt flag is set
       PIR1bits.TMR2IF = 0;  // Clear the interrupt flag
       count++;
       if (count >= 1000) {
         count = 0;
         LED1 = !LED1;
       }
     }
   }
   
   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 = 0b001001;		// GP0,3:INPUT GP1,2,4,5;OUTPUT
     CMCON0 = 0x07; // Enables comparator (CMCON0) (potentially for future use)
   }
   void pwmBegin(void){
       TRISIO2 = 0;           // GP2:PWM OUTPUT
       CCP1CON = 0b00001100;  // PWM mode active-high
       PR2 = 249 ;            // Set PWM period  approx. 8KHz
       CCPR1L = 249/2;        // Sets the duty cycle  MSB:8bit
       CCP1CONbits.DC1B=0x00; // Sets the duty cycle  MSB:2bit
       T2CON =0b00000100;     // set TMR2ON, Prescaler 1:1
       TMR2IE = 1;   // Enable Timer2 overflow interrupt
   }
   
   /* result
    Data: 11/128bytes 9%
    Program: 169/2048words 8%
    */
   /*
   PWM Period Calculation:
   T2CON = 0b00000100
   * TMR2ON=1 * prescale:1
   Timer2_Clock = FOSC / 4 / 1 (prescale)= 8 MHz / 4 /1 = 2 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 /2 MHz) = 250 * 0,5 us= 125us
    * approximately 8kHz
    */
   /*
   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 pinis 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
   */