Generation of multiple frequency Square Waveform based on Pin State of 2 Input Pins

Write a program to continuously monitor P2.0 and P2.1 in C language.

  1. When P2.1 = 1 and P2.0 = 1, generate a square waveform of 50 Hz frequency.
  2. When P2.1 = 1 and P2.0 = 0, generate a square waveform of 100 Hz frequency.
  3. When P2.1 = 0 and P2.0 = 1, generate a square waveform of 1 kHz frequency.
  4. When P2.0 = 0 and P2.1 = 0, generate a square waveform of 2 kHz frequency.

C program:

#include <reg51.h>
unsigned char z;
sbit mybit=P1^0;
void T0M1Delay(unsigned char);
void main(void)
 {
 while (1)
 {
 mybit=~mybit;
 z=P2;
 z=z&0x3;
 T0M1Delay(z);
 }
}
void T0M1Delay(unsigned char z)
 {
 TMOD=0x01;
 switch(z)
 {
 case (0):
 {
 TL0=0x19; // for 2 kHz frequency
 TH0=0xFF;
 break;
 }
 case (1):
 {
 TL0=0x33; // for 1 kHz frequency
 TH0=0xFE;
 break;
 }
 case (2):
 {
 TL0=0xFF; // for 100 Hz frequency
 TH0=0xED;
 break;
 }
 case (3):
 {
 TL0=0xFF; // for 50 Hz frequency
 TH0=0xDB;
 break; 
 }
 }
 TR0=1;
 while (TF0==0);
 TR0=0;
 TF0=0;
 }

Check waveform using Logic Analyzer window in Keil µVision C51.

Advertisements
%d bloggers like this: