PDA

View Full Version : Interrupt & device setup for a PIC16F628A in Microcode Studio Plus



wildpikachu
- 30th April 2008, 17:24
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

skimask
- 30th April 2008, 19:16
(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

wildpikachu
- 1st May 2008, 13:09
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.

skimask
- 1st May 2008, 13:41
I cant upgrade as the software is setup on the machines at the university I'm at...........
......................................

Acetronics2
- 1st May 2008, 13:57
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

wildpikachu
- 2nd May 2008, 09:46
@ 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?

Archangel
- 3rd May 2008, 01:26
. . . 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?

wildpikachu
- 3rd May 2008, 07:42
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.

Acetronics2
- 3rd May 2008, 10:12
@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

wildpikachu
- 3rd May 2008, 12:22
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

Acetronics2
- 3rd May 2008, 13:12
Hi, Picatchu

nice code template ... easy to read !

some tricks :




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



change for :




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)




dummy = (time((6 * temp)+ 3300)) ' calculate part of the full calculation



to me ( I can fail ...)
it might be :




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/

wildpikachu
- 3rd May 2008, 16:28
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