gcht101hello/code20240402.html 20240402
Sorce Code:P2MX683tone440
hello

    * P2MX683tone440 20240402
    * 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
    * device : lpf 1kohm, 1uF, speaker 
    * language: C XC8 v2.46 C99
    * IDE: MPLABX v6.20, XC8 v2.46
    * debug tool: PICkit3/PICkit4 high voltage programming
    * 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 speaker with lpf(1kohm,1uf)
   */
   //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
   struct Codeinfo{   // for code Information
       const char codeId[10];       // code information
       const char mcu[10];          // model micro controller unit         
       const int version[10];       // version number
       const long  createdDate;     // Date created code   
   };
   struct Toneperiod{  //for tone generation 
       unsigned int accumulator;  // 16bit accumulator,   overflow:65536
       const unsigned int incrementValue;  // incrementValue for tone ra:0xE6B=3691
   };
   struct Ledblink{     // for LED blinking.
       unsigned int counter;   // counter
       const unsigned int interval;  //interval for LED blinking.
   };
   void gpioBegin(void); //initialize I/O configuration.
   void timer1Begin(void);//Sets up the CCP1 module for PWM mode (active-high).
   //-------------------------------------------------
   void main(void){
       struct Codeinfo ci={"P2MX683","12F683",0,20240402};
       struct Toneperiod tp={0,0xE6B}; 
       struct Ledblink lb={0,10000};
       gpioBegin();   // Initializes General Purpose Input/Output (GPIO) pins.
       timer1Begin(); // Initializes timer1 16bit counter period:64uS,15.625kHz
       LED1=1;        // LRD1 turn on
   while(1){
       lb.counter++;   // cunter up for LED blink
       if(lb.counter>=lb.interval){  
           LED1^=1;  // LED toggle for run monitor
           lb.counter=0;   // reset counter
       }
       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
           tp.accumulator += tp.incrementValue; //(tone Freq. x2x65536)/15625
           if(STATUSbits.CARRY){ 
               SPK2 = !SPK2;       // if overflow, Tone output toggle
               tp.accumulator = 0;    // clear,  65536 16bit overflow
           }
       } //end if    
   }//end while
   }// end main
   void gpioBegin(void){
       OSCCON = 0x70;			// Sets internal oscillator to 8 MHz (OSCCON)
       ANSEL =  0b000000;    // 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;
       TMR1IF = 0;  // clear TMR1 flag
   }
   /*
    data: 64/128bytes 8%
    program: 177/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
   */