PIC12F1840 + I2C DS1307 RTC

The DS1307 IC is a serial I2C Real Time Clock (RTC) and calendar plus 56 bytes of SRAM.

Features:

  • I2C (SCL max frequency is 100 KHz).
  • Counts: Seconds, minutes, hours, date of the month, month, day of the week and year (with leap compensation valid up to 2100).
  • Can operate in either the 24-hour or 12-hour with AM/FM format.
  • Date and Time is in BCD format.
  • 56 Bytes general purpose SRAM (Battery backed).
  • Automatic power-fail detect and switch circuitry to a battery source.
  • Programmable square wave out signal.
  • Operating voltage: 5.0V

Pinout:

Typical operating circuit:

Function block diagram:

Continue Reading…

PIC12F1840 + I2C 24FC1025 EEPROM

The 24FC1025 is a serial I2C EEPROM memory fabricated by microchip, it has 1024Kbits (128KB) of memory space and it is divided in two parts each one of 512Kbits (64KB); the first part goes from address 0x0000 to 0xFFFF and the second part goes from 0x10000 to 0x1FFFF.

Features:

  • 128KB of memory space
  • 2-Wire serial interface (I2C)
  • Compatibility with 100 KHz, 400 KHz and 1.0 MHz clocks
  • 2 hardware address bits allowing up to 4 devices on bus
  • Hardware Write-Protect
  • 128-Byte page write buffer (3ms typical)
  • Operating voltage: 1.8V – 5.5V

Pinout PDIP:

Pin function table:

Function block diagram:

The control byte is composed by the static address (1010 = 0xA) plus the Bank select bit (It is 16th most significant address bit) plus the physical address on A1 and A0 pins (A2 pin cannot be used and it MUST be connected to VCC) and in the end the R/W bit as you can see in the image below:

The I2C sequences to read or write a byte or page from this device are:

After sending a write command for a byte or a page the memory will start the writing cycle, while it is still busy we must not send another command. To know if the memory is busy or not you must send the same control command used for write and check the acknowledge bit, if acknowledge is received memory is not busy, otherwise it is still busy writing the data.

The below flow chart shows what was previously described above:

At this point you can see it is easy to read and write data to this memory so we will follow with an example.

The example consist on generating the values to rotate a led from bit 0 to 7 and backwards (0x01,0x02,0x04,0x08,0x08,0x04,0x02,0x01) and store these values into the first 8 Bytes of the memory (0x00000:0x00007).

Then generate values backwards to the first sequence (0x08,0x04,0x02,0x01,0x01,0x02,0x04,0x08) and save them in the first 8 Bytes of the second half of the memory (0x10000:0x10007).

Next we are going to read each byte of the sequence and show it on a two 8b ports respectively, in the end we must see that one port is a mirror of the other. To do this I use the MCP23017 I/O Expander showing the first sequence on IOA and the other one in IOB.

/*
 *	Copyright (c) 2011-2014, http://www.proprojects.wordpress.com
 *	All rights reserved.
 *
 * 	Redistribution and use in source and binary forms, with or without modification,
 *	are permitted provided that the following conditions are met:
 *
 *	1.- Redistributions of source code must retain the above copyright notice,
 *          this list of conditions and the following disclaimer.
 *	2.- Redistributions in binary form must reproduce the above copyright notice,
 *          this list of conditions and the following disclaimer in the documentation
 *          and/or other materials provided with the distribution.
 *
 *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 *	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *	IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *	INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS
 *	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 *	WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*********************************************************************
 * By:              Omar Gurrola
 * Company:         https://proprojects.wordpress.com
 * Processor:       PIC12
 * Compiler:        XC8 v1.32
 * File Name:       main.c
 * Created On:      July 27, 2014, 10:38 AM
 * Description:
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Rev.     Date        Comment
 * 1.0      07/27/14    Initial version
 *********************************************************************/

/** INCLUDES *******************************************************/
#include "pic12f1840_cbits.h"  // Apropiate configuration bits header
#include "main.h"
#include "pic12f1840_i2c.h"
#include "mcp23017.h"
#include "m24fc1025.h"

/** DEFINES ********************************************************/

