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
  • 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.

Reply
  • 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.

Children
  • 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 ..

  • You should not say the things until you are as clever as me.

    You have a long way to go.

    I have written more than 17 applications in my year and I am always top of the class.