Microcontroller › 8051 › PWM and frquency 8051f021
- This topic has 0 replies, 1 voice, and was last updated 9 years, 5 months ago by
Boris.
Viewing 1 post (of 1 total)
-
AuthorPosts
-
August 31, 2015 at 2:22 pm #3949
Boris
ParticipantHello, 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 PWMsbit Button_down = P2^4; // decrease values of PWMsbit LED1 = P3^3;sbit LED2 = P3^2;sbit LED3 = P3^1;sbit valve_on = P0^5; // Valve is workingunsigned 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_upvoid but2obr(); // Button_downvoid 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 msTL0 = 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 1if(CNT2==5){CNT2=15;}// izobrazqvane ();}void but2do () // count up{BUT2FL_5=1;CNT2=CNT2+1; // trqbwa da promenqm s 1if(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 0TR0 = 1;TH0 = 0xDF; //10 ms ?TL0 = 0x73;ET0 = 1;IT0 = 1;EA = 1;CNT1 = 50; // umnojava timera i se poluchava zakusnenieto/* __asm // nulira operativnata pametMOV R0,#0x2FRES1: MOV @R0,#0DJNZ 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;} }} -
AuthorPosts
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.