You are on page 1of 18

EE1672-EMBEDDED SYSTEMS (INTEGRATED LAB)

PIC Based Programs:


1. Toggle pins and make an LED glow.
//Program to Blink LEDs
//Port D #8 bits are toggled
//----------------Connection--------------
//Port D0 -> LED1, D1->LED2... D7->LED8
//----------------------------------------
#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

void main()
{
while(1)
{
output_d(0xff); //PORTD all lines are high
delay_ms(1000); //wait for 1sec
output_d(0); //PORTD all lines are low
delay_ms(1000); //wait for 1Sec.
}
}
//-----------------------------------------------------
2. Buzzer alarm
//---------------------------------------------------------
// Program to RUN DC Motor
//---------------------------------------------------------
#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

#define EN input(PIN_B0)

#define BUZZER PIN_C1

void main()
{
while(1)
{
if(EN)
{
output_high(BUZZER);
}
else
{
output_low(BUZZER);
}
}
}
3. 4 x 4 keypad matrix and display a key
//Program to Read 4X4 Key Matrix
//-----KEY Matrix---------------------
// RB0 -> SC0 - Scan Lines
// RB1 -> SC1
// RB2 -> SC2
// RB3 -> SC3
// RB4 -> RT0 - Return Lines
// RB5 -> RT1
// RB6 -> RT2
// RB7 -> RT3
// ------------------------------------------------------

#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

//Key Board Scanning time = 2msec


// --------------------------------------------------------
//Timer 0 Internal clock = Fosc / 4 = 1Mhz (Fosc=4Mhz)
//Timer 0 Internal Clock after Pre-scaler = 3/32 Mhz = 31.25Khz (Pre-Scale 1:32)
//One Clock Interval = 32 microsec (one count increment time)
//Time Interval Required = 2msec = 2000 microsec
//Counts for 2msec = 2000/32 = 62.5
//Count to be loaded in timer0 = 256 - 62.5 = 193
#define INITIAL_COUNT 193
//------------------------------------------------------------
//Scan Lines
#define SC0 PIN_B0
#define SC1 PIN_B1
#define SC2 PIN_B2
#define SC3 PIN_B3
//Return Lines
#define RT0 input(PIN_B4)
#define RT1 input(PIN_B5)
#define RT2 input(PIN_B6)
#define RT3 input(PIN_B7)

//7 Segment Data for CA Display


int8 const seg_code[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67};
//This Array holds the value to be displayed
int8 disp[4];
int8 scan_ptr, key_val;
//------------------------------------------------------------
// This interrupt Service Routine
// is automatically called for every 2msec.
#INT_TIMER0
void timer0_isr() //Display Scanning Logic
{
set_timer0(INITIAL_COUNT); //Load Initial Count to Timer1

switch(scan_ptr)
{
case 0: //Select SC0
output_d(0); //A-G OFF, blank while switching
output_low(SC0); output_high(SC1); output_high(SC2); output_high(SC3);
output_d(seg_code[ disp[0] ]);
break;
case 1: //Select SC1
output_d(0); //A-G OFF, blank while switching
output_high(SC0); output_low(SC1); output_high(SC2); output_high(SC3);
output_d(seg_code[ disp[1] ]);
break;
case 2: //Select SC2
output_d(0); //A-G OFF, blank while switching
output_high(SC0); output_high(SC1); output_low(SC2); output_high(SC3);
output_d(seg_code[ disp[2] ]);
break;
default: //Select SC3
output_d(0); //A-G OFF, blank while switching
output_high(SC0); output_high(SC1); output_high(SC2); output_low(SC3);
output_d(seg_code[ disp[3] ]);
break;
}

//Check Key Press


if(!RT0) key_val = (scan_ptr*4)+1;
else if(!RT1) key_val = (scan_ptr*4)+2;
else if(!RT2) key_val = (scan_ptr*4)+3;
else if(!RT3) key_val = (scan_ptr*4)+4;

scan_ptr++;
if(scan_ptr==4) scan_ptr=0;

}
/*--------------------------------------------------------*/
void main()
{
int8 co;
//Configure Timer 0 for 2msec Overflow rate
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_32);
enable_interrupts(INT_TIMER0); //Enable Timer1 Interrupt
enable_interrupts(GLOBAL); //Enable Glonal Interrupt
set_timer0(INITIAL_COUNT); //Load Initial Count to Timer1
//----------------------------------------------------------

scan_ptr=0; key_val=0;
//Set initial value in array
for(co=0; co<4; co++) disp[co]=0; //Fill "0" in array

