Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

Laboratory Report for

20-437-0104 – Embedded Systems Laboratory (2023) by


ARJUN K
REGISTER NUMBER- 23156296

In partial fulfilment of the requirements for


the award of the degree of
Master of Technology (M.Tech)
Electronics and Communication Engineering

Submitted to

Department of Electronics
Cochin University of Science and Technology
Kochi – 682022, Kerala

OCTOBER 2023

1
DECLARATION

I hereby declare that the entire work embodied in this laboratory report has been
carried out by me and no part of it has been submitted for any degree or diploma
of any institution previously.

Place: Kochi
Date:20-10-2023 ARJUN K

2
ACKNOWLEDGEMENT

I would like to express my deepest appreciation to all those who provided me the
possibility to complete this report.

A special gratitude to our guide and faculty in charge, Smt. Sandhya Venugopal,
whose contribution in stimulating suggestions and encouragement, helped me to
co-ordinate the lab and project works and for the valuable guidance in writing this
report.

Furthermore, I would also like to acknowledge Dr . DEEPTHI DAS KRISHNA,


Professor and Head of the Department, Department of Electronics, CUSAT , for
providing us the platform and facilities to earn deeper insights on different
subjects by offering specializations.

Finally, I would also like to express a special thanks to my batchmates, for helping
me out at different points of lab experiments and mini project.

So, these words of acknowledgement come as a small gesture of gratitude


towards all these people, without whom the completion of this report would not
have been possible.

ARJUN K

3
TABLE OF CONTENTS

Exp no Experiment Name

FAMILIARIZATION OF PIC16F877A BOARD.


1 INTERFACE LED AND SWITCH WITH PIC16F877A

INTERFACE A 16X2 LCD TO PIC16F877A


MICROCONTROLLER AND DISPLAY
2 (i) ENGLISH WORD
(ii) MALAYALAM WORD

3 GENERATE A PWM SIGNAL USING


PIC16F877A

4 INTERFACE UART WITH PIC16F877A

INTERFACE A KEYPAD TO PIC16F877A MICRONCONTROLLER AND


5 DISPLAY A KEY
SWITCH BEING PRESSED IN THE BOARD

INTERFACE BUILT IN DS1307 REAL TIME CLOCK CHIP


6 AND DISPLAY TIME, DAY AND DATE

FAMILIARIZATION OF ARM AND LED


7 BLINK USING ARM CORTEX M3

8 FAMILIARIZATION WITH ULINK PRO

9
PRIME NUMBERS
LAB CYCLE NO: 1
FAMILIARIZATION OF PIC16F877A BOARD.
INTERFACE LED AND SWITCH WITH PIC16F877A

FAMILIARIZATION OF PIC16F877A

Peripheral Interface Controllers (PIC) is one of the advanced microcontrollers


developed by microchip technologies. These microcontrollers are widely used in
modern electronics applications. A PIC controller integrates all types of advanced
interfacing ports and memory modules. These controllers are more advanced than
normal microcontrollers like 8051.

All PIC microcontroller family uses Harvard architecture. This architecture has the
program and data accessed from separate memories so the device has a program
memory bus and a data memory bus (more than 8 lines in a normal bus). This improves
the bandwidth (data throughput) over traditional von Neumann architecture where
program and data are fetched from the same memory (accesses over the same bus).
Separating program and data memory further allows instructionsto be sized differently
than the 8-bit wide data word.Now we will move to PIC16F877A.

Microcontroller PIC16F877A is one of the PICmicro Family microcontrollers which is


popular at this moment, start from beginner until all professionals. Because very easy to
use PIC16F877A and use FLASH memory technology so that can be write-erase until
thousand times. The superiority this Risc Microcontroller compared to with another
microcontroller 8-bit especially at a speed of and his code compression.

The 16F877A is a capable microcontroller that can do many tasks because it hasa
large enough programming memory (large in terms of sensor and control projects) of 8k
words and 368 Bytes of RAM.

PIC16F877A has 40 pins by 33 paths of I/O. The 40 pins make it easier to use the
peripherals as the functions are spread out over the pins. This makes it easier to decide
what external devices to attach without worrying too much if there are enough pins to do
the job.
The pinout of the 16F877A is:

The PIC16F877A CMOS FLASH-based 8-bit microcontroller is upward compatiblewith


the PIC16C5x, PIC12Cxxx, and PIC16C7x devices. It features 200 ns instruction
execution, 256 bytes of EEPROM data memory, selfprogramming, an ICD, 2
Comparators, 8 channels of 10-bit Analog-to-Digital (A/D) converter,
2capture/compare/PWM functions, an asynchronous serial port that can beconfigured
as either 3-wire SPI or 2-wire I2C bus, a USART, and a Parallel Slave Port.

High-Performance RISC CPU

• Lead-free; RoHS-compliant
• Operating speed: 20 MHz, 200 ns instruction cycle
• Operating voltage: 4.0-5.5V
• Industrial temperature range (-40° to +85°C)
• 15 Interrupt Sources
• 35 single-word instructions
• All single-cycle instructions except for program branches (two-cycle)
Special Microcontroller Features

• Flash Memory: 14.3 Kbytes (8192 words)


• Data SRAM: 368 bytes
• Data EEPROM: 256 bytes
• Self-reprogrammable under software control
• In-Circuit Serial Programming via two pins (5V)
• Watchdog Timer with on-chip RC oscillator
• Programmable code protection
• Power-saving Sleep mode
• Selectable oscillator options

Peripheral Features

• 33 I/O pins; 5 I/O ports


• Timer0: 8-bit timer/counter with 8-bit Prescaler
• Timer1: 16-bit timer/counter with Prescaler o Can be incremented during Sleep via
external crystal/clock
• Timer2: 8-bit timer/counter with 8-bit period register, Prescaler, andpostscaler
• Two Capture, Compare, PWM modules
o 16-bit Capture input; max resolution 12.5 ns
o 16-bit Compare; max resolution 200 ns
o 10-bit PWM
• Synchronous Serial Port with two modes:
o SPI Master
o I2C Master and Slave
• USART/SCI with 9-bit address detection
• Parallel Slave Port (PSP)
o 8 bits wide with external RD, WR, and CS controls
• Brown-out detection circuitry

Analog Features

• 10-bit, 8-channel A/D


• Brown-Out Reset
• Analog Comparator module
o 2 analog comparators Programmable on-chip voltage reference module
o Programmable input multiplexing from device inputs and internalVREF
o Comparator outputs are externally accessible
LAB 1

INTERFACE LED AND SWITCH WITH PIC16F877A

Aim: Design and write a program to blink a LED with switch using microcontroller
PIC16F877A.

Software Required: MPLABX IDE, Proteus Professional

Theory: In this project, we interface a led and switch with PIC16F877A microcontroller.

The PIC microcontroller PIC16F877A is one of the most popular microcontrollers in the
industry. It has a total number of 40 pins and among which 33 pins are general-purpose
input-output pins. PIC16f877A has 40 pins among which 33 are general-purpose
input-output pins. The input/output pins are integrated as 5 ports, PORTA, PORTB,
PORTC, PORTD, PORTE. These ports are used for input /output operations by the
microcontroller. There will be a data direction register (TRIS) and a port register (PORT)
corresponding to each port in the microcontroller. TRIS register is used for configuring
the port as input or output and the value in the port register will be the state of the pins
of the corresponding port.
The figure is a schematic of interfacing a LED with the microcontroller. The anode of
LED is connected to the microcontroller and cathode is connected to ground through a
resistor. When the voltage at the microcontroller pin is high the LED will be turned ON
and when the voltage is low the LED will be turned off. In this project we have
connected RB0 of PIC16F877A to the resistor and RC0 to the switch/button. The
program is designed in such a way that when button is pressed LED will blink
continuously till when it released.

Procedure:

1. Design the circuit shown Figure1.3, components used are PIC16F877A


microcontroller, LED, resistor, switch.
2. Code the program in MPLAB in .c and build the program code.
3. Get the hex file path and load it to PIC16F877A in our circuit in proteus software.
4. Run the simulation and obtain the required results.
Program Code:

