Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] is parallel task possible in 89s52

Status
Not open for further replies.

prosenjit.calcutta

Newbie level 3
Joined
Jul 28, 2015
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
31
I want to write a code for the following requirements.

3 ports for RGB LED with PWM output.
1 ports for 1 pulse/Sec.

2 switch input for PWM width +/-.
1 switch input for 7 case.

I have completed that task using simple method except that 1pulse/sec.

I can't understand how to define that. If I define inside the while (1) loop, it will follow the PWM_width delay with his own 1 sec delay.


My code is as follows:

Code:
#include <reg51.h>

sbit  PL = P0^3;
sbit  R = P0^0;	
sbit  G = P0^1;
sbit  BL = P0^2;
sbit CSW = P2^0;
sbit USW = P2^1;
sbit DSW = P2^2;

int j;
int k;
int c=0;
int s=10;
int t=10;
int u=10;
int pwmdelay=15;
int freq=300;

void DelayMs(unsigned int);

void main(void)
{

while(1)
{
	

	[COLOR="#FF0000"]PL = 1;            // LED ON
        DelayMs(1000);
        PL = 0;            // LED OFF
        DelayMs(1000);[/COLOR]
		
	
  for(j=0;j<=freq;j++)
   {
    if (CSW==0)
	   {
					s=s+20;
					if (s>=20)
					{
					s=20;
					}
					DelayMs(1000);
		 }
				
		if (s==20)
					{
						c=c+1;
						if (c>6)
						{
							c=0;
						}
						s=10;
					}
			
		if (USW==0)
		{
			t=t+20;
			if (t>=20)
			{
				t=20;
			}
			DelayMs(1000);
			
		}
					
		if (t==20)
     {
	   pwmdelay=pwmdelay+5;
	   if (pwmdelay>80)
	    {
		 pwmdelay=80;
      }
		t=10;
		 }
		
		if (DSW==0)
		{
			u=u+20;
			if (u>=20)
			{
				u=20;
			}
		DelayMs(1000);	
		}
		 
		 if (u==20)
     {
	   pwmdelay=pwmdelay-5;
	   if (pwmdelay<15)
	    {
		 pwmdelay=15;
      }
			u=10;
	   }
		 
    switch (c)
			{
				case 0:
					{
						R=1;
						G=0;
						BL=0;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}
					
				case 1:
					{
						R=0;
						G=1;
						BL=0;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}				
				case 2:
					{
						R=0;
						G=0;
						BL=1;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}				
				case 3:
					{
						R=1;
						G=1;
						BL=0;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}				
				case 4:
					{
						R=1;
						G=0;
						BL=1;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}				
				case 5:
					{
						R=0;
						G=1;
						BL=1;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}				
				case 6:
					{
						R=1;
						G=1;
						BL=1;
						DelayMs((freq-j)/pwmdelay);
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);	
						break;
					}
			}
		}
	 
		DelayMs(1000);
		
	for (j=freq;j>=0;j--)
   {
    if (CSW==0)
	   {
					s=s+20;
					if (s>=20)
					{
					s=20;
					}
					DelayMs(1000);
		 }
				
		if (s==20)
					{
						c=c+1;
						if (c>6)
						{
							c=0;
						}
						s=10;
					}
			
		if (USW==0)
		{
			t=t+20;
			if (t>=20)
			{
				t=20;
			}
			DelayMs(1000);
			
		}
					
		if (t==20)
     {
	   pwmdelay=pwmdelay+5;
	   if (pwmdelay>80)
	    {
		 pwmdelay=80;
      }
			t=10;
		 }
		
		if (DSW==0)
		{
			u=u+20;
			if (u>=20)
			{
				u=20;
			}
		DelayMs(1000);	
		}
		 
		 if (u==20)
     {
	   pwmdelay=pwmdelay-5;
	   if (pwmdelay<15)
	    {
		 pwmdelay=15;
      }
			u=10;
	   }
		 
    switch (c)
			{
				case 0:
					{						
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=1;
						G=0;
						BL=0;
						DelayMs((freq-j)/pwmdelay);	
						break;
					}
					
				case 1:
					{
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=0;
						G=1;
						BL=0;
						DelayMs((freq-j)/pwmdelay);	
						break;
					}				
				case 2:
					{
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=0;
						G=0;
						BL=1;
						DelayMs((freq-j)/pwmdelay);	
						break;
					}				
				case 3:
					{
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=1;
						G=1;
						BL=0;
						DelayMs((freq-j)/pwmdelay);	
						break;
					}				
				case 4:
					{
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=1;
						G=0;
						BL=1;
						DelayMs((freq-j)/pwmdelay);	
						break;
					}				
				case 5:
					{
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=0;
						G=1;
						BL=1;
						DelayMs((freq-j)/pwmdelay);
						break;
					}				
				case 6:
					{
						R=0;
						G=0;
						BL=0;
						DelayMs(j/pwmdelay);
						R=1;
						G=1;
						BL=1;
						DelayMs((freq-j)/pwmdelay);
						break;
					}
			}
		}
	 
		DelayMs(1000);
		
		
	}
}


//---------------------------------------
// Delay mS function
//---------------------------------------
void DelayMs(unsigned int count) 
{  // mSec Delay 11.0592 Mhz 
    unsigned int i;		       	
    while(count) {
        i = 115; 
		while(i>
0) i--;
        count--;
    }
}
 
Last edited by a moderator:

Hi,

That's why there are interrupts and internal periferal hardware.

I don't know the internals of the 89s52.
The easiest way for a 1s pulse is to set up a timer/counter periferal as wave generating mode. Then you should be able to choose frequency and duty cycle. Once set up it needs no additional processing power, and no code.
The is a hardware solution.

For a software solution you could set up a timer to interrupt the main loop every half second, within the ISR toggle the desired pin. Since it needs two ISR loops (set port, clear port) you get a 1s pulse.

Klaus

- - - Updated - - -

Hi,

I just saw that you want 1s ON and 1s OFF... just setup the timer for 1s instead of half a second....

****
Hope this helps


Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top