51 MCU as a signal generator example programming

Publisher:dadigtLatest update time:2015-07-10 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Use the 51 single-chip microcomputer as a signal generator to output square waves of four frequencies at the same time.
//Requirement: The crystal oscillator is 12MHz, T0 is used as the timer, and the lower four bits of P1 output four frequencies of square waves:
// P1.3 = 1.25kHz, P1.2 = 2.5kHz, P1.1 = 5kHz, P1.0 = 10kHz
//In addition, the above four frequencies require a four-to-one data selector to select one of them for output. //
The outputs of P1.6 and P1.7 are used to control the selection bits of the four-to-one data selector, which are controlled by the P3.0 button.
//====================================================
//Now I have added PROTEUS simulation pictures to the blog.
//The title requires that the four frequencies output on the four adjacent interface lines have a double relationship, which can be completed using a timer interrupt.
//The title also requires the use of a data selector to select a frequency to output by pressing a button.
//The screenshots of PROTEUS simulation are shown below.
//In the figure, a frequency meter and a speaker are connected to the output end for frequency detection.
//When the button is pressed, the output frequency can be switched in turn.
//The screenshots of the detection results of the four frequencies are all shown in the figure. It can be seen that the output frequency is very ideal.
//The PC speaker can also hear the sound, the frequency of 10kHz, which is quite harsh.
//The following program has been simulated successfully.
//---------------------------------------------------
#include
#define uchar unsigned char
#define TL_0SET (256 - 50) //The timing time is selected as 50us
//The output square wave period will be 100us, 10kHz
sbit P3_2 = P3^2; //For buttons
sbit P1_0 = P1^0; //Four frequency outputs
sbit P1_1 = P1^1; //
sbit P1_2 = P1^2; //
sbit P1_3 = P1^3; //
sbit P1_6 = P1^6; //Two selection control outputs
sbit P1_7 = P1^7; //
 
bdata uchar x = 0;
sbit p16 = x^0;
sbit p17 = x^1;
 
uchar i, j;
//---------------------------------------------------
void e0int() interrupt 0 //External interrupt 0 acts as a switch to control the four-choice data selector
{
while(P3_2 == 0) {;} //Wait for the button to be released
x++;
if(x == 4) x = 0; //Change the current output selection
P1_7 = p16;
P1_6 = p17;
}
//---------------------------------------------------
void time0() interrupt 1 //T0's interrupt service routine
{ //T0 works in mode 2 and automatically loads the initial value
i++;
j = i % 16; //Get the lower four bits of i
j += x << 6; //Combined data selector control signal
P1 = j; //Output four frequencies of square wave and control signal
}
//---------------------------------------------------
void main()
{
TMOD = 0x02; //T0 timing mode 2
TH0 = TL_0SET; //Self-loaded timing initial value
TR0 = 1; //Start T0
ET0 = 1;
IT0 = 1; //External interrupt 0 is triggered by falling edge
EX0 = 1; //External interrupt 0 enables
EA = 1;
while(1) {;}
}
//---------------------------------------------------


 

Reference address:51 MCU as a signal generator example programming

Previous article:MCU serial communication source code
Next article:51 4WD wireless remote control car (200m effective distance)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号