#include <xc.h>
#define _XTAL_FREQ 8000000
#define sw RC0
#define led RB0
void main()
{
TRISB=0x00; // port b as output
TRISC=0x01; //port c as input
while(1)
{
if(sw==1) // if switch is pressed
{
led=1;
while(sw==1); // till switch is released
}
led=0;
}
}

Simulation Result:
Result:
In this project, a LED and switch is successfully interfaced with PIC16F877A
microcontroller and implemented a program for a led to blink when switch is pressed.
LAB CYCLE NO: 2
Interface a 16x2 LCD to PIC16F877A microcontroller &
display a Malayalam word & a English word

Aim: Interface a 16x2 LCD to PIC16F877A microcontroller and display a Malayalam and a
English word.

Software Required: MPLABX IDE, Proteus Professional

Theory: In this project, we interface a 16x2 alphanumeric LCD display with PIC16F877A
microcontroller. The pin diagram is shown;

Here VDD is LCDs power supply pin, VSS is ground pin. VEE can be connected to a
potentiometer to vary the contrast of the display. RS can be used as a data register for RS=1,
and an instruction register to input LCD commands for RS=0. RW is read/write command, LCD
is set to write the input on to display for RW=0 and read the input for RW=1. Now enable pin set
to unsure that this data is displayed on LCD. That is E=1 enables input into LCD and E=0 stalls
the input into LCD. D0-D7 are data input pins. We can either run the LCD in 4 bit 9mode or in 8
bit mode. We in this project runs it in 8 bit mode, that all the pinsfrom D0-D7 are used as inputs.

In this project we are required to display a Malayalam word and a English word which means
we need to display a custom character because an LCD do not support Malayalam characters if
not for custom character design. So, in order to do this custom character design we need to
make use of CGRAM memory of LCD display. This is a memory which can be modified by user.
So that we can store custom characters in this memory and thereby display it. This memory can
store up to 8 user defined characters.
CGRAM:
To define a custom character in CGRAM we must access their CGRAM address location. The
command for access CGRAM address is show on picture below.

0x40 is the first address of CGRAM. Every 1 custom character needs 8 CGRAM locations,
because the dot format of LCD is 5×8. Every 1 CGRAM location can store 1 row of 5×8 dot
format. So, for the first custom character pattern will be stored from address 0x40 to 0x47. After
we write some pattern to CGRAM location, the CGRAM pointer is increased automatically. Once
the custom characters are stored, we need to use the command 0X80 and then display the
characters from CGRAM location using a display command that we define.

LCD Commands: these are the different LCD commands, We can input these commands to
LCD when RS=0, that is when it is used as an instruction register.
Procedure:
1. Design the circuit shown Figure1.3, components used are PIC16F877A microcontroller, LED,
resistor, switch.
2. Code the program in MPLAB in .c and build the program code.
3. Get the hex file path and load it to PIC16F877A in our circuit in proteus software.
4. Run the simulation and obtain the required results.

Fig 1.3

Program Code:
#define _XTAL_FREQ 20000000

#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#include <xc.h>

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)


#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for
programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data
EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)

//LCD Functions Developed by Circuit Digest.

/**START OF LCD FUNCTIONS****/


void Lcd_SetBit(char data_bit) //Based on the Hex value Set the Bits of the Data Lines
{
if(data_bit& 1)
D4 = 1;
else
D4 = 0;

if(data_bit& 2)
D5 = 1;
else
D5 = 0;

if(data_bit& 4)
D6 = 1;
else
D6 = 0;

if(data_bit& 8)
D7 = 1;
else
D7 = 0;
}

void Lcd_Cmd(char a)
{
RS = 0;
Lcd_SetBit(a); //Incoming Hex value
EN = 1;
__delay_ms(4);
EN = 0;
}

Lcd_Clear()
{
Lcd_Cmd(0); //Clear the LCD
Lcd_Cmd(1); //Move the curser to first position
}

void Lcd_Set_Cursor(char a, char b)


{
char temp,z,y;
if(a== 1)
{
temp = 0x80 + b - 1; //80H is used to move the curser
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
}
else if(a== 2)
{
temp = 0xC0 + b - 1;
z = temp>>4; //Lower 8-bits
y = temp & 0x0F; //Upper 8-bits
Lcd_Cmd(z); //Set Row
Lcd_Cmd(y); //Set Column
}
}

void Lcd_Start()
{
Lcd_SetBit(0x00);
for(int i=1065244; i<=0; i--) NOP();
Lcd_Cmd(0x03);
__delay_ms(5);
Lcd_Cmd(0x03);
__delay_ms(11);
Lcd_Cmd(0x03);
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the
LCD
Lcd_Cmd(0x02); //02H is used for Return home -> Clears the RAM and initializes the
LCD
Lcd_Cmd(0x08); //Select Row 1
Lcd_Cmd(0x00); //Clear Row 1 Display
Lcd_Cmd(0x0C); //Select Row 2
Lcd_Cmd(0x00); //Clear Row 2 Display
Lcd_Cmd(0x06);
}

void Lcd_Print_Char(char data) //Send 8-bits through 4-bit mode


{
char Lower_Nibble,Upper_Nibble;
Lower_Nibble = data&0x0F;
Upper_Nibble = data&0xF0;
RS = 1; // => RS = 1
Lcd_SetBit(Upper_Nibble>>4); //Send upper half by shifting by 4
EN = 1;
for(int i=2130483; i<=0; i--) NOP();
EN = 0;
Lcd_SetBit(Lower_Nibble); //Send Lower half
EN = 1;
for(int i=2130483; i<=0; i--) NOP();
EN = 0;
}

void Lcd_Print_String(char *a)


{
int i;
for(i=0;a[i]!='\0';i++)
Lcd_Print_Char(a[i]); //Split the string using pointers and call the Char function
}

/**END OF LCD FUNCTIONS****/


const unsigned short Custom_Char5x8[] = {
0b01001,0b11101,0b10101,0b10101,0b10101,0b10101,0b10111,0b10111,
0b01001,0b10101,0b11101,0b10001,0b10001,0b10001,0b11111,0b11111,
};

int main()
{
unsigned int a;char i;
TRISD = 0x00;
Lcd_Start();

//* Load custom char into the CGROM***//////


Lcd_Cmd(0x04); // Set CGRAM Address
Lcd_Cmd(0x00); // .. set CGRAM Address
for (i = 0; i <= 63 ; i++)
Lcd_Print_Char(Custom_Char5x8[i]);
Lcd_Cmd(0); // Return to Home
Lcd_Cmd(2); // .. return to Home
//* Loading custom char complete***//////

while(1)
{
Lcd_Clear();
//Print all Custom characters//
Lcd_Set_Cursor(1,1);
Lcd_Print_Char(0); // Display Custom Character 0
Lcd_Print_Char(1); // Display Custom Character 1
Lcd_Print_Char(2); // Display Custom Character 2
Lcd_Print_Char(3); // Display Custom Character 3
Lcd_Print_Char(4);
Lcd_Print_Char(5); // Display Custom Character 4
Lcd_Print_Char(6); // Display Custom Character 5
Lcd_Print_Char(7); // Display Custom Character 6

Lcd_Set_Cursor(2,1);
Lcd_Print_String(" I am Arjun"); //binary value of alpha from data-sheet
__delay_ms(1000);
}
return 0;
}
Simulation Result:

Result:
In this project, a 16x2 LCD is successfully interfaced with PIC16F877A microcontroller and
implemented a program for to display a Malayalam and a English word.
LAB CYCLE NO: 3
Generation of PWM signal using PIC16F877A

Aim: To generate a PWM signal using PIC16F877A microcontroller.

Software Required: MPLABX IDE, Proteus Professional

Theory: Pulse-width modulation (PWM), or pulse-duration modulation (PDM), is


