This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

problem in receiving the text serially from port p3.o of At89c51 using pointer.

Hi! i am new to this forum. But still i am sure that this is the place where i could solve the problem regarding my code written in keil c.

Actually, I am working with the voice controlled project. I am sending the speech that has been converted to text through Usb to rs232c cable to p3.0 port of Atmel AT89c51 microcontroller. Now, I am controlling the microcontroller using the keil c. So, I need to receive the text serially using pointers or any other mechanism and the extracted word shoud be compared to either of the commands like right, left,stop,back and provide the appropriate logigic values to motor driving circuit to control the motor. So i have worked with this keil for long time but still i am not getting the respose from the microcontroller side toward the motor driving circuit.

So please help me in moving my project forward.
cheers.

Parents
  • My code for receiving the text like move and back sent from the pc is as below.
    Why my code can't receive the text serially. Can anyone correct my code.

    #include <reg51.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h> /* ports of l293d for making the motor in forward and backward direction. sbit la=P2^0; sbit lb=P2^1; sbit lc=P2^3; sbit ld=P2^5; sbit le=P2^6; sbit lf=P2^7; void serial(); void delay(); void moves(); void backs(); void lefts(); void rights(); char *rec,c; char *s[1]={"move"}; char *t[1]={"back"}; void main() { SCON = 0x50; /* Set Serial IO to receive and normal mode */ TMOD = 0x20;/*mode 1 and autoreload*/ TCON = 0x40; TH1 = 0xFD;/* value to set 9600 baud rate */ TR1 = 1;/* start timer to Receive */ IE = 0x90; EA = 1; ES = 1; RI=0; while(1) { serial(); if(*rec==*s) { la=1; lb=0; lc=1; ld=1; le=0; lf=1; delay(); } if(*rec==*t); {la=0; lb=1; lc=1; ld=1; le=1; lf=0; delay(); } }} void serial() interrupt 4 {while(1) { while(RI!=1) {} c=SBUF; if(c!=0) { *rec=c; rec++; c=0; RI=0; break; } else return; }}

    void delay()
    { int i,j;
    for(i=0;i<1000;i++);
    for(j=0;j<1000;j++);
    }

Reply
  • My code for receiving the text like move and back sent from the pc is as below.
    Why my code can't receive the text serially. Can anyone correct my code.

    #include <reg51.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h> /* ports of l293d for making the motor in forward and backward direction. sbit la=P2^0; sbit lb=P2^1; sbit lc=P2^3; sbit ld=P2^5; sbit le=P2^6; sbit lf=P2^7; void serial(); void delay(); void moves(); void backs(); void lefts(); void rights(); char *rec,c; char *s[1]={"move"}; char *t[1]={"back"}; void main() { SCON = 0x50; /* Set Serial IO to receive and normal mode */ TMOD = 0x20;/*mode 1 and autoreload*/ TCON = 0x40; TH1 = 0xFD;/* value to set 9600 baud rate */ TR1 = 1;/* start timer to Receive */ IE = 0x90; EA = 1; ES = 1; RI=0; while(1) { serial(); if(*rec==*s) { la=1; lb=0; lc=1; ld=1; le=0; lf=1; delay(); } if(*rec==*t); {la=0; lb=1; lc=1; ld=1; le=1; lf=0; delay(); } }} void serial() interrupt 4 {while(1) { while(RI!=1) {} c=SBUF; if(c!=0) { *rec=c; rec++; c=0; RI=0; break; } else return; }}

    void delay()
    { int i,j;
    for(i=0;i<1000;i++);
    for(j=0;j<1000;j++);
    }

