Showing posts with label Electronics. Show all posts
Showing posts with label Electronics. Show all posts

Saturday, September 7, 2013

I2C/TWI with AVR Data Transfer


We now take a look at the TWI/I2C API responsible for data transfer. The header file twi.h has the prototype. We'll take a look at the arguments and their description now:
TWI_RET_E twi_send_recv_buffer(TWI_SEND_RECV_BUFFER_PARAMS_X *px_send_buffer_params,
                               TWI_MODE_E e_mode);
The first argument is a pointer to TWI_SEND_RECV_BUFFER_PARAMS_X defined as follows:


typedef struct _TWI_SEND_RECV_BUFFER_PARAMS_X
{
   uint8_t uc_dev_addr;
   uint8_t uc_dev_id;
   uint8_t *puc_reg_addr;
   uint32_t ui_reg_addr_sz;
   uint8_t *puc_data;
   uint32_t ui_data_sz;
    
}TWI_SEND_RECV_BUFFER_PARAMS_X;

Where
  1. uc_dev_addr is the higher nibble of the I2C slave device address
  2. uc_dev_id is the lower nibble of the I2C slave device address. It is used where the slave address can be configured
  3. puc_reg_addr is the pointer to the buffer holding the register address of the slave device.
  4. ui_reg_addr_sz is the size of the buffer with the register address.
  5. puc_data is the pointer to the data buffer
  6. ui_data_sz is the size of the data buffer.
The second argument is an enumeration which indicates if a send or receive operation is to be done
typedef enum _TWI_STATE_MODE_E
{
   eTWI_MODE_READ = 0,
   eTWI_MODE_WRITE,
   eTWI_MODE_INVALID

}TWI_MODE_E;

Sunday, August 25, 2013

I2C/TWI with AVR Introduction

In this post we'll take a look at implementing an I2C/TWI library which can be used with Atmel's AVR micro-controller. The code has been uploaded to GitHub at https://github.com/zeuzoix/sardine and will be referenced here. The TWI library consists of the following files:
  1. twi.h
  2. twi.c
  3. twi_states.h
  4. twi_states.c
  5. twi_debug.h

There are two API which the application can use to interface with the I2C/TWI interface. The prototypes are in twi.h
   uint32_t ui_data_sz;
    
}TWI_SEND_RECV_BUFFER_PARAMS_X;
 
 
TWI_RET_E twi_init(TWI_INIT_PARAMS_X *px_init_params);
TWI_RET_E twi_send_recv_buffer(TWI_SEND_RECV_BUFFER_PARAMS_X *px_send_buffer_params,
                               TWI_MODE_E e_mode);
 
#endif

The first API twi_init is used to initialize the TWI interface in master mode by assigning a set of register values to the TWI registers in the AVR Atmel micro-controller.

The second API  twi_send_recv_buffer is used in both transmit and receive mode to send and receive data over the TWI SDA data line.


We'll take a look at the registers used in the programming of the TWI interface in the next section i.e.
I2C/TWI with AVR Register Descriptions.

Saturday, May 18, 2013

Real Time Clock with DS1307 Register Map Description

In the first part we went through a brief introduction of the DS1307 chip. 
We know the DS1307 can be programmed via the I2C bus i.e SDA and SCL. Through I2C we should be able to address the registers of this chip and write and read data based on our application description.

The internal registers which affect the behaviour of the RTC chip are as follows:















The first 7 registers are used to read and write the time and date.
Registers 00h and 01h have more or less the same byte definition and this is mainly because there are 60 seconds and 60 minutes.

An important difference is bit 7 of register 00h which is the clock halt bit. By default this bit is set to 1 on power up of the device which disables the oscillator and RTC functionality of the DS1307.

All values are in binary coded decimal. The range of seconds and minutes goes from 00 to 59. Therefore for the minutes and seconds we have bits 0 to 3 representing the lower digit i.e. 0 to 9 and bits 4 to 6 representing the higher digit.

The representation of hours can be in the form of either 12 or 24 hour notation. Bit 6 indicates the notation used. When high the 12 hour notation is used and bit 5 indicates AM/PM with high being PM. In this case the range of values goes from 01 to 12. Therefore we have bits 0 to 3 representing the lower digits i.e. 0 to 9 and bit 4 representing the higher digit i.e. 0 or 1.

In the 24 hour notation the range of values goes from 00 to 23. Therefore the lower digit representation remains the same i.e. 0 to 9 represented by bits 0 to 3 but bits 4 and 5 are used to represent the higher digit i.e. 0 to 2.

