gcht101hello/code20240330.html 20240330
Sorce Code:P2MX683tone440
hello

    * P2MX683tone440 20240330
    * description:generates tone 440Hz using timer2 based 64uS
    * incrementValue = (tone frequency:440Hz) x 2 x 65536 )/15625
    * board: Curiosity development with 12F683 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 
    * measuring tool: kyutech arduino scope
   //------------------------------------------------
    * 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 SPK2 GP2  // SPK2  speaker  signal generator output toggle: 440Hz
   #define LED1 GP1  // LED1  for run monitor
   unsigned int dutyValue=0;
   void gpioBegin(); //initialize I/O configuration.
   void timer1Begin(void);//Sets up the CCP1 module for PWM mode (active-high).
   uint16_t accumulator = 0;      // 16bit accumulator,   overflow:65536
   uint16_t incrementVaue = 0xE6B;  //0xE6B=3691,    (toneFreq x 2 x 65536)/15625
   unsigned int count = 0;        // for run monitor
   //-------------------------------------------------
   void main(void) {
       gpioBegin();   // Initializes General Purpose Input/Output (GPIO) pins.
       timer1Begin(); // Initializes timer1 16bit counter period:64uS,15.625kHz
   while (1){
       if(TMR1IF){                 // TMR1 Timeover Actual Period:64 us
           TMR1IF = 0;             // flag clear 
           TMR1H=255;              // set TMR1 counter:65536-128
           TMR1L=255-128;          // 0.5*128=64us,15.625kHz
           accumulator += incrementVaue; //incrementVaue=(ton Freqx2x65536)/15625
           if(STATUSbits.CARRY){ 
               SPK2 = !SPK2;       // if overflow, Tone output toggle
               accumulator = 0;    // clear,  65536 16bit overflow
               count=count+1;      // count up 
               if(count>100){
                   count=0;
                   LED1=!LED1;     // lED toogle for run monitor
               }
           }
       }//end if    
   }//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 = 0b001001;		// GP0,3:INPUT GP1,2,4,5;OUTPUT
     CMCON0 = 0x07; // Enables comparator (CMCON0) (potentially for future use)
   }
   void timer1Begin(void){
       T1CON  = 0b00000001;  // setup 16bit Timer1, PS_1/1, Enable 
                             //15.625 kHz (64us period) FOSC/4=2MHz, 0.5us
       TMR1H=255;            // TMR1 counter: 256*256-128
       TMR1L=255-128;
       PIR1bits.TMR1IF = 0;  // clear TMR1 flag
   }
   /*
    data: 10/128bytes 8%
    program: 65/2048words 3%
    */
   
   /*
   T1CON: TIMER1 CONTROL REGISTER
   T1GINV(1) TMR1GE(2) T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON
   bit 7 T1GINV: Timer1 Gate Invert bit(1)
       1 = Timer1 gate is active-high (Timer1 counts when gate is high)
       0 = Timer1 gate is active-low (Timer1 counts when gate is low)
   bit 6 TMR1GE: Timer1 Gate Enable bit(2)
       If TMR1ON = 0:
       This bit is ignored
       If TMR1ON = 1:
       1 = Timer1 is on if Timer1 gate is not active
       0 = Timer1 is on
   bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits
       11 = 1:8 Prescale Value
       10 = 1:4 Prescale Value
       01 = 1:2 Prescale Value
       00 = 1:1 Prescale Value
   bit 3 T1OSCEN: LP Oscillator Enable Control bit
       If INTOSC without CLKOUT oscillator is active:
           1 = LP oscillator is enabled for Timer1 clock
           0 = LP oscillator is off
       Else:
           This bit is ignored. LP oscillator is disabled.
   bit 2 T1SYNC: Timer1 External Clock Input Synchronization Control bit
       TMR1CS = 1:
       1 = Do not synchronize external clock input
       0 = Synchronize external clock input
       TMR1CS = 0:
       This bit is ignored. Timer1 uses the internal clock
   bit 1 TMR1CS: Timer1 Clock Source Select bit
       1 = External clock from T1CKI pin (on the rising edge)
       0 = Internal clock (FOSC/4)
   bit 0 TMR1ON: Timer1 On bit
       1 = Enables Timer1
   */