Cbesd File

32
C BASED EMBEDDED SYSTEM DESIGN LAB FILE Submitted to: Submitted by:

Transcript of Cbesd File

Page 1: Cbesd File

C BASED EMBEDDED SYSTEM DESIGN

LAB FILE

Submitted to: Submitted by: Mr. Neeraj Gupta Shubham Bansal Faculty-ECE ECE(B)-8th sem

ASET A50105110011

Page 2: Cbesd File

Experiment 1

Aim: To study 8051 Microcontroller kit.Theory:

Pin Description

0(p0.0 to p0.7):It is 8-bit bi-directional I/O port. It is bit/ byte addressable. During external memory access, it functions as multiplexed data and low-order address bus AD0-AD7.

Port 1 (p1.0 to p1.7): It is 8-bit bi-directional I/O port. It is bit/ byte addressable. When logic '1' is written into port latch then it works as input mode. It functions as simply I/O port and it does not have any alternative function. 

Port 2 (p2.0 to p2.7): It is 8-bit bi-directional I/O port. It is bit/ byte addressable. During external memory access it functions as higher order address bus (A8-A15). 

Port 3(p3.0 to port 3.7):It is 8-bit I/O port. In an alternating function each pins can be used as a special function I/O pin. 

P3.0-RxD:It is an Input signal. Through this I/P signal microcontroller receives serial data of serial communication circuit.

Page 3: Cbesd File

P3.1-TxD:It is O/P signal of serial port. Through this signal data is transmitted. 

P3.2- (INT0):It is external hardware interrupt I/P signal. Through this user, programmer or peripheral interrupts to microcontroller. 

P3.3-(INT1):It is external hardware interrupt I/P signal. Through this user, programmer or peripheral interrupts to microcontroller.

P3.4- T0:It is I/P signal to internal timer-0 circuit. External clock pulses can connects to timer-0 through this I/P signal. 

P3.5-T1:It is I/P signal to internal timer-1 circuit. External clock pulses can connects to timer-1 through this I/P signal. 

P3.6-[WR(bar)]:It is active low write O/P control signal. During External RAM (Data memory) access it is generated by microcontroller. when [WR(bar)]=0, then performs write operation. 

P3.7-[RD(bar)]:It is active low read O/P control signal. During External RAM (Data memory) access it is generated by microcontroller. when [RD(bar)]=0, then performs read operation from external RAM. 

XTAL1 and XTAL2:These are two I/P line for on-chip oscillator and clock generator circuit. A resonant network as quartz crystal is connected between these two pin. 8051 microcontroller also drives from external clock, then XTAL2 is used to drive 8051 from external clock and XTAL1 should be grounded. 

[EA(bar)]/VPP:It is and active low I/P to 8051 microcontroller. when (EA)= 0, then 8051 microcontroller access from external program memory (ROM) only. When (EA) = 1, then it access internal and external program memories (ROMS). 

[PSEN(bar)]:It is active low O/P signal. It is used to enable external program memory (ROM). When [PSEN(bar)]= 0, then external program memory becomes enabled and microcontroller read content of external memory location. Therefore it is connected to (OE) of external ROM. It is activated twice every external ROM memory cycle. 

Page 4: Cbesd File

ALE:Address latch enable: It is active high O/P signal. When it goes high, external address latch becomes enabling and lower address of external memory (RAM or ROM) latched into it. Thus it separates A0-A7 address from AD0-AD7. It provides properly timed signal to latch lower byte address. The ALE is activated twice in every machine cycle. If external RAM & ROM is not accessed, then ALE is activated at constant rate of 1/6 oscillator frequency, which can be used as a clock pulses for driving external devices.

RESET: It is active high I/P signal. It should be maintained high for at least two machine cycle while oscillator is running then 8051 microcontroller resets.

Page 5: Cbesd File

Experiment 2

Aim: Implementation using flash MagicSoftware used: Flash Magic 4.18.1224

Theory:

NXP Semiconductors produce a range of Microcontrollers that feature both on-chip Flash memory and the ability to be reprogrammed using In-System Programming technology. Flash Magic is Windows software from the Embedded Systems Academy that allows easy access to all the ISP features provided by the devices. These features include:

