Ee1672-Embedded Systems (Integrated Lab)
Ee1672-Embedded Systems (Integrated Lab)
Ee1672-Embedded Systems (Integrated Lab)
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)
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
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;
}
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
// --------------------------------------------------------
//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};
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;
//LCD Driver
#include "flex_lcd.c"
void main()
{ int16 adc_value;
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
//LCD Driver
#include "flex_lcd.c"
void main()
{
int16 adc_value, co;
co=0;
set_pwm1_duty(co); //Set Controller Power - PWM Ton 0-500
do{
co = adc_value;
}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
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
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);
}
}
/*------------------------------------------------------*/