Essentially the number of bits reserved for each digit is defined by the range of the value. For instance there are only 7 possible days and so we have 3 bits representing the day of the week in register 03h. And if we look at the date register 04h we see the range of values goes from 01 to 31 therefore the lower digit of the date can have a maximum value of 9 so four bits are reserved whereas the upper digit can have a maximum value of 3 therefore 2 bits are reserved.

Registers 05h and 06h follow the same pattern. Register 07h controls the behaviour of the square wave out signal. Bit 4 controls the functionality i.e. whether the pin produces a square wave or a fixed ouput. By default the value is 0 causing the pin to output a fixed value governed by the bit 7. If bit 4 is set to 1 the pin produces a square wave whose frequency is controlled by bits 0 and 1. The number of possible combinations is 4 with 2 bits and therefore the possibilities are 1Hz, 4.096kHz, 8.192kHz and 32.768kHz.

Registers 08h to 3Fh provide a 56x8 RAM area for use by the application.
For further information please go through the data sheet at:
http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

Real Time Clock with DS 1307 Introduction

You're developing a product and you need it to track time even when the power is switched off, what do you use? A real-time clock.

A real-time clock(RTC) for your system can be made available using chips like the DS1307. This has the ability to be programmed and keep track of the time in seconds, minutes, hours and date in binary coded decimal(BCD) format with compensation for leap years till 2100.

The DS1307 on inspection has 8 pins. The following describes the kind of circuit connections required for this chip.



Starting at the top left we see pins X1 and X2 connected to a crystal. The crystal provides the clock to the chip and is critical in accurately maintaining the time once programmed. Connections for X1 and X2 is a standard 32.768kHz quartz crystal.

The VCC and GND provide the power supply to the chip which is a 5V power supply.

SQW/OUT has dual functionality. It can be used to generate a square wave of 1Hz, 4kHz, 8kHz and 32kHz or it can simply output either a high or low signal.

VBAT is the connection for an external battery. The chip has an automatic power fail detect and switch circuitry which will switch to the battery in the event of the main power supply failing.

SDA and SCL are the I2C lines which is used by the CPU/Micro-controller to read and write the time and date registers.

In the next part we look at the register map of the DS1307.

Next: Real Time Clock with DS1307 Register Map Description


Tuesday, May 14, 2013

Atmega Development Boards and Arduino

I've been going through various sources trying to figure out what the Electronic DIY community uses to develop LED cubes, RC controlled cars, etc.

One of the popular micro-controllers in use today is the ATmega AVR from Atmel. This micro-controller is also used in the popular Arduino platfrom.

The popularity of the ATmega AVR is due to the availability of free and inexpensive development tools accelerated by the high number of development boards available.

The  ATmega AVR was the first micro-controller to have an on board flash for program storage. Compared to the other micro-controllers at the time which offered one time programmable ROM, EPROM and EEPROM options. The flash offers reduced cost of development as the frequency with which applications can be deployed and tested easily increases.

The Arduino platform is an open source hardware and software project meant for hobbyists, designers and artists. The hardware uses a ATmega AVR and the software consists of an easy to use JAVA like programming IDE based on Wiring.

I've got my hands on an AVR development board called the Device Driver Kit. The kit can be used to learn about Linux device drivers or can be used as a ATmega AVR development board for your electronics project.



The board has a USB interface through which you can program the ATmega and develop your own applications. This makes it easy to do rapid prototyping with just a PC and the board. I'll be posting some of my side projects which will use the board.

It shouldn't be too difficult to whip up a ATmega AVR development board or a Arduino board with the resources available online. However as a starting point it would be good to familiarize yourself with the AVR before you jump into building your own board.

Details of the DDK board are available at:
http://esrijan.com/index.php?pagefile=lddk

Sunday, May 12, 2013

An Introduction to I2C

I2C is a serial data bus used to interface low speed peripherals developed by Phillips. Data sheets of other semi-conductor companies refer to their I2C like interface as the Two Wire Interface.

I2C has two lines SDA and SCL. SDA is used for data input or output on the I2C interface. SCL is the clock interface and is used to synchronize the data communication on the SDA line.The device initiating and terminating the transmission is called the master. The master controls the SCL clock signal.

There is only one master active during data communication on the bus however it does not mean the bus has to have only one master. The I2C protocol can support multiple masters and multiple slaves. In fact multiple masters can co-exist if they support an arbitration mechanism.

Typically when a master initiates a transfer it will also sense the line in order to detect if another master is transmitting at the same time. The arbitration mechanism will force one master to hold back on the transmission. The master can wait and try again once the line is free or immediately switch to slave mode if the application requires the device to act in both master and slave modes.

More information at :
  1. http://en.wikipedia.org/wiki/I2C
  2. http://www.esacademy.com/en/library/technical-articles-and-documents/miscellaneous/i2c-bus.html