Children
  • following the instructions over the entry window

    did you think what you saw in preview was readabele?

    Erik

  • okay now i am following the stated instruction and can onyone find what is wrong in my code.
    As my code is compiled correctly and hex file has been created but why its not receiving the code sent serially from the pc.
    The code is as follows;

    #include <reg51.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
     sbit la=P2^0;
     sbit lb=P2^1;
     sbit lc=P2^3;
     sbit ld=P2^5;
     sbit le=P2^6;
     sbit lf=P2^7;
      void serial();
            void delay();
            void moves();
            void backs();
            void lefts();
            void rights();
            char *rec,c;
            char *s[1]={"move"};
            char *t[1]={"back"};
            void main()
            {
                            SCON = 0x50;    /* Set Serial IO to receive and normal mode */
            TMOD = 0x20;/*mode 1 and autoreload*/
            TCON = 0x40;
            TH1 = 0xFD;/* value to set 9600 baud rate */
            TR1 = 1;/* start timer to Receive */
     IE = 0x90;
            EA = 1;
            ES = 1;
                    RI=0;
                    while(1)
                    {
                            serial();
                            if(*rec==*s)
                            {
                                    la=1;
                                    lb=0;
                                    lc=1;
                                    ld=1;
                                    le=0;
                                    lf=1;
                                    delay();
                            }
                            if(*rec==*t);
                            {la=0;
                                    lb=1;
                                    lc=1;
                                    ld=1;
                                    le=1;
                                    lf=0;
                                    delay();
                            }
                    }}
                    void serial() interrupt 4
                    {while(1)
                            {
                            while(RI!=1)
                            {}
                                    c=SBUF;
                                    if(c!=0)
                                    {
                                    *rec=c;
                                      rec++;
                                            c=0;
                                            RI=0;
                                            break;
                                    }
                                    else
                                    return;
                            }}
    
                            void delay()
    {
    int i,j;
    for(i=0;i<1000;i++);
    for(j=0;j<1000;j++);
    }
    
    
    
    
    

  • void serial() interrupt 4

    {while(1) // first get rid of that

    please explain what in the world made you do that

    Erik

  • Thanx for responding for the my code.
    So in this code, what i am trying to do is receiving the text like move,left right etc sent from the pc.
    So i have tried to receive it using pointer *rec. I have used the serial interrupt(indicated by 4) in keil c to receive the byte from SBUF register.
    After receiving the text completely, i have compared with the text like move which is content of pointer s.Likewise, Back is stored as string in pointer t.
    While(1) is done to make the program work forever and ever as embedded c does not have link with OS.
    While simulating in proteus why it does not respond to dc motor.

  • Ok iam commenting each code properly so that u can understand what i mean.
    Let us suppose that i sending the command like move from the pc serially. And i am going to receive from port 3.0 of microcontroller as follows;

    
    #include <reg51.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
     sbit la=P2^0; //logic value 0 or 1 given from this port to in 1 pin of l293d
     sbit lb=P2^1;//logic value 0 or 1 given from this port to in 2 pin of l293d
     sbit lc=P2^3;//logic value 0 or 1 given from this port to Enable pin of l293d
     sbit ld=P2^5;//logic value 0 or 1 given from this port to in Enable pin of l293d
     sbit le=P2^6;//logic value 0 or 1 given from this port to in 3 pin of l293d
     sbit lf=P2^7;//logic value 0 or 1 given from this port to in 4 pin of l293d
      void serial();
            void delay();
            void moves();// fxn to make the motor in clockwise direction
            void backs();// function to make motor in anticlockwise direction.
    
            char *rec,c;
            char *s[1]={"move"};//assigning the text move command to pointer s;
            char *t[1]={"back"};//assigning the text back command to pointer t;
            void main()
            {
                            SCON = 0x50;    /* Set Serial IO to receive and normal mode */
            TMOD = 0x20;/*mode 1 and autoreload*/
            TCON = 0x40;
            TH1 = 0xFD;/* value to set 9600 baud rate */
            TR1 = 1;/* start timer to Receive */
     IE = 0x90;
            EA = 1;//enable global interrupt
            ES = 1;
                    RI=0;// allow the byte to tranfer
                    while(1)
                    {
                            serial();// call the interrupt service routine
                            if(*rec==*s)//comparing the received text with the stored  move command in pointer s;
                            {
                                    la=1;
                                    lb=0;
                                    lc=1;
                                    ld=1;
                                    le=0;
                                    lf=1;
                                    delay();
                            }
                            if(*rec==*t)//comparing the received text with the stored  back command in pointer s;
                            {la=0;
                                    lb=1;
                                    lc=1;
                                    ld=1;
                                    le=1;
                                    lf=0;
                                    delay();
                            }
                    }}
                    void serial() interrupt 4// interrupt service routine for serial interrupt
                    {while(1)
                            {
                            while(RI!=1)//wait until a byte is received
                            {}
                                    c=SBUF;//when a byte is received place the content from SBUF to c variable
                                    if(c!=0)// whether byte is last character of text or not
                                    {
                                    *rec=c;// place the byte into content of pointer
                                      rec++;//increment the pointer by 1
                                            c=0;//clear the content of c
                                            RI=0;// disable the RI for next byte
                                            break;//exits from the if statement and moves to while 1 parenthesis.
                                    }
                                    else//if the character is last the return to the main function.
                                    return;
                            }}
    
                            void delay()
    {
    int i,j;
    for(i=0;i<1000;i++);
    for(j=0;j<1000;j++);
    }
    
    
    

  • Again: please explain what, exactly, you think this actually does:

    if(*rec==*s)
    

    I think you have a fundamental misunderstanding here.

  • Hello Andrew thanks for replyng. Actually what i mean to do with that code is i wanted to compare the content of pointer s with the content of pointer t.
    So will the pointer rec receive the code in the format {m,o,v,e} or {move}? The problem i am having is do i need to define the address for the interrupt service routine or not?
    The 2nd problem is while using the interrupt service routine, the routine should not have function argument and no return type. But every operation should be carried out in main function and function should b called from main function. So without return type of interrupt service routine, i can't bring back the received text to main fnction for comparison. I think this might b the place where i had done mistake so that my code is not responding.

    I have tried calling the move , back functions from the interrupt routine rather from main function due to no return type but still the code didn't work.

    One mistake i have done in the pointer increment part where i need to use the index for pointing next location as below,

    if(c!=0)
    {
    int i =0;
    rec[i]=c;
    i++;
    c=0;RI=0;
    }
    


    Can we have the rule to make interrupt routine as return type in keil c?

  • Interrupts don't return values, you have to process things you need too and leave. You also shouldn't spin around in while(1) loops indefinitely.

    You can't do string comparisons with simple pointer comparisons, it doesn't work like that, and I don't think your code does what you need, learn to use things line strcmp(), strncmp(), etc. With string comparisons there must be NUL terminations, and you'll probably want to be able to resync/restart lines.

    Do some implementation and testing on a PC C compiler, get a better grasp of the language and function, as it's hard to learn/debug on embedded, and refine your coding skills.

  • While(1) is done to make the program work forever and ever as embedded c does not have link with OS.
    what is the while (1) doing in the ISR!!!

  • Thanx for replying WesternSupermare Pier,
    Ireally appreciate your suggestion and making me realizing my mistakes.
    Interrupts doesn't return values from the interrupt routine.
    That means i need to finish all my work within the routine then after that only i can come back to main function is that.
    But what my concept is that i need to receive the tect sent from the pc. Now the main thing is should i need to send the text in character wise seperately or can i send the text randomly without splitiing them into character?
    If i send the text randomly without splitiing them into characters, then will the text arrives through the serial cable in seperate character form or not?
    Now i have modified my code as follows;

    #include <reg51.h>
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
     sbit la=P2^0;
     sbit lb=P2^1;
     sbit lc=P2^3;
     sbit ld=P2^5;
     sbit le=P2^6;
     sbit lf=P2^7;
      void serial();
            void delay();
            void moves();
            void backs();
            int i=0;
            char *rec,c;
            char *s[1]={"move"};
            char *t[1]={"back"};
            void main()
            {
                            SCON = 0x50;    /* Set Serial IO to receive and normal mode */
            TMOD = 0x20;/*mode 1 and autoreload*/
            TCON = 0x40;
            TH1 = 0xFD;/* value to set 9600 baud rate */
            TR1 = 1;/* start timer to Receive */
     IE = 0x90;
    
                    RI=0;
                    while(1)
                    {while(RI!=1)
                            {}
                            serial();
    
                    }}
                    void serial() interrupt 4
                    {
                            c=SBUF;
                                    if(c!='\0')
                                    {
                                    rec[i]=c;
                                      i++;
                                            c=0;
                                            RI=0;
                                            return;
                                    }
    
    
                                            if(*rec==*s)
                            {
                                    la=1;
                                    lb=0;
                                    lc=1;
                                    ld=1;
                                    le=0;
                                    lf=1;
                                    delay();
                            }
                            if(*rec==*t)
                            {la=0;
                                    lb=1;
                                    lc=1;
                                    ld=1;
                                    le=1;
                                    lf=0;
                                    delay();
                            }
                    return;
                            }
    
                            void delay()
    {
    int i,j;
    for(i=0;i<1000;i++);
    for(j=0;j<1000;j++);
    }
    
    
    
    
    


    here i have used while 1 in the main loop for waiting the next byte. Until the next character wont come till that RI=0 and when the byte arrives at the RXd line the RI=1 and the while condition becomes wrong and Interrupt routine is called.
    In routine,The value stored in Sbuf is moved to variable c and the character is stored in pointer location rec[0] at first then till the c!='/0' the thing happens and when the c=='/0'
    the condition becomes wrong and compares with the stored text.

  • throw away what you have away and set it up with circular buffers. Most that try fancy or uninformed methods end up with the tried and true

    Erik

  • set it up with circular buffers
    Thats a good piece of advice. But here the OP is struggling with more simpler issues. So IMHO, that we suggest him to implement circular buffer only once he is very clear with the basics of arrays and pointers.

    ***,
    So without return type of interrupt service routine, i can't bring back the received text to main fnction for comparison
    Ya. So, you need to learn the "Variables and their scope".

  • you need to learn the "Variables and their scope"
    so that you understand what are global variables and how to use them, thus define global array where you can store your received data and then process it in other functions.

  • Give up your dream of being a cool programmer.

    Be a rice farmer instead.

  • Dear Ching Shu,
    Its easy to comment on others. But i hope u had a same problem at the beginning of ur coding. At that period u might b the same situation person seeking for a help and if someone had told u the same statement be a rice farmer how would u feel at that moment.
    So if u know the program share it but never become oversmart by making others down.
    So till now u haven't provided any of the example regarding my problem. The only 1 who has provided me note examples is Mr Pier only.
    As my intention is not to become a cool programmer but i am just learning n trying to add extra code that might help to move my project forward.

    Being Oversmart is a dangerous thing ..