The use of other peripheral devices of single chip microcomputer

Publisher:自由探索Latest update time:2017-01-09 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use of buzzer:


The buzzer frequency is roughly set to 20ms or 50hz.


#include

#define uint unsigned int

#define uchar unsigned char

sbit fen = P2^6;

void delay(uint z);

main()

{

    while(1)

    {

        delay(1); //Delay 10 milliseconds Note that the output here is a 20 millisecond square wave

                //Because a square wave must have positive and negative transitions to count as a cycle, here?

                //It jumps every 10 milliseconds, so the period is 200ms and the frequency is 50Hz  

        fen = ~fen;

    }

}


void delay(uint z)

{

    uint x,y;

    for(x=z;x>0;x--)

        for(y=110;y>0;y--);

    

}



Precise control of buzzer sound: using a timer


#include

#define uint unsigned int

#define uchar unsigned char

sbit fen = P2^6;

uchar num,ben;

void init();

void main()

{

    init();

    while(1);


}


void init()

{

    TMOD = 0x11;

    TH1 = (65536-50000)/256;

    TL1 = (65536-50000)%256;

    ET1 = 1;

    EA = 1;

    TR1 =1;

    

}

void time1()interrupt 3

{

    TH1 = (65536-5000)/256; //The timing is 50 milliseconds

    TL1 = (65536-5000)%256;

    num++;

    if(num==2)

    {

        num = 0;

        fen = ~fen;

    }


}



The buzzer can emit precise sound (control the frequency of 100ms, 200ms, 300ms, 400ms, 500ms)



error code:


//I have been debugging this program for a whole morning, and I feel something is wrong. The problem is  

//Timer interruption, that is, when two timers are working, will the original timer be interrupted?

//The function of this program is to make the speaker sound.

//100ms, 200, 300, 400, 500

//Originally, 5 frequencies should be heard, but only 3 are actually heard, indicating that the interrupts are misaligned and the interrupts affect each other     

#include

#define uint unsigned int

#define uchar unsigned char

sbit fen = P2^6;

uchar num,flag,tt;

void init();

void main()

{

    init();

    while(1);


}


void init()

{

    TMOD = 0x11;

    TH1 = (65536-50000)/256;

    TL1 = (65536-50000)%256;

    TH0 = (65536-50000)/256;

    TL0 = (65536-50000)%256;

    ET1 = 1;

    ET0 =1;

    EA = 1;

    TR0 =1;

    TR1 =1;


    

}

void time0()interrupt 1

{

    TH0 = (65536-50000)/256;

    TL0 = (65536-50000)%256;

    tt++;

    switch(tt/20)

    {

        case 0:

            flag = 1;

            break;

        case 1:

            flag = 2;

            break;

        case 2:

            flag = 3;

            break;

        case 3:

            flag = 4;

            break;

        case 4:

            tt=0;

            flag = 5;

            break;

    }

}


void time1()interrupt 3

{

    TH1 = (65536-5000)/256; //The timing is 50 milliseconds

    TL1 = (65536-5000)%256;

    num++;

    if(num==flag)

    {

        num = 0;

        fen = ~fen;

    }


}



Correct code


/*                                                          

Use two timers to control the buzzer sound at the same time.  

Timer 0 controls the frequency, and Timer 1 controls the same   

The duration of the frequency is output in sequence at 2s intervals.  

1, 10, 50, 100, 200, 400, 800,

1k (hz) square wave?

Assume the crystal frequency is 12MHz.

*/


#include

#include

#define uint unsigned int

#define uchar unsigned char

sbit beep = P2^3;

uchar tt;

uint fre,flag;


void main()

{

    fre = 50000;

    beep = 0;

    TMOD = 0x11;

    TH0 = (65536-fre)/256;

    TL0 = (65536-fre)%256;

    TH1 = (65536- 50000)/256;

    TL1 = (65536-50000)%256;

    EA = 1;

    ET0 = 1;

    ET1 =1;

    TR0 =1;

    TR1 = 1;

    while(1);

}


void timer0()interrupt 1

{

    TR0 = 0; //After entering the interrupt function, turn off timer 0 to prevent the internal program interrupt from being lost

    TH0 = (65536-50000)/256;

    TL0 = (65536-50000)%256;

    tt++;

    if(flag<40)

        if(tt==10)

            {

                tt = 0;

                fre = 50000; //Use fre to select the frequency timing of 50ms, so the frequency of the interrupt is 10hz

                beep = ~beep;

            }

    if(flag>=40&&flag<80)

        {

            tt = 0;

            fre = 50000; //Use fre to select the frequency timing of 50ms, so the frequency of the interrupt is 10hz

            beep =~beep;

        }

    if(flag>=80&&flag<120)

        {

            tt = 0;

            fre = 10000; //timing 10ms, period 20ms, frequency 50hz 

            beep =~beep;

        }

     if(flag>=120&&flag<160)

        {

            tt = 0;

            fre = 5000; // Timing 5 milliseconds, period 10ms, frequency 100zh 

            beep =~beep;

        }

     if(flag>=160&&flag<200)

        {

            tt = 0;

            fre = 2500; //timing 2.5, period 5ms, frequency 200  

            beep =~beep;

        }

          if(flag>=200&&flag<240)

        {

            tt = 0;

            fre = 1250; //timing 1.25, period 2.5ms, frequency 400 

            beep =~beep;

        }

        if(flag>=240&&flag<280)

        {

            tt = 0;

            fre = 625; //timing 2.5, period 5ms, frequency 800  

            beep =~beep;

        }

        if(flag>=280&&flag<320)

        {

            tt = 0;

            fre = 312; // frequency is 1600

            beep =~beep;

        }

        if(flag>=320&&flag<360)

        {

            tt = 0;

            fre = 156; // frequency is 3200

            beep =~beep;

        }

        TR0 = 1;

        

}


void timer1()interrupt 3

{

        TH1 = (65536 - 50000)/256;

        TL1 = (65536 - 50000)%256;

        flag++;

        if(flag == 360)

        {

            flag = 0;

            fre = 50000;

        }

}


Keywords:MCU Reference address:The use of other peripheral devices of single chip microcomputer

Previous article:51 Writing multiple file systems
Next article:Timer counter application improvement

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号