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.

timer running on background for PIC16F788

Status
Not open for further replies.

brew

Member level 3
Joined
Mar 4, 2011
Messages
59
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,693
timer running in background for PIC16F88

hi guys.

i am trying to create a timer that runs in the background while the pic is doing the main program.
the flow is like this

pic is doing something, timer = reset
pic is idle, timer starts and counts until the pic does something again

when the pic has been idle for a specific time, the pic goes to sleep and can be waked up when interrupted

i was thinking of using a 555 timer but this consumes a lot of current. the current consumption for the system is crucial and i need to minimize/conserve it as much as i can.

thanks in advance
regards, brew.
 
Last edited:

IIRC, the 16 series PICs I used in the past had two primary types of timer functions... an overflow counter, and a watchdog (those were all I used at the time). The overflow counter was set to some value less than the overflow value of the register. When the background count overflowed, it would trigger an interrupt, which would call the ISR (Interrupt Service Routine) function that I coded. If it was an 10-bit PIC, then the max value was 11 1111 1111 (plus 1) = 1024. If it took 1 ms per count, and you wanted something to happen every 50 ms, then you'd set the initial value of the counter to 1024 - 50 = 974. Once the counter overflowed, it'd flag the interrupt, the ISR would be called and run, do something, then reset the counter to 974 and the process would repeat (counter runs in background, and PIC goes back to where it was in the main program).

The watchdog timer is similar, except that you'd use it to reset the PIC if the watchdog counter elapsed. To keep that from happening, you'd call a function every once in a while in your main program that would "kick the watchdog", reseting the counter, so the PIC wouldn't time-out and automatically reset.

Others can help you with the code, but that's the basic premise of the timers. Some of that may not be totally correct, as I haven't done PIC coding in ~7 yrs.
 
  • Like
Reactions: brew

    brew

    Points: 2
    Helpful Answer Positive Rating
hey enjunear, thanks for the reply. the problem for me is, the count per increment for the timer is very limited, which means that it will overflow without reaching the desired duration. i want the idle time for the pic to go to sleep is at around 10 mins and i don't know how i will reach that value.

regards, brew
 

PIC16F788?

Is this a typo or some ancient PIC 16F, that Microchip has long forgotten?

I can most likely advise you with this issue.
However, I need additional details before making a recommendation:

What is the PIC you are using? If it truly is a PIC16F788, upload a datasheet.

What are all the duties or interrupts the PIC is servicing when not idle? The more details the better.

Is this a battery power app? If so, what is the source, battery type, etc?

I may need additional info, but this will get me started.
 

hi bigdoggur, thanks for the reply and yes, it's a typo, i am very sorry. it's PIC16F88, i'll edit the title.

for the details, i haven't enabled any interrupt yet. when not idle, the pic is communicating with another device which is an nRF24L01+ through SPI.
i have an input pin; if high, sends an SPI command to the nRF24L01+. the SPI command will command the nRF24L01+ to send a byte of data wirelessly.

the system is battery operated.

regards, brew.
 

it's PIC16F88, i'll edit the title.

Ok, that is helpful.

for the details, i haven't enabled any interrupt yet. when not idle, the pic is communicating with another device which is an nRF24L01+ through SPI.
i have an input pin; if high, sends an SPI command to the nRF24L01+. the SPI command will command the nRF24L01+ to send a byte of data wirelessly.

What data is being transmitted through the nRF24L01+? How is the data obtained, a sensor, user input, etc? Is it a bidirectional RF link, in other words, does the PIC16F88 receive any data from the other end?

I need to know what if any other devices are attached to the PIC and powered by the battery.

the system is battery operated.

What type of battery, lithium, lead acid, etc? What is its rating? Do you have a model or part number for the battery?

I'll download a copy of the PICs datasheet and start look over the details.
 

the data to be sent, for now, is a hex number; 0xAA. the one that triggers the PIC to send an SPI command to the nRF24L01+ is a logic high on the input pin.
the PIC16F88 is not receiving anything from the other end because i configured the nRF24L01+ as a dedicated transmitter.

for the battery, i don't have any specifics since i'm still using a DC supply.

thanks :) i'll also attach the code that i have created so far.
 

Attachments

  • TX2.txt
    8.3 KB · Views: 57

Re: timer running in background for PIC16F88

when the pic has been idle for a specific time, the pic goes to sleep and can be waked up when interrupted

Based on your specifications, I believe you should take the opposite approach. You should initialize the PIC and the RF transmitter, then put the PIC to sleep, which will be awoken when an external interrupt occurs:

logic high on the input pin

the PIC will then transmit the data and go back to sleep waiting for the next external interrupt to occur.

The above routine will provide for maximum battery life, while still providing the functionality you desire.

You could power down the transmitter as well and save additional power, however this may require you to reinitialize the unit before transmitting. I would have to take a look at the transmitter datasheet to formulate the best approach to dealing with it.

I'll see what documentation I can provide to aid you in the coding of this approach.

Hope the advise helps you in your endeavors.
 
  • Like
Reactions: brew

    brew

    Points: 2
    Helpful Answer Positive Rating
Re: timer running in background for PIC16F88

thank you, sir. the application that i will be using this set-up for is a wireless keyboard. the sleep part is for the power management of the transmitter and the pic itself. i am looking forward to your next reply. :)

regards, brew.
 

If you could provide the datasheet for the nRF24L01+ you will be incorporating into you design, it would be appreciated. I have a few examples of the approach I outlined above on the server. I would like to take a look at the datasheet for the RF transmitter before I'll dig them up and post them for you.
 

hey enjunear, thanks for the reply. the problem for me is, the count per increment for the timer is very limited, which means that it will overflow without reaching the desired duration. i want the idle time for the pic to go to sleep is at around 10 mins and i don't know how i will reach that value.

regards, brew

Make a loop-of-loops, so to speak. Set your counter to run the full duration (gives you X milliseconds per interrupt event). When the ISR runs, you simply add 1 to a global variable. Inside the ISR, you check that variable against some threshold number (10 minutes, converted to milliseconds, divided by X). Inside that IF statement is where you then execute the the "enter sleep" commands, rather than right at the top of the ISR, itself.
 
  • Like
Reactions: brew

    brew

    Points: 2
    Helpful Answer Positive Rating
Make a loop-of-loops, so to speak. Set your counter to run the full duration (gives you X milliseconds per interrupt event). When the ISR runs, you simply add 1 to a global variable. Inside the ISR, you check that variable against some threshold number (10 minutes, converted to milliseconds, divided by X). Inside that IF statement is where you then execute the the "enter sleep" commands, rather than right at the top of the ISR, itself.

thanks :) i'l try to this one.
 

hey, quick question. is the ISR function for all the interrupts? if not, how will assign a specific ISR function to a specific interrupt? thanks :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top