Generation of 750 Hz and 500 Hz Square Waveform based on Input Pin State

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

  1. When P2.0 = 1, generate a square waveform of 750 Hz frequency.
  2. When P2.0 = 0, generate a square waveform of 500 Hz frequency.

C program:

#include <reg51.h>
sbit mybit=P1^5;
sbit SW=P2^0;
void T0M1Delay(unsigned char);
void main(void)
 {
 SW=1;
 while (1)
 {
 mybit=~mybit;
 if (SW==0)
 T0M1Delay(0);
 else
 T0M1Delay(1);
 }
 }
void T0M1Delay(unsigned char c)
 {
 TMOD=0x01;
 if (c==0)
 {
 TL0=0x67; // For 500 Hz frequency
 TH0=0xFC;
 }
 else
 {
 TL0=0x9A; // For 750 Hz frequency
 TH0=0xFD;
 }
 TR0=1;
 while (TF0==0);
 TR0=0;
 TF0=0;
 }

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

Advertisements