EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / PWM and frquency 8051f021

PWM and frquency 8051f021

|

Microcontroller › 8051 › PWM and frquency 8051f021

  • This topic has 0 replies, 1 voice, and was last updated 10 years, 6 months ago by Boris.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • August 31, 2015 at 2:22 pm #3949
    Boris
    Participant

    Hello, I need a help for my project. I want to change the frequency and pwm without using PCA, only timers.  I wrote the code for the frequency, but I don't know how do write the code for pwm.  The frequency is changing from 1Hz to 30Hz by 1Hz  ,PWM from 5ms to 25ms by 1ms. The signal must be in one chanel. This is the code for changing the frequency.

    #include "c8051F020.h"
    #include <intrins.h>
     
    sbit Button_up = P2^3; // increase values of PWM
    sbit Button_down = P2^4; // decrease values of PWM
    sbit LED1 = P3^3;
    sbit LED2 = P3^2;
    sbit LED3 = P3^1;
    sbit valve_on = P0^5; // Valve is working
    unsigned char bdata BUT1FL = 0x100;
    sbit BUT1FL_0 = BUT1FL^0;
    sbit BUT1FL_1 = BUT1FL^1;
    sbit BUT1FL_2 = BUT1FL^2;
    sbit BUT1FL_3 = BUT1FL^3;
    sbit BUT1FL_4 = BUT1FL^4;
    sbit BUT1FL_5 = BUT1FL^5;
    sbit BUT1FL_6 = BUT1FL^6;
    sbit BUT1FL_7 = BUT1FL^7;
     
     
    void but1obr(); // Button_up
    void but2obr(); // Button_down 
    void Valve_on();
    unsigned char bdata BUT2FL = 0x101;
    sbit BUT2FL_0 = BUT2FL^0;
    sbit BUT2FL_1 = BUT2FL^1;
    sbit BUT2FL_2 = BUT2FL^2;
    sbit BUT2FL_3 = BUT2FL^3;
    sbit BUT2FL_4 = BUT2FL^4;
    sbit BUT2FL_5 = BUT2FL^5;
    sbit BUT2FL_6 = BUT2FL^6;
    sbit BUT2FL_7 = BUT2FL^7;
     
    unsigned char  CNT1;
    unsigned char  CNTB1;
     
    unsigned char  CNT2;
    unsigned char  CNTB2;
     
    void timer_1 (void)
    {
       TH0 = 0xDF;       //5 ms
       TL0 = 0x73;
       CNT1–;
         if(CNT1==0)
       {
           CNT1=CNT2;
          Valve_on();
       }
       
       but1obr();
       but2obr();
     //  izobrazqvane ();
    }    
     
     
    void but1do () // count down
    {   
      BUT1FL_5=1;
      CNT2=CNT2-1; // trqbwa da promenqm s 1
      if(CNT2==5)
         {
            CNT2=15;
     
         }
    // izobrazqvane ();
        
    }
     
    void but2do () // count up
    {
      BUT2FL_5=1;
      CNT2=CNT2+1; // trqbwa da promenqm s 1
      if(CNT2==250)
         {
            CNT2=240;
            
         }
    //   izobrazqvane ();    
         
    }
     
    void but1obr ()
    {
    if(BUT1FL_7==1)
    {
        if(BUT1FL_5==0)      //ako e 1 – butonat e otraboten
        {
           if(BUT1FL_0==0)   //startira 30 ms zakasnenie
           {
            BUT1FL_0=1;
            CNTB1=3;
           }
     
           else
           {
               if(BUT1FL_2==0)
               {
               CNTB1–;
                  if(CNTB1==0)
                  {
                   BUT1FL_2=1;
                  }
     
                  else
                  {
                    ;
                  }
              }
     
              else
              {
              but1do();
              }
           }
        }
     
        else
        {
         ;
        }
    }
    else
    {
    BUT1FL=0;
    }
    }
     
    void but2obr ()
    {
    if(BUT2FL_7==1)
    {
        if(BUT2FL_5==0)      //ako e 1 – butonat e otraboten
        {
           if(BUT2FL_0==0)   //startira 30 ms zakasnenie
           {
            BUT2FL_0=1;
            CNTB2=3;
           }
     
           else
           {
               if(BUT2FL_2==0)
               {
               CNTB2–;
                  if(CNTB2==0)
                  {
                   BUT2FL_2=1;
                  }
     
                  else
                  {
                    ;
                  }
              }
     
              else
              {
              but2do();
              }
           }
        }
     
        else
        {
         ;
        }
    }
    else
    {
    BUT2FL=0;
    }
    } 
     
     
    void Valve_on (void)
    {
       
       valve_on = 0;
    }
    void main(void)
    {
     
     
    TMOD = 0x11;       // timer 0 
         TR0 = 1;
         TH0 = 0xDF;       //10 ms ?
        TL0 = 0x73;
         ET0 = 1;
         IT0 = 1;
         EA = 1;
         CNT1 = 50; // umnojava timera i se poluchava zakusnenieto
    /*     __asm // nulira operativnata pamet
           MOV   R0,#0x2F
    RES1:   MOV @R0,#0
          DJNZ   R0,RES1
         __endasm; */
     while(1)
       {
       
          if(Button_up==0)
          {
          BUT1FL_7=1;
          }
          else
          {   
          BUT1FL_7=0;
          }
                if(Button_down==0)
          {
          BUT2FL_7=1;
          }
          else
          {   
          BUT2FL_7=0;

     

    }     }} 
  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • H bridge current direction logic March 11, 2026
  • LM741 alternatives? March 11, 2026
  • Very Curious Issue With DS3231 RTC Clock March 10, 2026
  • Distorted output from buffer March 10, 2026
  • Motor Getting Hot March 10, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise