[SOLVED] switching ON Led using PC and 8051 MCU

Status
Not open for further replies.

BALKRISHNA TULSYAN

Junior Member level 3
Joined
Apr 19, 2013
Messages
29
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Location
BANGALORE
Visit site
Activity points
1,522
hello experts...
I have written one simple program to control LED using PC but LED is not getting switched ON. May be there is some problem with code which in tern may be some ASCII related problem. PLEASE SOLVE

#include <REG51F.H>
#define LED P1 //4 LED'S are connected to port p1.0 ,p1.1, p1.2, p1.3
void serial_init();
void delay(unsigned int);
unsigned char data_key;

void int_ser(void) interrupt 4
{
if(RI)
{
RI =0;
data_key = SBUF;
delay(100);
switch(data_key)
{
case '1':
{
LED = 0x01;
delay(1000);
break;
}
case '2':
{
LED = 0x02;
delay(1000);
break;
}
case '3':
{
LED = 0x03;
delay(1000);
break;
}

default:
{
LED = 0xFF;
delay(1000);
LED = 0x0A;
delay(1000);
break;
}

}
}
if(TI)
TI =0;
}

void main()
{
LED = 0x00;
serial_init();
delay(100);
while(1)
{
}
}

void serial_init()
{
SCON = 0x50;
TMOD = 0x20;
TH1=0xFD;
TL1=0xFD;
TR1=1;
EA=1;
ES=1;
TI=1;
RI=0;
}

void delay(unsigned int X)
{
int i=0,j=100;
for(i=0;i<=X;i++)
{
for(j=100;j>=0;j--);
}
}
 

You inserted a DELAY function inside interruption handler, and this proceeding must be avoided.


+++
 

    V

    Points: 2
    Helpful Answer Positive Rating
I have removed a DELAY function from the ISR and also removed 0x0A from the deafult value of the SWITCH CASE statement. NOW when i am pressing key 1 or key 2 or key 3, i am always getting the dafault output. i.e 0xFF always . MODIFIED PROGRAM IS PASTED BELOW. please solve

#include <REG51F.H>
#define LED P1
void serial_init();
void delay(unsigned int);
unsigned char data_key = 0;

void int_ser(void) interrupt 4
{
if(RI)
{
RI =0;
data_key = SBUF;

switch(data_key)
{
case '1':
{
LED = 0x01;
break;
}
case '2':
{
LED = 0x02;
break;
}
case '3':
{
LED = 0x03;
break;
}

default:
{
LED = 0xFF;
break;
}

}
}
if(TI)
TI =0;
}

void main()
{
LED = 0x00;
serial_init();
delay(100);
while(1)
{

}
}

void serial_init()
{
SCON = 0x50;
TMOD = 0x20;
TH1=0xFD;
TL1=0xFD;
TR1=1;
EA=1;
ES=1;
TI=1;
RI=0;
}

void delay(unsigned int X)
{
int i=0,j=100;
for(i=0;i<=X;i++)
{
for(j=100;j>=0;j--);
}
}

- - - Updated - - -

I have removed a DELAY function from the ISR and also removed 0x0A from the deafult value of the SWITCH CASE statement. NOW when i am pressing key 1 or key 2 or key 3, i am always getting the dafault output. i.e 0xFF always . MODIFIED PROGRAM IS PASTED BELOW. please solve

#include <REG51F.H>
#define LED P1
void serial_init();
void delay(unsigned int);
unsigned char data_key = 0;

void int_ser(void) interrupt 4
{
if(RI)
{
RI =0;
data_key = SBUF;

switch(data_key)
{
case '1':
{
LED = 0x01;
break;
}
case '2':
{
LED = 0x02;
break;
}
case '3':
{
LED = 0x03;
break;
}

default:
{
LED = 0xFF;
break;
}

}
}
if(TI)
TI =0;
}

void main()
{
LED = 0x00;
serial_init();
delay(100);
while(1)
{

}
}

void serial_init()
{
SCON = 0x50;
TMOD = 0x20;
TH1=0xFD;
TL1=0xFD;
TR1=1;
EA=1;
ES=1;
TI=1;
RI=0;
}

void delay(unsigned int X)
{
int i=0,j=100;
for(i=0;i<=X;i++)
{
for(j=100;j>=0;j--);
}
}
 

See if this works. The original data you are sending is being modified like inverted or baudrate is not matching. So, the data received is not matching with '1', '2', or '3'. So, default case is being executed. See if baudrate is matching or not. Try to send data from uC to PC and see if you get the original data or not. If not, then problem is baudrate or invert.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <REG51F.H>
 
#define LED P1
 
void serial_init();
void delay(unsigned int);
 
unsigned char data_key = 0;
 
void int_ser(void) interrupt 4
{
    if(RI)
    {
        RI =0;
        data_key = SBUF;
 
        switch(data_key)
        {
            case '1':
            
                LED = 0x01;
                break;
            
            case '2':
            
                LED = 0x02;
                break;
            
            case '3':
            
                LED = 0x03;
                break;
            
            default:
                
                LED = 0xFF;
                break;
    
 
        }   
    }
    
    if(TI)
        TI =0;
    }
 
void main()
{
    LED = 0x00;
    P3.0 = 1;
    P3.1 = 0;
    serial_init();
    delay(100);
 
    while(1)
    {
 
    }
}
 
void serial_init()
{
    SCON = 0x50;
    TMOD = 0x20;
    TH1=0xFD;
    TL1=0xFD;
    TR1=1;
    EA=1;
    ES=1;
    TI=1;
    RI=0;
}
 
void delay(unsigned int X)
{
    int i=0,j=100;
    for(i=0;i<=X;i++)
        for(j=100;j>=0;j--);
}

 
Last edited:
YES, u are correct . The problem was with BAUD RATE. in my program i am using 9600 baud rate but from PC data was coming with 4800 baud rate. EXCELLENT MAN. HATS OFF..THANK YOU....:-D
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…