· Erasing the Flash memory (individual blocks or the whole device)

· Programming the Flash memory

· Reading Flash memory

· Performing a blank check on a section of Flash memory

· Reading and writing the security bits

· Direct load of a new baud rate (high speed communications)

Under Windows, only one application may have access the COM Port at any one time, preventing other applications from using the COM Port. Flash Magic only obtains access to the selected COM Port when ISP operations are being performed. This means that other applications that need to use the COM Port, such as debugging tools, may be used while Flash Magic is loaded.Flash Magic provides a clear and simple user interface to these features and more as described below:

Page 6: Cbesd File

Burning the program in a microcontroller

1. Open the flash magic.2. Select microcontroller AT89C51.3. Set baud rate.4. Select com port.5. Now check the box ERASE ALL FLASH.6. Check the box VERIFY AFTER PROGRAMMING.7. Click on browse to browse program Hex file.8. Click START to start the burning process.

Page 7: Cbesd File

Experiment 3

Aim: Write a program to add two 8-bit numbers using microcontroller 8051.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224

Program:

Output: 55H 01010101

Page 8: Cbesd File

Experiment 4

Aim: Write a program to subtract two 8-bit numbers using microcontroller 8051.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224

Program:

Output: 10H 00010000

Page 9: Cbesd File

Experiment 5

Aim: Write a program to multiply two 8-bit numbers using microcontroller 8051.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224

Program:

Output: 15H 00010101

Page 10: Cbesd File

Experiment 6

Aim: Write a program to divide two 8-bit numbers using microcontroller 8051.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224

Program:

Output: 00000010

Page 11: Cbesd File

Experiment 7

Aim: Blinking of LED.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224

Program:

Output: Blinking LED

Page 12: Cbesd File

Experiment 8

Aim: Write a program for analog to digital conversion using microcontroller 8051.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:

#include <P89C51RD2.h>#include <stdio.h>#include "fundef.h"#include "defines.h"#include "vars.h"#include "lcd16x2.h"#include "uart.h"void putchar (char putthis){lcd16x2_write (putthis,0x82) ;}void small_delay(void){unsigned char l_temp ;for (l_temp = 0; l_temp < 20; l_temp++) ;}void main(void){unsigned int delay ;float voltage = 0 ;ADC_CS = 0 ;EA = 0 ;lcd16x2_init() ;lcd16x2_clear() ;lcd16x2_cursor(0x80) ;lcd16x2_write_string ("Channel 0 ") ;lcd16x2_cursor (0xC0) ;P3 = 0x02 ;

