PWM and serial communication based on 51 single chip microcomputer

Publisher:电子科技爱好者Latest update time:2018-05-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This example sets up a button to adjust the square wave of different frequencies (multiples of 100) from 100-1000hz. In addition, serial port communication is added, and the corresponding frequency can be set through the numbers sent by the serial port;

#include  

#include "uart.h"  

  

sbit PWMOUT = P1^0;  

sbit led = P2^2;  

sbit KEY1 = P1^2;  

sbit KEY2 = P3^3;  

  

unsigned char HighRH = 0; //high byte of high level reload value  

unsigned char HighRL = 0; //low byte of high level reload value  

unsigned char LowRH = 0; //high byte of low level reload value  

unsigned char LowRL = 0; //low byte of low level reload value  

unsigned int fr;  

unsigned int k=0,t,i;  

unsigned int s = 100;  

unsigned char a[3];  

unsigned int b=0;  

     

void ConfigPWM(unsigned int fr, unsigned char dc);  

void ConfigUART(unsigned int baud);  

void delay(unsigned int n){ while (n--);}  

  

void main()  

{     

    bit backup1 = 1;  

    bit backup2 = 1;  

    bit keybuf1 = 1; // Temporary storage of key values, temporarily saving the scanned values ​​of keys  

    bit keybuf2 = 1; // Temporary storage of key values, temporarily saving the scanned values ​​of keys  

    //keybuf1 = KEY1; //temporarily save the current scan value  

    EA=1;  

    led = 0;  

    ConfigUART(9600); //Configure the baud rate to 9600  

    ConfigPWM(100,50);  

    //Timer1Init();  

    while (1)  

    {  

      if(k==1)  

      {  

          k=0;  

          ConfigPWM(s,50);  

          led = ~led;delay(2000);  

      }  

  

            keybuf1 = KEY1; //temporarily store the current scan value  

          if (keybuf1 != backup1) //The current value is not equal to the previous value, indicating that the key is in action  

          {   

            delay(1000); //delay about 10ms  

            if (keybuf1 == KEY1) //Judge whether the scan value has changed, i.e. the key is jittering  

            {  

               if (backup1 == 0) //If the previous value is 0, it means the current action is a pop-up  

                  {  

                    s = s-50;        

                }  

                backup1 = keybuf1; //Update the backup to the current value for the next comparison  

            }  

            ConfigPWM(s,50);  

          }  

          keybuf2 = KEY2; //temporarily store the current scan value  

          if (keybuf2 != backup2) //The current value is not equal to the previous value, indicating that the key is in action  

          {  

            delay(1000); //delay about 10ms  

            if (keybuf2 == KEY2) //Judge whether the scan value has changed, that is, the key is jittering  

            {  

                if (backup2 == 0) //If the previous value is 0, it means the current action is a pop-up  

                {  

                    s = s+50;        

                }  

                backup2 = keybuf2; //Update the backup to the current value for the next comparison  

            }  

            ConfigPWM(s,50);  

          }  

             

  

  

  

    }  

}  

  

void ConfigUART(unsigned int baud)  

{  

    SCON = 0x50; //Configure the serial port to mode 1  

    TMOD &= 0x0F; // Clear the control bit of T1  

    TMOD |= 0x20; //Configure T1 to mode 2  

    TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value  

    TL1 = TH1; //initial value equals reload value  

    ET1 = 0; //Interrupt T1  

    ES = 1; //Serial port interrupt  

    TR1 = 1; //Start T1  

}  

  

void ConfigPWM(unsigned int fr, unsigned char dc)  

{  

unsigned int high, low;  

unsigned long tmp;  

tmp = (11059200/12)/fr; //Calculate the count value required for one cycle  

high = (tmp*dc) / 100; //Calculate the count value required for the high level  

low = tmp - high; //Calculate the count value required for the low level  

high = 65536 - high + 12; //Calculate the high level reload value and compensate for interrupt delay  

low = 65536 - low + 12; //Calculate the low level reload value and compensate for interrupt delay  

HighRH = (unsigned char)(high>>8); //Split the high-level reload value into high and low bytes  

HighRL = (unsigned char)high;  

LowRH = (unsigned char)(low>>8); // split the low level reload value into high and low bytes  

LowRL = (unsigned char)low;  

TMOD &= 0xF0; // Clear the control bit of T0  

TMOD |= 0x01; //Configure T0 to mode 1  

TH0 = HighRH; //Load T0 reload value  

TL0 = HighRL;  

ET0 = 1; // Enable T0 interrupt  

TR0 = 1; //Start T0  

PWMOUT = 1; //output high level  

}  

  

void InterruptTimer0() interrupt 1  

{  

    if (PWMOUT == 1) //When the current output is high, load the low level value and output low level  

    {  

        TH0 = LowRH;  

        TL0 = LowRL;  

        PWMOUT = 0;  

    }  

    else //When the current output is low level, load the high level value and output high level  

    {  

        TH0 = HighRH;  

        TL0 = HighRL;  

        PWMOUT = 1;  

    }  

}  

  

/*void Timer1Init() 

    TMOD|=0X10; //Select timer 1 mode, working mode 1, only use TR1 to start. 

 

    TH1=0XFC; //Assign initial value to the timer, set the timer to 1ms 

    TL1=0X18;    

    ET1=1; //Enable timer 1 interrupt 

    EA=1; //Open the general interrupt 

    TR1=1; //Turn on the timer            

void Timer1() interrupt 3 

    static u16; 

    TH1=0XFC; //Assign initial value to the timer, set the timer to 1ms 

    TL1=0X18; 

    i++; 

    if(i==1000) 

    { 

        i=0; 

        t++; 

        if(t%2==0) 

        { 

           SBUF=t/10; 

           SBUF=t%10; 

        } 

    }    

}*/  

  

void ComINT() interrupt 4 //interrupt receiving program  

{     

    if(RI) //Judge whether the reception is complete. After the reception is completed, the hardware sets the RI bit  

    {  

      RI=0;                                    

      //SBUF=SBUF;  

      a[b]=SBUF;  

      b++;  

       if(b==3)  

       {  

         /*if(a[0]=='a'&&a[1]=='b'&&a[2]=='c')  

         {led = 0; delay(2000);} 

         else 

         {led = 1;}*/  

         b=0;s=(a[0]-0x30)*100+(a[1]-0x30)*10+(a[2]-0x30);  

          k=1;  

          //SBUF = 'T';  

       }    

}       

if(TI)  

{  

      TI=0;  

  

    }  

}  



Reference address:PWM and serial communication based on 51 single chip microcomputer

Previous article:Interrupt timer and running light
Next article:Use 51 single chip microcomputer to write a program to generate 38K square wave

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号