Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC12F675 First Interrupt Project

Status
Not open for further replies.

wuchy143

Member
Recently I've decided to learn how to program micros in C. I"m a hardware guy trying to broaden my knowledge so when it comes to programming I"m quite a noob. That said:

My project is simple. I'd like to design a keyboard hour meter which will basically log the amount of time the keyboard has been powered up throughout it's life. The highest it can go is up to 9999 hours. My chip needs to be able to figure out how long it has been powered up and then spit that out on a port serially every minute.(It will be hours but in ascii code) I will then hook that up to my serial port on my computer and see the amount of hours on a hyperterminal connection every minute.

I have attached a block diagram of where I am in the design so far. Logically I think it should work but wanted to see what people on here think about my path so far. What I"m missing or overlooking. I know nothing about timers/interrups but I do know that they are going to be needed in this application. Basically can someone give me some direction but not just the answer. That won't help me any as I"d like to do a lot of this myself. It's the only way I learn.

Responses much appreciated!!
 

Attachments

  • block_diagram.JPG
    block_diagram.JPG
    78.9 KB · Views: 397
Recently I've decided to learn how to program micros in C. I"m a hardware guy trying to broaden my knowledge so when it comes to programming I"m quite a noob. That said:

My project is simple. I'd like to design a keyboard hour meter which will basically log the amount of time the keyboard has been powered up throughout it's life. The highest it can go is up to 9999 hours. My chip needs to be able to figure out how long it has been powered up and then spit that out on a port serially every minute.(It will be hours but in ascii code) I will then hook that up to my serial port on my computer and see the amount of hours on a hyperterminal connection every minute.

I have attached a block diagram of where I am in the design so far. Logically I think it should work but wanted to see what people on here think about my path so far. What I"m missing or overlooking. I know nothing about timers/interrups but I do know that they are going to be needed in this application. Basically can someone give me some direction but not just the answer. That won't help me any as I"d like to do a lot of this myself. It's the only way I learn.

Responses much appreciated!!

Base your interrupt on a timer roll-over. Increment a variable in the ISR when the interrupt occurs, to count the roll overs until you mark one second, then set a flag which you read in the main loop. Look up Real Time Clock for examples. What is important is that you do not try to do 'too much' in your service routine. You want to do all the heavy lifting in your main routine.
 
BeeBop,

Thanks for the reply!! Let me do a little research on what you said and I will get back to you so we can have somewhat of an intelligent convo :)

-mike
 
Also I get a lot out of examples so if anyone knows of any great examples of how you implement an interrupt I'd love to take a look
 
Does anyone know where I could find some information/examples on how to set-up a simple interrupt in C code for a PIC/Micro. I understand what interrupts are. Why we use them and why it applies to my current project but I'm having some trouble getting down to the fine details so I can then apply interrupts to my design in C. Please help :(
 
Does anyone know where I could find some information/examples on how to set-up a simple interrupt in C code for a PIC/Micro. I understand what interrupts are. Why we use them and why it applies to my current project but I'm having some trouble getting down to the fine details so I can then apply interrupts to my design in C. Please help :(

Which compiler are you using? That will dictate how to write your handler. In SourceBoost, I do it like this:
Code:
void interrupt( void ) {

    static short new_value;
    static short old_value;
/*    Timer 1 will overflow at 65535 and if it does we have to take this into account in our math
    freq is a global which can hold a long value so lets put the result there
*//
    if (test_bit(pir1,CCP1IF)) { // 4. When a CCP interrupt occurs:
    //    test = 0x01;
        //    a) Subtract saved captured time (old_value) from captured time (new_value)
        // if no rollover of timer 1 has occurred, then the difference is our time

        MAKESHORT(new_value, ccpr1l, ccpr1h);    // or ccpr1l and ccpr1h 
        // now record timer1 value

        if (test_bit(pir1,TMR1IF)) {
            // timer 1 had overflowed so we need to add 65535 to the old_value before we subtract
            // the value of 
            freq = = old_value - new_value + 0xFF;
            clear_bit(pir1, TMR1IF);
        } // end if
        //    and store (use Timer1 interrupt flag as overflow indicator.)
        else {
            freq = old_value - new_value;
    
        } // end else
        //    if (pir1.CCP1IF) {    // must be cleared in software
        clear_bit(pir1, CCP1IF);    // clear capture flag
        old_value = new_value;
    } // end if
    first_time = FALSE;
} // end interrupt

If I remember, in HiTech it is something like this:
Code:
void interrupt tmr0_int(void) {
    char temp;
    
    if(T0IF) {        // check timer 0 interrupt flag
        T0IF = 0;    // timer 0 overflowed
        RTC++;        // incriment real time clock

        CurrentRTC = (RTC & 0x0FF) - ((SaveRTC >> 8) & 0x0FF);
        if (CurrentRTC <0)
            CurrentRTC = 0 - CurrentRTC;
        } // end if
    
} // end interrupt

Don't count on the above actually working; I just copied a couple from my src directory... :p
 
Thanks Beebop. Unfortunately I can't afford a real compiler so I"m using High Tech C lite. Though, for my application it suits my needs just fine. Plus it's free :)

I'm going to take a look at this now. Thanks again. I found reading the high tech C manual to be very helpfull. So if anyone is reading this and is at the same point I"m at I highly suggest going to section 5.21 in the manual and read "interrupt handling in C" haha RTFM!

-mike
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top