/** PROTOTYPES *****************************************************/
#define SetClockTo32Mhz()  OSCCONbits.IRCF = 0b1110; OSCCONbits.SPLLEN = 1
void delay_ms(uint16_t);

/** GLOBAL VARIABLES ***********************************************/

/** CODE DECLARATIONS ****************************************/
void main(void) {
    SetClockTo32Mhz();

    delay_ms(1000); // Wait for proteus to load simulation

    i2c_init(I2C_SPEED_STANDARD_100KHZ);
    m24fc1025_init(0b00);
    mcp23017_init(0b001); // Init MCP23017 with the address 001
    mcp23017_write_reg(MCP23017_REG_IODIRA, 0x00); // IOA configure as output
    mcp23017_write_reg(MCP23017_REG_IODIRB, 0x00); // IOB configure as output

    // Write first 16 Bytes of memory
    uint32_t addr = 0;
    for (uint8_t c = 0x01; addr < 8; c = c << 1, addr++) { // Rotate bit
        m24fc1025_write_byte(addr, c);
        while (m24fc1025_is_write_busy()); // Wait for write to finish
    }
    for (uint8_t c = 0x80; addr < 16; c = c >> 1, addr++) { // Rotate bit to the other side
        m24fc1025_write_byte(addr, c);
        while (m24fc1025_is_write_busy()); // Wait for write to finish
    }

    // Write on the address 0x1000+ 16 Bytes of memory
    uint32_t addr = 0;
    for (uint8_t c = 0x80; addr < 8; c = c >> 1, addr++) { // Rotate bit
        m24fc1025_write_byte(addr + 0x1000, c);
        while (m24fc1025_is_write_busy()); // Wait for write to finish
    }
    for (uint8_t c = 0x01; addr < 16; c = c << 1, addr++) { // Rotate bit to the other side
        m24fc1025_write_byte(addr + 0x1000, c);
        while (m24fc1025_is_write_busy()); // Wait for write to finish
    }

    for (;;) {
        // Read data and write it to IOA
        for (uint32_t addr = 0; addr < 16; addr++) {
            mcp23017_write_reg(MCP23017_REG_GPIOA, m24fc1025_read_byte(addr)); // Write to IOA
            mcp23017_write_reg(MCP23017_REG_GPIOB, m24fc1025_read_byte(addr + 0x1000)); // Write to IOB
            delay_ms(250); // Wait some ms between cycles
        }
    }
}

void delay_ms(uint16_t t_ms) {
    for (t_ms; t_ms > 0; t_ms--) {
        for (uint16_t c = 886; c > 0; c--);
    }
}

Below you can see the schematic of the example simulated on proteus.

The circuit was armed on the breadboard and it is working as expected.

Well that’s all for today, happy programming and until next time.

References:

PIC12F1840 + I2C MCP23017 16b I/O Expander

MCP23017 is a 16b I/O expander with I2C interface, it allows us to control 16 I/O pins independently by using only two pins from the uC using the I2C interface.

Features:

  • 16b I/O
  • High Speed I2C (Operating voltage)
    • 100 KHz (1.8V – 5.5V)
    • 400 KHz (2.7V – 5.5V)
    • 1.7 MHz (4.5V – 5.5V)
  • 3 Hardware address bits allowing up to 8 devices on bus (equivalent to 128 I/O)
  • Configurable interrupt output pins (INTA and INTB can be configured to operate independently or together)
  • Polarity inversion register
  • External reset input
  • Output current from one pin: 25mA

Pinout PDIP:

Function block diagram:

The I2C sequences to read or write a register from this device are:

The opcode (OP) is the same it has the device address; 7 bit length composed by the static address plus the physical address of the device (0100 A2 A1 A0) like it is shown below:

The list of registers and addresses are:

For this example we are going to configure both IO Ports to outputs and send some specific data to them so the only registers we need to care about are IODIRx (Direction) and GPIOx (Write and Read) as it is shown below:

The example consist on rotating a led on IOA and IOB will be a mirror of IOA so we must see the some rotation on both ports. There is a delay between rotations of 250mS

