Sunteți pe pagina 1din 15

Q1. Generate the following delays using Timer0 and simulate them in KEIL only.

Dont use For Loop. 1. 10ms 2. 25ms 3. 50ms

1. 10ms: #include <REGX51.H> void Delay10ms(); sbit LED_RED = P1^0; sbit LED_YELLOW = P1^1; sbit LED_GREEN = P1^2; void main() {LED_RED = 0; LED_YELLOW = 0; LED_GREEN = 0; while(1) {LED_RED = 1; Delay10ms(); LED_RED = 0; LED_YELLOW = 1; Delay10ms(); LED_YELLOW = 0; LED_GREEN = 1; Delay10ms(); LED_GREEN = 0; }}

void Delay10ms() {TMOD = 0x01; TL0 = 0xFF; TH0 = 0xDB; TR0=1; while(TF0==0); TR0=0; TF0=0;} Output:

2. 25ms: Funcation to generate 25 ms delay remaning code is same as above : void Delay25ms() { TMOD = 0x01; TL0 = 0xFF; TH0 = 0xA5; TR0=1; while(TF0==0); TR0=0; TF0=0; } Output (Before):

Output (After):

3. 50ms: Funcation to generate 50 ms delay remaning code is same as above : void Delay50ms() { TMOD = 0x01; TL0 = 0xFD; TH0 = 0x4B; TR0=1; while(TF0==0); TR0=0; TF0=0; }

Output (Before):

Output (After):

Q2. Generate the same delays (as in Q1) using Timer1 and simulate then in KEIL only. Dont use For Loops. 10ms Timer1: Funcation to generate 10 ms delay remaning code is same as above :

void _10_ms_Delay() {TMOD = 0x10; TL1 = 0xFF; TH1 = 0xDB; TR1=1; while(TF1==0); TR1=0; TF1=0;} Output (Before):

Output (After):

25ms Timer1: Funcation to generate 25 ms delay remaning code is same as above : void Delay25ms() {TMOD = 0x10; TL1 = 0xFF; TH1 = 0xA5; TR1=1; while(TF1==0); TR1=0; TF1=0;} Output (Before):

Output (After):

50ms Timer1: Funcation to generate 50 ms delay remaning code is same as above :

void Delay50ms() {TMOD = 0x10; TL1 = 0xFD; TH1 = 0x4B; TR1=1; while(TF1==0); TR1=0; TF1=0;} Output (Before):

Output (After):

Q3. Generate a Delay of 10ms using Timer0 and 25ms using Timer1 in the same program. You can use any settings as you want. But you are not allowed to change the settings of TMOD Register once you configure it. Toggle any pin of your choice after 35ms Delay. void main() { LED_RED = 0; while(1) { LED_RED=1;

Delay10ms(); Delay25ms(); LED_RED=0; Delay10ms(); Delay25ms(); } } Output (Before):

Output (After):

Q3. Use the serial port to transmit My name is yourname to the Virtual Terminal in Proteus after every sec. #include <REGX51.H> void serial_init(); void serial_trans(unsigned char x);

void setup_interrupt(); int j=0; char a; void main() { char str_[19]="My name is yourname"; setup_interrupt(); serial_init(); while(1) {a=str_[j++]; serial_trans(a); if(j==20) { j=0; serial_trans('\r'); one_second_delay(); }}} void serial_init() {TMOD=0x20; TH1=0xFD; SCON=0x50; TR1=1; } void serial_trans(unsigned char x) {if(x=='_')SBUF=13;//carriage return SBUF=x; while(TI==0); TI=0;} void setup_interrupt()

{EA=1; IT1=1;}

Q.4 Toggle P2.1 pin using interrupt of Timer0 8bit Auto Reload setting to generate a frequency of 5 KHz.

#include <REGX51.H> sbit wave=P2^1;

void setup_interrupt(); void timer_0() interrupt 1 {wave=~wave;} void main() {wave=1; setup_interrupt(); TMOD=0x02; TH0=0x48; TR0=1; while(1) { while(TI==0); TI=0;}} void setup_interrupt() {EA=1; ET0=1;}

Q.5 Toggle P2.1 pin using interrupt of Timer1 16bit setting to generate a delay of 10ms. #include <REGX51.H>

sbit wave=P2^1; void setup_interrupt(); void timer_1() interrupt 3 { wave=~wave;} void main() {wave=1; setup_interrupt(); TMOD=0x10; while(1) { TH1=0xDB; TL1=0xFF; TR1=1; while(TI==0); TI=0;TR1=0;}} void setup_interrupt() {EA=1; IT1=1;}

Q.6 Use External Interrupt 0 (INT0) with Edge Detection settings and toggle P2.1 in its subroutine. Take a Pulse generator in Proteus and connect it to INT0 pin. Use different frequency of the source and check if the toggling occurs at the same rate as of the Pulse Generator. #include <REGX51.H> sbit CLK=P3^2; sbit wave=P2^1; void setup_interrupt(); void ex_interrupt_0() interrupt 0 {wave=~wave;} void main() {wave=1; CLK=1; setup_interrupt(); while(1);} void setup_interrupt() {EA=1; EX0=1; } Output:

Q.7 Design a Frequency Meter with the same specification mentioned in the Lab.

#include <REGX51.H> # include<stdio.h> void serial_init(); void serial_trans(unsigned char x); void setup_interrupt(); void one_second_delay(); unsigned int counter=0; unsigned int s=0; void ex_interrupt_0() interrupt 0 {counter++;} int j=0; char a; void main() { char str_[8]; setup_interrupt(); serial_init(); while(1) { counter=0; one_second_delay(); s=counter; sprintf(str_,"%u\0",s); while(str_[j]!='\0'){

a=str_[j++]; serial_trans(a);} j=0; serial_trans('\r');}} void serial_init() {TMOD=0x21; //8 bit auto-initialize register//timer0 16 bit timer TH1=0xFD; //buad rate 9600 SCON=0x50; //mode 8 bit timer,1 stop bit,receive enable TR1=1; //run timer1} void serial_trans(unsigned char x) {SBUF=x; while(TI==0); TI=0;} void setup_interrupt() {EA=1; IT1=1; IT0=1; EX0=1;} void one_second_delay() {int b=0; for(b=1;b<=20;b++) { TL0=0xFD; TH0=0x4B; TR0=1; while(TF0==0); TR0=0; TF0=0; }}

SCHEMATIC:

OUTPUT:

S-ar putea să vă placă și