gcht101hello/code20240321.html 20240321
Sorce Code:P2MX683giosimple
hello

    /* 
    * P2MX683gpiosimple 20240321
    * description: An LED is blinking , controlled by button
    * board: Curiosity development  PIC12F683  5V INTOSC8MHz 
    * MPU; PIC12F683 Flash:2kW, SRAM:128B, EEPROM:256B  
    * TIMER8:2,timer16:1, comp:1,ADC10bit:4 
    * device: LED
    * language: C XC8 v2.46 C99
    * IDE: MPLABX v6.20 on windows11
    * debug tools: PICkit4/PICkit3
   //---------------------------------------
   PIC12F683 DIP 8Pin  on Curiosity development
   5V     1 VDD       VSS 8 GND
   BUTA5  2 GP5       GP0 7 ICSPDAT 
   -      3 GP4       GP1 6 ICSPCLK 
   RST    4 MCLR/GP3  GP2 5 LED2
   //---------------------------------------- 
   //IO MAP 
   // GP5  GP4  GP3 GP2  GP1  GP0 
   //                
   // IN   OUT  IN  OUT  OUT  OUT 
   // BUT       RST LED2  
   //-----------------------------------
   // LED D7  D6   D5  D4
   //     NC  GP2  PCL NC
    */ 
   //---------------------------------------------------
   //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 // int osc 8MHz
   #define LED2 GP2   //Defines pin connected to the LED
   #define BUT5 GP5   //Defines pin connected to the button 
   void gpioBegin(void);
   //------------------------------------------------------
   void main(void) {
       gpioBegin();  //initialize GPIO.
   //------------------------------------------------------    
   while(1){                         //Checks button state (BUT5)
            if(BUT5) __delay_ms(1000);   // slow   button is not pressed
                else  __delay_ms(100);  // quick If button is pressed 
            LED2 = ! LED2;             // Toggles the LED state
   }//end while
   }//end main
   void gpioBegin(void){
       OSCCON=0x70;    // Configures oscillator for 8MHz internal clock.
       GPIO  =0;       // Clears all General Purpose Input/Output (GPIO) pins.
       ANSEL =0;       // Sets all pins to digital mode (ANSEL = 0).
       TRISIO=0b101000;// Sets GP3 and GP5 as inputs .
       ADCON0=0;       // Disable A/D converter
       CMCON0=7;       // digital I/O	
       WPU   =0b101000;// Sets GP3 and GP5 to have pull-up resistors enabled (WPU = 0b101000)
       nGPPU  =0;      // overall pull-up enable (nGPPU = 0).
   }
   //----------------------------------------------------
   /*result
    * Data: 5/128bytes 4%
    * Program: 59/2048bytes 2%
    */
   // OSCCON - Oscillator Control Register
   // NC     IRCF2 IRCF1  IRCF0 OSTS(1) HTS  LTS  SCS
   // GPIO - General Purpose Input/Output Register
   // NC     NC      GP5   GP4   GP3    GP2  GP1  GP0
   //ANSEL - Analog Select Register
   //NC      ADCS2   ADCS1 ADCS0 ANS3   ANS2 ANS1 ANS0
   //TRISIO - TRIS Register for I/O Ports
   //NC NC TRISIO5 TRISIO4 TRISIO3 TRISIO2 TRISIO1 TRISIO0
   //ADCON0 - A/D Converter Control Register 0
   //ADFM   VCFG    NC     NC    CHS1 CHS0 GO/DONE ADON
   //CMCON0 - Comparator Module Control Register
   //NC     COUT    NC    CINV  CIS     CM2 CM1   CM0
   // WPU - Weak Pull-Up Register
   // NC    NC      WPU5 WPU4    —    WPU2 WPU1   WPU0
   // nGPPU - Global Pull-Up Enable
   //nGPPU = 1: Disables GPIO pull-ups (individual control using WPU register).
   //nGPPU = 0: Enables overall pull-up enable (individual control 
   //still possible using WPU).
   
   /*  register **********************************
   
   1. OSCCON - Oscillator Control Register
   This register configures the internal oscillator (OSC) of the PIC.
   In your code, OSCCON=0x70 sets the oscillator to 8MHz internal mode (INTOSCIO).
   Refer to the PIC12F683 datasheet for a detailed explanation of 
   all OSCCON bits and configuration options.
   
   2. GPIO - General Purpose Input/Output Register
   This register directly controls the state of each GPIO pin (GP0-GP5).
   Setting a bit in GPIO makes the corresponding pin an output 
   and clears it to make it an input.
   Your code uses GPIO = 0; to clear all GPIO pins, likely 
   as an initialization step.
   
   3. ANSEL - Analog Select Register
   This register controls whether each pin is configured for analog or digital input/output.
   Setting a bit in ANSEL selects the corresponding pin for analog mode.
   The code uses ANSEL = 0; to set all pins to digital mode.
   
   4. TRISIO - TRIS Register for I/O Ports
   This register determines the direction (input or output) of each pin on Port I/O (GP0-GP5).
   Setting a bit in TRISIO makes the corresponding pin an input, while clearing it makes it an output.
   The code uses TRISIO = 0b101000; to configure GP3 and GP5 (button) 
   as inputs and other pins as outputs.
   
   5. ADCON0 - A/D Converter Control Register 0
   This register controls the operation of the Analog-to-Digital Converter (ADC) on the PIC.
   Setting bits in ADCON0 enables the ADC and configures its operation.
   The code uses ADCON0 = 0; to disable the ADC since it's not being used for reading analog signals.
   
   6. CMCON - Comparator Module Control Register
   This register controls the configuration of the on-chip comparators in the PIC.
   The provided code uses CMCON0 = 7; which likely sets the comparators for digital I/O operation 
   (consult the datasheet for specific bit settings).
   
   7. WPU - Weak Pull-Up Register
   This register enables weak pull-up resistors on individual pins.
   Setting a bit in WPU enables the pull-up resistor for the corresponding pin.
   The code uses WPU = 0b101000; to enable pull-up resistors on GP3 and 
   GP5 (button pins) to ensure a defined logic level when the button is not pressed.
   
    8. nGPPU - Global Pull-Up Enable
   This register enables or disables pull-up resistors for all pins simultaneously.
   Setting nGPPU = 0; disables the global pull-up enable, 
   allowing individual pin pull-up configuration using the WPU register.
    */