AT89C51ac3 serial data packet problem

Discussion in 'Embedded Systems and Microcontrollers' started by swinch, Jan 24, 2010.

  1. swinch

    Thread Starter New Member

    Jan 24, 2010
    5
    0
    Hi,
    At the moment I have a project involving running 3 motors via a micro controller, the user controls the motors using a 3-axis joystick connected to a computer.
    I have the joystick working perfectly, and the program sending the serial data packet to the micro correctly (I used a couple of serial port monitors to check this). The data sent is ASCII characters.

    So the problem I'm having is that the micro only understands a section of the data being sent, it understands the automatic address character, the end of packet characters and the first 3 data characters, but not the rest. I'm sending a 15 character array including the address and the end chars.
    i.e. if I send a12345678911ff it will reckonise the automatic address character 'a' and the end characters 'ff' and the '123' but not the rest.
    The idea with the packet is to use each group of 3 to control each motor and the last 2 characters to choose whether the motors should be on or off.

    I've added the serial interrupt method so you can see whats being done, basically I take the data from the serial buffer and put it in an array, I then use another method to convert the ASCII char to an int so that the PWM can be set.

    I appreciate the help
    Steve

    Code ( (Unknown Language)):
    1.  
    2. interrupt void Serial(){
    3.    
    4.     if(TI == 1){        //check for the transmit flag
    5.         TI = 0;        //clear the flag
    6.         transmit = 1;    //clear the global flag used for transmitting
    7.     }
    8.    
    9.     if(RI == 1){                //check for the receive flag    
    10.         UART_received = 1;        //set the flag to indicate that the UART byte has been received
    11.            
    12.         if(SM2 == 1){            //check to see if the automatic address is set to high
    13.             SM2 = 0;        //turn off the address recognition and set it to data mode
    14.             RI = 0;            //clear the receive flag
    15.             i = 0;            //reset the global pointer to 0
    16.         }
    17.         else {                //ready for the data transmission
    18.             buffer[i] = SBUF;    //extract the information and store it inside the array
    19.             if((buffer[i] == 0x66 || 0x46) && (flag == 1)){    //check if the termination bit is a f and check the previous character
    20.                 SM2 = 1;    //if the last 2 characters have been ff turn back on the address recognition
    21.                 flag = 0;    
    22.                 complete = 1;    
    23.                 i=0;
    24.                 RI = 0;        //clear the receive flag
    25.             }
    26.             else if(buffer[i] == 0x66|| 0x46){    //if this is the 1st f wait for another one
    27.                 flag = 1;        //set the 1st f flag high
    28.                 i++;            //increment the array position
    29.                 RI = 0;            //clear the flag
    30.             }        
    31.             else{            //otherwise it is a normal byte coming in            
    32.                 i++;        //increment the array position
    33.                 RI = 0;        //clear the flag
    34.             }                
    35.         }            
    36.     }                            
    37. }
    38. [/i][/i][/i]
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4


    The above statements are wrong. You have to test both conditions separately and then OR the results.

    Code ( (Unknown Language)):
    1.  
    2.   if ((buffer[i] == 0x66 || buffer[i] == 0x46) && (flag == 1))
    3. [/i][/i]
     
  3. swinch

    Thread Starter New Member

    Jan 24, 2010
    5
    0
    You Rock, :D
    I can't believe I made such a stupid mistake, that fixes it all!
    I can now get all 3 PWMs to work with the joystick.
    Thanks
    Steve
    :D
     
Loading...