0% found this document useful (0 votes)
19 views

Interfacing EEPROM and Interrupt

Uploaded by

Avinash cliffe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Interfacing EEPROM and Interrupt

Uploaded by

Avinash cliffe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex.

No:
DATE : Interfacing EEPROM and Interrupt

AIM
To develop a C-Language program to write and read a data in EEPROM and also to analyze its
performance with the interrupt.
APPARATUS & SOFTWARE REQUIRED

1. VSK-SCM4 Development board.


2. IAR IDE software.
3. Flash Loader Demonstrator.

THEORY

Serial-interface EEPROM’s are used in a broad spectrum of consumer, automotive, telecommunication,


medical, industrial and PC related markets. Primarily used to store personal preference data and
configuration/setup data, Serial EEPROM’s are the most flexible type of nonvolatile memory utilized today.
Compared to other NVM solutions, Serial EEPROM devices offer a lower pin count, smaller packages, lower
voltages, as well as lower power consumption.

PROCEDURE
1. Double click IAR Embedded Workbench in the Desktop.
2. To create a new project, choose Project>Create New Project. In the Dialog box, set the tool chain to
ARM and select the project template as empty project.
3. To type the program, select new from file menu and save it with the name (anyname.c)
4. Add the necessary library files to the project.
5. Build the program. Hex file will be generated if the program has no errors.
6. Change the mode of the program into downloading mode by using Flash loader software.
7. To view the output, after downloading your program change the board to execution mode in cortex
M4 development board and reset it.
8. In first step, some data will be written in EEPROM at the starting address 0x01.
9. Then read the data from that memory location. The read data will be displayed in the terminal
window.

PROGRAM
/*Header Files*/
#include "stm32f4xx.h"
#include "stdio.h"

/*Definitions*/
#define SLAVE_ADDRESS1 0xA6 // the EEPROM address for write
#define SLAVE_ADDRESS2 0xA7 // the EEPROM address for read

/* prototype functions */
void I2C_stop();
void I2C_write(uint8_t data);
void I2C_start(uint8_t address);
void init_I2C1();
void Delay(int t);
void USART2_config();
int EEPROM_read(uint16_t address);
void EEPROM_write(uint16_t address,uint8_t value);
void TIM2_Config(void);
void TIM2_IRQHandler();
void switch_config(void);

unsigned int address,count;

/*Main Routine*/
int main(void)
{
int val;
USART2_config();
printf("\n\rInterfacing EEPROM and Interrupt");
switch_config();
init_I2C1();
address=0x0000;
EEPROM_write(address,0);
TIM2_Config();
while(1)
{
val = ((GPIOE->IDR)&0x14); //Read PORTE
if(val == 0x10) //if decrement switch is pressed
{
if(count==0)
count=0;
else
count=count-1;
NVIC->ISER[0] |= (0<<28); //Interrupt Disable
EEPROM_write(address,count); //write the count value in the EEPROM
NVIC->ISER[0] |= (1<<28); //Interrupt Enable
while(((GPIOE->IDR)&0x04)==0x00); //wait till the key is released
}
else if(val == 0x04) //If increment switch is pressed
{
count=count+1;
if(count>=255)
{
count=0;
address=address+1;
}
NVIC->ISER[0] |= (0<<28); //Interrupt Disable
EEPROM_write(address,count);
NVIC->ISER[0] |= (1<<28); //Interrupt Enable
while(((GPIOE->IDR)&0x10)==0x00); //wait till the key is released
} }}

/*Delay Routine*/
void Delay(int t)
{
int y,u;
for(y=0;y<t;y++)
for(u=0;u<t;u++);
}

/*Function to read the EEPROM through I2C*/


int EEPROM_read(uint16_t address)
{
int value,LSB,MSB;
LSB=address&0x00FF; //Split the MSByte and LSByte of the Address
MSB=(address&0xFF00)>>8;
I2C_start(SLAVE_ADDRESS1); // I2C start to write
I2C_write(LSB); //Write EEPROM lSB address
I2C_write(MSB); //Write EEPROM MSB address
Delay(200);
I2C_stop(); //I2C Stop
Delay(200);
I2C_start(SLAVE_ADDRESS2); // I2C Start to read
Delay(200);
value = I2C1->DR; //read the data
I2C_stop();
Delay(500);
return(value);
}

/*Function to wrie the data into EEPROM through I2C*/


void EEPROM_write(uint16_t address,uint8_t value)
{
int LSB,MSB;
LSB=address&0x00FF;
MSB=(address&0xFF00)>>8;
I2C_start(SLAVE_ADDRESS1); // I2C start
I2C_write(LSB); //Write LSB address
I2C_write(MSB); //Write MSB address
I2C_write(value); //send data to the address
I2C_stop();
Delay(500);
}

