Frequency and duty cycle adjustable program based on STC89C52RC microcontroller

Publisher:自在逍遥Latest update time:2019-11-06 Source: 51heiKeywords:STC89C52RC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Note: The microcontroller must be 52rc and cannot be 60s2



*/


#include "main.h"

#include "peizhi.h"

#include "smg.h"

#include "eeprom.h"

sfr WDT_CONTR=0XE1;

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

/**************************Button definition**********************************/

bit d1 = 1;

bit d2 = 1;

bit d3 = 1;


uint8 ci;

uint8 ca = 0;

uint8 ca1 = 0;

uint16 cb = 0;


void key(); //Key function declaration

void ConfigPWM(unsigned int fr, unsigned char dc); //Frequency and duty cycle adjustment function


void main()

{

   bit q1 = 1;

   bit q2 = 1;

   bit q3 = 1;

   ca = EEPROMReadByte(0);

   cb = EEPROMReadByte(1)*255+EEPROMReadByte(2);


   EA = 1; // Enable general interrupt

   peizhit1(1); //Configure T0 timing 2ms


   while(1)

   {        

                  ConfigPWM(cb, ca); //Frequency 100Hz, duty cycle 10%


                  if(d1 != q1)

                  {

                          q1 = d1;

                        if(d1 == 0)

                        {


                                ca1++;

                                if(ca1 >= 2)

                                {

                                          ca1 = 0;

                                }


                        }

                  }


           if(ca1 == 1)

           {

                  if(d2 != q2)

                  {

                                  q2 = d2;

                                if(d2 == 0)

                                {

                                   

                                        ca++;

                                        EEPROMSectorErase(0);

                                        EEPROMWriteByte(0,ca);

                                        EEPROMWriteByte(2,cb);

                                        EEPROMWriteByte(1,cb>>8);

                                        if(ca >= 99)

                                        {

                                                ca = 99;

                                        

                                        }

                                        

                                }

                          }

                        

                          if(d3 != q3)

                          {

                                  q3 = d3;

                                if(d3 == 0)

                                {

                                        if(ca>0)

                                        {

                                                ca--;

                                                EEPROMSectorErase(0);

                                            EEPROMWriteByte(0,ca);

                                                EEPROMWriteByte(2,cb);

                                                EEPROMWriteByte(1,cb>>8);

                                         }

                                }

                          } 

                  }

                 

                if(ca1 == 0)

                {   

                          if(d2 == 0)

                          {

                                  

                                  cb++;

                                  EEPROMSectorErase(0);

                                   EEPROMWriteByte(0,ca);

                                  EEPROMWriteByte(2,cb);

                                  EEPROMWriteByte(1,cb>>8);

                                  if(cb >= 200)

                                        {

                                                cb = 200;

                                        

                                        } 

                                

                          }

                          

                          if(d3 == 0)

                          {

                                  

                                        if(cb>0)

                                        {

                                                cb--;

                                                EEPROMSectorErase(0);

                                                EEPROMWriteByte(0,ca);

                                        EEPROMWriteByte(2,cb);                        

                                                EEPROMWriteByte(1,cb>>8);                

                                         }                        

                          } 


          }

                xianshi1(cb);  

                xianshi2(ca);  


   }

}


void key()

{

        static uint8 saomiaozhi[] = {1,1,1,1};

        saomiaozhi[0] = (saomiaozhi[0]<<1) | in1;

        saomiaozhi[1] = (saomiaozhi[1]<<1) | in2;

        saomiaozhi[2] = (saomiaozhi[2]<<1) | in3;

        

        

        if(saomiaozhi[0] == 0x00)

        {

                d1 = 0;

        }

        if(saomiaozhi[0] == 0xff)

        {

                d1 = 1;

        }

        if(saomiaozhi[1] == 0x00)

        {

                d2 = 0;

        }

        if(saomiaozhi[1] == 0xff)

        {

                d2 = 1;

        }

        if(saomiaozhi[2] == 0x00)

        {

                d3 = 0;

        }

        if(saomiaozhi[2] == 0xff)

        {

                d3 = 1;

        }

        

}


/* Configure and start PWM, fr-frequency, dc-duty cycle*/

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

}


/* T0 interrupt service function, generate PWM output */                  

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;

    }

}


/* T0 interrupt service function, complete digital tube, key scanning and stopwatch counting*/

void t1() interrupt 3

{

        

    TH1 = T1RH; //Reload the reload value

    TL1 = T1RL;

        ci++;

        ssmg(); //digital tube scanning


                 

        if(ci>=2)

    {

                ci = 0;

                key();

    }

}


Keywords:STC89C52RC Reference address:Frequency and duty cycle adjustable program based on STC89C52RC microcontroller

Previous article:MCU key long and short press source program (no need to wait to determine)
Next article:SUKON touch screen learning microcontroller modbus communication program

Recommended ReadingLatest update time:2024-11-15 18:28

Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 01 - Lighting up an LED
1 Schematic Take lighting up the LED of the P2.2 port of the microcontroller as an example: It is not difficult to see from the figure that the right side of the LED is connected to VCC through a resistor. If you want to light up the LED, you have to pull down the port on the left side of the LED. According to the
[Microcontroller]
Practical Analysis of 8051 MCU (Taking STC89C52RC as an Example) | 01 - Lighting up an LED
STC89C52RC MCU realizes serial port printing function
The crystal oscillator of the 89c52rc development board of stc is 12m. This is because the minimum system I bought has this frequency. When the baud rate is 9600 and TL and TH are set to 0xfd, both English and Chinese are garbled. The baud rate of the 12M crystal oscillator can only be 2400. In the case of 9600, the
[Microcontroller]
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号