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…

Advertisements

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: 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: PIC18F14K50 Tablilla de Desarrollo

Para empezar a retomar el tema de los microcontroladores empezare con los PIC18 (Arquitectura 8-bits) de Microchip por la facilidad de uso, cantidad de información y código fuente que hay en internet.

La mayoría de las personas elijen el uC PIC18F4550 para aprender por tener mucha memoria flash, RAM, puertos I/O, módulos y USB 1.1/2.0. A mi punto de vista está muy grande el integrado (40 pines) para la mayoría de los proyectos que realizare. Buscando uno con características similares incluyendo el USB 1.1/2.0 pero de menor pines encontré el PIC18F14K50.

Características importantes del PIC18F14K50:

Continuar leyendo…

Módulo de LEDs 8-bits

Justificación

El día de hoy hice el módulo para LEDs de 8-bits, lo utilizare para algunas prácticas básicas de manejo de microcontroladores. La ventaja es el ahorro de espacio en la tablilla de pruebas.

Materiales

  • 8x LED 1.8v-2.1v
  • 8x Resistencia 270-330 Ohm
  • 1x Tablilla perforada
  • 1x Tira 90* 9-pines
  • Cautín, Soldadura, Flux, etc.

Continuar leyendo…