Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16f84A with High TECH C -SOME DOUBTS

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
751
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,054
At first, let me tell that, am a beginner in PIC programming. Am using HTC PRO compiler.
I Have some doubts in C programming, i dont know asm.
In the below program,

#include <htc.h>
#define _XTAL_FREQ 4e6 // 4MHz
__CONFIG(0x3FFA);
void main()
{
TRISB=0x00;
TRISA=0xFF;
RB0=1; //STEP 1
__delay_ms( 30000 ); //DELAY ONE
RB0=0; //STEP 2
RB1=1; //STEP 3
__delay_ms( 30000 ); //DELAY ONE
RB1=0;
}

Is there any way to jump from DELAY ONE to STEP 3 while DELAY ONE is on its way (may be just started or just going to finish or any where in between 0 to 30 seconds) on giving a +trigger to any pin in the PIC.
 

well there is, one way to use it is interrupts. the event on port A will generate and interrupt which would essentially break the delay loop and make the program skip. Another approach would be to make a delay in the timer interrupt and your main loop would be free to check events like triggers. If you use your existing program structure the pseudo code will be:

declare loop variable as volatile global;

Interrupt block:
If trigger?
delay variable = 30001

main program:
TRISB=0x00;
TRISA=0xFF;
RB0=1; //STEP 1
for(loop variable = 0; loop variable < 30000; loop variable ++)
__delay_ms(1)

if(loop variable >30000)
goto STEP 3

STEP 2
STEP 3

<rest of the code>

///////
Without interrupts:




main program:
TRISB=0x00;
TRISA=0xFF;
RB0=1; //STEP 1
for(loop variable = 0; loop variable < 30000; loop variable ++)
{
__delay_ms(1)

If trigger?
delay variable = 30001

}

if(loop variable >30000)
goto STEP 3

STEP 2
STEP 3

<rest of the code>



If interrupts aren't your way yet, the second method is for you. if an event occurs you can set the variable to 30001 that will make it exit the delay loop next time the condition is checked. The only down side is that the trigger must be longer than 1mS


hope it helps
 
actually wats the difference between the two methods u discribed? @SPIRALBRAIN
 

the answer depends on what you meant by "jumping".

the simplest solution, which is also the most complicated solution, is to use a task scheduler to run two "near simultaneous" tasks, one for the pin and one counts the delay. that solution is more generic and depending on your skills can be faster to implement.

another is to use a timer interrupt. let's say that you use a 1s timer interrupt. in the interrupt service routine, you increment an interrupt counter.

in the main code, you have a piece of the code that checks to see if the interrupt counter has reached 30 (indicating that 30 seconds have passed). if that is true, you reset the interrupt counter and flip a pin. this solution is simpler but can be more difficult to implement for you.

---------- Post added at 11:42 PM ---------- Previous post was at 11:09 PM ----------

here is an example of a task scheduler, implemented on C51 - the code can be easily implemented on other platforms as well - I posted one for PIC for example.

in this particular example, the code has four tasks (+ 1 idle process - not implemented in this particular example), each running on 10ms time slots (user configurable). so every 40ms, you run through every task once.

Task0 updates the LCD based on the adc results from Task3, and flips P2.0, once every 8 runs -> 320ms.
Task1 flips P2.2, once every 25 runs -> 25*40ms=1000ms.
Task2 flips P2.6, once every 80 runs -> 80*40ms=3.2 seconds.
Task3 does simulated ADC, once every 6 runs and passes the results to Task0.

the timing chart attached confirms the timing described above.

so this shows you one way of implementing "multiple" delays on any mcu - all it requires is a timer interrupt.
 

Attachments

  • C51 XOS.PNG
    C51 XOS.PNG
    41.7 KB · Views: 138

the task scheduler itself is very small: less than 100 bytes on PIC and about 150 bytes on C51, including start-up overhead. so it adds very little to any existing program.

it does have some limitations, like each task has to be finished within the allotted time slot (10ms in this case). But for people wanting to run "near simultaneous" code without an OS, it saves lots of time.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top