Serout/serin issues, advice required


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2005
    Location
    Cambridge UK
    Posts
    45

    Default Serout/serin issues, advice required

    Hi,
    I have been trying to send data between two pics, my end goal is to use a wireless link. At the moment this link is hardwired. The code below is supposed to read a adc input, then encode it using manchester encoding and send it to a receiving pic. Code below;

    Code:
    @ DEVICE PIC12F683, INTRC_OSC_NOCLKOUT
    ' System Clock Options (Internal) 
    @ DEVICE PIC12F683, WDT_ON
    ' Watchdog Timer
    @ DEVICE PIC12F683, PWRT_ON
    ' Power-On Timer
    @ DEVICE PIC12F683, MCLR_OFF
    ' Master Clear Options (Internal)
    @ DEVICE PIC12F683, BOD_OFF
    ' Brown-Out Detect
    @ DEVICE PIC12F683, CPD_ON
    ' Data Memory Code Protect
    @ DEVICE PIC12F683, PROTECT_ON
    
    OSCCON  = %01110000     ' Internal 8MHz osc
    'PICBASIC PROGRAM
    ANSEL = %00010000
    ADCON0 = %10000001
    VRCON = 0
    CMCON0 = 7
    DEFINE ADC_BITS 10      'Defines bit resoloution
    DEFINE ADC_CLOCK 3		'Defines adc clock speed	
    DEFINE ADC_SAMPLEUS 50	'Defines sammple time of 50mS
    'DEFINE OSCCAL_1K 1
    Define OSC 8                ' Define oscilator speed
    INCLUDE "modedefs.bas"
    
    txout VAR GPIO.0 : Output txout : dataout VAR BYTE
    ledcount VAR BYTE
    speed var byte
    Manch var byte
    index var byte
    led1 VAR GPIO.1 : Output led1
    key VAR GPIO.5 : Input key 
    gpio = %00000000
    led1 = 1 : pause 500 : led1 = 0 
    trisio.4=1
    trisio.2=0
    
    mainloop:
    ansel = %00001000		'Turns on a2d two
    ADCIN 3,speed
    speed = speed/4
    hpwm 2,speed,7000
    IF key = 0 Then 'button not pressed
    GoTo mainloop
    EndIF
    IF key = 1 Then
    Pause 50 'wait 50ms for switch to debounce then check again
    IF key = 1 Then 'Tx data out
    call encode
    dataout = $55 '($55 = manchester encoded $0)
    SerOut txout, n2400, [ dataout, dataout, dataout, dataout, dataout ]
    pause 10
    SerOut txout, n2400, [ $aa,Manch ]
    EndIF
    EndIF
    goto mainloop
    
    Encode:
       For Index = 0 to 7		' loop 8-bits
         Manch.0[(Index<<1)] = ~speed.0[Index] ' Encode bits 0,2,4,6,8,10,12,14 in Manch
         Manch.0[(Index<<1)+1] = speed.0[Index]' Encode bits 1,3,5,7,9,11,13,15 in Manch
       Next Index
       Return
    
    End
    Once sent the following is supposed to receive it and decode it. The encode/decode routines for the tx'd info was an example posted on this forum.
    Receiver code;

    Code:
    @ DEVICE PIC12F683, INTRC_OSC_NOCLKOUT
    ' System Clock Options (Internal) 
    @ DEVICE PIC12F683, WDT_ON
    ' Watchdog Timer
    @ DEVICE PIC12F683, PWRT_ON
    ' Power-On Timer
    @ DEVICE PIC12F683, MCLR_OFF
    ' Master Clear Options (Internal)
    @ DEVICE PIC12F683, BOD_OFF
    ' Brown-Out Detect
    @ DEVICE PIC12F683, CPD_ON
    ' Data Memory Code Protect
    @ DEVICE PIC12F683, PROTECT_ON
    
    OSCCON  = %01110000     ' Internal 8MHz osc
    ANSEL = %00000000
    CMCON0 = 7
    Define OSC 8                ' Define oscilator speed
    INCLUDE "modedefs.bas"
    rxin VAR GPIO.5 : Input rxin : datain VAR BYTE
    Manch var word
    index var byte
    datain = 0
    trisio.2 = 0
    mainloop:
    
    SerIn rxin, n2400, [$aa], Manch
    call decode
    hpwm 2,datain,7000
    goto mainloop
    
    Decode:
       For Index = 0 to 7   ' loop for 8 bits
         datain.0[Index] = ~Manch.0[Index<<1]' ByteOut=NOT Manch bits 0,2,4,6,8,10,12,14
       Next Index  
       Return
    
    End
    I can send a single byte of data between two pics, any byte that gives an even spead of 1's and 0's etc for the manchester encoding works fine. If anyone can see where I have gone wrong can they let me know please. Most of the time I fine my answers on this forum, but not this time.
    thanks for your time Nick

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    2 PICs, both running on the internal clock, TX'ing and RX'ing 2400 baud serial...
    Almost sounds like a recipe for 'no worky' to me.
    Any way you can modify the circuit to plug in a crystal or resonator and try it out that way?

  3. #3
    Join Date
    Nov 2005
    Location
    Cambridge UK
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    Hi Skimask,
    I had the simple data tx/rx working ok over wireless on the internal clock, I could try it on crystals (once I order some). Does the code look ok apart from that? Have not used serin/out much.
    regards Nick

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    I'll take a better look at the code later on...(heading out the door)...
    I think the fact that it was working and now it's not working is just luck. Internal freq varies with temp, voltage, humidity, sun spot cycle, color of pants, you name it, it varies because of it...ok maybe not everything
    Moral of the story, usually internal osc's are hit/miss when using serial.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    SerOut txout, n2400, [ $aa,Manch ] is only sending $aa followed by the low byte of Manch.

    SerOut txout, n2400, [ $aa,Manch.LowByte,Manch.HighByte] will send both bytes of Manch.

    Modify the receive routine also. SerIn rxin, n2400, [$aa], Manch.LowByte,Manch.HighByte.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Nov 2005
    Location
    Cambridge UK
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,
    Thanks for that, was not 100% sure if I could send a word out in a single statement, just looked at the serout command in the manual and found the answer on the 2nd line.
    I will modify the code later tonight and see what happens.
    regards Nick

    If anyone uses the code I have posted you need to change-- Manch var byte--to--Manch var word---in the Tx code. This have me stumped for 10 mins..lol.
    Last edited by Agent36; - 26th May 2008 at 17:58.

  7. #7
    Join Date
    Nov 2005
    Location
    Cambridge UK
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    Hi Skimask and Bruce,
    Thanks for the info, I now have my project up and running. The only other changes I had to make apart from the ones already listed, was to change the variables used in the ADC result and PWM output.
    regards Nick

  8. #8


    Did you find this post helpful? Yes | No

    Default BiPhase modulation beats Manchester 95% of the time.

    If your system loses or gains half a bit time due to noise, clock slip, whatever, then Manchester goes out of sync and stays out for the rest of the message. BiPhase on the other hand is brought back into sync with the arrival of the very next '1' bit. BiPhase does not propagate errors. The code for biphase is virtually identical to manchester. Manchester needs very high accuracy clocks at both ends. BiPhase is much more tolerant of clock differences. The downside of biphase is that the channel needs a somewhat wider bandwidth.

    Search for biphase code in the forum. I have posted on it before.

    HTH
    BrianT

  9. #9
    Join Date
    Nov 2005
    Location
    Cambridge UK
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    Hi Brian,
    Thanks for the info, I have just searched biphase and found a couple of posts. How do you encode and decode in laymans terms?
    This is as far as I have got with my rf;
    I can toggle the output of a pic by sending a set of 0's and 1's to balance the data slicer, followed by a data byte. The rf modules I am using are AM and are supplied from RF solutions.
    My next step was the code posted above, the aim of this was to control the speed of a motor using the rf modules and pics. By encoding the adc result and out putting it in the form of pwm.
    Once I had the code working on the breadboard I transfered it to the Tx/Rx, at this moment in time I can control the motor at a 4 meter range, I have not tried any further as I have not had the time. This has all been acheived using the internal clock set at 8meg, I have taken skimasks advice and ordered some crystals to improve the timing errors. Any other improvements I can make would be an advantage.
    Kind regards Nick

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    You're code show a Manchester Encoding/Decoding routine... so it has to work. Maybe not a bad idea to have a bigger $AA preamble before sending your data.

    Crystal are always welcome....

    You could still waste quite of your time and try to fine tune the internal one with OSCTUNE register.
    Last edited by mister_e; - 29th May 2008 at 22:54.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Need "PIC16F84A" Controler schematic Advice...
    By Kyo_89 in forum Schematics
    Replies: 1
    Last Post: - 27th May 2009, 23:03
  2. Your OTP advice?
    By truvahorse in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 28th June 2008, 16:37
  3. Advice needed on 'neat' Project!
    By vacpress in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th February 2007, 06:21
  4. bankA issues
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 23rd July 2006, 08:43
  5. Advice on PS/2 Scan Codes
    By penelopepug in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 13th January 2006, 18:05

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts