Skip to content
 

PIC12F683 using Digital I/O

One of the simplest usages for any MCU is controlling its digital I/O (Input/Output) ports.

In one of my recent projects, I was required to use the PIC12F683 from Microchip. an 8 Pin MCU with many peripheral features like Analog Comparator, A/D Converter, Timers and PWM.

The development tools were MPLAB 8.40 from Microchip and HI-TECH C Complier for PIC10/12/16. Creating the project and adding the main c file was pretty straight forward. The tricky part was initializing the MCU ports for digital I/O. I used the GP0, GP1, GP2, GP4 and GP5, as you can see in the datasheet, every port has at least 4 different configurations. After briefly reading the datasheet and googled, I finally found the “magic” code.

 

Microchip PIC12F683 datasheet:

http://ww1.microchip.com/downloads/en/DeviceDoc/41211D_.pdf

Download the latest MPLAB:

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en019469&part=SW007002

Download HI-TECH C Compiler:

http://www.htsoft.com/products/compilers/PICClite.php

 

Here’s an example of simple square wave generator:

 

#include <htc.h>

#include <stdlib.h>

 

__CONFIG(UNPROTECT & BOREN & MCLRDIS & PWRTEN & WDTDIS & INTIO);

 

#define _XTAL_FREQ 4000000

 

void main(void)

{

unsigned char delayCount;

 

GPIO = 0; clear all GPIO pins

ANSEL = 0; // disable analog inputs

TRISIO = 0; // set all GPIO pins to output

OPTION = 0b11010000;

CMCON0 = 0; // disable comparator 0

CMCON1 = 0; // disable comparator 1

 

While(1)

{

GPIO = 0×7;

for (delayCount = 0 ; delayCount < 10 ; delayCount++)

__delay_ms(100);

 

GPIO = 0;

for (delayCount = 0 ; delayCount < 10 ; delayCount++)

__delay_ms(100);

}

}

 

GPIO – General Purpose I/O

ANSEL – Analog Select Register

TRISIO – Input or Output GPIO selection

OPTION – Option Register

CMCON0 – Comparator 0 Configuration Register

CMCON1 – Comparator 1 Configuration Register

One Comment

  1. Henix says:

    Upload some pictures of the project !!

Leave a Reply