while (1){DACDATA = 0xFF ;ADCDATA = 0x01 ;P3 = 0x10 ; small_delay() ;P3 |= 0x40 ;small_delay() ;P3 &= 0xE2 ;small_delay() ;

Page 13: Cbesd File

P3 &= 0x02 ;while (ADC_EOC != 1) ;small_delay() ;ADCDATA = 0xFF ;ADCDATA = 0xFF ;ADC_CS = 1 ;small_delay() ;adc_data = ADCDATA ;small_delay() ;adc_data = ADCDATA ;ADC_CS = 0 ;voltage = (float)adc_data ;voltage = (voltage * 5.0) / 256.0 ;lcd16x2_cursor(0xC0) ;printf_fast_f (" ") ;lcd16x2_cursor(0xC0) ;printf_fast_f ("Hex = %2x ",adc_data) ;//,(unsigned int)adc_data) ;lcd16x2_cursor(0xC0) ;P3 = 0x02 ;for (delay = 0; delay < 30000;delay++) ;}}

Page 14: Cbesd File

Experiment 9

Aim: Digital to Analog Converter.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:

#include <P89C51RD2.h>#include <stdio.h>#include "fundef.h"#include "defines.h"#include "vars.h"#include "lcd16x2.h"#include "uart.h"#include "misc.h"#include "isr.h"

void putchar (char putthis){ uart_send_char(putthis) ;}unsigned char getkey1(void){unsigned char l_temp ;unsigned int delay ;KBDPORT = 0xFF ;if ((KBDPORT & 0xFF) == 0xFF)return (0) ;l_temp = KBDPORT & 0xFF ;for (delay = 0; delay < 2000; delay++) ;if ((KBDPORT & 0xFF) != l_temp)return(0) ;wait_release:while ((KBDPORT & 0xFF) != 0xFF) ;for (delay = 0; delay < 1000; delay++) ;if ((KBDPORT & 0xFF) == 0xFF) return (l_temp) ;goto wait_release ;}void main (void){unsigned char key ;lcd16x2_init() ;lcd16x2_cursor(0x80) ;lcd16x2_write_string("Waveform") ;lcd16x2_cursor(0xC0) ;lcd16x2_write_string("Generator") ;init_timer0() ;EA = 1 ;

Page 15: Cbesd File

while(1){key = 0 ;while ((key = getkey1()) == 0) ;if (key == 0xFE){EA = 0 ;waveform = 1 ;dacdata = 0 ;direction = 1 ;EA = 1 ;}if (key == 0xFD){EA = 0 ;waveform = 2 ;dacdata = 0 ;direction = 1 ;EA = 1 ;}if (key == 0xFB){EA = 0 ;dacdata = 0 ;waveform = 0 ;sineptr = sinelookup ;EA = 1 ;}if (key == 0xF7){EA = 0 ;waveform = 3 ;dacdata = 0 ;EA = 1 ;}if (key == 0xEF){if (sampledelay > 1)sampledelay-- ;}if (key == 0xDF){if (sampledelay < 0xff)sampledelay++ ;}}}

Page 16: Cbesd File

Experiment 10

Aim: Write a program to generate a square wave.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:

CPU"8051.TBL"HOF"INT8"P0: EQU 80HP1: EQU 90HP2: EQU 0A0HP3: EQU 0B0H R0: EQU 00H A: EQU 0E0HB: EQU 0F0HLCDRS: EQU 0B5HLCDE: EQU 0A7HPort34: EQU 0B4H Count: EQU 020h Counthi: EQU 021hORG 0000HSTRT:

MOV P2,#00H MOV P1,#00H MOV P3,#00H MOV P0,#00H

OUTPUT:MOV P0, #00HCLR Port34CLR LCDRSLCALL DELAYMOV P0, #0FFHSETB Port34CLR LCDRSLCALL DELAY

HERE: NOPSJMP OUTPUT

DELAY:MOV R1,#0FFH

DLY1: MOV R2,#0FHH1: DJNZ R2, H1

DJNZ R1,DLY1 RET END

Page 17: Cbesd File

Experiment 11

Aim: Write a program to operate a Stepper AC motorSoftware used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:CPU"8051.TBL"HOF"INT8"P0: EQU 80HP1: EQU 90HP2: EQU 0A0HP3: EQU 0B0H R0: EQU 00H R1: EQU 01HR2: EQU 02HR3: EQU 03HR4: EQU 04HR5: EQU 05HR6: EQU 06HR7: EQU 07HDPL: EQU 82HDPH: EQU 83HLCDRS: EQU 0B5HLCDE: EQU 0A7HLCDRW: EQU 0B4HDIR: EQU 090H Count: EQU 020h Counthi: EQU 021hORG 0030HMOV P0,#0hSTART:MOV A,P0JNB DIR,REVMOV P0,#00hACALL DELAYMOV P0,#02hACALL DELAYMOV P0,#06hACALL DELAYMOV P0,#04hACALL DELAYMOV P0,#0ChACALL DELAYMOV P0,#08hACALL DELAYMOV P0,#09h

Page 18: Cbesd File

ACALL DELAYMOV P0,#01hACALL DELAYMOV P0,#03hACALL DELAYSJMP STARTREV:MOV P0,#03hACALL DELAYMOV P0,#01hACALL DELAYMOV P0,#09hACALL DELAYMOV P0,#08hACALL DELAYMOV P0,#0ChACALL DELAYMOV P0,#04hACALL DELAYMOV P0,#06hACALL DELAYMOV P0,#02hACALL DELAYMOV P0,#00hACALL DELAYSJMP STARTDELAY:MOV R2,#100DH1: MOV R3,#255DH2: DJNZ R3,H2DJNZ R2,H1RETEND

Page 19: Cbesd File

Experiment 12

Aim: Write a program to operate a Stepper DC motorSoftware used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:#include <P89C51RD2.h>#include <stdio.h>#include "fundef.h"#include "defines.h"#include "vars.h"#include "lcd16x2.h"#include "uart.h"#include "isr.h"#include "misc.h"void main (void){lcd16x2_init() ;lcd16x2_clear() ;init_timer0() ;init_pca_pwm_cex0() ;EA = 1 ;stepper:P0 &= 0xF0 ;lcd16x2_cursor(0x80) ;printf ("Stepper Drive") ;lcd16x2_cursor (0xC0) ;while (1){stepno = 0 ;key_release = 0 ;while (key_release != 1){if (half_step == 0)P0 = stepper_seq[stepno] ;elseP0 = hstepper_seq[stepno] ;gptimeout = step_delay ;gptimeout_done = 0 ;while (gptimeout_done != 1) ;stepno += direction ;if (half_step == 0)stepno &= 0x03 ;elsestepno &= 0x07 ;printf ("Step %u",(unsigned int)stepno) ;

Page 20: Cbesd File

lcd16x2_cursor(0xC0) ;}if (key == STEP_TOGGLE)half_step ^= 1 ;if (key == DIRECTION)direction ^= 0xfe ;if ((key == CFAST)||(key == FFAST))if (key == FFAST)if (step_delay > 20)step_delay -= 10 ;}else{if (step_delay > 200)step_delay -= 50 ;}}if ((key == FSLOW)||(key == CSLOW)){if (SBUF == FSLOW){if (step_delay < 2000)step_delay += 10 ; }else{if (step_delay < 1500)step_delay += 50 ;}}if (key == DCSTEPPER){P0 &= 0xF0 ;goto dc_motor_run ;}}dc_motor_run:lcd16x2_clear() ;lcd16x2_cursor(0x80) ;printf ("DC Drive") ;lcd16x2_cursor (0xC0) ;printf_fast_f ("%f",100.0-(float)pwm_duty * 100.0/256) ;DCFORWARD = 0 ;DCREVERSE = 1 ;while (1){key_release = 0 ;while (key_release != 1) ;if (key == DIRECTION){

Page 21: Cbesd File

DCFORWARD = 1 ;DCREVERSE = 1 ;if (dc_direction == 0)DCREVERSE = 0 ;elseDCFORWARD = 0 ;dc_direction ^= 1 ;}if ((key == CFAST)||(key == FFAST)){if (key == FFAST){if (pwm_duty > 0)pwm_duty-- ;}else{if (pwm_duty > 10)pwm_duty -= 10 ;}CCAP0H = pwm_duty ;lcd16x2_cursor (0xC0) ;printf_fast_f ("%f",(float)(256- pwm_duty) * 100.0/256) ;}if ((key == FSLOW)||(key == CSLOW)){if (key == FSLOW){if (pwm_duty < 0XFF)pwm_duty++ ;}else{if (pwm_duty < 0XF5)pwm_duty += 10 ;}CCAP0H = pwm_duty ;lcd16x2_cursor (0xC0) ;printf_fast_f ("%f",(float)(256 - pwm_duty) * 100.0/256) ;}if (key == DCSTEPPER){DCREVERSE = 1 ;DCFORWARD = 1 ;lcd16x2_clear() ;goto stepper ;} } }