while(1)
{
disp[3] = key_val%10;
disp[2] = key_val/10;
}
}
/*--------------------------------------------------------*/
4. Seven-segment Display
//Program to display value in mux.7Seg.Display
//Display Scanning using timer Scan time = 2msec
//Timer 0 is used to generate 2msec delay interrupt

//------------Connection-----------------------------------
// Port D => 7Seg. A to G & Dp (i.e. D0 -> A, D1 -> B ...)
// DS1 -> RC0
// DS2 -> RC1
// DS3 -> RC2
// DS4 -> RC3
// --------------------------------------------------------
#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

#define DS1 PIN_B0


#define DS2 PIN_B1
#define DS3 PIN_B2
#define DS4 PIN_B3

// --------------------------------------------------------
//Timer 0 Internal clock = Fosc / 4 = 1Mhz (Fosc=4Mhz)
//Timer 0 Internal Clock after Pre-scaler = 3/32 Mhz = 31.25Khz (Pre-Scale 1:32)
//One Clock Interval = 32 microsec (one count increment time)
//Time Interval Required = 2msec = 2000 microsec
//Counts for 2msec = 2000/32 = 62.5
//Count to be loaded in timer0 = 256 - 62.5 = 193
#define INITIAL_COUNT 193
//------------------------------------------------------------
//7 Segment Data for CA Display
int8 const seg_code[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67};

//This Array holds the value to be displayed


int8 disp[4];
int8 scan_ptr;

// This interrupt Service Routine


// is automatically called for every 2msec.
#INT_TIMER0
void timer0_isr() //Display Scanning Logic
{
set_timer0(INITIAL_COUNT); //Load Initial Count to Timer1

switch(scan_ptr)
{
case 0: //Select DS1
output_d(0); //A-G OFF, blank while switching
output_low(DS1); output_high(DS2); output_high(DS3); output_high(DS4);
output_d(seg_code[ disp[0] ]);
scan_ptr=1;
break;
case 1: //Select DS2
output_d(0); //A-G OFF, blank while switching
output_high(DS1); output_low(DS2); output_high(DS3); output_high(DS4);
output_d(seg_code[ disp[1] ]);
scan_ptr=2;
break;
case 2: //Select DS3
output_d(0); //A-G OFF, blank while switching
output_high(DS1); output_high(DS2); output_low(DS3); output_high(DS4);
output_d(seg_code[ disp[2] ]);
scan_ptr=3;
break;
default: //Select DS4
output_d(0); //A-G OFF, blank while switching
output_high(DS1); output_high(DS2); output_high(DS3); output_low(DS4);
output_d(seg_code[ disp[3] ]);
scan_ptr=0;
break;
}
}
/*--------------------------------------------------------*/
void main()
{
int8 co;

//Configure Timer 0 for 2msec Overflow rate


setup_timer_0(RTCC_INTERNAL | RTCC_DIV_32);
enable_interrupts(INT_TIMER0); //Enable Timer1 Interrupt
enable_interrupts(GLOBAL); //Enable Glonal Interrupt
set_timer0(INITIAL_COUNT); //Load Initial Count to Timer1
//----------------------------------------------------------
scan_ptr=0;
//Set initial value in array
for(co=0; co<4; co++) disp[co]=co+1; //Fill "1234" in array
while(1);
}
/*--------------------------------------------------------*/
5. Analog-to-Digital conversion
//Program to read ADC & Display adc count in LCD
//------------Connection---------------------------------
//Program to display String in LCD (4bit Interface)
//------------Connection---------------------------------
//--LCD Data Lines----------
// LD0 to LD3 - No Connection
// LD4 -> RB4
// LD5 -> RB5
// LD6 -> RB6
// LD7 -> RB7
//----LCD Cotrol Signals----
// LRS -> RB1
// LRW -> RB2
// LEN -> RC0
// ----------------------------------------------------
// POT => AN0 (RA0)
// ----------------------------------------------------
#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

//LCD Driver
#include "flex_lcd.c"

void main()
{ int16 adc_value;

lcd_init(); //Initialize LCD Module


setup_adc_ports(sAN0); //Config ADC inputs
setup_adc(ADC_CLOCK_INTERNAL); //Setup ADC Clock
set_adc_channel(0); //Select ADC Channel 0
delay_us(20);

while(1)
{
adc_value = read_adc(); //Read ADC
printf(lcd_putc, "\fADC = %lu ",adc_value); //Print ADc value
delay_ms(1000);
}
}
/*------------------------------------------------------*/

