Amazon

Thursday 30 April 2015

Interfacing with the BQ24725 battery charger

The BQ24725 is a 1-4 cell battery charge manager with SMBus protocol from Texas Instruments. Here's an example C firmware to interface with the charger using a PIC16 from Microchip. This example is made to charge a 2-cell Lithium-Ion battery 8.4V, 10000 mAh. Note that it is using I2C protocol which is equivalent to SMBUS.


#define CHARGER_OPTIONS_INIT_H 0x09
#define CHARGER_OPTIONS_INIT_L 0x24 //Charger init options
                                                                           //BAT Depletion 62.65%                                                                                                                                                         //(~2.631V/cell),charge current
#define CHARGER_CURRENT_L 0x00
#define CHARGER_CURRENT_H 0x08 //Set Charger current to 2048mA
#define CHARGER_VOLTAGE_L 0xD0
#define CHARGER_VOLTAGE_H 0x20 //Set Charger voltage to 8400mV
#define INPUT_CURRENT_L 0x00 
#define INPUT_CURRENT_H 0x0D //Set Input current to 3328mA
#define Charger_addr 0x12                                 //I2C address of BQ24725

void CHARGER_init()                          //BQ24725 initialization
{
SSP2CON1bits.SSPEN = 1;

    //Set Option to Iout = charge current
I2C_SendCommand_2Bytes(Charger_addr,0x12,CHARGER_OPTIONS_INIT_L,CHARGER_OPTIONS_INIT_H);  

    //Set Charger current                                                                                              
I2C_SendCommand_2Bytes(Charger_addr,0x14,CHARGER_CURRENT_L,CHARGER_CURRENT_H);

   //Set Charger voltage 
I2C_SendCommand_2Bytes(Charger_addr,0x15,CHARGER_VOLTAGE_L,CHARGER_VOLTAGE_H);

    //Set input current
I2C_SendCommand_2Bytes(Charger_addr,0x3F,INPUT_CURRENT_L,INPUT_CURRENT_H);

SSP2CON1bits.SSPEN = 0;
}


void CHARGER_ChargingEnable(char enabled)         //Enable / Disable the charging
{
enabled = 1 - enabled;

SSP2CON1bits.SSPEN = 1;

    //Send the Instruction to the charger
I2C_SendCommand_2Bytes(Charger_addr,0x12,CHARGER_OPTIONS_INIT_L+enabled, CHARGER_OPTIONS_INIT_H);

SSP2CON1bits.SSPEN = 0;
}


If you need a routine for the I2C communication:


void I2C_SendCommand_2Bytes(unsigned char Address, unsigned char Command, unsigned char LowByte, unsigned char HighByte)
{
SSP2CON1bits.SSPEN = 1;
SSP2CON2bits.SEN = 1; //Send start
WaitBufferEmpty();

SSP2BUF = Address;
WaitBufferEmpty();

SSP2BUF = Command; //Send command
WaitBufferEmpty();

SSP2BUF = LowByte; //Send low byte
WaitBufferEmpty();

SSP2BUF = HighByte; //Send high byte
WaitBufferEmpty();

SSP2CON2bits.PEN = 1; //Send stop
WaitBufferEmpty();
return;
}

unsigned char I2C_ReadCommand(unsigned char Address, unsigned char Command)
{
unsigned char Byte_read;

SSP2CON2bits.SEN = 1; //Send start
WaitBufferEmpty();

SSP2BUF = Address;
while (SSP2CON2bits.ACKSTAT); //wait for ACK received
WaitBufferEmpty();

SSP2BUF = Command; //Send command
WaitBufferEmpty();

SSP2CON2bits.RSEN = 1; //Send repeat start
WaitBufferEmpty();

SSP2BUF = Address+1;
while (SSP2CON2bits.ACKSTAT); //wait for ACK received
WaitBufferEmpty();

SSP2CON2bits.RCEN = 1; //Set in receive
WaitBufferEmpty();

Byte_read = SSP2BUF; //Read buffer

SSP2CON2bits.ACKDT = 0;
SSP2CON2bits.ACKEN = 1; //Start Acknowledge sequence
WaitBufferEmpty();

return Byte_read;;
}

unsigned char I2C_ReadByte()
{
char Byte_read;

SSP2CON2bits.RCEN = 1; //Set in receive
WaitBufferEmpty();

Byte_read = SSP2BUF; //Read buffer

SSP2CON2bits.ACKDT = 1;
SSP2CON2bits.ACKEN = 1; //Start Not Acknowledge sequence
WaitBufferEmpty();
SSP2CON2bits.PEN = 1; //Send stop
WaitBufferEmpty();

  return Byte_read;;
}

void WaitBufferEmpty()
{
if (SSP2CON1bits.WCOL)
SSP2CON1bits.WCOL = 0;

SSP2IF = 0;
}





Friday 10 April 2015

Interfacing with TPL9201 relay driver using a PIC

The TPL9201 is a 8-channel relay driver that uses serial communication. For the purpose of this article, I will be using a PIC16F1782 from Microchip to interface but the code can be used with any microcontroller from the PIC16 family. The C compiler used is from Hi-Tech

To interface with the TPL9201, you need 3 I/O pins from the microcontroller; Chip Select (CS), Data (SDO), and Clock (SCK).

In this example, relays that are used are EC2-5NU from NEC. To activate a relay, you need to send a "1" to the corresponding control register bit of the TPL9201. On the other hand, if you want to release the relay, you need to send a "0".
The control register is illustrated below:

MSB                                           LSB 
IN8 IN7 IN6 IN5 IN4 IN3 IN2 IN1 
 0      0      0     0      0     0      0     0 

