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] 7 State RGB LED using PWM of 89s52

Status
Not open for further replies.

prosenjit.calcutta

Newbie level 3
Newbie level 3
Joined
Jul 28, 2015
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
31
Code:
[syntax=csharp]
// pwm
#include <reg51.h>


sbit  R = P0^0;
sbit  G = P0^1;
sbit  BL = P0^2;

int j;

void pwm_setup();
void DelayMs(unsigned int);

unsigned char pwm_width;
bit a=0;
void main(void)
{
pwm_setup();
while(1)
{
	for (j=0;j<=255;j++)	
	
	pwm_width=j;
	DelayMs(100);

	/*
	I want to use total 7 case, R,G,B,RG,RB,GB,RGB...
	
	Case 1:
	{
	G=0;
	BL=0;
	break;
  }
	case 2:
	{
	R=0;
	BL=0;
	break;
	}
	*/

}
}

void pwm_setup(){
        TMOD = 0;
        pwm_width = 160;
        EA = 1;
        ET0 = 1;
        TR0 = 1;
}


void timer0() interrupt 1 
{
	  R=~R;
	  G=~G;
	  BL=~BL;
		a=~a;
        if(a==0)
 		TH0 = pwm_width;        //Load timer
        else 
        TH0 = 255 - pwm_width;  //Load timer

}


//---------------------------------------
// 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--;
    }
}
[/syntax]

In a single for loop all port using same PWM output. I am unable to turn off individually.

Please help me on this..
 

Not sure what you are trying to do. The code you have posted toggles PORT0 pins 0,1 and 2 in in unison using the Timer0 interrupt service routione. (The interrupt service routine may be missing the TF0 = 0 instruction).
Are you trying to get the interrupt service routine to display 1 of the 7 patterns you mention in your comments ? So, that in a series of 7 interrupts, it will have displayed each of the 7 patterns once ?
 

I am planning to use a switch input for switch - case.
Case1: PWM o/p only comes from port 0. port 1 and port 2 will be 0v.
Case2: PWM o/p pnly comes from port 1.
.
.
.
case6: PWM o/p comes from all three port.
 

So, what was the problem you were having with the code you posted ? You stated that you were "unable able to turn off individually".
 

i am only able to control pwm_width. That will be apply on all three ports.
In the loop i want to turn-off any required port like:

for (i=0;i<=255;i++)
{
pwm_width=i
P0^1=0;
P0^2=0;
delay(X);
}

But it's not happening. If i do so, all three ports act respect to pwm_width...
 

The code posted is using sbit to try to define R,G, and BL as PORT0 bits. The #include <reg51.h> file has already used sbit to defined these bits as P0_0, P0_1 and P0_2. Instead of using sbit, just use defines as below:

Code:
/* sbit  R = P0^0;  reg51.h already uses sbit to set sets bit access to PORT0.
   sbit  G = P0^1;  So, the #define statements below can be used to assign 
   sbit  BL = P0^2; R,G,BL to PORT0 bits
*/
#define  R   P0_0
#define  G   P0_1
#define  BL  P0_2

PORT0 outputs are "open drain", so external pullup resistors are needed to get a logic 1 on the outputs. If a logic 0 turns the LED off, then the pullup is needed to turn it on when the PORT0 bit is a logic 1.

The timer0 interrupt service routine is toggling PORT0 pins 0-2. So if you want to control the LED's in the "for loop", you don't need the interrupt routine running, as it will undo anything you do in the for loop.

In the interrupt service routine as it is written inthe code above, the statement R=~R; didn't seem to work when running the code as a test. The lines below seemed to work better:

Code:
R  ^= 0x01;  // exclusive or will toggle PORT0 bits
G  ^= 0x01;
BL ^= 0x01;

The interrupts may be toggling the bits so fast that it is hard to see them.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top