6. Digital-to-Analog conversion
7. Generation of a PWM signal
//Program to Generate PWM output
//PWM ON Time controlled by ADC input

#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

#define DBDLY 100

//LCD Driver
#include "flex_lcd.c"

void main()
{
int16 adc_value, co;

//************ PWM Settings ***************************


setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM

// The cycle time will be (1/clock)*4*t2div*(period+1)


// In this program clock=4000000 and period=127 (below)
// (1/4000000)*4*1*128 = 128 uS or 7.8 khz
setup_timer_2(T2_DIV_BY_1, 127, 1);

co=0;
set_pwm1_duty(co); //Set Controller Power - PWM Ton 0-500

lcd_init(); //Initialize LCD Module


setup_adc_ports(sAN0); //Config ADC inputs
setup_adc(ADC_CLOCK_INTERNAL); //Setup ADC Clock
set_adc_channel(0); //Select ADC Channel 0
delay_us(20);

printf(lcd_putc, "\fPWM GENERATION");

do{

adc_value = read_adc(); //Read ADC

co = adc_value;

set_pwm1_duty(co); //Set Controller Power - PWM Ton 0-500

delay_ms(DBDLY); //Sampling delay 0.5 Sec

}while(1);

}
/*------------------------------------------------------*/
8. a) Interface a Stepper motor
//---------------------------------------------------------
// Program to RUN Stepper motor
//---------------------------------------------------------
#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

#define DIR input(PIN_B0) //Direction bit

void main(void);
void delay(unsigned int dly); /* Value "dly" in msec. */
void mov_clk();
void mov_aclk();

int8 tptr;
int8 step_table[]={ 0x3,0x9,0xc,0x6 };
int8 sdat;

void main()
{
tptr=0;

while(1)
{ if(DIR) mov_clk();
else mov_aclk();
}
}
/* Program Rotates Motor Clock Wise */
void mov_clk()
{
sdat = step_table[tptr++];
tptr = tptr & 3;
output_d(sdat);
delay_ms(5);
}
/*-----------------------------------------*/
/* Program Rotates Motor Counter Clock Wise */
void mov_aclk()
{
sdat = step_table[tptr--];
tptr = tptr & 3;
output_d(sdat);
delay_ms(5);
}
/*-----------------------------------------*/

8. b) Interface a DC motor
//---------------------------------------------------------
// Program to RUN DC Motor
//---------------------------------------------------------
#include <16F887.H>
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

#define EN input(PIN_B0)
#define DIR input(PIN_B1) //Direction bit
#define M1 PIN_D0
#define M2 PIN_D1
void main()
{
while(1)
{
if(!EN)
{
output_low(M1);
output_low(M2);
}
else if(DIR)
{
output_low(M1);
output_high(M2);
}
else
{
output_high(M1);
output_low(M2);
}
}
}
9. Interfacing a temperature sensor
//Program to read ADC, Calculate Temperature & Display it on LCD
//------------Connection----------------------------------------
//Program to display String in LCD (4bit Interface)
//------------Connection----------------------------------------
//--LCD Data Lines----------
// D0 to LD3 - No Connection
// D4 -> RB4
// D5 -> RB5
// D6 -> RB6
// D7 -> RB7
//----LCD Control Signals----
// RS -> RB1
// RW -> RB2
// EN -> RC0
// ----------------------------------------------------
// LM35 => AN1 (RA1)
// ----------------------------------------------------
#include <16F887.H>
#device adc=10
#fuses INTRC_IO, NOWDT, PUT, MCLR, NOCPD, NOPROTECT, NOBROWNOUT,
NOLVP, NODEBUG, NOFCMEN, NOIESO, NOWRT
#use delay (clock=4000000) // 4MHz Internal clock

//LCD Driver
#include "flex_lcd.c"

void main()
{ int16 adc_value;
float temp;
lcd_init(); //Initialize LCD Module

setup_adc_ports(sAN1); //Config ADC inputs


setup_adc(ADC_CLOCK_INTERNAL); //Setup ADC Clock
set_adc_channel(1); //Select ADC Channel 0
delay_us(20);

while(TRUE)
{
adc_value = read_adc(); //Read ADC
temp = 0.4888 * (float)adc_value;
adc_value = (int16) temp;
printf(lcd_putc, "\fTemp = %lu ",adc_value); //Print ADc value
delay_ms(1000);
}
}
/*------------------------------------------------------*/

You might also like