need a low frequency pulse out while still performing serial comms
+ Reply to Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2017
    Posts
    25

    Default need a low frequency pulse out while still performing serial comms

    I have an application which will need to product a low frequency pulse ranging from 1hz to 6 hz. I need this output to be operating in the background because the PIC18F4680 will also be handling communications to the PC and if the PC doesn't have response back in time, it will force a reset of the PIC.

    In the past I simply used the Pause to create the timing frequency when I needed it, but I think I need a more complete solution this time.
    Is it possible to use a timer?
    What happens if the timer kicks while the serial port is being read?
    Is this something which should go to a separate PIC which receives the frequency from the main uController and then runs independently when enabled?

    The other options is to figure out how long the communications transfer last between the two and use the pause time to service the communications.

    Thanks
    George

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,686

    Default Re: need a low frequency pulse out while still performing serial comms

    Is it possible to use a timer?
    yes , a 16 bit timer with a x16 prescaler can easily accomodate that @ 4mhz clock

    What happens if the timer kicks while the serial port is being read?
    serin or hserin ?
    if done properly there would be no impact
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,891

    Default Re: need a low frequency pulse out while still performing serial comms

    Hserin I guess is the only way to be absolutely sure.

    Also Hserin should be working based on Interrupts and on a ring array to collect one byte at a time for minimum delay in the ISR.

    Pulse also produced by another ISR running on a timer.

    Ioannis

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,247

    Default Re: need a low frequency pulse out while still performing serial comms

    Doing it with interrupts is the obvious way but if you're not used to it is a bit of learning curve and there certainly are other ways but there are so many unknown specs that coming up with a suitable solution is more or less a shot in the dark.

    What frequency resolution do you need? Is 50% dutycycle what you need? What baudrate is the serial comms? How fast is the PIC running?

    /Henrik.

  5. #5
    Join Date
    Sep 2017
    Posts
    25

    Default Re: need a low frequency pulse out while still performing serial comms

    My plan is to run the PIC at 20mhz. The duty cycle is active for 95%, with a short low pulse 5%.
    I will be running 57.6 kbaud Generally the packets are between 6 to 10 bytes and during the time in which I need to product the pulse, the packet length is fixed to 10 bytes. There is the packet to the pc and the return, so 20 bytes in all.

    At the moment my comms routine, using HSERIN, is on a loop which has some variability in its timing. So far I have not used the timer or the ISR, so yes, there will be a learning curve. I think I will need to set the timer when the pulse goes high, keep running the loop and when the timer ticks, it will create an interrupt unless I am in the middle of the hserin or hserout. As soon as a comms message is received or sent, then I will need to check the timer and act accordingly. I think this would mean that there is a worst case possibility the timing could be off by as much as about 11ms. I don't think that will be terrible.

    Thanks for the input. This forum has been an amazing resource and possibly the best thing about PBP.

    George

  6. #6
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,231

    Default Re: need a low frequency pulse out while still performing serial comms

    George,

    If you use DT's interrupt, you can have all these solved automatically.

    Have RX_INT first
    Have TMR1_INT second.

    And sit back.
    Last edited by sayzer; - 16th April 2019 at 06:10.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,247

    Default Re: need a low frequency pulse out while still performing serial comms

    See, the fact that it's a narrow pulse and not a symetric squareware makes a big difference.

    Keep the HSERIN/HSEROUT stuff in your main loop (if that works OK now). Use TMR2 to create a timebase with whatever suitable resolution you need for your output. 6Hz is a period of 166ms, 5% of that is around 8ms, perhaps a 10ms timebase would be close enough? (Meaning that the shortest pulse you'll be able to do is 10ms and the frequency can be changed in steps of 0.01Hz).

    The interrupt will obviously fire at a much faster rate than you want as your output so you'll need to "keep count" in the ISR, pull the output low at the start of the period, wait x number of interrupts, pull output high, wait y number of interrupts, do it again. If accurate frequency and pulsewidth are really important you might need a higher frequency timebase, 250Hz, 500Hz, 1000Hz?

    Why TMR2? Because, unlike TMR1 for example, there's no need to mess around with reloading it every interrupt.

    /Henrik.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,891

    Default Re: need a low frequency pulse out while still performing serial comms

    If the serial communication is in the main loop there might be a possibility data will be corrupted.

    Better serial be on ISR too, since there will be 10 bytes receiving (or sending), ring array is better than waiting for the complete string to arrive.

    When a character is available in the USART an interrupt is generated and the ISR collects that byte, advances the array index and returns fast.

    In your main routine you have the option to process the array based on the index or reset the index and start over. I suppose you have a start character on the string, so you can find the start of data in the ring array.

    Ioannis

  9. #9
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,231

    Default Re: need a low frequency pulse out while still performing serial comms

    Quote Originally Posted by Ioannis View Post
    If the serial communication is in the main loop there might be a possibility data will be corrupted.

    Better serial be on ISR too, since there will be 10 bytes receiving (or sending), ring array is better than waiting for the complete string to arrive.

    When a character is available in the USART an interrupt is generated and the ISR collects that byte, advances the array index and returns fast.

    In your main routine you have the option to process the array based on the index or reset the index and start over. I suppose you have a start character on the string, so you can find the start of data in the ring array.

    Ioannis
    I see the point there.
    As much as I have experienced, even if Hserin is in the main loop and another interrupt is being generated during Hserin execution, it does not effect anything.
    That is I think is because there is a buffer for UART in the background, and read is done via hardware in a separate process; read operation comes from buffer.

    So when interrupt occurs, no effect on Hserin.

    This is my experience with 1ms timer interrupt in DT's int routine, and receiving 50-bytes via Hserin in completely separate operations in main loop.
    1ms is pretty fast.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,891

    Default Re: need a low frequency pulse out while still performing serial comms

    May be, may be not. Buffer is 2 byte long only. There might be a situation of first 2 bytes received and then the timer interrupt happens while next byte is coming in USART. Obviously it won't be serviced.

    Ioannis

  11. #11
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,247

    Default Re: need a low frequency pulse out while still performing serial comms

    As with most things it depends on the circumstances but in my experience 57k6 is slow enough to not be upset by a simple timer interrupt.

    Let's do some math:
    At 20MHz the context saving, using DT-ints without LONGs, will take around 23us (give or take) the context restore will be roughly the same (in reality a little less) but lets be concervative and call it 50us. Add to that the execution time of the actual ISR code, perhaps another 50us (?). So, each time an interrupt occurs the main loop will be suspended for 100us.

    At 57k6 it takes 173us for one byte to fill the input shift register after which that byte gets transfered to RCREG where HSERIN has another 173us to grab it before the next byte overflows the buffer. Even if the interrupt occurs just as the last bit is shifted in (thus "stalling" HSERIN for 100us) there will be 73us to spare (which is plenty for HSERIN to see that there's a byte in RCREG and grab it) before it fall to pieces.


    Lets look at from another point of view:
    Lets say you DO implement interrupts for both the timer and the serial. What happens if the timer interrupt occurs just before the serial interrupt? Now the timer ISR will run for 100us before the serial ISR can so what's the difference?

    So here comes the idea of using low and high priority interrupts and it gets even more complicated.

    I'm not saying interrupt driven receiver routine with ring buffer etc wrong, they are GREAT and robust and all that but it DOES complicate stuff quite a bit and based on what's been disclosed I don't really see the need. I'd try it the simple way first.

    A simple handshake or polling method could work too...

    /Henrik.

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    1,686

    Default Re: need a low frequency pulse out while still performing serial comms

    if you use a chip with a clc the pulse can be generated solely by the hardware , no fuss no bother
    This is more entertaining than Free to Air TV

  13. #13
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,231

    Default Re: need a low frequency pulse out while still performing serial comms

    Quote Originally Posted by Ioannis View Post
    May be, may be not. Buffer is 2 byte long only. There might be a situation of first 2 bytes received and then the timer interrupt happens while next byte is coming in USART. Obviously it won't be serviced.

    Ioannis
    I understand, but the buffer is cleared once it is read; so you are actually 2 bytes in shadow of the actual data that is coming two bytes of time ahead of you.
    Thus, every time you are reading the buffer, you are clearing it automatically behind you, and the packet is still being entered into the buffer ahead of you.
    So, at least in my case, it works with no problem at all.

    And I am using 64Mhz
    Sorry to say it now.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  14. #14
    Join Date
    Sep 2017
    Posts
    25

    Default Re: need a low frequency pulse out while still performing serial comms

    I wanted to provide an update on this thread. Since I only needed the low frequency pulse out with one out of four modes, what I did was to have the ucontroller do its standard loop and communications within that loop for the first three modes. In the mode which required the pulse out, the pulses needed to be, 0.5, 1,2,3,4,5,6,7.5 hz with a 90% duty cycle. The PC sets the frequency via the rs232, then when the conditions are correct the ucontroller will provide the pulse out for whatever length of time is required.

    My solution was to implement DT interrupts and based upon the elapsed timer, I simply used it to tick away at 1ms intervals. The PC sets a variable which controls the frequency. I prevent communications until that 10% rest period between pulses.

    This implementation worked well and the variation is +-1ms on a 250ms pulse, so I am very pleased with the result. I thought I would need to pause the interrupt for the hserin and hserout, but so far, it seems to never miss a beat

    thanks

Similar Threads

  1. Low frequency generator
    By lerameur in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 29th September 2013, 21:53
  2. HPWM at very low frequency
    By lerameur in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 9th May 2013, 08:41
  3. Low-Frequency Counter
    By Zeke in forum General
    Replies: 7
    Last Post: - 19th January 2006, 18:06
  4. Pulse Frequency Multiplication
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st August 2005, 10:39
  5. Low frequency output
    By barkerben in forum General
    Replies: 5
    Last Post: - 16th November 2004, 15:25

Members who have read this thread : 24

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