Menu

Actual Reading/Writing speed of I/O ports on a 16F684 Pic

ikonsgr74
2017-05-29
2017-06-07
1 2 > >> (Page 1 of 2)
  • ikonsgr74

    ikonsgr74 - 2017-05-29

    Hi,

    I'm working on a DIY project where i need to read a signal coming from a PIc 16f684 to another Pic 16f684 (both using internal osc @ 8Mhz) using individual pins of pic's IO ports, and i come up wondering 2 things:

    • What is the actual speed of setting an output to a desired level (High or Low), e.g. how many time must pass after giving a SET command to a specific output ,until it gets the desired value (high or low) .I'm asking this because, i noticed that when i was trying to set multiple individual outputs , using SET commands in sequence (e.g. SET PORTA.0 HIGH, then SET PORTA.2 low, etc.) i had to insert wait commands between SETs in order for the code to work right. The thing is, what is the minimum time i can use? I'm currently using 10us pauses, but i was wondering how much can i reduce it, considering the specific pic 16f684@8Mhz of course

    • What is the actual reading speed of an individual input? To be more specific,i need to "interogate" an input pin using a loop (something like: Do while portaA.1=value loop). So i was wondering how many times/sec this code can read the input? From some tests i already made,i concluded that the "interrogation" speed must be in the order of ~100 readings/sec. Considering the pic i'm using , is this right? Is there any other way for reading an input faster (say ~1000reads/sec) ,or i have to increase clock speed (or maybe using a faster pic)?

    Thanks.

     

    Last edit: ikonsgr74 2017-05-29
  • kent_twt4

    kent_twt4 - 2017-05-29

    Welcome.

    You have posted in the Contributers section of the forum, should be in the Help section for future reference.

    If speed is important than use a 20Mhz crystal, that would be a big improvement. Lookup I/O timing parameters in the electrical specifications chapter of the data sheet for some answers to timing questions. Talking about sub microsecond values to change an input or output.

    Using port read and setting port outputs could be helpful in this instance? Bit masks could come into play also. Something like:

    'read inputs on PortC.0 and PortC.1
    'and output on PortA.0 and PortA.1
    Do
    ReadInput = PortC AND b'00000011'
    PortA = ReadInput AND b'00000011'
    Loop
    

    It is possible to use the two comparators of the 16f684 for fast communication with virtually no software overhead.

    Yet more information is needed for more specific solutions.

     
  • ikonsgr74

    ikonsgr74 - 2017-05-29

    Thanks for answering. Perhaps an admin can move the thread to correct section :-)

     
  • Bert

    Bert - 2017-05-30

    If you need the highest speed and also want to protect against the inevitable clock drift between the 2 CPUs, which could result in an occasional 'bit slip', consider a DIY 2-bit synchronous interface (I haven't done this, I'm just dreaming this up). The sender CPU is the master and sets the data bit to the desired value and then sets the clock bit high. The slave CPU monitors the clock bit (maybe with an interrupt) and when it goes high, it reads the data bit. The master has to set the clock low again after a bit to be ready for the next bit transmission.

    You could also use a built-in SPI interface between the 2 chips if it is supported and makes sense, but I'm not sure it would be faster.

     
  • Chris Roper

    Chris Roper - 2017-05-30

    Is there any other way for reading an input faster ....

    If any of the PortA pins change it will interrupt the Controller and you can determine what changed at leisure. Interrupts fire within a couple of clock cycles, nothing is faster than that.

        On Interrupt PORTAChange Call MyPinChangeSub
    

    If the input you want to monitor is on PortC consider changing it to PortA.

    Cheers
    Chris

     
  • William Roth

    William Roth - 2017-05-30

    i noticed that when i was trying to set multiple individual outputs , using SET commands in sequence >(e.g. SET PORTA.0 HIGH, then SET PORTA.2 low, etc.) i had to insert wait commands between SETs >in order for the code to work right...


    This looks like the "Read Modify Write" problem on PIC chips without LAT registers. Below is a pretty good link that explains the issue.

    https://download.mikroe.com/documents/compilers/mikroc/pic/help/rmw.htm

    If you want high speed I/O operations I highly recommend you update to a newer PIC chip that supports "LAT". A good replacement for your outdated 16F684 would be 16F1825. This chip supports LAT and can operate up to 32MHz without a crystal.

    William

     
  • Anobium

    Anobium - 2017-05-30

    Also, note we have #option volatile to further improve high speed operations.

     
  • ikonsgr74

    ikonsgr74 - 2017-06-01

    Thanks for your answers.
    So, it seems that one good solution is to use an interrupt on portchange event, as this has indeed a very fast responce.
    Now, if i want to change the state of just one bit of some port, using SET, can anyone tell me what would be aproximately the actual time needed for the specific pin to change it's state (either from low to high or high to low), lets say, in the case of a 16F6XX @ 8mhz pic? Would it be in the range of 10microsec, 1microsec, 100nanosec?
    And , will the #option volatile reduce that time?

     
  • William Roth

    William Roth - 2017-06-01

    At FOSC of 8 MHz the instruction clock is FOSC/4 = 2 MHz. One instruction clock cycle is therefore 500 nanaseconds (2,000,000 / 1,000,000 = .5us = 500ns)

    Set PORTC.1 ON compiles to ASM "bsf PORTC,2". IF we look at the PIC Datasheet we can see that "bsf" executes in 1 instruction cycle or 500ns at 8 Mhz.

     
  • ikonsgr74

    ikonsgr74 - 2017-06-02

    Thanks, william,i thought it would be something like that, but as i'm new in.... "PICing" i wanted a confirmation :-) .
    Except of instruction time, would be right to add the time for I/O pin responce? For instance, the rise/fall times and any kind of propagation delays? Of course, these would be much less than 500ns, but if we use 20Mhz clock, or a faster PIc,they might be more significant.

     
  • William Roth

    William Roth - 2017-06-02

    John,

    Perhaps tell me why you need all of these detailed timings. What are you making where you need to know timings down to the nanoecond ?

     
  • ikonsgr74

    ikonsgr74 - 2017-06-04

    Well, i'm trying to make a pic to communicate with an amstrad cpc through the expansion port. :-)
    As amstrad's board works in 1Mhz, everything is done in steps of 1micro, hence the need of accuracy and fast repsonce down to microsecond.

     
  • William Roth

    William Roth - 2017-06-04

    Retro Time.

    Sounds like you have decided to bit-bang the comms betwen the microcontroller and the expansion port. But is this the best way to go? It will be tedious and you will probaboly need a scope with a resolution/accuracy of ~100 nanoseconds or so. Alternatively you could use one of those 8 channel Logic Analyzers (Saleae Clone). These cost between $6 and $12 US (Depending upn the source) and work really well for debugging stuff like this.

    From what I read the Amstrad used several different methods for comms via the expansion port, and it was not necessarily to any common standard.

    Also suggest you update your microcontroller to one that can operate up to 32 Mhz. This will reduce processing overhead and the Timers can be configured so that each timer tick will be precisely 62.5 nanoseconds so that 16 timer ticks = 1 us.

    The rise and fall times of the signals on the PIC I/O pins will be irrelevant. But if you want to know what they are, you can look in the PIC Datasheet for the respective Chip. See the "Electrical Characteristics" >>>> AC (TIming). Shoud be around 15ns each. I measure 10 ns into a 10K load on a PIC 18F25K22.

    Good Luck

     
  • ikonsgr74

    ikonsgr74 - 2017-06-05

    Thanks William. The first and most critical thing to do is to find an accurate way for the pic to respond in signals amstrad sent. I'm thinking of using the I/O port approach, meaning that amstrad will comunicate with pic through read/write basic commands to/from specific port. Afte some study, i end up that this can be done by combining 4 signals with specific values(IORQ, M1, and 2 specific address bits). So, what i'm thinking of, is to feed those four signals into a fast logic gate chip (most probable some combination of NOR gates), and the combined output will go to an input pin of the pic.
    The pic must read constantly and as fast as possible this input pin (from asm code, i found out that a "while portx.1=on/off loop", can be done in 3 instruction cycles or 12 clock ticks, so if we have a 20Mhz clock this can be done every 0.6micro seconds, much less than amstrad's 1microsecond cycle) , and the first thing it will do, is to pause amstrad sending a proper signal to Z80 (it can be done by reseting the READY signal on amstrad's expansion port). Then, we can do what ever we want at our leisure, and after reading/writing the data bus, amstrad will be unpaused (by changing the output ready pin to input, thus "disconecting it" from amstrad) and pic will loop back to start (reading input pin).

    Using a new generation pic,( those with 32Mhz internal clock), is a good idea, but currently i don't have a programmer for those, although i ordered a pickit3 a few days ago. Also, these very cheap logi analyzers you mention ,might be very usefull (in examining the sequence of amstrad's various signals in parallel ), although everything i found on ebay was having chinese manuals only, so i'm afraid that will not be able to learn how ot use them :-)

     
  • kent_twt4

    kent_twt4 - 2017-06-05

    The fastest clock that can be achieved is 64Mhz with the 18fxxKxx series chips. That would be with a 16Mhz clock and 4x PLL clock enabled.

    Also some PIC devices have a configurable logic cell (CLC) module that might be worth looking into for the analog logic gates? Apparently they can be had in an 18f device 18fxxK42 (not sure if these are supported yet) and quite a few enhanced midrange devices. Have yet to try it personally.

    The Chinese clone Saleae logic device I got relies on downloading the Saleae app and included english manual.

     

    Last edit: kent_twt4 2017-06-05
  • ikonsgr74

    ikonsgr74 - 2017-06-05

    CLC? That's a nice feature i didn't know about it. It's worth considering, to minimize the circuit to only a pic, although in current state, i must first find out if the concept is feasible or not.

    The logic analyzer clone you mention, is something like this?
    http://www.ebay.ie/itm/USB-saleae-Logic-Analyzer-Device-Set-USB-Cable-24MHz-8CH-24MHz-MCU-ARM-FPGA-/141694353386?hash=item20fda43fea:g:-nMAAOSwNSxVf51W
    It says indeed in the description that you download the software from saleae. Is it difficult to learn to use it? I will only need to investigate 4-5 signals at the same time, so having 8 channels is more than enough.

     
    • Chris Roper

      Chris Roper - 2017-06-05

      The Logic Analyzer's are very easy to use, especially as you knew what a CLC was.
      I have a 16 Channel device with two Analog Ports and never once opened the documentation. The software is self explanatory and they only have simple triggering.

      You didn’t mention what programmer you have but if it is a PICKit2 or clone thereof, it has a simple 4 channel logic Analyzer built in. It is a toy next to the Saleae and the like but may be good enough to give you an idea of what is happening.

       
  • kent_twt4

    kent_twt4 - 2017-06-05

    Yes, the 8 channel one is exactly like the one I got. Have not tried fast communications with the device, but for servo output on single channel look here (further on down the page) for a pic https://sourceforge.net/p/gcbasic/discussion/projects%26guides/thread/66153766/#a821. Pretty easy to use, and for the price who can argue.

     
  • stan cartwright

    stan cartwright - 2017-06-05

    CPC...464? john. That is so cool. I did work on them before the ula was made. z80 bbc micro. Interesting thread.I'm trying to make a frequency timer and using compare module stuff but it's not fun and haven't got it yet..

     
  • ikonsgr74

    ikonsgr74 - 2017-06-05

    Having such price,i couldn't resist and i bought the 8channel logic analyzer. :-)
    Chris,i currently have a cheap K150 programmer but i already ordered a pickit3+zif socket seat,i hope it will work with MPLAB IPE, and be able to program any pic with it!

    Stan,i have a CPC 6128 connected externally with a 3.5" disk drive (in fact, it's the same one i had since 1985... ;-) ). Using cpcdiskxp you can transfer images to real disks in half a minute, so it's perfectly usable for gaming and programming, even today! On the contrary, 464 had cassete deck and no floopy controller on board, so it's not so convinient to use it today (unless you grow up with it, and for some strange reason you still enjoy waiting minutes for loading a game :-))) )

     
  • William Roth

    William Roth - 2017-06-05

    @John,

    Your 16F684 has a Timer1 with Gate and a Capture/Compare Module, Depending upon the charcteristics/ protocol of the signal sent by the expansion port, it might be possible to use one of these to capture the data sent by the Amstrad (or whatever you decide to put between the expansion port and the PIC.)

    And remember, Great Cow Basic efficiently compiles the Basic source code in to PIC Assembly Language, It is then assembled in to hex for programming the chip. So there is generally no need to write any ASM. You can also compile the BASIC source and then view the compiled ASM to see how the compiler handles certain BASIC command & functions. It is quite good with code execution comparable to or better than most C. compilers.

    William.

     
    • Chris Roper

      Chris Roper - 2017-06-05

      I second what Bill has to say.

      When I first started using GCBASIC it was for the IDE (I hate MPLABx) and the fact that I could write ASM rather than basic but let GCBASIC take care of the housekeeping and memory management.
      I have 30 odd years of hands on experience in ASM from the MC6800 and Several PLCs through to the latest PIC's.

      You can imagine my surprise ( make that Shock / Horror ) when I decided to create my own ASM routines for things like HWSerPrint only to find that GCBASIC generated smaller and faster code than Microchip XC8 or my own ASM coding could produce.

      Don't think in the “BASIC = inefficient” mind set that we grew up with, GCBASIC is very well optimized and has the benefit of being portable too, which none of my ASM code ever was.

      Cheers
      Chris

       
  • ikonsgr74

    ikonsgr74 - 2017-06-06

    William, what's the typical speed of a ccp module? Because i will need something that could sence and rect to an input pulse (either to falling or rising edge) in less than 1micro. I suppose the method to use it with gcbasic, is something like "on interrupt CCP1" event, right? From what i've read in 16f684 datasheet, interrupts need at leas 4-5 instructions cycles to react, that is ~1micro for a 20Mhz clock. Instead, i noticed frm asm code,that the "interrogation" of an input pin through a "Do while portX.y=on/off" loop takes only 3 instructions cycles, so in theory, this kind of loop should be able to sence an input change in 3 instructions cycles at most.
    Chris, my first experience with programming was Locomotive basic on a cpc 6128! I think that the "myth" about Basic=slow/inefficient, has to do with the fact, that back in 80's, all home micros were equiped with basic interpreters and not compilers, thus they were VERY slow in execution (since they need to constantly convert each basic line to asm code) .On the other hand, all other "serious" languages (mainly C and Pascal ) were availiable only as compilers, meaning they produce much faster execution code (since the source code was translated-compiled in asm once, and then was executed much,much faster)!

     
  • stan cartwright

    stan cartwright - 2017-06-06

    Hi John, The cpc had a good keyboard for the day. Locomotive basic was good for 8bit. I'm amazed it still all works,ram and all! Publishing the rom guide was a canny move at the time but you needed machine code for graphics..C64 scrolling games didn't convert to amstrad CPC464. The 128 was paged and not much use I thought.Silly floppy discs.
    There were compiled basic games and the games still had the compiler built in and could compile new basic code,you just called an address and it would compile any basic to ram to save.
    Nostalgia. Off topic,sorry.
    You can see the asm generated by GCB,a brill educational feature. Interesting to examine.
    Evan posted a video of GCB vs arduino for doing a task. Another test would be GCB vs microchip compiler...anyone try?

     
  • William Roth

    William Roth - 2017-06-06

    @John

    Sensing the input change is one thing, what actions are taken upon sensing the change is another. How much time is there between pulse edges? What are the minimum and maximum pulse times ?

    Unless I can see the actual signal being sent to the PIC microcontroller I can only speculate/guess as to what methods may or may not work. My guess is that attempting to bit-bang an any ncomming signal > than about 400KHz will be a futile effort even on an 18F@ 64MHz . You need to not only sense the bits, but you then need to save them to a buffer or write them directly to a variable before the next bit arrives.

    If you insist upon using a 20MHz microcontroller the options narrow and the more difficult it will be. Suggest you spend the extra quid for a better/faster microconroller.

    When you get your logic analyzer and see the signal you need to work with, post a screenshot of that signal here on this forum and we can be of more help.

     

    Last edit: William Roth 2017-06-06
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.