/*Fuction to Configure Serial Port*/


void USART2_config()
{
RCC->AHB1ENR |= 1 << 0; //Serial port is conncted in PortA
RCC->APB1ENR |= 1 <<17; //Clock for USART2
GPIOA->MODER |= 0x000000A0; //Alternate mode of PORTA
GPIOA->AFR[0] |= 0x00007700; //USART mode
USART2->BRR = 0x16D; //115200 baud rate
USART2->CR1 = 0x200C; //Enable transmission and reception
}

/*Function to write a character in the serial port*/


int putchar(int data)
{
USART2->DR = (data & 0x01FF);
while((USART2->SR & 0x40) ==0)
{}
return data;
}

/*Function to Initialize I2C*/


void init_I2C1()
{
RCC->AHB1ENR |= 1<<1; //CLK to port B,SCL and SDA lines are connectedto portB
RCC->APB1ENR |= 1<<21; // CLK to I2C1
GPIOB->MODER = 0x00082000; //Alternate Function
GPIOB->AFR[0] = 0x04000000; //I2C Function PB6 & PB9 =>i2c pins (SDA,SCL)
GPIOB->AFR[1] = 0x00000040;
GPIOB->OTYPER = 0xFFFFFFFF; //open drain
GPIOB->OSPEEDR =0xAAAAAAAA; //50 MHZ
GPIOB->PUPDR = 0x55555555; //pull up

I2C1->OAR1 |=0x0000; //Own Address Register


I2C1->CR1 |=(0<<10)|(0<<0); //Disable I2C and acknowledgement
I2C1->CR2 |=0x08; //Standard Mode,100kHz
I2C1->CCR |=0x28; //Clock Control Reister
I2C1->TRISE = 0x09; //Rise Time
I2C1->CR1 |=0x0001; //Enable I2C Peripheral
}
/*Function to start and write the slave address*/
void I2C_start(uint8_t address)
{
while(((I2C1->SR2)&0x0002)==0x0002); //Check the bus is free or not
I2C1->CR1 |=(1<<8); //Generate START condition
while(((I2C1->SR1)&0x0001)==0x0000); //Wait for start condition to get over

I2C1->DR = address; //Write the slave address


while(((I2C1->SR1)&0x0002)==0x0000); //ADDR flag is reset
while(((I2C1->SR2)&0x0002)==0x0000); //BUSY flag is reset
}
/*Function to write the data to the slave device*/
void I2C_write(uint8_t data)
{
I2C1->DR = data; //Move the value to I2C data register
while(((I2C1->SR1)&0x0004)==0x0000); //Wait until the byte get transferred
}
/*Function for STOP condition*/
void I2C_stop()
{
I2C1->CR1 |=(1<<9); //Generate STOP condition
}
/*Function to configure Timer2*/
void TIM2_Config(void)
{ RCC->APB1ENR |= (1 << 0); // clock for TIM
TIM2->PSC = 0; //Timer prescalar
TIM2->CNT = 0xE6F4BE7C; //Load the value for 5 seconds
TIM2->DIER |=(1<<0); //Interrupt Enable Register
TIM2->EGR |=(1<<0); //Event Generation
TIM2->CR1 |=(1<<0); //Enable Timer for counting
NVIC->STIR = 28; //Interrupt number for TIM2
NVIC->ISER[0] |= (1<<28); //Interrupt Enable
NVIC->IP[7] = 0; //Interrupt Priority
}
void TIM2_IRQHandler()
{ int value;
TIM2->SR = 0x0000; //Clear the flag
value = EEPROM_read(address); //read EEPROM
printf("\n\rThe value at address %d is %x",address,value);
TIM2->CNT = 0xE6F4BE7C;
}

/*Fuction to configure increment and decrement switch*/


void switch_config(void)
{
RCC->AHB1ENR |= (1 << 4 ); // enable the clock to portE
GPIOE->MODER |= 0x00000000; //set portE as i/p mode
GPIOE->OSPEEDR = 0xAAAAAAAA; //Speed 50MHz
}

RESULT

Thus the C-Language program to write and read a data in EEPROM and also to analyze its performance with
the interrupt is developed and is verified.

S.No Mark Allocation Max. Marks


Marks Awarded
1 Preparation 2
2 Interest and
2
Involvement
3 Skill in completing the
4
experiments & results
4 Viva-Voce 2
Total 10
Date Sign

You might also like