a method of reducing the average power delivered by an electrical signal, by
effectively chopping it up into discrete parts. The average value of voltage (and
current) fed to the load is controlled by turning the switch between supply and
load on and off at a fast rate. The longer the switch is on compared to the off
periods, the higher the total power supplied to the load. Along with maximum
power point tracking (MPPT), it is one of the primary methods of reducing the
output of solar panels to that which can be utilized by a battery.[1] PWM is
particularly suited for running inertial loads such as motors, which are not as
easily affected by this discrete switching, because their inertia causes them to
react slowly. The PWM switching frequency has to be high enough not to affect
the load, which is to say that the resultant waveform perceived by the load must
be as smooth as possible. The main advantage of PWM is that power loss in the
switching devices is very low. When a switch is off there is practically no current,
and when it is on and power is being transferred to the load, there is almost no
voltage drop across the switch. Power loss, being the product of voltage and
current, is thus in both cases close to zero. PWM also works well with digital
controls, which, because of their on/off nature, can easily set the needed duty
cycle. PWM has also been used in certain communication systems where its duty
cycle has been used to convey information over a communications channel. In
electronics, many modern microcontrollers (MCUs) integrate PWM controllers
exposed to external pins as peripheral devices under firmware control by means
of internal programming interfaces. These are commonly used for direct current
(DC) motor control in robotics and other applications.

Duty Cycle: The term duty cycle describes the proportion of 'on' time to the
regular interval or 'period' of time; a low duty cycle corresponds to low power,
because the power is off for most of the time. Duty cycle is expressed in percent,
100% being fully on. When a digital signal is on half of the time and off the other
half of the time, the digital signal has a duty cycle of 50% and resembles a
"square" wave. When a digital signal spends more time in the on state than the
off state, it has a duty cycle of >50%. When a digital signal spends more time in
the off state than the on state, it has a duty cycle of <50%. Here is a pictorial that
illustrates these three scenarios:

Procedure:
1. Design the circuit shown below, components used are PIC16F877A microcontroller,
LED, resistor, switch.
2. Code the program in MPLAB in .c and build the program code.
3. Get the hex file path and load it to PIC16F877A in our circuit in proteus software.
4. Run the simulation and obtain the required results.
Program Code:
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3/PGM pin has PGM function; low-voltage
programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
(Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
(Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit
(Code protection off)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 20000000
#define TMR2PRESCALE 4
#include <xc.h>
long PWM_freq = 5000;
PWM_Initialize()
{
PR2 = (_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1;
//Setting the PR2 formulae using Datasheet Makes the PWM work in 5KHZ
CCP1M3 = 1;
CCP1M2 = 1; //Configure the CCP1 module
T2CKPS0 = 1;T2CKPS1 = 0; TMR2ON = 1; //Configure the Timer module
TRISC2 = 0; // make port pin on C as output
}
PWM_Duty(unsigned int duty)
{
if(duty<1023)
{
duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE));
// On reducing //duty = (((float)duty/1023)*(1/PWM_freq)) / ((1/_XTAL_FREQ) *
TMR2PRESCALE);
CCP1X = duty & 1; //Store the 1st bit
CCP1Y = duty & 2; //Store the 0th bit
CCPR1L = duty>>2;// Store the remining 8 bit
}
}
void ADC_Initialize()
{
ADCON0 = 0b01000001; //ADC ON and Fosc/16 is selected
ADCON1 = 0b11000000; // Internal reference voltage is selected
}
unsigned int ADC_Read(unsigned char channel)
{
ADCON0 &= 0x11000101; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D Conversion
while(GO_nDONE); //Wait for A/D Conversion to complete
return ((ADRESH<<8)+ADRESL); //Returns Result
}
void main()
{
int adc_value;
TRISC = 0x00; //PORTC as output
TRISA = 0xFF; //PORTA as input
TRISD = 0x00;
ADC_Initialize(); //Initializes ADC Module
PWM_Initialize(); //This sets the PWM frequency of PWM1
do
{
adc_value = ADC_Read(4); //Reading Analog Channel 0
PWM_Duty(adc_value);
__delay_ms(50);
}while(1); //Infinite Loop
}

Simulation Result:

Result:
PWM signal is successfully generated using PIC16F877A microcontroller.
EXPERIMENT 4
INTERFACE A KEYPAD TO PIC16F877A

Aim : To interface a keypad to PIC16F8771 microcontroller and display a


key switch being pressed in the board

Software Required : MPLAB X IDE, Proteus Professional

Theory : In this project we used a 4x4 numeric phone keypad. It has 4


rows and 4 columns as shown in the figure. Here we give inputs to columns
and take the output from rows, namely we give input to 1, 2, 3, 4 in the
figure and read the output values from A, B, C, D rows. A, B, C, D shows a
high value when the key from the respective row is pressed.

1, 2, 3, 4 columns are set to 1 at a time in a loop and the output from its
rows are checked. That is if 1=1, then rows A, B, C, D are checked for an
output of 1. If 17 it's all zero, no key is pressed. If any of the rows give an
output 1, the corresponding key pressed is displayed on the LCD. For
example, If 1 = 1, D=1 implies key ‘*’ is pressed. This way the program
continuously scans for a key press and displays a successful find of the key
press.

Procedure:
1. Design the circuit shown below, components used are PIC16F877A
microcontroller, LCD, Keypad.
2. Code the program in MPLAB in .c and build the program code.
3. Get the hex file path and load it to PIC16F877A in our circuit in proteus
software.
4. Run the simulation and obtain the required results.

Program Code:

#include <pic.h>

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_ON &
LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);

#define rs RD2
#define en RD3

#define R1 RB0
#define R2 RB1
#define R3 RB2
#define R4 RB3
#define C1 RB4
#define C2 RB5
#define C3 RB6
#define C4 RB7

void lcd_init();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay();

unsigned char key();


void keyinit();

unsigned char keypad[4][4]={{'7','8','9','/'},{'4','5','6','*'},{'1','2','3','-'},{'C','0','=','+'}};


unsigned char rowloc,colloc;

void main()
{
unsigned int i;
TRISD=0;
lcd_init();
keyinit();
unsigned char b;
cmd(0x80);
show(" Enter the Key ");
while(1)
{
cmd(0xc7);
b=key();
dat(b);

}
}

void lcd_init()
{
cmd(0x02);
cmd(0x28);
cmd(0x0e);
cmd(0x06);
cmd(0x80);
}

void cmd(unsigned char a)