INn = 0: Output OFF 
INn = 1: Output ON

Following is the code to activate all 8 relays and then release them.

#include "htc.h"
#include "pic16f1782.h"

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 16000000
#endif

#define CS RA5
#define SCK RC3
#define SDO RC5

void spi_writebit(unsigned char SPI_DATAbit)
{
SDO = SPI_DATAbit;
__delay_ms(1);
SCK = 1;
__delay_ms(1);
SCK = 0;
}

void spi_write(unsigned char SPI_DATA)
{
char Bit_counter;
char Bit_send;

CS = 0;
SDO = 0;
SCK = 0;
__delay_ms(1);

for (Bit_counter = 0;Bit_counter<=7;Bit_counter++)
{
Bit_send = (SPI_DATA>>(7-Bit_counter))&0x01;
spi_writebit(Bit_send);
}
SCK = 0;
SDO = 0;
__delay_ms(1);
CS = 1;
}

void Relay_Control(unsigned char Relay_no, unsigned char Relay_State)
{
if (Relay_State)
Buffer_Value = Buffer_Value | (1<<(Relay_no-1));
else
Buffer_Value = Buffer_Value & (0xFF - (1<<(Relay_no-1)));

spi_write(Buffer_Value);
}

void main()
{
  int Relay_number;

  for (Relay_number = 0;Relay_number<=7;Relay_number++)
Relay_Control(Relay_number,1); //Activate all 8 relays

for (Relay_number = 0;Relay_number<=7;Relay_number++)
Relay_Control(Relay_number,0); //Release all 8 relays
}

Monday 29 December 2008

HID Mouse in PSoC

Recently, I had to simulate a 3-button mouse with a PSoC connected to a CPU via the USB link.  The PSoC is a CY8C24894 and the CPU is a ICH-7 from Intel.

Here's the code I wrote to simulate the left, right, down, and up movements as well as the right and left click buttons. You need to had your own routine on how to determine the mouse actions.

First, you need to configure the USB module using the USB Setup Wizard in PSoC designer. You need to get the Vendor ID and Product ID of the ICH-7 CPU. In this case, Vendor ID is 8086h and the Product ID is 27CAh (this is in relation with which UHCI controller you are using). For the rest of the attributes, refer to the USBFS datasheet provided in PSoC designer.

//----------------------------------------------------------------------------
// HID Mouse
// with USBFS module
//----------------------------------------------------------------------------

#include        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "psocgpioint.h"
#include "globalparams.h"
#include

int move_left;
int move_right;
int move_up;
int move_down;
int right_click;
int left_click;

BYTE Mouse_data[3] = {0,0,0}; 

void main()   
{   
M8C_EnableGInt;                       // Enable Global Interrupts

USBFS_1_Start(0, USB_3V_OPERATION); //Start USBFS Operation using device 0  
                                 //and with 3.3V operation  
 while (!USBFS_1_bGetConfiguration()); //Wait for Device to enumerate
//Enumeration is completed load endpoint 1. Do not toggle the first time
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_NO_TOGGLE);

while(1) 
{

if (move_left) 
Mouse_data[1] = 0xFB; //Move cursor left 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[1] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE);

if (move_right) 
Mouse_data[1] = 0x05; //Move cursor right 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[1] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 

if (move_down) 
Mouse_data[2] = 0x05; //Move cursor down 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[2] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
}

if (move_up) 
Mouse_data[2] = 0xFB; //Move cursor up 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[2] = 0x00; //Stop cursor 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 

if (left_click) 
Mouse_data[0] |= 0x01; //Left click 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[0] = 0x00;
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data Mouse_data, 3, USB_TOGGLE); 

if (right_click) 
Mouse_data[0] |= 0x02; //Right click 
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
Mouse_data[0] = 0x00;
while (!USBFS_1_bGetEPAckState(1)); //Wait for ACK before loading data
//ACK has occurred, load the endpoint and toggle the data bit
USBFS_1_LoadInEP(1, Mouse_data, 3, USB_TOGGLE); 
}
}
}

Wednesday 2 July 2008

Microchip MPLAB releases

Microchip has recently released new versions of MPLAB C compilers.

You have version v3.21 of MPLAB C compiler MPLAB C Compiler for PIC18 MCUs

and version v3.10c of MPLAB C Compiler for PIC24 and dsPIC DSCs device (you'll need to go to any of the three16-bit compiler pages linked here for the update).

See the Release Notes for full details on new device support and changes made to these interim releases.

Also there is the MPLAB IDE v8.14 interim release .

This release has the following new features:
- 64-bit USB Drivers. USB drivers for Windows Vista 64-bit operating system allows MPLAB ICD 2, MPLAB REAL ICE and other tools using USB interfaces to operate in both Windows 32-bit and 64-bit platforms (XP-64 and Vista-64).
- Shadow SFRs. Some parts contain banked SFR sets, such as devices with CAN peripherals. MPLAB IDE now allows full monitoring and write access to those registers for debugging.
- Fast Stepping with 32 kHz Oscillators. Debugging targets at 32 kHz can be slow, since data speeds between MPLAB IDE and the device are rates limited by the clock speed. MPLAB IDE will switch to a faster internal oscillator when communicating to the device to speed up debugging.
- Peripheral Pin Select support in MPLAB SIM.
- Interface for Difference Tool. A generic facility is provided to launch a file difference checker.
- Persistent Bookmarks. Bookmarks will now be stored with the workspace, so that a re-launch of the workspace will also restore all bookmarks.

See the MPLAB v8.14 Release Notes for more information and a list of devices supported.