• AVR Freaks

Hot!Receive 40 bytes through clock and data input pins

Author
totoco
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2019/12/14 15:20:59
  • Location: 0
  • Status: offline
2020/01/02 12:42:11 (permalink)
0

Receive 40 bytes through clock and data input pins

Hi,
Im using PIC18F26k22 and need to recive serial data through RA5 as DATA and RC0 as Clock. Is not possible to use one other way.  it's about 40 characters every 2 seconds.
 
I already have configured TIMER3 linked to de RC0 to recive the clock pulses.
 
My problem is how I convert the pulses to a string. I'm afraid there will be a lot of code inside interrupt and it will slow down the process.
 
Can someone explain to me what is the best whay to do this and if possible some code?
 
To do this with my ardware of course.
 
Thanks
 
#1

11 Replies Related Threads

    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11577
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/02 13:21:37 (permalink)
    0
    Why are you using a timer?  If RC0 is your clock, that should be your interrupt.  When you get an interrupt, read RA5 and add that bit to your buffer.
    #2
    totoco
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2019/12/14 15:20:59
    • Location: 0
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/02 13:42:26 (permalink)
    0
    jtemples
    Why are you using a timer?  If RC0 is your clock, that should be your interrupt.  When you get an interrupt, read RA5 and add that bit to your buffer.


    Hi jtemples,
    I use the timer becaouse the RC0 is not associated to one interrupt.
     
    I apreciate yor answare but my main question is the best code to shift my buffer.
     
    Thenks
     
     if (TMR3 > 0) {
     
    My Code...
     
     
           TMR3H = 0x00; //Init timer
            TMR3L = 0x00;
            TMR3IF = 0;
            TMR3 = 0;
    }
    #3
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/02 14:00:46 (permalink)
    0
    totoco
    I use the timer becaouse the RC0 is not associated to one interrupt.
     
    I apreciate yor answare but my main question is the best code to shift my buffer.

    How fast is your serial clock? Preset Timer3 to interrupt at each bit, and read RA5 when interrupt occurs.
     
    That being said, why not use one of the hardware serial interfaces that come with the PIC device; i.e. USART, SPI, etc.?
     
    Assuming MSb first, shift data likes this:
    data <= 1;
    if (PORTAbits.RA5) data |= 1;

    #4
    totoco
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2019/12/14 15:20:59
    • Location: 0
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/02 15:41:53 (permalink)
    0
    1and0
    totoco
    I use the timer becaouse the RC0 is not associated to one interrupt.
     
    I apreciate yor answare but my main question is the best code to shift my buffer.

    How fast is your serial clock? Preset Timer3 to interrupt at each bit, and read RA5 when interrupt occurs.
     
    That being said, why not use one of the hardware serial interfaces that come with the PIC device; i.e. USART, SPI, etc.?
     
    Assuming MSb first, shift data likes this:
    data <= 1;
    if (PORTAbits.RA5) data |= 1;



    All other ports are busy with others.
     
    And when the first byte is full how to shift to the others bytes?
     
    Thanks
    #5
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Receive 40 bytes through clock and data input pins 2020/01/02 15:46:41 (permalink)
    0
    totoco
    ...
    And when the first byte is full how to shift to the others bytes?

    Maintain a pointer to an array to act as a buffer.
    After each 8 bits, copy your "data" variable to the array, and increment the pointer.
    Using a pointer is much more efficient than using a variable index into the array.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    LdB_ECM
    Super Member
    • Total Posts : 236
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/02 22:29:01 (permalink)
    0
    totoco
    All other ports are busy with others.

    Who cares about the rest of the port data the if test is specifically on RA5?
    That is the port your data is on isn't it and the test is a bit test on if it is 1?
     
    totoco
    And when the first byte is full how to shift to the others bytes?

    Pretty obvious count the bits :-)
     

    // Preset variables
    static int bitcount = 7;
    static unsigned char data = 0;
     
    // in interrupt
    data <= 1;
    if (PORTAbits.RA5) data |= 1;
    if (bitcount == 0) {
        bitcount = 7;
        // STICK data SOMEWHERE .. YOU HAVE A BYTE
     
        // To be nice we might clear data strictly not needed but might help in debugging
        data = 0;
    } else bitcount--;

    post edited by LdB_ECM - 2020/01/02 22:30:41
    #7
    totoco
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2019/12/14 15:20:59
    • Location: 0
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/18 12:31:40 (permalink)
    0
    Hi,
    Now im ready to continue this task.
    I recive continuasly one string like this: "@k123.00F123.45J456.45X342.78CR"
    the character "@" is the firs caracter of the string, and the others is variables.
     
    First i test to recive the "@" character.
    it is necessary to wait for the first character "@", then continue reading the others
     
    Inside of the interrupt I tested a lot of situations like this:
     
     
    #define EstMetDataData  PORTAbits.RA5
     
            //String detection "@"
            if (EstMetDataData) {
                TXT_InEstMet[cntbi] = (TXT_InEstMet[cntbi] << 1) | 0x01;
            } else {
                TXT_InEstMet[cntbi] = (TXT_InEstMet[cntbi] << 1);
            }
            if (TXT_InEstMet[0] == 0x40 && cntbi == 0) {
                cntbi = 0;
                cntby = 1;
            }
     
    I don't have much experience, can someone help me solve this and show me a eficiente code?
     
     
     
     
    post edited by totoco - 2020/01/18 12:34:41
    #8
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Receive 40 bytes through clock and data input pins 2020/01/18 20:01:47 (permalink)
    0
    What is "cntbi" and why does it have to be zero?
    Why bother clearing it if you have already just tested if it is zero?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    NorthGuy
    Super Member
    • Total Posts : 5917
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/18 21:56:53 (permalink)
    0
    if (TXT_InEstMet[0] == 0x40 && cntbi == 0) {

     
    This assumes perfect synchronization. What if some sort of glitch produces extra clock and shifts everything by a bit?
     
    It would be nice to be able to detect byte boundaries.
    #10
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/18 23:17:46 (permalink)
    0
    totoco
    I recive continuasly one string like this: "@k123.00F123.45J456.45X342.78CR"
    the character "@" is the firs caracter of the string, and the others is variables.
     
    First i test to recive the "@" character.
    it is necessary to wait for the first character "@", then continue reading the others
    ...
            if (TXT_InEstMet[0] == 0x40 && cntbi == 0) {

    Use of the ASCII character '@' is more readable than 0x40. ;)
     
    #11
    LdB_ECM
    Super Member
    • Total Posts : 236
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: Receive 40 bytes through clock and data input pins 2020/01/19 10:11:35 (permalink)
    0
    You are building a parser you don't do the parsing in the interrupt you just store the characters from the start character to the end character.
    @k123.00F123.45J456.45X342.78CR

    Your line to parse starts at @ and ends at CR (I assume that is #13) and that is all the interrupt needs to know
     
    Something like this
    static uint8_t parse_pos = 0; // Parse position
    static char StringToParse [32]; // Array needs to be large enough to hold longest string
     
    /* In interrupt */
    if (parse_pos > 0)   // Parse started looking for end
    {
        if (data != #13)
        {
            StringToParse[parse_pos] = data;
            parse_pos++;
        } else {
            StringToParse[parse_pos] = 0; // Make a C string to parse out of interrupt
            parse_pos = 0; // Parse goes back to looking for "@"
            /* Signal main loop code to parse StringToParse its ready */
           // Put signal code here
       }
    } else {
       if (data == '@') // Ignore all characters until '@' found
       {
           StringToParse[parse_pos] = data;
           parse_pos++;
       }
    }

    #12
    Jump to:
    © 2020 APG vNext Commercial Version 4.5