/*
*	Copyright (c) 2011-2014, http://www.proprojects.wordpress.com
*	All rights reserved.
*
* 	Redistribution and use in source and binary forms, with or without modification,
*	are permitted provided that the following conditions are met:
*
*	1.- Redistributions of source code must retain the above copyright notice,
*          this list of conditions and the following disclaimer.
*	2.- Redistributions in binary form must reproduce the above copyright notice,
*          this list of conditions and the following disclaimer in the documentation
*          and/or other materials provided with the distribution.
*
*	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
*	EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
*	IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
*	INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS
*	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
*	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
*	WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*********************************************************************
* By:              Omar Gurrola
* Company:         https://proprojects.wordpress.com
* Processor:       PIC12
* Compiler:        XC8 v1.32
* File Name:       main.c
* Created On:      July 27, 2014, 10:38 AM
* Description:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Rev.     Date        Comment
* 1.0      07/27/14    Initial version
*********************************************************************/

/** INCLUDES *******************************************************/
#include "pic12f1840_cbits.h"  // Apropiate configuration bits header
#include "main.h"
#include "pic12f1840_i2c.h"
#include "mcp23017.h"

/** DEFINES ********************************************************/

/** PROTOTYPES *****************************************************/
#define SetClockTo32Mhz()  OSCCONbits.IRCF = 0b1110; OSCCONbits.SPLLEN = 1
void delay_ms(uint16_t);

/** GLOBAL VARIABLES ***********************************************/

/** CODE DECLARATIONS ****************************************/
void main(void) {
	SetClockTo32Mhz();

	delay_ms(1000); // Wait for proteus to load simulation
	i2c_init(I2C_SPEED_STANDARD_100KHZ);
	mcp23017_init(0b001); // Init MCP23017 with the address 001
	mcp23017_write_reg(MCP23017_REG_IODIRA, 0x00); // IOA configure as output
	mcp23017_write_reg(MCP23017_REG_IODIRB, 0x00); // IOB configure as output

	for (;;) {
		uint8_t value;
		for (uint8_t c = 1; c > 0; c = c << 1){ // Rotate bit
			mcp23017_write_reg(MCP23017_REG_GPIOA, c); // Write data to IOA
			value = mcp23017_read_reg(MCP23017_REG_GPIOA); // Read data from IOA
			mcp23017_write_reg(MCP23017_REG_GPIOB, value); // Write read data to IOB
			delay_ms(250); // Wait some ms between cycles
		}
	}
}

void delay_ms(uint16_t t_ms) {
	for (t_ms; t_ms > 0; t_ms--) {
		for (uint16_t c = 886; c > 0; c--);
	}
}

Below you can see the schematic of the example simulated on proteus.

The circuit was armed on the breadboard and it is working as expected.

Well that’s all for today, happy programming and until next time.

References

SE – uC: Puertos de Entrada y Salida (IO Ports)

SE – uC: El Cargador de Arranque (Boot Loader)

El cargador de arranque o en inglés Boot Loader (BL) es un programa (firmware) que va alojado en el uC, permitiendo reprogramar el uC sin necesidad de un costoso programador especial, facilitando las pruebas y actualizaciones del dispositivo. El uC utiliza el BL para comunicarse con un programa especializado que se encuentra en una computadora y recibir el firmware al que se desea actualizar.

La carga del nuevo programa se puede realizar por alguno de los periféricos de comunicación con las que cuentan los uC como: I/O Pin, Serial (RS-232), CAN, Ethernet, USB, etc.

Las ventajas de utilizar un BL son:

  • No se requiere de un programador especial
  • Carga más rápida el firmware nuevo (Si se utiliza un periférico de alta velocidad como USB)
  • No se requiere retirar el uC de la tablilla para ser reprogramado
  • El firmware puede ser actualizado hasta por el cliente final si así se requiere
  • El BL no se borra, escribe o modifica durante la carga del firmware nuevo

Las desventajas son:

  • Solo la primera vez se requiere de un programador especial para cargar el BL
  • El BL ocupa memoria de programa
  • Depende en que región se encuentre alojado puede requiere una reubicación de los vectores de interrupción

Actualmente existen muchos BL para PIC18, la mayoría de uso libre y otros con algún costo. En la siguiente tabla se muestra una comparativa entre algunos BL libres.

Continuar leyendo…

SE – uC: Los Osciladores Del PIC18F14K50

El modulo del oscilador tiene una variedad de fuentes de reloj y funciones para adaptarlo a diferentes aplicaciones, mejorando el rendimiento y disminuyendo la potencia consumida.

Las fuentes de reloj que permite este uC son las siguientes, las cuales se explicaran con más detalle en los siguientes subcapítulos:

  • Oscilador externo primario (Primary external oscillator)
  • Oscilador externo secundario (Secondary external oscillator)
  • Oscilador interno (Internal oscillator)

Los módulos que utilizaran el oscilador seleccionado son los siguientes:

Continuar leyendo…

SE – uC: Osciladores Por Hardware

Tipos De Osciladores:

Los osciladores por hardware son circuitos indispensables para los microcontroladores y otros dispositivos electrónicos, ya que generan los pulsos de reloj necesarios para su correcto funcionamiento.

Los diferentes tipos de osciladores por hardware (Principalmente en los PICs) son:

La razón por la que se utilizan más los osciladores tipo XT y HS es por la estabilidad y la velocidad que ofrecen al generar los pulsos, haciendo que el rendimiento del microcontrolador sea mucho mejor.

RC (Resistor-Capacitor):

Este tipo de oscilador se utiliza en aplicaciones donde la precisión de tiempo no sea un requisito, es uno de los osciladores más baratos y fáciles de fabricar. Esta formado por una resistencia y un capacitor en serie, donde la frecuencia esta dada por el tiempo de carga y descarga del capacitor.

La frecuencia de este tipo de oscilador esta influida por la estabilidad de la fuente de alimentación, la resistencia, la capacitancia, temperatura y las tolerancias en la resistencia y el capacitor.

Para calcular la frecuencia del circuito es la siguiente:

Continuar leyendo…

SE – uC: Organización De Memoria Del PIC18F14K50

Este uC tiene los siguientes tipos de memoria:

  • Memoria de programa (Program Memory):
    En esta memoria flash se almacena el código del programa que se ejecutara (Software o Firmware), también se pueden almacenar datos o tablas. Esta memoria se puede escribir durante la ejecución del programa.
  • Pila LIFO (Stack):
    Esta pila LIFO sirve para almacenar y recuperar las direcciones de memoria de programa en cada salto que se realice durante la ejecución del programa (CALL, RCALL, RETURN, RETFIE, RETLW).
  • Memoria de datos (Data Memory):
    Esta memoria se utiliza para almacenar las variables y datos temporales necesarios por el programa y el usuario. Esta memoria es volátil, lo que significa que se pierden los datos una vez apagado el uC.
  • Memoria de configuración (Configuration Memory):
    Esta memoria se utiliza para configurar algunos aspectos iniciales del uC como el oscilador, la velocidad, activación y desactivación de algunos periféricos, etc.

Memoria De Programa (Program Memory) y Pila LIFO (Stack):

Todos los uC PIC18 tienen un PC (Program Counter) de 21 bits, el cual puede acceder a 2 MB de memoria de programa, pero si se lee arriba de la cantidad de memoria implementada regresara cero (NOP). Este uC tiene implementada 16 KB o 8K Words (0000h-3FFFh) de memoria de programa. La función del PC consiste en apuntar a la dirección de memoria de programa de la siguiente instrucción que debe buscar para ejecutar.
Continuar leyendo…

SE – uC: Descripción General Del PIC18F14K50

Antes de iniciar a programar este microcontrolador vamos a revisar las características básicas que tiene.

En la siguiente tabla se muestran las características generales del PIC18F14K50:

Continuar leyendo…

SE – uC: MPLAB C18: Librerías ANSI C

En esta ocasión realice una guía con ejemplos básicos sobre las librerías ANSI C y algunas otras adicionales que trae C18, todos los ejemplos desarrollados en MPLAB C18. Con la intención de facilitar el desarrollo de programas utilizando funciones ya existente. 😀

Bajar todas las prácticas del documento

MPLAB C18: Librerías ANSI C por Omar Gurrola – ProProjects.wordpress.com se encuentra bajo una Licencia Creative Commons Atribución-NoComercial-CompartirIgual 3.0 Unported.