Page 22: Cbesd File

Experiment 13

Aim: Write a program to generate Traffic light sequence.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:

MOV P2,#0 H MOV P1,#0 H MOV P3,#0 H MOV P0,#0 H STRT: MOV A,P0MOV P1,ASJMP STRT

Page 23: Cbesd File

Experiment 14

Aim: Write a program to operate BCD seven segment display.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:

CPU"8051.TBL"HOF"INT8"P0: EQU 80HP1: EQU 90HP2: EQU 0A0HP3: EQU 0B0H R0: EQU 00H R1: EQU 01HR2: EQU 02HR3: EQU 03HR4: EQU 04HR5: EQU 05HR6: EQU 06HR7: EQU 07HDPL: EQU 82HDPH: EQU 83HORG 0000HSTRT: MOV P2,#7FHMOV P1,#0FFH MOV P3,#0FFH MOV P0,#0FFH MOV P0,#88HMOV P3,#0FEHACALL DLYMOV P0,#0ABHMOV P3,#0FDHACALL DLYMOV P0,#92HMOV P3,#0FBHACALL DLYMOV P0,#89HMOV P3,#0F7HACALL DLYMOV P0,#0C1HMOV P3,#0EFHACALL DLYMOV P0,#0AAHMOV P3,#0DFH

