Wednesday 6 February 2013

I2C FUNCTIONS FOR MIDRANGE PICs USING SPI

I2CInit(void)
{
        TRISC3 = 1;      /* SDA and SCL as input pin */
        TRISC4 = 1;      /* these pins can be configured either i/p or o/p */
        SSPSTAT |= 0x80; /* Slew rate disabled */
        SSPCON = 0x28;   /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
        SSPADD = 0x28;    /* 100Khz @ 4Mhz Fosc */
}
 I2CStart()
{
        SEN = 1;         /* Start condition enabled */
        while(SEN);      /* automatically cleared by hardware */
                     /* wait for start condition to finish */
}
I2CStop()
{
        PEN = 1;         /* Stop condition enabled */
        while(PEN);      /* Wait for stop condition to finish */
                     /* PEN automatically cleared by hardware */
}
I2CRestart()
{
        RSEN = 1;        /* Repeated start enabled */
        while(RSEN);     /* wait for condition to finish */
}
I2CAck()
{
        ACKDT = 0;       /* Acknowledge data bit, 0 = ACK */
        ACKEN = 1;       /* Ack data enabled */
        while(ACKEN);    /* wait for ack data to send on bus */
}
I2CNak()
{
        ACKDT = 1;       /* Acknowledge data bit, 1 = NAK */
        ACKEN = 1;       /* Ack data enabled */
        while(ACKEN);    /* wait for ack data to send on bus */
}
I2CWait()
{
        while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
    /* wait for any pending transfer */
}
I2CSend(unsigned char dat)
{
        SSPBUF = dat;    /* Move data to SSPBUF */
        while(BF);       /* wait till complete data is sent from buffer */
        while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );       /* wait for any pending transfer */
}
unsigned char I2CRead(void)
{
        unsigned char temp;
        RCEN = 1;        /* Enable data reception */
        while(!BF);      /* wait for buffer full */
        temp = SSPBUF;   /* Read serial buffer and store in temp register */
        while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );       /* wait to check any pending transfer */
        return temp;     /* Return the read data from bus */
}

READING PROCEDURE
                    // i2c reading starting here
                    I2CStart();
                    I2CSend(0xD0);
                    I2CSend(0x00);
                    I2CRestart();
                    I2CSend(0xD1);
                    for(a=0;a<8;a++)
                        {    tdata[a]=I2CRead();
                            I2CAck();       
                        }//or X=I2CRead();
                    I2CStop();




Thursday 1 March 2012

SIMPLE AND SAMPLE PROGRAMS

To blink an LED connected at the PORTD

#include<htc.h>
#define _XTAL_FREQ 4000000
void main()
{
int i;
__CONFIG (0x3F7A);
TRISD=0X00;

while(1)
{
PORTD=0XFF;
__delay_ms(1000);
PORTD=0X00;
__delay_ms(1000);
}

}

Tuesday 28 February 2012

LET US START



               To program in hitech c we must have some basic knowledge in the architecture and configurations of pic microcontroller. means how to use timers, counters,serial port etc. also understanding some basic assembly programs is also useful but it is not essential.
               MPLAB is a free compiler cum simulator debugger and programmer from Microchip. to include hitech c compiler in MPLAB we have to choose the option while installing. usage of MPLAB is very easy.
firstly select the compiler suite to hitech c in mplab. then take new project and configure.

Then to the syntax of Hitech-C
firstly i recommend some links for u
www.gooligum.com
also refer the manual available with hitech c suite.it contains variables, syntaxes,  and header files.