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.

[51] How to send&receive string form PC to C51

Status
Not open for further replies.

sherryliu

Member level 1
Joined
Jan 17, 2011
Messages
32
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,535
Hi all
I have a project need control relay via COM Port send command control it.I have a confuse of the issue.Here is code:


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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <reg51.h>
 
 
unsigned char Buffer[5]={0};
 
 
unsigned char prefix=0x00;
unsigned char suffix=0x00;
 
 
void Delay(){
    unsigned char i,j;
    for(i=0;i<255;i++)
      for(j=0;j<255;j++);
}
 
void URATinit( )
{
 TMOD=0x20;
 SCON=0x50;
 
 TH1=0xfd;
 TL1=0xfd;
    
 EA=1;
 ES=1;
 TR1=1;
}
void controlbit(unsigned int ch){
    
    prefix=(ch>>8)&0xff;
    suffix=(char)ch;
    
    switch(prefix){
        
        case 0x00:
             P0=suffix;break;
        case 0x01:
             P1=suffix;break;
        case 0x02:
             P2=suffix;break;
        case 0x03:
             P3=suffix;break;
    }
}
 
 
 
void send_char_com(unsigned char cha)
{
    
    SBUF=cha;
    while(0==TI);
    TI=0;
}
void send_str_com(unsigned char *str,unsigned int strlen){
    
    unsigned int k=0;
    for(k=0;k<strlen;k++){
        
        send_char_com(*(str+k));
    }
}
 
void get_str_com(){
    
    
    unsigned int y=0;
    
    for(y=0;y<2;y++){
        Buffer[y]=SBUF;
        while(0==RI);
        RI=0;}
    
    }
 
 
 
 
void  main()
{
 URATinit( );
    
    
    while(1){
        get_str_com();
        
    //  send_str_com(Buffer,2);
        controlbit((unsigned int)Buffer);
        
        Delay();
 
    }
}



if I want to control P2.0,send 0x02fe command,but I want receive a command as when sent 0x02fe,How to do it.thanks!
 

Attachments

  • QQ Photo20161015171858.jpg
    QQ Photo20161015171858.jpg
    87 KB · Views: 114
Last edited by a moderator:

you have the get_str_com() for reading the received byte.

what else is required ?
 
Re: How to send&amp;receive string form PC to C51

appreciated you reply.Could you help me take out what else required .

- - - Updated - - -

attached file for refer.
 

Attachments

  • control realy.rar
    23.6 KB · Views: 104

Your code is wrong.

In your circuit you are using only P2 for relay control and in switch statement you are assigning the processed data to P0, P1, P2 and P3 based on case value. You only have to assign the processed value to P2.

If you are not sending \r\n at the end of the serial data then you just need one byte variable to hold the serial data and it can be directly assigned to P2 without the need of switch statement if received data is integer data. If received data is ascii character then you have to subtract 0x30 from it.

Also you have to use 15 pf Capacitors for the Oscillator Circuit.

Are you using 12 MHz Crystal or 11.0592 MHz Crystal ?

Do you just want to assign 1 byte data to relay port to control it ?

During Port initialization before while(1) loop you have to use

Code:
P3 = 0x03;
 

Thank for your reply.My purpose is simple send command to control relay anywhere in four port,such as P0,P1,P2,P3.

Send command 0x00fe,it's prefix=0x00,suffix=0xfe; P0=0xfe.How to add answer code:0x00?
Send command 0x01fe,it's prefix=0x01,suffix=0xfe,P1=0xfe.How to add answer code:0x01?
send command 0x02fe,it's prefix=0x02,suffix=0xfe,P2=0xfe.How to add answer code:0x02?

The problem is where i need to change or add else statement in the code.
 

Attachments

  • QQ Photo20161015225209.jpg
    QQ Photo20161015225209.jpg
    270.3 KB · Views: 113

Re: How to send&amp;receive string form PC to C51

The it is very easy. You have to receive 2 bytes in the Serial Interrupt. and you have to stop reception when 0xFE is received then assign the second byte received to Px based on 1st byte value.

I don't have Keil C51 and hence I have written code in mikroC PRO 8051. You can easily port the code to C51. Use Serial Interrupt for receiving data.

Check your Private Message (PM).


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
unsigned char uart_rd[2], index, myFlags = 0;
 
sbit data_received_flag at myFlags.B0;
 
void UART1_INT() iv IVT_ADDR_ES ilevel 0 ics ICS_AUTO {
     uart_rd[index++] = UART1_Read();
     
     if(index >= 2) {
        ES_bit = 0;
        index = 0;
        data_received_flag = 1;
     }
 
     RI_bit = 0;
}
 
void  main() {
 
    P0 = 0x00;
    P1 = 0x00;
    P2 = 0x00;
    P3 = 0x03;
    
    UART1_Init(9600);
    Delay_ms(200);
    
    ES_bit = 1;
    EA_bit = 1;
 
    while(1) {
 
           if(data_received_flag)  {
                P0 = 0x00;
                P1 = 0x00;
                P2 = 0x00;
                
                switch(uart_rd[0]) {
                    case 0:
                          P0 = uart_rd[1];
                          break;
                    case 1:
                          P1 = uart_rd[1];
                          break;
                    case 2:
                          P2 = uart_rd[1];
                          break;
                };
 
                memset(uart_rd, 0, sizeof(uart_rd));
                data_received_flag = 0;
                ES_bit = 1;
           }
    }
}



- - - Updated - - -

If you are only testing in Proteus then no need for COMPIM. You can connect a Virtual terminal's TxD to RxD of 8051 and send the data in .hex format.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top