prosenjit.calcutta
Newbie level 3
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:
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: