Electronic – PIC12F683 or PIC16F688 assembly or C blinking LED example with XC8 compiler, MPASM or gputils

assemblycmicrochippicxc8

Hello I am new to PIC programming and I finally have got my dev environment working, I downloaded the XC8 compiler from microchip and also as backup I downloaded the gputils assembler. I have not been able to get anything compiled/running and I would appreciate an example. If someone could write a small program demonstrating how to set the config bits and the crystal frequency in either assembly to assemcle it with MPASM or gputils or C to compile with XC8 I would appreciate it. I have a PIC12F683 and a PIC16F688 although for this example I dont think much platform specific stuff maters. Anyways I gave it a shot and here is what I got, but it is not working. Thank you

#pragma config FOSC = INTOSCCLK // Oscillator Selection bits (INTOSC oscillator: 

CLKOUT function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)


#define _XTAL_FREQ 8000000
//INCLUDES
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>


void main(void) {
 GPIO = 0x00;
 ANSEL = 0x00;
 TRISIO = 0x00;
 ADCON0 = 0x00;
 CMCON0 = 7;
    while(1)
    {
        GP0 = 1;
//dont know which delay function to use
        GP0 = 0;

    }

Best Answer

Apart from missing the end bracket (copy/paste missed the last character?) your code works for me. However there are a couple of omissions that might make you think it doesn't:-

  1. #define _XTAL_FREQ 8000000 tells the compiler what frequency the PIC should be running at, but does not actually set it. On start up the 12F683 runs at 4MHz. To change it to 8MHz you must set OSCCONto 0x70 (bits 6-4 all '1').

  2. Without a delay after changing the I/O pin's state it will toggle very fast (with an unequal up/down ratio). XC8 has built-in functions __delay_us() and __delay_ms() for creating long delays.

Here is your code modified to run the 12F683 at 8MHz with GP0 toggling at 1Hz.

void main(void) {
  OSCCON = 0x70; // switch to 8MHz system clock
  GPIO = 0x00;
  ANSEL = 0x00;
  TRISIO = 0x00;
  ADCON0 = 0x00;
  CMCON0 = 7;
    while(1)
    {
        GP0 = 1;
        __delay_ms(500); // wait 500 milliseconds
        GP0 = 0;
        __delay_ms(500); // wait 500 milliseconds
    }
}