2

My friend asks me to help him write a small program for PIC12 MCU. We want

  1. The program stops running when input voltage is less than 1.9V during 2 seconds.
  2. The program reacts immediately when input voltage exceeds 2.5V.

I try to solve the 1st problem by reading and comparing the system's timestamp:

#include <time.h>
... ...
time_t beg, end;
beg = 0;
end = 0;
while(1){
    if(INP_VOL < 1.9){
        if(beg == 0){
            /* Read timestamp when voltage < 1.9 */
            gmtime(&beg);
        }
        /* Campare timestamp */
        gmtime(&end);
        if(end - beg > 2){
            break; /* !!stop running!! */
        }
    }
    else{
        /* if voltage back to normal, reset beg timestamp. */
        beg = 0;
    }
}

I've found the function gmtime(time_t *) in PIC12 User Manual, but I'm not sure if it a good solution.

But I can't figure out how to solve the 2nd problem. It should be kind of independent thread which moniters the input voltage during the execution of the program. And the program should react immediately (by calling an other function) before the circuit is damaged.

I'm computer programmer, but I've never coded for MCU. I'd like to know if it's possible to do such thing in HI-TECH C ?

Allopopo
  • 171
  • 1
  • 9
  • Time, I've found, is best measured by a counter on a timer interrupt. When it reaches your threshold, set a flag that can be processed in your main loop. – kenny Feb 08 '12 at 15:14

3 Answers3

4

The typical thing to do here is to use interrupts, specifically timer interrupts.

You set up an interrupt to run e.g. every 1 ms, and in that interrupt code you do whatever the program needs to react quickly to. That leaves the normal execution flow alone, and emulates that the two tasks are done in parallel.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I think this is the only solution that I can try. Thank you unwind, I'll read the chapter of Interrupt Hanlding in C. – Allopopo Feb 06 '12 at 16:46
1

You could have a circuit attached to the external interrupt pin, that gives 1 when voltage goes above 2.5. The external interrupt can be programmed to kick whenever its input goes from 0 to 1.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • I don't know how he's designed his circuit, as I said I'm only a computer programmer. Thank you anyway ;) – Allopopo Feb 06 '12 at 16:44
  • +1 FYI, most the PICs have a comparator input that can provide an interrupt on some voltage threshold. – kenny Feb 08 '12 at 15:13
0

I don't think C language is the best solutions for PIC12 family.

My suggest is to use ASM. It's very simple by few instructions.

After set the ADC you can use substraction instruction and check the C (carry) In this manner you can verify IF > or IF < Test C and skip if zero. Skip the next instruction, the one with the call.

you can also change micro and use PIC18 for better c code performance.

Emanuele
  • 9
  • 2
  • Thank you for your answer Emanuele, really appreciate it. But unfortunately I don't know assembly language ;) – Allopopo Feb 06 '12 at 16:43
  • C is fine IMO, if it fits it ships. Optimize for size/speed only if you need to. – kenny Feb 08 '12 at 15:12
  • C is always a better solution; easier to port, more readable, understandable across platforms, and modern compilers can be almost as efficient. – Adam Casey May 08 '12 at 17:11