Interrupt & device setup for a PIC16F628A in Microcode Studio Plus


Closed Thread
Results 1 to 12 of 12
  1. #1

    Default Interrupt & device setup for a PIC16F628A in Microcode Studio Plus

    Hi all

    I have got an ultrasonic distance measuring project to do for one of my varsity subjects and have hit a snag or three. (Im using a PIC16F628A, PBP version 2.45 and microcode studio plus version 2.1.0.7)

    basically the program after turn on waits for a button (RB5) to be pressed which starts the measurement process, after that HPWM on RB3 sends out a 40kHz signal for 2ms then stops, after that timer1 starts at a preset value of 1000 and a prescaler value of 1:2, then it must wait for an external interrupt on RB0 (a change in digital value i.e. 0 to 1 or 1 to 0) and as soon as that interrupt is detected timer1 must stop and a speaker must sound for 1 second (RB4). after that the temperature measurement must take place (using OWIN & OWOUT, DS1820) then the distance can be calculated and then displayed on an LCD.

    First question: how do I setup Timer1 to start at a preset value of 1000 and a prescaler value of 1:2

    Second question: how do i setup the PIC16F628A? i have found the "@ DEVICE" stuff but it wont work in microcode studio.

    Third question: How to get the interrupt working as the interrupt must be enabled immediately after timer1 starts and disabled immediately after timer1 stops, the time frame could be anywhere from 2.5ms to 120ms

    thought of using this:

    T1CON.0 = 1 'start timer1
    tmp:
    if (PORTB.0 = 1) then goto tmp 'check for port change low to high
    T1CON.0 = 0 'stop timer1
    SOUND PORTB.4, [128,84] 'sound buzzer at about 10kHz for about 1 second

    the problem is what if portb.0 already is high? hence the need for a change type of interrupt but again the interrupt must only be active during that particular point in the code

    so it would need to go something like this:

    T1CON.0 = 1
    enable interrupt
    PAUSE 150
    disable interrupt

    if interrupt is triggered then
    disable interrupt
    T1CON.0 = 0
    SOUND PORTB.4, [128,84]
    return to the end of the pause

    any help would be greatly appreciated as I'm a bit of a novice with this and this is my first time using PBP and Microcode Studio Plus

    Cheers
    Guy

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wildpikachu View Post
    (Im using a PIC16F628A, PBP version 2.45 and microcode studio plus version 2.1.0.7)
    ???????????????????????????????????
    2 words...Up.....Grade.....

    First question: how do I setup Timer1 to start at a preset value of 1000 and a prescaler value of 1:2
    Second question: how do i setup the PIC16F628A? i have found the "@ DEVICE" stuff but it wont work in microcode studio.
    Third question: How to get the interrupt working as the interrupt must be enabled immediately after timer1 starts and disabled immediately after timer1 stops, the time frame could be anywhere from 2.5ms to 120ms
    .........................
    any help would be greatly appreciated as I'm a bit of a novice with this and this is my first time using PBP and Microcode Studio Plus
    Quite frankly, I think you're in over your head........for the moment anyways......
    Why not put together a small circuit with an LED first and make it blink, call it Mr. Blinky.
    Then add a switch, and make it blink with the switch...
    Then add some code to make the LED blink with a timer somehow
    Then add some code to do.............whatever......
    As far as the "@ DEVICE stuff" goes....
    http://www.picbasic.co.uk/forum/showthread.php?t=543

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Hey skimask.

    thanks for the reply.
    someone is never in over their head, they just have a steeper learning curve thats all :-)

    I cant upgrade as the software is setup on the machines at the university I'm at.
    perhaps i should have clarified better, I have been working with PIC's for a sometime now but this is my first attempt at using anything other than assembly and we also only use a program called PI so code, simulate and program the PICs in, it was created by one of our lecturers and has menu options for all the device setup options so we never put anything in our code about what device we are using and what options are set or not set hence my query about the "@ DEVICE" which does not seem to be intended for MCSP however "@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON & _LVP_OFF & _CP_ALL & _DATA_CP_ON" gives me an error too.

    The question about interrupts is, in my view, a rather simple question: can interrupts be set so that they are only "active" at a set point in the program. but then again i have no experience with interrupts as it is only covered at BTECH (post grad) level

    As for changing the project i wont, the project is for an electronic measurements subject and as such is not testing our ability to code its putting what theory we have learned about electronic measurement devices into practice i.e. load cells, capacitance level meters and tachometers etc.

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wildpikachu View Post
    I cant upgrade as the software is setup on the machines at the university I'm at...........
    ......................................

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi, PiKachu

    I think reading carefully the '628a datashhet, Chapter CCP, sub chapter CAPTURE could greatly :

    1) improve your knowlege
    2) show you you're re-inventing the wheel
    3) solve your timing headache.

    May be your reflected signal input will have to be directed to PortB.3 ... but I do not think it will be an unavoidable problem ...


    Now, ... you have a PbP command called RCTime with a 2µs @ 20Mhz resolution ... ( see manual for how it works !!! )

    2*1E-6 * 330 = .66 E-3 m ... no comment !
    2*65535*1E-6*330 = 43.25 m ... more than your sensors could ever sense !!!


    ... and remember all period measurements also can be done like that ...

    wish you happy hours

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  6. #6


    Did you find this post helpful? Yes | No

    Default

    @ skimask. yeah i know but we make do with what we have.

    @acetronics: I chose BR0/INT because the receiving circuit is a single frequency passband filter, with a peak at 40.3kHz and the -3db points at 39.2kHz and 41.6kHz, which is then connected to a comparator giving me a change of 0VDC-5VDC hence wanting to use the external interrupt on a change of PORTB.0, I'm using the RB3/CCP to generate a 40kHz signal.
    I'm aiming for a range of 0.5m to 20m but with the attenuation on a 40kHz signal in air 10m is more realistic, if only i could get my hands on a 25kHz transducer set (they dont bring them into south africa).

    ok think i've got my timer1 problems sorted. wanting a prescaler value of 1:2, a preset value of 1000. is the following code right?


    preset Var Word
    preset = 1000 ' to give me a preset value of 2ms at a prescaler value of 1:2 for timer1
    TMR1H = preset.highbyte
    TMR1L = preset.lowbyte
    T1CON = %00010000 'prescaler value of 1:2, timer diabled
    INTCON = %11010000 ' GIE & PEIE & INTE enabled
    PIE1.0 = 0 ' TMR1 interrupt disabled
    PIR1.0 = 0 ' TMR1 interrupt flag reset

    start code......
    ........
    .......
    T1CON.0 = 1 ' timer1 enable with a preset value of 1000 and a prescaler value of 1:2
    pause 2000 'wait 2 seconds, this is the only place that the external interrupt must be active, still working on that tho.
    T1CON.0 = 0 ' timer 1 disable
    more code....
    .........
    .........
    end code

    i've read through the datasheet and it doesnt explain GIE and PEIE very well, what is their purpose? does GIE enable every interrupt or just allow the enabled interrupts to happen throughout the entire program? do i have to set PEIE to enable an external interrupt?

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wildpikachu View Post
    . . . if only i could get my hands on a 25kHz transducer set (they dont bring them into south africa) . . .
    I have got to ask . . . why not?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Hi JoeS.

    No one brings them into South Africa, not even RS components, and its going to cost about double the entire project to ship one over so its not worth it just for an estimated 5meter distance gain and my lecturer has told me that i will get 100% if i can measure up to 10 meters with 5% accuracy because its more about implementing the theory than actual long distance measuring ability. I can always change it later, 2 lines of code and 4 resistors on the filter.

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by wildpikachu View Post

    @acetronics: I chose BR0/INT because the receiving circuit is a single frequency passband filter, with a peak at 40.3kHz and the -3db points at 39.2kHz and 41.6kHz, which is then connected to a comparator giving me a change of 0VDC-5VDC hence wanting to use the external interrupt on a change of PORTB.0, I'm using the RB3/CCP to generate a 40kHz signal.
    I'm aiming for a range of 0.5m to 20m but with the attenuation on a 40kHz signal in air 10m is more realistic, if only i could get my hands on a 25kHz transducer set (they dont bring them into south africa).

    ok think i've got my timer1 problems sorted. wanting a prescaler value of 1:2, a preset value of 1000. is the following code right?


    preset Var Word
    preset = 1000 ' to give me a preset value of 2ms at a prescaler value of 1:2 for timer1
    TMR1H = preset.highbyte
    TMR1L = preset.lowbyte
    T1CON = %00010000 'prescaler value of 1:2, timer diabled
    INTCON = %11010000 ' GIE & PEIE & INTE enabled
    PIE1.0 = 0 ' TMR1 interrupt disabled
    PIR1.0 = 0 ' TMR1 interrupt flag reset

    start code......
    ........
    .......
    T1CON.0 = 1 ' timer1 enable with a preset value of 1000 and a prescaler value of 1:2
    pause 2000 'wait 2 seconds, this is the only place that the external interrupt must be active, still working on that tho.
    T1CON.0 = 0 ' timer 1 disable
    more code....
    .........
    .........
    end code

    i've read through the datasheet and it doesnt explain GIE and PEIE very well, what is their purpose? does GIE enable every interrupt or just allow the enabled interrupts to happen throughout the entire program? do i have to set PEIE to enable an external interrupt?
    Hi, Pikachu

    I see you have locked your hardware definition ... so, let's try to do with.

    in your code, you forget to enable TMR1 INT ( PIE1) when using TMR1 ... you can enable it in your header, if TMR1 just works during your 2s Pause.

    PEIE is intended to control PERIPHERALs interrupts such from Timer1, ADC, COMP ( ~ External modules )...

    GIE controls ALL interrupts wherever they come ... AND, of course, allow them IF they are individually allowed ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  10. #10


    Did you find this post helpful? Yes | No

    Default

    Hi Alain.
    thanks for the reply, really cleared things up for me. I wasnt going to use timer1 to generate an interrupt but have decided to let it now. I'm using a simple repeated check on RB0 to check for a change instead of using an interrupt.

    I've attached my code, seems right to me but if anyone finds any problems or has any advice please let me know.

    Cheers
    Guy
    Attached Files Attached Files

  11. #11
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Picatchu

    nice code template ... easy to read !

    some tricks :

    Code:
      if (PORTB.0 = rb0old) then goto changetest ' If there is no change then test again
    change for :

    Code:
    While (PORTB.0 = rb0old)
    Wend
    this will loop till a change occurs ... ( much faster ! )

    Note here I did not understand why use the "change" feature : from your explanations ... if "back" signal detected, it simply turns PortB.0 input ON ( ... or OFF depending Hardware)

    Code:
        dummy = (time((6 * temp)+ 3300))    ' calculate part of the full calculation
    to me ( I can fail ...)
    it might be :

    Code:
        dummy = (time*((6 * temp)+ 3300))    ' calculate part of the full calculation
    By the Way : Speed of sound in air = SQR ( 13 * 287* (273.15 + Temp) /10 )

    ... @ sea level ...

    ...

    Alain

    PS: May be you could try a "samples" request here ...

    http://www.midascomponents.co.uk/
    Last edited by Acetronics2; - 3rd May 2008 at 13:47.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  12. #12


    Did you find this post helpful? Yes | No

    Default

    Hi alain.

    thanks for the "while" and maths suggestions and for the complement on my code, I will implement them. the checking for a change on RB0 is because the receiving circuit is set to be extremely sensitive and I am not sure what state the comparator, that is connected to RB0, will be in (either 0V or 5V) thats why i'm checking for a change so that regardless of what state the comparator is initially i will stop timer1 as soon as possible. if the code was this: if PORTB.0 = 1 and initially the comparator was at a high i would get an instant stop without even receiving a signal. well in my mind it makes sence :-)

    The calculation is got from one of my physics books and modified it to get rid of any decimal places and to work the distance out in one. it uses temperature in celcius rather than Kelvin.

    thanks for the link, lets see if they are willing to send overseas :-)

    Cheers and thanks again for the help!

    - Guy

Similar Threads

  1. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  2. Microcode studio - PIC16F877A Interrupts
    By mcbeasleyjr in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th January 2009, 06:10
  3. CDlite to MicroCode Studio
    By grounded in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 30th January 2007, 15:46
  4. MicroCode Studio Plus ICD
    By mwhatley in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 17th August 2006, 20:55
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 0

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