{
rs=0;
PORTD&=0x0F;
PORTD|=(a&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
PORTD&=0x0f;
PORTD|=(a<<4&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
}

void dat(unsigned char b)


{
rs=1;
PORTD&=0x0F;
PORTD|=(b&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
PORTD&=0x0f;
PORTD|=(b<<4&0xf0);
en=1;
lcd_delay();
en=0;
lcd_delay();
}

void show(unsigned char *s)


{
while(*s) {
dat(*s++);
}
}

void lcd_delay()
{
unsigned int lcd_delay;
for(lcd_delay=0;lcd_delay<=1000;lcd_delay++);
}

void keyinit()
{
TRISB=0XF0;
OPTION_REG&=0X7F; //ENABLE PULL UP
}

unsigned char key()


{
PORTB=0X00;
while(C1&&C2&&C3&&C4);
while(!C1||!C2||!C3||!C4) {
R1=0;
R2=R3=R4=1;
if(!C1||!C2||!C3||!C4) {
rowloc=0;
break;
}
R2=0;R1=1;
if(!C1||!C2||!C3||!C4) {
rowloc=1;
break;
}
R3=0;R2=1;
if(!C1||!C2||!C3||!C4) {
rowloc=2;
break;
}
R4=0; R3=1;
if(!C1||!C2||!C3||!C4){
rowloc=3;
break;
}
}
if(C1==0&&C2!=0&&C3!=0&&C4!=0)
colloc=0;
else if(C1!=0&&C2==0&&C3!=0&&C4!=0)
colloc=1;
else if(C1!=0&&C2!=0&&C3==0&&C4!=0)
colloc=2;
else if(C1!=0&&C2!=0&&C3!=0&&C4==0)
colloc=3;
while(C1==0||C2==0||C3==0||C4==0);
return (keypad[rowloc][colloc]);

Simulation Result:
Result:
Interfaced a keypad to PIC16F8771 microcontroller and displayed a key
switch being pressed in the board
EXPERIMENT 5
Interfacing Stepper Motor with PIC16F877A

Aim: To Interface a stepper motor to PIC16F877A microcontroller and rotate the


motor in both direction.

Software Required: MPLABX IDE, Proteus Professional

Theory: Stepper Motor is a specially designed motor which rotates in steps.


Speed of stepper motor depends on the rate of electrical signal applied to it.
Different patterns can control stepper motor’s direction and rotation type. Mainly
two types of stepper motors are available, Unipolar and Bipolar. Unipolar is
easier to operate, control and also easier to get. Here in this tutorial we are
interfacing Stepper Motor with PIC Microcontroller PIC16F877A. We are using
28BYJ-48 stepper motor for this project which is cheap and easily available. It is
5V DC unipolar stepper motor. We are also using a Module available with this
motor which consist ULN2003 Stepper Motor Driver IC. ULN2003 is a Darlington
pair array, which is useful to drive this motor, since PIC microcontroller couldn’t
provide enough current to drive. ULN2003A is capable to drive 500mA of load
with 600mA of
peak current

Working of stepper motor


Inside the motor there are two centre tapped coils available. Red wire is the
common for both which will be connected at VCC or 5V. Other 4 wires pink, red,
yellow and blue will control the rotation depending . Also, depending on the
movement. This motor can be controlled using 3 steps. Full drive mode, Half
Drive mode and Wave drive mode.
Full Drive
If two stator electromagnets are energized at a time, the motor will run at full
torque referred as full-drive sequence mode.

Half-Drive
When alternatively one and two phases are energized, the motor will run in half
drive mode. It’s used to increase the angular resolution. Drawback is less torque
produced in this movement.
Wave Drive
In this mode, one stator electromagnet is turned on. Its follows 4 steps same as
Full-drive mode. It consumes low power with low torque.
Procedure:
1. Design the circuit shown below, components used are PIC16F877A ,stepper
motor, driver circuit
2. Code the program in MPLAB in .c and build the program code.
3. Get the hex file path and load it to PIC16F877A in our circuit in proteus
software.
4. Run the simulation and obtain the required results.

Program Code:

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)


#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT
disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial
Programming Enable bit (RB3/PGM pin has PGM function; low-voltage
programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
(Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
(Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit
(Code protection off)
#include <xc.h>
#include <stdio.h>
/*
Hardware related definition
*/
#define _XTAL_FREQ 200000000 //Crystal Frequency, used in delay
#define speed 1 // Speed Range 10 to 1 10 = lowest , 1 = highest
#define steps 250 // how much step it will take
#define clockwise 0 // clockwise direction macro
#define anti_clockwise 1 // anti clockwise direction macro
/*
*Application related function and definition
*/
void system_init (void); // This function will initialise the ports.
void full_drive (char direction); // This function will drive the motor in full drive
mode
void half_drive (char direction); // This function will drive the motor in full drive
mode
void wave_drive (char direction); // This function will drive the motor in full drive
mode
void ms_delay(unsigned int val);
/*
* main function starts here
*/
void main(void)
{
system_init();
while(1){
/* Drive the motor in full drive mode clockwise */
for(int i=0;i<steps;i++)
{
full_drive(clockwise);
}
ms_delay(1000);
/* Drive the motor in wave drive mode anti-clockwise */
for(int i=0;i<steps;i++)
{
wave_drive(anti_clockwise);
//full_drive(anti_clockwise);
}
ms_delay(1000);
}
}
/*System Initialising function to set the pin direction Input or Output*/
void system_init (void){
TRISB = 0x00; // PORT B as output port
PORTB = 0x0F;
}
/*This will drive the motor in full drive mode depending on the direction*/
void full_drive (char direction){ if (direction == anti_clockwise){
PORTB = 0b00000011;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00001001;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
}
if (direction == clockwise){
PORTB = 0b00001001;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
PORTB = 0b00001001;
ms_delay(speed);
}
}
/* This method will drive the motor in half-drive mode using direction input */
void half_drive (char direction){ if (direction == anti_clockwise){
PORTB = 0b00000001;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00001000;
ms_delay(speed);
PORTB = 0b00001001;
ms_delay(speed);
}
if (direction == clockwise){
PORTB = 0b00001001;
ms_delay(speed);
PORTB = 0b00001000;
ms_delay(speed);
PORTB = 0b00001100;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00000110;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000011;
ms_delay(speed); PORTB =
0b00000001; ms_delay(speed);
}
}
/* This function will drive the the motor in wave drive mode with direction input*/
void wave_drive (char direction){ if (direction == anti_clockwise){
PORTB = 0b00000001;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00001000;
ms_delay(speed);
}
if (direction == clockwise){
PORTB = 0b00001000;
ms_delay(speed);
PORTB = 0b00000100;
ms_delay(speed);
PORTB = 0b00000010;
ms_delay(speed);
PORTB = 0b00000001;
ms_delay(speed);
}
}
/*This method will create required delay*/
void ms_delay(unsigned int val)
{
unsigned int i,j;
for(i=0;i<val;i++)
for(j=0;j<1650;j++);
}

Output:
Result:
Successfully Interfaced a stepper motor PIC16F877A microcontroller and rotate
the motor in both direction.
EXPERIMENT 6
Interface built in DS1307 real time clock chip and display time,
day and date

Aim: To Interface DS1307 real time clock chip with PIC16F877A and
display the time, day and date.

Software Required: MPLABX IDE, Proteus Professional

Theory: The DS1307 serial real-time clock (RTC) is a lowpower, full


binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV
SRAM. Address and data are transferred serially through an I2 C,
bidirectional bus. The clock/calendar provides seconds, minutes,
hours, day, date, month, and year information. The end of the month
date is automatically adjusted for months with fewer than 31 days,
including corrections for leap year. The clock operates in either the
24-hour or 12- hour format with AM/PM indicator. The DS1307 has a
built-in powersense circuit that detects power failures and
automatically switches to the backup supply. Timekeeping operation
continues while the part operates from the backup supply The DS1307
supports the I2 C protocol. A device that sends data onto the bus is
defined as a transmitter and a device receiving data as a receiver. The
device that controls the message is called a master. The devices that
are controlled by the master are referred to as slaves. The bus must
be controlled by a master device that generates the serial clock (SCL),
controls the bus access, and generates the START and STOP
conditions. The DS1307 operates as a slave on the I2C bus.
VCC, GND: These pins are used to provide power to the chip. When
5V is applied within normal limits, the device is fully accessible and
data can be written and read. When a 3V battery is connected to the
device and VCC is below 1.25 x VBAT, reads and writes are inhibited.
However, the timekeeping function continues unaffected by the lower
input voltage. As VCC falls below VBAT the RAM and timekeeper are
switched over to the external power supply (nominal 3.0V DC) at
VBAT.
X1-X2:Pins to connect the external 32.768kHz oscillator that provides
the clock source to the chip
Vbat: A 3.3v Lithium battery can be connected to this pin in order to
provide the power source when the external supply voltage is not
available. Battery voltage must be held between 2.0V and 3.5V for
proper operation.
SCL: This pin must be connected to the SCL pin of the I2C
Bus/Master SDA: This pin must be connected to the SDA pin of the
I2C Bus/Master.
SQW/OUT: When enabled, the SQWE bit set to 1, the SQW/OUT pin
outputs one of four square wave frequencies (1Hz, 4kHz, 8kHz,
32kHz).

DS1307 uses I2C Protocol and acts as a slave device and I2C Master
can read/write the register of RTC. To communicate with the slave
device, the master needs the slave address of a device connected to
the bus. DS1307 has a fixed slave address which makes it impossible
to connect two RTC devices on the same bus, don’t worry occurrence
of such a scenario is close to zero.

Slave address for I2C Write: 0b11010000 = 0xD0


Slave address for I2C Read: 0b11010001 = 0xD1
Procedure:
1. Draw the simulation diagram in proteus design suite.
2. Connect the circuit with the following pins
LCD:
RS – RB0
RW – RB1
EN – RB2
Data Lines – Port D
EEPROM:
SDA – RC4
SCL – RC3
3. Create a project window in mp lab and write the code and save it as
.c file
and also add lcd.h file.
4. Simulate the output.

Circuit Diagram:
Program Code:
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS
oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT
disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit
(PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR
enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply)
In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on
MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code
Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write
Enable bits (Write protection off; all program memory may be written
to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code
Protection bit (Code protection off)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#include<pic.h>
#include"lcd.h"

unsigned char sec,min,hour,day,date,month,year;


void rtc_int();
void rtc_start();
void rtc_stop();
void rtc_ack();
void rtc_nak();
void rtc_res();
void rtc_send(unsigned char a);
void rtc_send_byte(unsigned char addr,unsigned char data);
unsigned char rtc_read();
unsigned char rtc_read_byte(unsigned char addr);
void waitmssp();
unsigned char convup(unsigned char bcd);
unsigned char convd(unsigned char bcd);

void main()
{
lcd_init();

show("Time:");
cmd(0xc0);
show("Date:");
rtc_int();
while(1) {
sec =rtc_read_byte(0x00);
min =rtc_read_byte(0x01);
hour =rtc_read_byte(0x02);
day =rtc_read_byte(0x03);
date =rtc_read_byte(0x04);
month=rtc_read_byte(0x05);
year =rtc_read_byte(0x06);
cmd(0x85);
dat(convup(hour));
dat(convd(hour));
dat(':');
dat(convup(min));
dat(convd(min));
dat(':');
dat(convup(sec));
dat(convd(sec));

cmd(0xc5);
dat(convup(date));
dat(convd(date));
dat(':');
dat(convup(month));
dat(convd(month));
dat(':');
dat(convup(year));
dat(convd(year));
dat('/');
dat(convup(day));
dat(convd(day));

}
}

void rtc_int()
{
TRISC3=TRISC4=1;
SSPCON=0x28;
SSPADD= (((11059200/4)/100)-1);
}

void waitmssp()
{
while(!SSPIF); // SSPIF is zero while TXion is progress
SSPIF=0;
}

void rtc_send(unsigned char a)


{
SSPBUF=a;
waitmssp();
while(ACKSTAT);
}

void rtc_send_byte(unsigned char addr,unsigned char data)


{
rtc_start();
rtc_send(0xD0);
//rtc_send(addr>>8);
rtc_send(addr);
rtc_send(data);
rtc_stop();
}

unsigned char rtc_read()


{
RCEN=1;
waitmssp();
return SSPBUF;
}
unsigned char rtc_read_byte(unsigned char addr)
{
unsigned char rec;
L: rtc_res();
SSPBUF=0xD0;
waitmssp();
while(ACKSTAT){goto L;}
//rtc_send(addr>>8);
rtc_send(addr);
rtc_res();
rtc_send(0xD1);
rec=rtc_read();
rtc_nak();
rtc_stop();
return rec;
}

void rtc_start()
{
SEN=1;
waitmssp();
}

void rtc_stop()
{
PEN=1;
waitmssp();
}

void rtc_res()
{
RSEN=1;
waitmssp();
}

void rtc_ack()
{
ACKDT=0;
ACKEN=1;
waitmssp();
}

void rtc_nak()
{
ACKDT=1;
ACKEN=1;
waitmssp();
}

unsigned char convup(unsigned char bcd)


{

return ((bcd>>4)+48);
}

unsigned char convd(unsigned char bcd)


{
return ((bcd&0x0F)+48);
}
Simulation Result:

Result:
Successfully Interfaced DS1307 real time clock chip with
PIC16F877A and displayed the time, day and date.
EXPERIMENT 7
Familiarization of deployment flow ARM Development
Kit, Keil Mvision IDE, Flash Programming
Deployment flow ARM Development Kit:

General overview of the deployment flow for an ARM Development Kit:

1. Setting up the Development Environment:

● Install the necessary development tools and IDEs, such as Keil MDK,
ARM Development Studio, or IAR Embedded Workbench.
● Ensure that you have the required drivers and software libraries for
your specific ARM-based microcontroller or processor.

2. Creating or Importing a Project:

● Create a new project in your chosen development environment or


import an existing one if you continue working on an existing project.
● Configure the project settings to match your target microcontroller,
including selecting the appropriate ARM architecture (e.g., ARM
Cortex-M3, Cortex-M4, Cortex-A53) and specifying compiler options.

3. Writing and Debugging Code:

● Write and debug your embedded software code in C/C++ or assembly


language using the development environment's code editor and
debugger.
● Set up breakpoints, watch variables, and use other debugging features
to identify and resolve issues in your code.

4. Compilation:

● Compile your code to generate a binary or executable file that can run
on the ARM-based microcontroller or processor.
● The compiler optimizes the code for the specific ARM architecture you
are targeting.

5. Building the Firmware:


● Depending on your development environment, you may need to build
the firmware, combining the compiled code with additional resources
like startup code and linker scripts.
● This step produces the final binary image that you will deploy to the
target hardware.

6. Programming the Target Hardware:

● Use a hardware programmer or debugger (e.g., J-Link, ST-Link,


CMSIS-DAP) to program the firmware onto the ARM-based
microcontroller or processor.
● Connect the programmer to the target hardware via JTAG or SWD
(Serial Wire Debug) interfaces.

7. Testing and Debugging on the Target:

● Power up the target hardware and execute the deployed firmware.


● Monitor the behavior of the application on the target and use the
debugger to troubleshoot any runtime issues.
● Ensure that all hardware peripherals and interfaces are functioning as
expected.

8. Optimization and Fine-Tuning:

● Profile the application's performance and optimize it for resource


usage and power efficiency, if necessary.
● Make any required adjustments to the code or configuration to meet
performance and power constraints.

9. Documentation and Release:

● Document your project thoroughly, including code comments,


configuration settings, and any special considerations for deployment.
● Prepare your project for release, including creating release notes and
packaging the final firmware for distribution.

10. Deployment to Production:

● If your project is intended for mass production, work with your


manufacturing team to deploy the firmware to the production units
using programming and testing procedures suitable for the production
environment.

Keil Mvision IDE:

Keil MDK (Microcontroller Development Kit) is an integrated development


environment (IDE) specifically designed for programming and developing
software for microcontrollers. It is widely used in the embedded systems
industry to develop microcontrollers and microprocessors firmware.

Keil MDK provides a comprehensive set of tools and features for embedded
system development, including:

1. Integrated Development Environment (IDE):

● Keil MDK offers a user-friendly IDE where you can write, compile, and
debug your embedded software.
● It supports various programming languages like C and assembly.

2. Compiler:

● Keil MDK comes with a C/C++ compiler that is optimized for


microcontroller architectures.
● It can generate efficient code for a wide range of microcontrollers from
various manufacturers.

3. Debugger:

● The IDE includes a powerful debugger with features like real-time


variable monitoring, code breakpoints, and memory inspection.
● This makes it easier to identify and fix issues in your firmware.

4. Peripheral Simulation:

● Keil MDK provides simulation capabilities that allow you to test your
code without needing physical hardware.
● You can simulate the behavior of microcontroller peripherals and test
your software in a controlled environment.
5. RTOS (Real-Time Operating System) Support:

● It offers support for various RTOSs commonly used in embedded


systems, such as CMSIS-RTOS and RTX.

6. Device-specific Packs:

● Keil MDK includes device-specific packs that simplify the process of


configuring and setting up your project for a specific microcontroller.
● These packs provide predefined register definitions and startup codes
for different microcontroller families.

7. Integrated Version Control:

● It integrates with version control systems like Git, making it easier for
teams to collaborate on embedded projects.

8. Code Templates and Examples:

● Keil MDK includes a library of code templates and examples for various
microcontrollers and peripherals.
● These templates can help you get started quickly and serve as a
reference for best practices.

9. IDE Extensions:

● You can extend the functionality of Keil MDK through plugins and
extensions to tailor it to your specific project requirements.

Keil MDK supports a wide range of microcontroller families, including ARM


Cortex-M, ARM Cortex-R, and others. It is commonly used in industries like
automotive, industrial automation, consumer electronics, and more, where
embedded systems are prevalent.

Flash Programming:

Flash programming, in the context of embedded systems and


microcontrollers, refers to the process of writing or programming firmware or
software into the flash memory of a microcontroller. Flash memory is a type
of non-volatile storage used to store the program code and data that an
embedded system needs to run. Here's an overview of the flash
programming process:

1. Compile your Code:

● Before you can program the flash memory, you need to have the
compiled binary or hex file of your firmware.
● This file contains the machine code generated from your source code
(written in C/C++ or assembly).

2. Connect the Programming Hardware:

● To program the flash memory of a microcontroller, you typically need


a programming hardware tool such as a JTAG (Joint Test Action
Group) debugger, SWD (Serial Wire Debug) interface, or a dedicated
programmer like the ST-Link or J-Link.
● Connect this tool to the microcontroller on your development board or
target hardware.

3. Select the Target Device:

● In your programming software (usually integrated into your


development environment), select the target device or microcontroller
that you want to program.
● Ensure that you choose the correct device to avoid accidental
overwriting of firmware on the wrong microcontroller.

4. Erase the Flash Memory:

● Before writing new firmware, it's often necessary to erase the existing
contents of the flash memory.
● This step ensures that no remnants of old firmware interfere with the
new one.
● The erasing process may be complete or sector-based, depending on
the microcontroller.

5. Program the Flash Memory:

● Use your programming software and hardware to write the compiled


firmware binary to the flash memory.
● The programming software will typically allow you to select the binary
file and specify the starting address in flash memory where the code
should be written.

6. Verify the Programming:

● After programming, it's a good practice to verify that the written data
matches the original binary file.
● Verification ensures that the programming process is successful and
that the firmware in flash memory is consistent with your intended
code.

7. Configure Memory Protection (Optional):

● Many microcontrollers offer memory protection mechanisms to prevent


unauthorized access to or modification of the firmware.
● Depending on your application and security requirements, you may
configure memory protection settings at this stage.

8. Test the Program:

● Power up the microcontroller and execute the newly programmed


firmware.
● Monitor the behavior and functionality of your embedded application
on the target hardware to ensure it operates as expected.

9. Debug and Iterate (if necessary):

● If you encounter issues or bugs in your firmware during testing, you


can use debugging tools and techniques to identify and resolve them.
● You may need to go back to the code development stage, make
changes, and repeat the Flash programming process.

10. Document and Release:

● Document the programming procedure, firmware version, and any


relevant information for future reference and for the production team if
the project is intended for mass production.
Flash a sample blinky program into ARM Cortex M3

AIM: To flash a sample blinky program into ARM Cortex M3.

SOFTWARE REQUIRED: Keil Mvision IDE

THEORY:

Arm cortex m3:

The ARM Cortex-M3 is a 32-bit microcontroller core designed for high-


performance embedded systems. It is part of ARM's Cortex-M family, which
is known for its energy efficiency, low power consumption, and real-time
performance. Here are some key features and characteristics of the ARM
Cortex-M3:

1. 32-bit RISC Architecture:

● The Cortex-M3 employs a 32-bit Reduced Instruction Set Computer


(RISC) architecture, which simplifies instruction decoding and
execution, resulting in efficient and predictable performance.

2. Harvard Architecture:

● It uses Harvard architecture, meaning that it has separate instruction


and data buses, allowing for parallel instruction fetch and data access.

3. Pipeline Architecture:

● The Cortex-M3 uses a three-stage pipeline, which includes instruction


fetch, decode/execute, and write-back stages.
● This pipeline helps to improve instruction throughput and performance.

4. Single Precision FPU (Floating-Point Unit):

● Some Cortex-M3 implementations include a single-precision FPU,


which allows for efficient floating-point arithmetic operations.
● However, not all Cortex-M3-based microcontrollers have this feature.

5. Memory Protection Unit (MPU):


● The Cortex-M3 often includes an optional MPU that enables memory
protection and access control for different parts of the memory,
enhancing security and stability.

6. Nested Vectored Interrupt Controller (NVIC):

● The NVIC is an integral part of the Cortex-M3 architecture and provides


efficient and low-latency interrupt handling.
● It supports prioritized interrupts and nested interrupt handling.

7. Low Power Consumption:

● The Cortex-M3 is designed to be energy-efficient and offers various


low-power modes, making it suitable for battery-powered and energy-
sensitive applications.

8. Clock Speeds:

● Different microcontrollers using the Cortex-M3 core can run at various


clock speeds, typically ranging from a few MHz to over 100 MHz,
depending on the specific implementation.

9. Wide Range of Peripherals:

● Cortex-M3-based microcontrollers come with a wide range of


integrated peripherals, including GPIO (General-Purpose
Input/Output), UARTs, SPI, I2C, timers, PWM controllers, and more,
making them versatile for various applications.

10. Development Ecosystem:

● ARM provides a robust development ecosystem for Cortex-M3


microcontrollers, including a variety of development boards, software
development tools (like Keil MDK and ARM Development Studio), and
an extensive software library known as CMSIS (Cortex Microcontroller
Software Interface Standard).

Cortex-M3-based microcontrollers are commonly used in a wide range of


applications, including automotive systems, industrial automation, consumer
electronics, medical devices, and IoT (Internet of Things) devices. They are
known for their real-time performance, which makes them suitable for
applications that require precise timing and control. As of my knowledge
cutoff date in September 2021, the Cortex-M3 core remains a popular choice
in the embedded systems industry. However, newer Cortex-M cores, such
as the Cortex-M4 and Cortex-M7, offer additional features, including DSP
(Digital Signal Processing) capabilities and floating-point support, which may
be preferred for certain applications.

PROCEDURE:

1. Open or Create a Project:

● Launch Keil MDK.


● You can either open an existing project or create a new one.
● To create a new project, go to Project > New µVision Project... and
follow the wizard to select your microcontroller, save location, and
project name.
2. Configure Project Settings:

● Once you have a project open, you need to configure various project
settings.
● Set the microcontroller or processor type and clock configuration in the
project settings.
● You may also need to configure memory settings, debug settings, and
other project-specific options.

3. Add Source Files:

● Add your source code files to the project.


● You can do this by right-clicking on the project in the Project window
and selecting Add New Item to Group... or by dragging and dropping
your source files into the project.

4. Write and Debug Code:

● Write or import your code into the source files.


● Use Keil's built-in code editor and debugger to write, debug, and test
your code.
● Set breakpoints, watch variables, and use the debugging tools as
needed to identify and fix issues.

5. Compile and Build:

● Build your project by clicking the "Build" button (or pressing F7).
● The compiler will compile your source code and generate an output
binary file (e.g., a .hex or .bin file) that contains the machine code.

6. Load the Program:

● Connect your target microcontroller or evaluation board to your PC via


a debugging interface like JTAG, SWD, or the appropriate hardware
debugger.
● In Keil, go to Debug > Start/Stop Debug Session to establish a
connection to the target hardware.
● If prompted, select your debugging probe and microcontroller model.
7. Program the Microcontroller:

● Load the compiled binary onto the microcontroller by clicking the


"Flash" button (or pressing Alt+F8).
● This step programs the microcontroller's flash memory with your
firmware.

8. Run the Program:

● Start running your program on the microcontroller by clicking the "Run"


button (or pressing F5).
● Your code will begin executing on the microcontroller, and you can
monitor its behavior using the debugging tools if necessary.

9. Test and Debug:

● Observe the behavior of your program on the target hardware.


● Use the debugging features in Keil MDK to identify and fix any runtime
issues or bugs.

10. Document and Release:

● Document your project, including any changes made during


debugging.
● If your project is intended for production, prepare it for release,
including creating release notes and packaging the final firmware for
distribution.

PROGRAM:

INTERFACE AN LED WITH ARM CORTEX M3:

#include<LPC17xx.h>
int main()
{
uint32_t i;
LPC_GPIO1->FIODIR|=1<<31;

SystemInit();
while(1)
{
LPC_GPIO1->FIOSET|=1<<31;//on
for(i=0;i<10000000;i++);//for delay
LPC_GPIO1->FIOCLR|=1<<31;//off
for(i=0;i<10000000;i++);

INTERFACE SWITCH AND LED ON ARM CORTEX M3:

# include <lpc17xx.h>
int getpinstate(int pin_no);
int main()
{
SystemInit();
LPC_GPIO2->FIODIR&=~(1<<10); // TO MAKE PIN AS INPUT P2.10
which is the switch
LPC_GPIO1->FIODIR|=(1<<31);// TO MAKE P1.31 OUTPUT

while(1)
{
if(getpinstate(10))
{
LPC_GPIO1->FIOSET |=1<<31;
}
else
{
LPC_GPIO1->FIOCLR |=1<<31;
}
}
}
int getpinstate(int pin_no)
{
int state= LPC_GPIO2->FIOPIN; // LPC_GPIO0-
>FIOPIN|=1<<10,TOCHECK WHETHER 10TH PIN STATUS IS 1 OR
NOT
int read=state&(1<<pin_no)?1:0;
return read;
}

INTERFACE JOYSTICK AND LED ON ARM CORTEX M3:

# include <lpc17xx.h>
int getpinstate(int pin_no);
int main()
{
SystemInit();
LPC_GPIO1->FIODIR&=~(1<<25); // TO MAKE PIN AS INPUT
P1.25(to make pin as input ANDing will be done;left shifting one 25
times
LPC_GPIO1->FIODIR|=(1<<29);// TO MAKE P1.29 OUTPUT,to
make pin as output OR ing will be done,left shifting 1 25 times and
ORing

while(1)
{
//GLCD_DisplayChar(2,2,2,'0');
if(getpinstate(25))//if pinno 25 is 1 in joystick
{
LPC_GPIO1->FIOSET |=1<<29;//then led at pin p1.29
should blink,since it is p1.29,lpc_gpio1
}

else
{
LPC_GPIO1->FIOCLR |=1<<29;//off ing it
}
}
}
int getpinstate(int pin_no)
{
int state= LPC_GPIO1->FIOPIN;
int read=state&(1<<pin_no)?1:0;//shifting 1 at pin_no times and and
ing with state and check whether it is 1 or 0
return read;
}
RESULT:

Flashed a sample blinky program into ARM Cortex M3 and got the necessary
output.
EXPERIMENT - 8
Familiarization with Ulink pro
Ulink Pro:
The ULINK Pro is a popular debugging and programming tool used in
embedded systems development, particularly with ARM-based
microcontrollers. It provides various features and capabilities for developers
to effectively debug, flash, and analyze code running on target
microcontrollers. Here's an overview of the theory and key features of the
ULINK Pro:

1. Debugging and Trace:

● Debugging: ULINK Pro allows developers to connect their


development environment (e.g., Keil MDK, IAR Embedded
Workbench) to a target microcontroller. It provides essential debugging
features like setting breakpoints, stepping through code, and
inspecting variable values during program execution.

● Trace: Some ULINK Pro variants offer trace capabilities, allowing you
to capture detailed program execution information. Trace data is
valuable for in-depth analysis of code behavior and performance
optimization.

2. Flash Programming:

● ULINK Pro can program the target microcontroller's flash memory with
compiled firmware.

● This is essential for transferring your application code to the


microcontroller for execution.

3. Debug Interfaces:

● ULINK Pro connects to the target microcontroller using various


hardware debugging interfaces, such as JTAG (Joint Test Action
Group), SWD (Serial Wire Debug), or cJTAG.
● These interfaces provide a standardized way to communicate with and
control the microcontroller's debugging features.

4. Real-Time Data Transfer:

● ULINK Pro supports real-time data transfer between the host computer
and the target microcontroller.

● This is crucial for monitoring variables and debugging in real time as


your program runs.

5. Advanced Debugging Features:

● ULINK Pro typically offers advanced debugging features like:


➢ Hardware Trace: For capturing trace data to analyze program
execution flow.
➢ Event Counters: To count specific events during program
execution.
➢ Performance Analysis: For profiling and optimizing code.
➢ Instruction Trace: To capture executed instructions for in-depth
analysis.

6. Compatibility:

● ULINK Pro is designed to work with a wide range of ARM-based


microcontrollers and is compatible with popular development
environments and software tools.

7. JTAG/SWD Debugging:

● ULINK Pro often supports both JTAG and SWD debugging protocols,
making it versatile and compatible with various ARM microcontrollers.

8. Debugging Software Integration:

● It seamlessly integrates with development environments like Keil MDK,


IAR Embedded Workbench, and others, offering a unified debugging
experience.

9. Code Optimization:
● While ULINK Pro itself doesn't perform code optimizations, it helps
developers identify and analyze code optimizations performed by the
compiler or processor.

● By inspecting generated assembly code and using profiling tools,


developers can understand how the code is optimized for execution
efficiency.

10. Real-time Interaction:

● ULINK Pro allows developers to interact with the target microcontroller


in real time.

● This is crucial for diagnosing and fixing issues during development and
testing.

AIM: To flash a sample program with a loop containing arithmetic operations.


SOFTWARE REQUIRED: Keil Mvision IDE

THEORY:

Arm cortex m3:

The ARM Cortex-M3 is a 32-bit microcontroller core designed for high-


performance embedded systems. It is part of ARM's Cortex-M family, which
is known for its energy efficiency, low power consumption, and real-time
performance. Here are some key features and characteristics of the ARM
Cortex-M3:

1. 32-bit RISC Architecture:

● The Cortex-M3 employs a 32-bit Reduced Instruction Set Computer


(RISC) architecture, which simplifies instruction decoding and
execution, resulting in efficient and predictable performance.

2. Harvard Architecture:

● It uses Harvard architecture, meaning that it has separate instruction


and data buses, allowing for parallel instruction fetch and data access.
3. Pipeline Architecture:

● The Cortex-M3 uses a three-stage pipeline, which includes instruction


fetch, decode/execute, and write-back stages.
● This pipeline helps to improve instruction throughput and performance.

4. Single Precision FPU (Floating-Point Unit):

● Some Cortex-M3 implementations include a single-precision FPU,


which allows for efficient floating-point arithmetic operations.
● However, not all Cortex-M3-based microcontrollers have this feature.

5. Memory Protection Unit (MPU):

● The Cortex-M3 often includes an optional MPU that enables memory


protection and access control for different parts of the memory,
enhancing security and stability.

6. Nested Vectored Interrupt Controller (NVIC):

● The NVIC is an integral part of the Cortex-M3 architecture and provides


efficient and low-latency interrupt handling.
● It supports prioritized interrupts and nested interrupt handling.

7. Low Power Consumption:

● The Cortex-M3 is designed to be energy-efficient and offers various


low-power modes, making it suitable for battery-powered and energy-
sensitive applications.

8. Clock Speeds:

● Different microcontrollers using the Cortex-M3 core can run at various


clock speeds, typically ranging from a few MHz to over 100 MHz,
depending on the specific implementation.

9. Wide Range of Peripherals:

● Cortex-M3-based microcontrollers come with a wide range of


integrated peripherals, including GPIO (General-Purpose
Input/Output), UARTs, SPI, I2C, timers, PWM controllers, and more,
making them versatile for various applications.

10. Development Ecosystem:

● ARM provides a robust development ecosystem for Cortex-M3


microcontrollers, including a variety of development boards, software
development tools (like Keil MDK and ARM Development Studio), and
an extensive software library known as CMSIS (Cortex Microcontroller
Software Interface Standard).

Cortex-M3-based microcontrollers are commonly used in a wide range of


applications, including automotive systems, industrial automation, consumer
electronics, medical devices, and IoT (Internet of Things) devices. They are
known for their real-time performance, which makes them suitable for
applications that require precise timing and control. As of my knowledge
cutoff date in September 2021, the Cortex-M3 core remains a popular choice
in the embedded systems industry.
PROCEDURE:

1. Open or Create a Project:

● Launch Keil MDK.


● You can either open an existing project or create a new one.
● To create a new project, go to Project > New µVision Project... and
follow the wizard to select your microcontroller, save location, and
project name.

2. Configure Project Settings:

● Once you have a project open, you need to configure various project
settings.
● Set the microcontroller or processor type and clock configuration in the
project settings.
● You may also need to configure memory settings, debug settings, and
other project-specific options.

3. Add Source Files:

● Add your source code files to the project.


● You can do this by right-clicking on the project in the Project window
and selecting Add New Item to Group... or by dragging and dropping
your source files into the project.

4. Write and Debug Code:

● Write or import your code into the source files.


● Use Keil's built-in code editor and debugger to write, debug, and test
your code.
● Set breakpoints, watch variables, and use the debugging tools as
needed to identify and fix issues.

5. Compile and Build:

● Build your project by clicking the "Build" button (or pressing F7).
● The compiler will compile your source code and generate an output
binary file (e.g., a .hex or .bin file) that contains the machine code.
6. Load the Program:

● Connect your target microcontroller or evaluation board to your PC via


a debugging interface like JTAG, SWD, or the appropriate hardware
debugger.
● In Keil, go to Debug > Start/Stop Debug Session to establish a
connection to the target hardware.
● If prompted, select your debugging probe and microcontroller model.

7. Program the Microcontroller:

● Load the compiled binary onto the microcontroller by clicking the


"Flash" button (or pressing Alt+F8).
● This step programs the microcontroller's flash memory with your
firmware.

8. Run the Program:

● Start running your program on the microcontroller by clicking the "Run"


button (or pressing F5).
● Your code will begin executing on the microcontroller, and you can
monitor its behavior using the debugging tools if necessary.

9. Test and Debug:

● Observe the behavior of your program on the target hardware.


● Use the debugging features in Keil MDK to identify and fix any runtime
issues or bugs.

10. Document and Release:

● Document your project, including any changes made during


debugging.
● If your project is intended for production, prepare it for release,
including creating release notes and packaging the final firmware for
distribution.

PROGRAM CODE:
#include<lpc17xx.h>
int main()
{
int i,sum=0;
for(i=0;i<=100;i++)
{
sum=sum+i;
}
}

RESULT:
To flash a sample program with a loop containing arithmetic operations.
EXPERIMENT - 9
Prime Numbers
AIM: To write a program to find the first 20 prime numbers and save it in an
array, run the program till N=20 and get the sum of all prime numbers.

SOFTWARE REQUIRED: Keil Mvision IDE

THEORY:

Arm cortex m3:

The ARM Cortex-M3 is a 32-bit microcontroller core designed for high-


performance embedded systems. It is part of ARM's Cortex-M family, which
is known for its energy efficiency, low power consumption, and real-time
performance. Here are some key features and characteristics of the ARM
Cortex-M3:

1. 32-bit RISC Architecture:

● The Cortex-M3 employs a 32-bit Reduced Instruction Set Computer


(RISC) architecture, which simplifies instruction decoding and
execution, resulting in efficient and predictable performance.

2. Harvard Architecture:

● It uses Harvard architecture, meaning that it has separate instruction


and data buses, allowing for parallel instruction fetch and data access.

3. Pipeline Architecture:

● The Cortex-M3 uses a three-stage pipeline, which includes instruction


fetch, decode/execute, and write-back stages.
● This pipeline helps to improve instruction throughput and performance.

4. Single Precision FPU (Floating-Point Unit):

● Some Cortex-M3 implementations include a single-precision FPU,


which allows for efficient floating-point arithmetic operations.
● However, not all Cortex-M3-based microcontrollers have this feature.

5. Memory Protection Unit (MPU):

● The Cortex-M3 often includes an optional MPU that enables memory


protection and access control for different parts of the memory,
enhancing security and stability.

6. Nested Vectored Interrupt Controller (NVIC):

● The NVIC is an integral part of the Cortex-M3 architecture and provides


efficient and low-latency interrupt handling.
● It supports prioritized interrupts and nested interrupt handling.

7. Low Power Consumption:

● The Cortex-M3 is designed to be energy-efficient and offers various


low-power modes, making it suitable for battery-powered and energy-
sensitive applications.

8. Clock Speeds:

● Different microcontrollers using the Cortex-M3 core can run at various


clock speeds, typically ranging from a few MHz to over 100 MHz,
depending on the specific implementation.

9. Wide Range of Peripherals:

● Cortex-M3-based microcontrollers come with a wide range of


integrated peripherals, including GPIO (General-Purpose
Input/Output), UARTs, SPI, I2C, timers, PWM controllers, and more,
making them versatile for various applications.

10. Development Ecosystem:

● ARM provides a robust development ecosystem for Cortex-M3


microcontrollers, including a variety of development boards, software
development tools (like Keil MDK and ARM Development Studio), and
an extensive software library known as CMSIS (Cortex Microcontroller
Software Interface Standard).
Cortex-M3-based microcontrollers are commonly used in a wide range of
applications, including automotive systems, industrial automation, consumer
electronics, medical devices, and IoT (Internet of Things) devices. They are
known for their real-time performance, which makes them suitable for
applications that require precise timing and control. As of my knowledge
cutoff date in September 2021, the Cortex-M3 core remains a popular choice
in the embedded systems industry.

PROCEDURE:

1. Open or Create a Project:

● Launch Keil MDK.


● You can either open an existing project or create a new one.
● To create a new project, go to Project > New µVision Project... and
follow the wizard to select your microcontroller, save location, and
project name.
2. Configure Project Settings:

● Once you have a project open, you need to configure various project
settings.
● Set the microcontroller or processor type and clock configuration in the
project settings.
● You may also need to configure memory settings, debug settings, and
other project-specific options.

3. Add Source Files:

● Add your source code files to the project.


● You can do this by right-clicking on the project in the Project window
and selecting Add New Item to Group... or by dragging and dropping
your source files into the project.

4. Write and Debug Code:

● Write or import your code into the source files.


● Use Keil's built-in code editor and debugger to write, debug, and test
your code.
● Set breakpoints, watch variables, and use the debugging tools as
needed to identify and fix issues.

5. Compile and Build:

● Build your project by clicking the "Build" button (or pressing F7).
● The compiler will compile your source code and generate an output
binary file (e.g., a .hex or .bin file) that contains the machine code.

6. Load the Program:

● Connect your target microcontroller or evaluation board to your PC via


a debugging interface like JTAG, SWD, or the appropriate hardware
debugger.
● In Keil, go to Debug > Start/Stop Debug Session to establish a
connection to the target hardware.
● If prompted, select your debugging probe and microcontroller model.

7. Program the Microcontroller:


● Load the compiled binary onto the microcontroller by clicking the
"Flash" button (or pressing Alt+F8).
● This step programs the microcontroller's flash memory with your
firmware.

8. Run the Program:

● Start running your program on the microcontroller by clicking the "Run"


button (or pressing F5).
● Your code will begin executing on the microcontroller, and you can
monitor its behavior using the debugging tools if necessary.

9. Test and Debug:

● Observe the behavior of your program on the target hardware.


● Use the debugging features in Keil MDK to identify and fix any runtime
issues or bugs.

10. Document and Release:

● Document your project, including any changes made during


debugging.
● If your project is intended for production, prepare it for release,
including creating release notes and packaging the final firmware for
distribution.

PROGRAM CODE

#include<lpc17xx.h>

int main()

int n,i=3,count,sum,c,a[20];

n=5;
sum = 0;
if(n>=1)
{

a[0]=2;
}

for(count = 2; count <= n ; i++)

{
//iteration to check c is prime or not

for (c=2;c < i; c++)

{
if(i%c ==0)

break;

}
if(c == i) // c is prime

a[count-1]=i;

count++;
for(j=0;j<=count;j++)

sum = sum + a[j];

}
}

return 0;
}
RESULT

wrote a program to find the first 20 prime numbers, saved them in an array,
ran the program till N=20, and got the sum of all prime numbers.

You might also like