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

PIC18F14K50: Basic Alarm System with sEOS

After reading the “Embedded C” book from Michael J. Pont I built the alarm system where I must integrate all the techniques that the book has. I built the alarm system using the PIC18F14K50 because it has all the peripherals. At first I thought it was going to be easy because most of the libraries I needed I already had them but after moving forward in the project I saw that my libraries were designed for blocking so I could not use them for the sEOS and I had to update them, even added buffer functions to give them optimal functionality.

The software architectures used for this system were sEOS (Simple Embedded Operating System – Time based) with MSS (Multi-State System).

The modules that this system uses are:

  • FIFO buffer.
  • 4×4 Keypad with FIFO buffer.
  • UART with FIFO buffer.
  • sEOS (Time-based).
  • Alarm System.

The alarm system is designed to monitor one main door and five windows and as outputs it has one led and a buzzer speaker. The sensors are simulated with switch buttons.

Alarm functionality:

  • When you turn on the system, the alarm will enter into a DISARM state waiting for the correct password.
  • After entering the correct password the alarm will change to ARMING state where it will wait for 60s before checking the sensors so you can go out the place or if you need it to turn it off.
  • After that the alarm goes into ARMED state where it will start monitoring all the sensors.
  • If the main door is open it is going to change into DISARMING state where you have 60s to insert the correct password and disarm the alarm.
  • In case that the 60s has passed or one of the window sensors are triggered the system will change to INTRUDER state where the led and the buzzer are going to alert of intruder.

After the alarm is in INSTRUDER state it will only be disarmed with the correct password and go to DISARMED state.

Thanks for reading and happy programming.

References

Michael J. Pont, “Embedded C”, 2002

 

PICKit 3 Debug Express

It’s been more than 7 years since I built my serial PIC programmer for PIC16/18 uC based on the JMD Programmer, but now I need to work with new microcontroller families like PIC10/12/24/32 so I decided to buy the PICKit 3 for programming and debugging those uC.

It’s been almost three weeks since I received my PICKit3 Debug Express by FedEx, it arrived very fast (2 days) and it came with:

  • PICKit 3 Programmer/Debugger (Obviously :P)
  • USB Cable (Very long)
  • A Poster with pin-out information, etc.
  • 44-Pin PIC18F45K20 Demo Board
    • ICSP pins
    • 1x Switch (RB0/INT0)
    • 8x Red leds (PORTD)
    • 1x Potentiometer (RA0/AN0)
    • Small area for prototyping

The PIC18F45K20 Demo Board came free because in the month of June if you bought the debug express version microchip gave you 60% off, so from 69.99 USD in the end I only paid 42.00 USD + shipping, it’s almost the same price than buying the PK3 alone.

Even though the Demo Board is very simple it helped me a lot to learn how to program and debug my code using the PK3. Also I did all the lessons but they are for C18 compiler only so I had to migrate them to XC8 (If someone wants them ask me in the comment section or by email).

The lessons were:

  1. Hello LED
  2. Blink LED
  3. Rotate LED
  4. Switch Input
  5. Timer0
  6. Using PK3
  7. ADC
  8. Interrupts
  9. Internal Oscillator
  10. EEPROM
  11. Program Memory
  12. PWM

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

C18 to XC8 Migration Reference

This document aims to provide a quick reference on the changes that must be considered or performed in the source code when migrating from C18 to XC8. This information is based on the Microchip C18 to XC8 Migration Guide.

To continue reading click on the picture below or the link to open the PDF.

Click this link to open the PDF.

Migración de C18 a XC8

Referencia rápida sobre los cambios que se deben realizar o considerar en el código fuente al migrar un proyecto realizado en C18 a XC8. Toda esta información está basada en la guía de migración de microchip.

Para continuar leyendo da click en el siguiente dibujo o en el link inferior para abrir el PDF.

Da click en la imagen o da click en este link para ver el PDF.

PIC18F14K50: MSSP-SPI

Explicación de cómo utilizar el MSSP del PIC18F14K50 con el protocolo SPI y algunos ejemplos como: Memorias Winbond W25QXX, nRF24L01+ 2.4GHz, DAC TLC56XX y SD en modo RAW.

Para continuar leyendo da click en el siguiente dibujo o en el link inferior para abrir el PDF.

Da click en la imagen de ISSUU o da click en este link para ver el PDF.

PIC18F14K50: EUSART (SERIAL)

El módulo EUSART (Enhanced Universal Synchronous Asynchronous Receiver Transmitter) se utiliza para protocolos de comunicación en serie como: RS-232, RS-485 y LIN (Local Interconnect Network) 2.0.

Para continuar leyendo da click en el siguiente dibujo o en el link inferior para abrir el PDF.

Da click en la imagen de ISSUU o da click en este link para ver el PDF.

Bus SPI (Serial Peripheral Interface)

Hola a todos, este documento trata sobre el bus SPI (Serial Peripheral Interface), el bus spi es un bus de comunicación serial, es muy utilizado en microcontroladores para comunicar diferentes dispositivos a corta distancia, incluso se puede decir que es un protocolo muy sencillo y basico dentro de la rama de embebidos.

Debido a que ya no puedo publicar los pdfs directamente aqui, por cuestiones de actualizacion de ISSUU, pondre nadamas el link al documento para que lo puedan ver.

Da click en la imagen de ISSUU o da click en este link para ver el PDF.