I/O
connection:
SCL of EEPROM->C3
SDA of EEPROM->C4
#include<htc.h>
#include"string.h"
#include<stdio.h>
#define
_XTAL_FREQ 20000000
#define
I2C_FREQ 100 // 100khz at 4Mhz
#define
FOSC 20000 // 20Mhz==>20000Khz
void
WaitMSSP(void); //function to
wait for a operation to complete
void
i2c_init(void); //Initialize
the UART
void
I2C_Start(void); //function to send a start bit
void
I2C_Stop(void); //function to
send a stop bit
char
I2C_Read_Data(void);
//function
to read a data
char
I2C_Write_Data(unsigned char); //function to
write the data
void
I2C_Reset(void);
//function
to reset the bit
void
DelayMs(unsigned int); //function to
provide a delay
char
UART_Init(const long int); // function to
initialize UART
void
UART_Write_Text(char *); //function to
write the string
void
UART_Write(char ); //function to
write the byte
char
UART_Data_Ready(void); //function
to check if data ready
char
UART_Read(void); //function to
read the data
void main()
{
char a;
UART_Init(9600); //initialize
the UART
DelayMs(1000); //Provide a
delay of 1s
i2c_init(); //initialize the I2C
DelayMs(1000); //Provide a
delay of 1s
while(1)
{
I2C_Start(); //start bit is set in this function
DelayMs(100); //Provide a
delay
I2C_Write_Data(0xa0); //write the data on to the location 0xa0(device
address)
DelayMs(100); //Provide a delay
I2C_Write_Data(0x20); //write the data on to location 0x20
DelayMs(100); //Provide a
delay
I2C_Write_Data('a'); //send character ‘a’
DelayMs(100); //Provide a
delay
I2C_Stop(); //stop bit is
set in this function
DelayMs(100); //Provide a
delay
I2C_Start(); //start bit is
set in this function
DelayMs(100); //Provide a
delay
I2C_Write_Data(0xa0); //write the data on to the location 0xa0(device address)
DelayMs(100); //Provide a
delay
I2C_Write_Data(0x20); //write the data on to location 0x20
DelayMs(100); //Provide a
delay
I2C_Reset(); //this function
is used to reset
DelayMs(100); //Provide a
delay
I2C_Write_Data(0xa1); //write the data on to the location 0xa0(device address)
DelayMs(100); //Provide a
delay
a=I2C_Read_Data(); //this function reads the data stored in EEPROM
UART_Write(a); //display the
character on hyper terminal
DelayMs(100); //Provide a
delay
I2C_Stop(); //stop bit is
set in this function
DelayMs(100); //Provide a
delay
}
}
char
I2C_Write_Data(unsigned char data)
//This function is used to write the
data onto EEPROM
{
//WaitMSSP(); // wait for the operation to be finished
SSPBUF=data;
//Send Slave address write command
WaitMSSP(); //wait for
operation to complete
}
void
I2C_Start() //this function is used to set start bit
{
SEN=1; //start bit is set
WaitMSSP();
//wait for operation to complete
}
void I2C_Stop() //this function is used to set start bit
{
PEN=1;
//stop bit is set
WaitMSSP(); //wait for operation to complete
}
void
I2C_Reset() //this function is used to reset start bit
{
RSEN=1;
//
Send re-start bit
WaitMSSP(); //wait for operation to complete
}
char
I2C_Read_Data() //this function
is used to read data from
EEPROM
{
RCEN=1;
// Enable
receive
WaitMSSP(); //wait for operation to complete
ACKDT=1; // Acknowledge data 1: NACK, 0: ACK
ACKEN=1; // Enable ACK to send
WaitMSSP(); //wait for operation to
complete
return SSPBUF; // Send the received data to PC
DelayMs(30);
}
void WaitMSSP() // function for wait for operation to complete
{
while(!SSPIF); // while SSPIF=0 stay here else exit the loop
SSPIF=0; // operation
completed clear the flag
}
void i2c_init()
//function to initialize I2C
{
TRISC3=1;
// Set up I2C lines by
setting as input
TRISC4=1;
SSPCON=0x28; // SSP port, Master mode, clock = FOSC / (4 * (SSPADD+1))
SSPADD=(FOSC / (4 * I2C_FREQ)) - 1; //clock 100khz
SSPSTAT=80; // Slew rate control disabled
}
void
DelayMs(unsigned int Ms) //function to provide a delay
{
int delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst = 0;delay_cnst
<220;delay_cnst++);
}
}
char
UART_Init(const long int baudrate)
{
unsigned int x;
x = (_XTAL_FREQ -
baudrate*64)/(baudrate*64);
if(x>255)
{
x = (_XTAL_FREQ -
baudrate*16)/(baudrate*16);
BRGH
= 1; //High Baud Rate Select bit set to high
}
if(x<256)
{
SPBRG = x; //Writing SPBRG register
SYNC = 0; //Selecting Asynchronous Mode
SPEN = 1; //enables
serial port
TRISC7 = 1;
TRISC6 = 1;
CREN = 1; //enables
continuous reception
TXEN = 1; //enables continuous transmission
return 1;
}
return 0;
}
char UART_TX_Empty()
{
return TRMT; //Returns Transmit Shift Status bit
}
char
UART_Data_Ready()
{
return RCIF; //Flag bit
}
char UART_Read() //this function is used to read a byte
{
while(!RCIF); //Waits for Reception to complete
return RCREG; //Returns the 8 bit data
}
void
UART_Read_Text(char *Output, unsigned int length) //this function is used to read a text
{
int i;
for(int i=0;i<length;i++)
Output[i] = UART_Read();
}
void
UART_Write(char data) //this function is used to write a
byte
{
while(!TRMT);
TXREG = data; //transmit register
}
void
UART_Write_Text(char *text) //this function is used to write a string
{
int i;
for(i=0;text[i]!='\0';i++)
UART_Write(text[i]);
}
Lab 11. Working with RTC and controller
Pinconnection:
SCL of RTC->C3
SDA of RTC->C4
#include<htc.h>
#define
_XTAL_FREQ 20000000
#include
"string.h"
#define
LC01CTRLIN 0xd0
#define
LC01CTRLOUT 0xd1
#define
I2C_FREG 100
#define
FOSC 10000
unsigned char
sec,min,hour,day,date,month,year;
unsigned char
data[7]={0x45,0x59,0x71,0x01,0x13,0x10,0x13};
int i;
void
DS1307Write(unsigned char,unsigned char);
void
WaitMSSP();
unsigned char
DS1307Read(unsigned char);
void
i2c_init(void);
char
UART_Init(const long int);
void
ds1307_init(void);
void
DelayMs(unsigned int);
void main()
{
int count=0;
DelayMs(20); //provide
a delay
ds1307_init(); //initialize
ds1307
UART_Init(9600); //initialize
the UART
for(i=0;i<7;i++)
DS1307Write(i,data[i]);
DelayMs(20); //provide a delay
while(1)
{
sec=DS1307Read(0); // Read second
min=DS1307Read(1); // Read minute
hour=DS1307Read(2); // Read hour
day=DS1307Read(3); // Read day
date=DS1307Read(4); // Read date
month=DS1307Read(5); // Read month
year=DS1307Read(6); // Read year
printf("Time:
%x : %x : %x ",(hour&0x1f),min,sec); //Display the Hours, Minutes,
Seconds(hours is taken from 5 LSB bits )
printf("Date: %x / %x / %x \r",date,month,year); //Display the Date, Month, Year
DelayMs(150); //provide a delay
}
}
void DS1307Write(unsigned char addr, unsigned
char data)
{
SEN=1;
//Initiate Start condition on SDA
& SCL pins
WaitMSSP();
SSPBUF=LC01CTRLIN; // Slave
address + Write command
WaitMSSP();
SSPBUF=addr; // Write the location
WaitMSSP();
SSPBUF=data; // Write the Data
WaitMSSP();
PEN=1; // Enable the
Stop bit
WaitMSSP();
}
unsigned char
DS1307Read(unsigned char addr)
{
unsigned char x;
RSEN=1;
// Enable the
repeated Start Condition
WaitMSSP ();
SSPBUF=LC01CTRLIN; // Slave address + Write command
WaitMSSP ();
SSPBUF=addr;
//Write
the location (memory address
of Hour,minute,etc...)
WaitMSSP ();
RSEN=1;
// Enable the
repeated Start Condition
WaitMSSP ();
SSPBUF=LC01CTRLOUT; // Slave address + Read command
WaitMSSP ();
RCEN=1; // Enable to
receive data
WaitMSSP ();
ACKDT=1;
// Acknowledge the operation (Send
NACK)
ACKEN=1;
// Acknowledge
sequence on SDA & SCL pins
PEN=1;
// Enable the Stop bit
WaitMSSP ();
x=SSPBUF;
// Store the
Receive value in a variable
return (x);
}
void WaitMSSP()
{
while(!SSPIF); // SSPIF is zero while TXion is
progress
SSPIF=0;
}
void ds1307_init()
{
TRISC3=1;
// RC3,RC4 set to I2C Mode(Input)
TRISC4=1;
SSPCON=0x28; // Enable the
SDA,SCL & I2C Master Mode
SSPADD=(FOSC / (4 * I2C_FREG)) – 1; // SSP baud rate
100Khz
SSPSTAT=0x80; // Disable slew Rate control
PORTC=0x18;
DS1307Write(0,0x00);
}
void
putch(unsigned char byte) //Required for
printf statement
{
while(!TXIF); // Wait for the
Transmit Buffer to be empty
TXREG = byte; // Transmit the
Data
}
void
DelayMs(unsigned int Ms)
//Function
to provide a delay
{
int delay_cnst;
while(Ms>0)
{
Ms--;
for(delay_cnst = 0;delay_cnst
<220;delay_cnst++);
}
}
char
UART_Init(const long int baudrate)
//function
to initialize the UART
{
unsigned int x;
x = (_XTAL_FREQ -
baudrate*64)/(baudrate*64);
if(x>255)
{
x = (_XTAL_FREQ -
baudrate*16)/(baudrate*16);
BRGH = 1; //High Baud Rate Select bit set to high
}
if(x<256)
{
SPBRG = x; //Writing SPBRG register
SYNC = 0; //Selecting Asynchronous Mode
SPEN = 1; //enables
serial port
TRISC7 = 1;
TRISC6 = 1;
CREN = 1; //enables continuous reception
TXEN = 1; //enables continuous transmission
return 1;
}
return 0;
}