1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

Need help creating a PIC simulation hardware controller(USO)

Discussion in 'Miscellaneous' started by NuMcA_[.gr], Dec 26, 2009.

  1. NuMcA_[.gr]

    NuMcA_[.gr] New Member

    Joined:
    Jul 12, 2009
    Messages:
    66
    Balance:
    3Coins
    Ratings:
    +0 / 0 / -0
    I am trying to create my own controller, which will receive data from USO and then use them to output values to gauges, fans, and relays etc.

    I am basically using a PIC16F887 microcontroller programmed trough MikroBasic and a MAX232 signal adapter IC to connect to my PCs serial port..

    I have been able to read and output a single value from USO and X-SIM, but I am having problems writing the code to read 2 or more values. Could you please explain me how should it be done? What would be the general idea?

    My current approach is to output from USO a string like this:
    A~a01~B~a02~

    A: indicates that the following value will be for CHANNEL A
    ~a01~: value for CHANNEL A
    B: indicates that the following value will be for CHANNEL B
    ~a02~: value for CHANNEL B


    and the PIC would run this code:
    Code:
    while (TRUE)                         'Endless loop
      if (UART1_Data_Ready() <> 0) then      ' If data is received,
        UART_RX = UART1_Read()               ' read the received data,
      end if
    
    select case UART_RX                         'Examine what is the data that was received
      case A                                  'If it is A, then the next value is for A channel
        if (UART1_Data_Ready() <> 0) then      ' If data is received,
        VALUE.A = UART1_Read()                   ' read the received data for CHANNEL A
      end if
    
      case B                                 ' If it is B, then the next value is for B channel
        if (UART1_Data_Ready() <> 0) then      ' If data is received,
       VALUE.B= UART1_Read()                   ' read the received data for CHANNEL B
      end if
     
      case else                                ' IF it is nothing from the above, then start re-reading
        goto start
    end select
    But it does not work. It instantly makes VALUE.A = 255 and VALUE.B =255 and nothing else..
    Any help would be greatly appreciated!
    Thank you!
  2. Frakk

    Frakk Active Member

    Joined:
    Apr 15, 2009
    Messages:
    1,144
    Balance:
    328Coins
    Ratings:
    +4 / 0 / -0
    I think this is a wrong forum to ask. I would try a PIC related site where people have more knowledge of your chip and the compiler your using.

    I am working on a controller as well and I would be happy to help, but I program AVR's in AVRStudio4.
    My packets have 2 start bytes: ST~a01~~a02~... and so on. After I receive the two start bytes, I begin to fill up an array with the axis data. It works for any number of axis, I just have to make the array large enough. Then I can put the bytes together for a 16bit value or do any scaling, calculations an whatnot.

    Your method I like better and I will probably do something similar. It is more flexible because you can identify and do specific things to each axis data. Using 8-16-32 bit resolution and mixing them is a lot easier for this reason too. The only thing I would be concerned about is the fact that a character is just an other 8bit number as well. If your axis data matches your character code, things can get messy and data can be mixed up.
  3. NuMcA_[.gr]

    NuMcA_[.gr] New Member

    Joined:
    Jul 12, 2009
    Messages:
    66
    Balance:
    3Coins
    Ratings:
    +0 / 0 / -0
    Guys, thank you for your answers! I am on holiday now, away from my internet connection and i have to sniff around to find a hotspot to check the forum.

    I have finally nailed it! I made it possible to read as much as 255 values from serial USO, using any PIC microcontroller.


    I am using a PIC16F887 to read values from USO, and then use them to 2 PWM outputs (could be used as motor-driver, or as dimable LED driver). In this example, i use 2 LEDs to output 2 values, for LEFT engine RPM (LEFT LED) and RIGHT engine RPM (RIGHT LED).

    X-sim sends (through USO) a string of BYTES (8-bits) of game values followed by a stop character (S) . The PIC receives the bytes coming from USO and stores them consequentially into the OUTPUT variable. The reading stops when the stop character S is received AT A CERTAIN PREDEFINED POSITION. After saving this string to OUTPUT, you are able to choose which value you want to use. The values are saved in the order they are sent through USO.

    If for example you have:
    ~a01~ = 11111111
    ~a02~ = 00000000
    S = 01010011

    and you have set USO to send this : ~a01~~a02~S

    then the OUTPUT would be: 111111110000000001010011

    and you can use:
    OUTPUT[0] = 11111111 (which is ~a01~)
    OUTPUT[1] = 00000000 (which is ~a02~)
    OUTPUT[2] = 01010011 (= S) in whatever way you find it necessary.

    My testboard consists of a PIC16F887, a MAX233 IC (adapter between PIC and PC), and 2 LEDs with 470Ohms resistors, just to output the % of each value. 0% is fully off LED, and 100% is fully enlightened LED. Total cost of around 14Euros here in Greece.


    The general idea of it, is described by this code written in MikroBasic PRO:
    Code:
    program PicUSO
    ' * DESCRIPTION
    ' PWM Fan controller through PIC16F887. RS232 (serial port) communication.
    ' To be used with X-SIM software, USO output. NuMcA 12-2009.
    '
    ' * TEST CONFIGURATION
    '     MCU:            P16F887
    '     Dev.Board:      breadboard 
    '     Oscillator:     internal 8Mhz
    
    dim dutyR, dutyL as byte	' Values for Left LED and Right LED
    dim output as string[3]		' Output variable holds the USO values. 
    				' [output] = [VALUE.A] +[VALUE.B] +... +[S to stop]  
    
    sub procedure InitMain()	' Configure PIC16F887 options
      OSCCON = %1111101	     ' Internal oscillator set to 8MHz
      ANSEL = %00000001          ' Configure AN0 pin as analog
      TRISA = %00000001          ' PORTA is input
    
      TRISC = %00000000          ' PORTC is output for pulses
      PORTC = %00000000
    
      TRISD = %00000000          ' PORTD is output (not used)
      PORTD = %00000000          ' initialize PORTD
    end sub
    
    sub procedure InitLED()      ' blink twice the indicator LEDs on startup
      PORTC.1 = %1
      PORTC.2 = %1
      delay_ms(100)
      PORTC.1 = %0
      PORTC.2 = %0
      delay_ms(400)
      PORTC.1 = %1
      PORTC.2 = %1
      delay_ms(100)
      PORTC.1 = %0
      PORTC.2 = %0
      delay_ms(400)
    end sub
    
    main:
    InitMain()              'Initialize MCU registers
    
    InitLED()               'Blink LEDs twice on start-up
    
    UART1_Init(9600)        ' Initialize UART module at 9600 bps
    Delay_ms(100)           ' Wait for UART module to stabilize
    
    PWM1_Init(25000)         ' Initialize PWM1 module at 25KHz
    PWM2_Init(25000)         ' Initialize PWM2 module at 25KHz
    
    PWM1_Start()             ' start PWM1
    PWM1_Set_Duty(0)         ' Set current duty for RIGHT LED to 0
    
    PWM2_Start()             ' start PWM2
    PWM2_Set_Duty(0)         ' Set current duty for LEFT LED to 0
    
    while (TRUE)                         'Endless loop
      if (UART1_Data_Ready() <> 0) then  ' If data is received,
        UART1_Read_text(output,S,3)    ' read the received data and save it to OUTPUT, until 					     ' the 3rd character is S.
      end if
      dutyL = output[0]                  ' Data for LEFT channel is 1rst byte
      PWM2_Set_Duty(dutyL)             ' Set current duty for LEFT LED
      dutyR = output[1]                  ' Data for RIGHT channel is 2nd byte
      PWM1_Set_Duty(dutyR)             ' Set current duty for RIGHT LED
    wend
      PWM1_stop                          ' Stop RIGHT PWM
      PWM2_stop                          ' Stop LEFT PWM
    end.
    
    BOTTOMLINE:
    if (UART1_Data_Ready() <> 0) then____' If data is received,
    UART1_Read_text(output,S,3) _______' read the received data and save it to OUTPUT character string,
    end if _____________________________' until the 3rd character is S.


    I have trouble finding internet connection here, so i will post a MUCH more detailed description of the project, along with schematics and compiled code when i get back, or find a steady internet connection.. Hope you find it helpful, THANK YOU FOR YOUR ANSWERS!
  4. tronicgr

    tronicgr

    Balance:
    Coins
    Ratings:
    +0 / 0 / -0
    Γεια σου NuMcA_[.gr]

    Το ξερω οτι δειχνει δυσκολο, και μακαρι να μπορουσα να βοηθησω με τους PIC αλλα παντα προτιμουσα τους AVR... Αν εισαι Αθηνα παντως ευχαριστως να βρισκομασταν για καφε να το συζητουσαμε και να σου εξηγουσα πως λειτουργει ωστε να προσαρμοσεις τον κωδικα στο δικο σου.

    Θανος
  5. NuMcA_[.gr]

    NuMcA_[.gr] New Member

    Joined:
    Jul 12, 2009
    Messages:
    66
    Balance:
    3Coins
    Ratings:
    +0 / 0 / -0
    Thank you Tronic, for your kind words, we will speak through PM!
    Happy New Year to all!