Page 24: Cbesd File

ACALL DLYMOV P0,#88HMOV P3,#0BFHACALL DLYMOV P0,#0ABHMOV P3,#07FHACALL DLYSJMP STRTDLY: MOV R2,#0FFHUP2: MOV R3,#0FFHUP1: MOV R4,#05HHERE: DJNZ R2,HEREDJNZ R3,UP1DJNZ R2,UP2RETEND

Page 25: Cbesd File

Experiment 15

Aim: Write a program to operate both seven segment and group of leds using keys.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:#include <P89C51RD2.h>#include <stdio.h>#include "fundef.h"#include "defines.h"#include "vars.h"#include "lcd16x2.h"#include "misc.h"#include "isr.h"void main (void){init_timer0() ;lcd16x2_init() ;lcd16x2_clear() ;lcd16x2_cursor(0x80) ;lcd16x2_write_string ("Key") ;lcd16x2_cursor(0xC0) ;EA = 1 ;out_dev = 0 ;keyscandemo = 1 ; while (1){DISPDATA7 = 0xFF ;kbdflush() ;while (key_press != 1) ;EA = 0 ;lcd16x2_cursor(0xC0) ;printf (" ") ;lcd16x2_cursor(0xC0) ;printf (" %c ",key, (unsigned int)keycol,(unsigned int)keyrow) ;EA = 1 ;if (key == 'F'){gptimeout = 4000 ;gptimeout_done = 0 ;while (gptimeout_done != 1) ;break ;}}out_dev = 1 ;keyscandemo = 2 ;

Page 26: Cbesd File

key = 0 ;while (key != 5){EA = 0 ;DISPMUX7 = 0xF7 ;dispselect = 0 ;disp_pointer = 0 ;printf ("SHUBHAM") ;EA = 1 ;display_update = 1 ;while (display_update != 0) ;gptimeout = 3500 ;gptimeout_done = 0 ;while (gptimeout_done != 1) ;key++ ;}keyscandemo = 3 ;key = 0 ;while (1){for (disp_pointer = 0 ;disp_pointer < 8;disp_pointer++)disp_buff[disp_pointer] = disp_matrix[disp_pointer] ;display_update = 1 ;while (display_update != 0) ;}}

Page 27: Cbesd File

Experiment 16

Aim: Write a program to display letter ‘A’.Software used: Keil uvision 2.14 and Flash Magic 4.18.1224Program:CPU"8051.TBL"HOF"INT8"P0: EQU 80HP1: EQU 90HP2: EQU 0A0HP3: EQU 0B0H R0: EQU 00H R1: EQU 01HR2: EQU 02HR3: EQU 03HR4: EQU 04HR5: EQU 05HR6: EQU 06HR7: EQU 07HDPL: EQU 82HDPH: EQU 83HORG 0000HSTRT: MOV P2,#7FHMOV P1,#0FFH MOV P3,#0FFH MOV P0,#0FFH MOV P0,#7EHMOV P3,#0FEHACALL DLYMOV P0,#7EHMOV P3,#0FDHACALL DLYMOV P0,#7EHMOV P3,#0FBHACALL DLYMOV P0,#0HMOV P3,#0F7HACALL DLYMOV P0,#7EHMOV P3,#0EFHACALL DLYMOV P0,#07EHMOV P3,#0DFHACALL DLY

Page 28: Cbesd File

MOV P0,#0BDHMOV P3,#0BFHACALL DLYMOV P0,#0C3HMOV P3,#07FHACALL DLYSJMP STRTDLY: MOV R2,#0FFHUP2: MOV R3,#0FFHUP1: MOV R4,#05HHERE: DJNZ R2,HEREDJNZ R3,UP1DJNZ R2,UP2RETEND