Hi everyone..
I know that with the PIC16F84A it is possible to make a sort of instructions "delay" using a subroutine, with a loop + counter etc..that would give about 1 sec. of delay.. But it is possible to obtain different (and precise) delay times? like just 200 ms, or 400ms, and so on..doing some math?
I'd like to say that I don't mean treating the audio signal with that, just want to know if it is possible to give a 5V out every 400ms for example..
thanks
Dimitri
In nothing but assembly, I would loop a bunch of NOP's. Probably a very naive approach but I use c for most everything now. Each asm statement has an exact number of machine cycles it takes to happen. In the pic24, NOPs take two. So, what ever speed the chips running at, NOPs happen at half that frequency. But, you gotta take into consideration the loop instructions too. Basically, you can know how long it takes to do each instruction, do a whole lot of nothing to eat up machine cycles, then boom, high your pin. It's better to use a timer but I honestly don't know how to implement it outside of c.
if someone doesn't come around, i'll attempt to code something around your chip.
Quote from: Dimitree on April 13, 2009, 06:39:04 PM
Hi everyone..
I know that with the PIC16F84A it is possible to make a sort of instructions "delay" using a subroutine, with a loop + counter etc..that would give about 1 sec. of delay.. But it is possible to obtain different (and precise) delay times? like just 200 ms, or 400ms, and so on..doing some math?
I'd like to say that I don't mean treating the audio signal with that, just want to know if it is possible to give a 5V out every 400ms for example..
thanks
Dimitri
If you want to get 0v and 5v on 400ms intervals you can use a simple delay subroutine in a loop. So every 400ms you will have 5v and ov intervals.
CG
thanks guys--
@ trendyironicname:
what is NOP?
I can program in C, so no problem there, but I didn't know it was possible for the PIC16F84.. So it is possible?
@ CGDARK:
400ms was just an example, I'd like to have several intervals, and choosing one of them with Midi messages, for example a Control change of 84 will give me 360 ms of delay, Control change of 85 will give 370 ms..
Ok I now understand how to code that and obtain these delays.. Now I'd like to know if there is a software that can emulate/simulate the PIC16F4, because I don't have the PIC and the Programmer here, so first I want to test the code..so I need a software that shows the program I wrote.. It is possible? I have MPLAB but can't find this function
thanks!!
This might help on at least finding the simulator in it.
http://www.swarthmore.edu/NatSci/echeeve1/Ref/MPLab/PicSim.html
Quote from: Dimitree on April 14, 2009, 11:07:10 AM
Ok I now understand how to code that and obtain these delays.. Now I'd like to know if there is a software that can emulate/simulate the PIC16F4, because I don't have the PIC and the Programmer here, so first I want to test the code..so I need a software that shows the program I wrote.. It is possible? I have MPLAB but can't find this function
thanks!!
Try this:
http://www.oshonsoft.com/pic.html
CG
thanks
the one in MPLAB doesn't seem to allow a "monitor" view, for example using a led on a port and see if it powers on, or a LCD display, etc..
Dimitree:
Consider using a PIC 16F628A or a PIC 16F648. Perhaps MPLAB can simulate or single-step those better. You might also go to www.piclist.com and inquire about simulation.
Quote from: Dimitree on April 14, 2009, 12:31:25 PM
thanks
the one in MPLAB doesn't seem to allow a "monitor" view, for example using a led on a port and see if it powers on, or a LCD display, etc..
You can do this with the one I recommended but the only problem is that the delays won't be in real time but you can check if the circuit is working properly.
CG
Most of these microcontrollers have onboard timers. You can run these with a specific "reload' value that will allow it to count on a designated interval. Let's say you set your timer so it overflows every 10 microseconds. If you count 10,000 that is 1 second. This would be most suited for interrupt driven operation which you may or may not be familiar with. The basic idea is to tell the microcontroller to jump to "interrupt" code, a handling routine, every 10 microseconds. In that routine you add 1 to a counter, then you compare, is the counter greater than your desired value? If so do something. Else exit. You can set that target value based on your desired delay and make a very accurate system that way. Much more accurate than a loop of NOPs.
These have already been mention above but to summarize you have two or three options really.
1. Create a subroutine where you kill time by looping through NOP instructions. An example is below (It is not accurate just a general idea)...
delay CLRF count1 ;Set count1 to zero.
loop NOP ;These do nothing except burn up time.
NOP
INCF count1,F ;Increase count to indicate once more pass of loop.
MOVLW 0xFF ;Load desired number of loops into working register.
SUBWF count1,W ;Subtract 0xFF from count1 and place result in working register.
BTFSS STATUS,Z;If results was zero count1 had reached desired number of loops so return. If not continue looping.
GOTO loop
RETURN
Be aware the above is just a general idea of how the delay subroutine would work. The code above is just an idea. It could be full of errors. I have just typed it out as I've been writing this. In calculating the time for the loop you have got to consider the control structures around the NOP too as they use instruction cycles and obviously the clock cycle influences the instruction cycle.
2. If you are not using the program for other things you could just set a counter going and constantly poll it until it indicates the correct time has elapsed.
3. If you want to do other things apart from output this signal there is an alternative to option 1. Set a counter to overflow after the desired time and trigger an interrupt. Then all you need to do is use the ISR to toggle the output high and low accordingly.
Hope that helps and I hope I haven't made any mistakes. Please bare in mind though that the method you are using to provide the clock for the PIC is significant in the accuracy of the time periods.
I've found this helpful. Doesn't require any use of the PIC timers.
http://www.piclist.com/techref/piclist/codegen/delay.htm