bagusdj
Member level 3
- Joined
- Nov 25, 2012
- Messages
- 67
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,766
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include<reg52.h> void main() { unsigned char temp; P1=0Xff; P2=0Xff; while(1) { temp=P2; P1=temp; while(P2==temp); } }
#include <reg51.h>
void delay()
{
unsigned int i,j;
for(i=0;i<5000;i++)
for(j=0;j<120;j++);
}
void main()
{
P2=0xff;
P0=0x00;
P1=0x00;
while(1)
{
if(P2^0==0)
{
P0=0xff;
delay();
P0=0;
P1=0xff;
delay();
P1=0x00;
}
}
}
just try this code..
Code:#include <reg51.h> void delay() { unsigned int i,j; for(i=0;i<5000;i++) for(j=0;j<120;j++); } void main() { P2=0xff; P0=0x00; P1=0x00; while(1) { if(P2^0==0) { P0=0xff; delay(); P0=0; P1=0xff; delay(); P1=0x00; } } }
hope this works
#include<reg52.h>
unsigned char temp;
void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
while(1)
{
P0=0xff;
delay(660);
P0=0x00;
delay(660);
if(P2 == 1) {
P1 = 1;
}
}
}
#include<reg52.h>
unsigned char temp;
void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
while(1)
{
P0=0xff;
delay(660);
P0=0x00;
delay(660);
if(P2 == 1) {
P1 = 0;
}
}
}
no luck also.. the same as I tried before. different way but same result. the switch is not working.
#include<reg52.h>
unsigned char temp;
unsigned char k;
void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
while(1)
{
P0=0xff;
P3=0xff;
for (k = 0; k < 22; k++){
if (P2 == 1){
P1 = 1;
}
else{
P1 = 0;
}
delay(30);
}
P0=0x00;
P3=0xff;
for (k = 0; k < 22; k++){
if (P2 == 1){
P1 = 1;
}
else{
P1 = 0;
}
delay(30);
}
}
}
remove the 10k resistor going to led. It will work.
Your led connections are wrong. zip and post your proteus file and i will send the proper circuit.
Which led is not lighting, the D1? You have to use pullups on port 0
i think i configure the led by turning the mcu into common ground. many sources tell me to put it that way even my lecturer.
i have the code for blink and switch. by that design, both of them works perfectly. but i have to make it as 1 code. thats my problem. the switch should light up D2. D1 is for blinking 5 sec and its works fine. but D2 not work properly when i tried to mix the code..
View attachment 83506
#include<reg52.h>
unsigned char temp;
void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
while(1)
{
P0=0xff;
delay(660);
P0=0x00;
delay(660);
if(P2 == 1) {
P1 = 0;
}
}
}
Try this code. It will work.
Code:#include<reg52.h> unsigned char temp; void delay(int time) //This function produces a delay in msec. { int i,j; for(i=0;i<time;i++) for(j=0;j<1275;j++); } void main() { while(1) { P0=0xff; delay(660); P0=0x00; delay(660); if(P2 == 1) { P1 = 0; } } }
The problem is with the checking.
Try with this:
Code:#include<reg52.h> unsigned char temp; unsigned char i; void delay(int time) //This function produces a delay in msec. { int i,j; for(i=0;i<time;i++) for(j=0;j<1275;j++); } void main() { while(1) { P0=0xff; P3=0xff; for (i = 0; i < 22; i++){ if (P2 == 1){ P1 = 1; } else{ P1 = 0; } delay(30); } P0=0x00; P3=0xff; for (i = 0; i < 22; i++){ if (P2 == 1){ P1 = 1; } else{ P1 = 0; } delay(30); } } }
I've added P3 along with P0. Remember that P0 needs pull-up. Connect LED like this:
I think this should work.
Hope this helps.
Tahmid.
#include<reg52.h>
unsigned char temp;
unsigned char k;
void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
while(1)
{
P0=0xff;
P3=0xff;
for (k = 0; k < 22; k++){
if (P2 == 1){
P1 = 1;
}
else{
P1 = 0;
}
delay(30);
}
P0=0x00;
P3=0xff;
for (k = 0; k < 22; k++){
if (P2 == 1){
P1 = 1;
}
else{
P1 = 0;
}
delay(30);
}
}
}
okay so i configures all led that way, and now the Led that will be switched (P1) keep light up. the 5 seconds delay one works fine. yeah i think that way also. it just not check the switch somehow
So, you tried with this code?
Can you clarify this? What do you mean by "not check the switch"?
use interrupt for switch.
#include<reg52.h>
unsigned char temp;
unsigned char k;
void delay(int time) //This function produces a delay in msec.
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void main()
{
P2=0xff;
while(1)
{
P0=0xff;
P3=0xff;
for (k = 0; k < 22; k++){
if ((P2 & 0x01) == 1){
P1 = 1;
}
else{
P1 = 0;
}
delay(30);
}
P0=0x00;
P3=0x00;
for (k = 0; k < 22; k++){
if ((P2 & 0x01) == 1){
P1 = 1;
}
else{
P1 = 0;
}
delay(30);
}
}
}
Realized the mistake in the code.
Try with this now:
However, as bigdogguru mentions, use of interrupt and timer would be a more efficient method.
Hope this helps.
Tahmid.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?