Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

[SOLVED] Time Delay with micro-controller

Status
Not open for further replies.

psb64

Newbie level 4
Joined
Jun 5, 2011
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Sri Lanka
Activity points
1,325
I want to keep a relay off for 20hrs and keep it on for 14 hrs using a micro-controller. Please help me by giving the necessary program (in mikroBasic/Assembly) and schematic diagram to do so.:)
 
Last edited:

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,021
Reputation
4,158
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
I don't think you will find anyone to provide you a ready solution, this is a forum to help with your project when you have a specific problem not do the projects for you.
What you want to do can be achieved using an MCU with a timer, set the timer to work at a specific rate and calculate the times that the timer needs to overflow.
To achieve the delay you want, keep a variable to count the overflows of the timer and when the number matches the one you have calculated change the output.
If you are using an ARM based mcu then you will not need to do an overflow at all because the counter is 32 bit (usually there is at least one 32bit timer) and can be set to count for many days before it overflows, in that case a compare match register set to the correct value will be all that you need.

Alex
 
Last edited:

arbj

Full Member level 4
Joined
Oct 5, 2008
Messages
238
Helped
31
Reputation
62
Reaction score
20
Trophy points
1,298
Activity points
2,601
I can give you a hint, you need a micro-controller, and a RTC, I suppose a watch crystal would also do.

Setting up an external watch crystal with the PIC 16F628 is quite easy, the schematic and code for this is available in the datasheet for the device, also there are numerous projects in the internet that use this combination. it should not be difficult to search for a suitable one.

Try searching in google, select a few projects that you think can do the job for you, then post back to this forum for more help.
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
you have to use micro-controller with RTC so that you can get your functionality i.e write a Assembly code or Embedded C code for switching ON-OFF i.e set the ON time and OFF time of relay...I hope this will hep to you
 

psb64

Newbie level 4
Joined
Jun 5, 2011
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Sri Lanka
Activity points
1,325
I want to use the PIC16F84A to do this and the cristal should be 4Mhz

---------- Post added at 21:47 ---------- Previous post was at 21:36 ----------

I found the following circuit in mikroBasic. Will it work?

program timer84EDA

symbol relay = PORTB.B0

const ontime = 43200000
const offtime = 75600000

main:
TRISB = 0
PORTB = 0
relay = 0 'relay off
delay_ms(offtime) 'twenty-one hour delay
relay = 1 'relay on
delay_ms(ontime) 'twelve hour delay
relay = 0 'relay off
while true 'done
wend
end.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,508
I never used MikroBasic, is the delay_ms() parameter a type long?

The 75600000 is quite a large number for MCU, so if the delay_ms() doesn't take a type long that will be a problem.

If it does take a type long, then there is only one way to test it out, compile and program your 16F84A and see.
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

xaccto

Full Member level 5
Joined
Oct 30, 2005
Messages
253
Helped
41
Reputation
82
Reaction score
35
Trophy points
1,308
Activity points
3,300
I've never used mikro tools but do have the wiz development environment from PIC C Compilers and development ki...

you can download their demo, and it should go alright for a 16F84 size chip.

whats more you can simulate your solution easily before wiring any hardware
App designer also has ready built application blocks like timers, clocks, ports etc you plug into your application - builds a workable shell
for your app.
check it out at PIC C Compilers and development ki...
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,508
I want to keep a relay off for 20hrs and keep it on for 14 hrs using a micro-controller. Please help me by giving the necessary program (in mikroBasic/Assembly) and schematic diagram to do so.:)
I want to use the PIC16F84A to do this and the cristal should be 4Mhz

---------- Post added at 21:47 ---------- Previous post was at 21:36 ----------

I found the following circuit in mikroBasic. Will it work?

program timer84EDA

symbol relay = PORTB.B0

const ontime = 43200000
const offtime = 75600000

main:
TRISB = 0
PORTB = 0
relay = 0 'relay off
delay_ms(offtime) 'twenty-one hour delay
relay = 1 'relay on
delay_ms(ontime) 'twelve hour delay
relay = 0 'relay off
while true 'done
wend
end.
I'm not familiar with MikroBasic, however I believe you placement of the while loop is incorrect.

If you wish to have the relay cycle on/off repeatedly, you may want to use this version instead:

Code:
program timer84EDA

symbol relay = PORTB.B0

const ontime = 43200000
const offtime = 75600000

main:
  TRISB = 0
  PORTB = 0

  while true 'done
     relay = 0 'relay off
     delay_ms(offtime) 'twenty-one hour delay
     relay = 1 'relay on
     delay_ms(ontime) 'twelve hour delay
  wend

end.
This encloses the portion of the code you wish to repeat continually. Also a little indentation would help you understand the code flow.

Due to the 34 hours cycle period, each new cycle will be offset by 10 hours. So if you power up the MCU at Midnight, the 2nd cycle with start at 10AM day 2, the 3rd cycle at 8PM day 3 and so forth.....

The continually shifting start time makes the use of a RTC unnecessary and somewhat impractical. If the cycle period were equal to or less than 24 hours it would be a practical alternative.

Also your choice of the PIC 16F84A, unless it is what you have on hand, ill fitted for this application. A baseline PIC like the PIC 12F50X family with an internal 4MHz oscillator and low cost would be a better choice. The use of a PIC 12F50X would not only be a less expensive option, it would alleviate the need for an external crystal or RC pair. However if this is what you have on hand, then it is understandable.

Hope the info helps in your endeavors.
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

JayantD

Member level 3
Joined
Nov 2, 2010
Messages
58
Helped
9
Reputation
18
Reaction score
9
Trophy points
1,288
Location
Pune, India
Activity points
1,549
You need to be careful about the accuracy of your system clock. Any small error is going to accumulate i.e. add up and show as an inaccuracy at the end of your 20 hours time period. If this cycle is to continue, drift due to ambient temperature also becomes significant. Stray capacitance around the crystal also needs to be tackled by careful routing the tracks on the printed circuit board.

I would suggest decide the upper and lower tolerence limits first, and then go about designing your circuit.
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

cool.man

Full Member level 5
Joined
May 29, 2008
Messages
317
Helped
42
Reputation
84
Reaction score
29
Trophy points
1,308
Location
Earth
Activity points
3,247
const ontime = 43200000
const offtime = 75600000

delay_ms(offtime) 'twenty-one hour delay
If you have no other function to perform then you can use delay_ms() function, but this function is not suitable i think because you are defining a large delay which is in hours,suppose in the delay routine your processor hangs up then what will happens? you have to restart your microcontroller,then the whole process starts again.
you can define delays in terms of loop like "for loop" and in for loop you can use watchdog timer which prevents the microcontroller from restart.

Best regards
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

sankar.m8

Advanced Member level 4
Joined
Apr 15, 2011
Messages
106
Helped
14
Reputation
28
Reaction score
14
Trophy points
1,298
Location
Chennai, Tamil Nadu, India
Activity points
1,884
If you are using a 4MHz crystal....

The operating frequency will be 4MHz/4 = 1MHz (4 machine cycle constitute 1 instruction cycle)

ie, it will execute 1,000,000 instructions in 1 second.

so to get a 1 second, you have to use a for loops which will consume 1,000,000.

for a minute 60 times the 1second loop

for hour, 60times the 1minute loop..

from this you will get the exact timing...

This will be effective only when your microcontroller is dedicated for this ON/OFF of the relay(you cant give any other work to the microcontroller other than this delay counting)...else you have to use any other methods like Timers,external RTC chip.
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,508
If you are using a 4MHz crystal....

The operating frequency will be 4MHz/4 = 1MHz (4 machine cycle constitute 1 instruction cycle)

ie, it will execute 1,000,000 instructions in 1 second.

so to get a 1 second, you have to use a for loops which will consume 1,000,000.

for a minute 60 times the 1second loop

for hour, 60times the 1minute loop..

from this you will get the exact timing...
The only way to achieve timing delays of high accuracy, without exhaustive testing and some accurate test equipment, is to write the delay routines in assembler, accounting for every cycle consumed by each instruction under each circumstance. Using any high level language to directly generate a delay is inherently inaccurate due to the fact the resulting code is highly compiler and optimization dependent.

Delay routines such as delay_ms(), if they have a high degree of accuracy, are typically written in assembler and furnished as relocatable code modules.

Even if these measures are taken, exact timing delays do not exist, due to the inherent inaccuracies of the oscillator/clock and variations due to temperature changes and other factors.
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

sankar.m8

Advanced Member level 4
Joined
Apr 15, 2011
Messages
106
Helped
14
Reputation
28
Reaction score
14
Trophy points
1,298
Location
Chennai, Tamil Nadu, India
Activity points
1,884
nsec_a:
movlw D'100'
movwf delay1
nsecloop_a:
nop
nop
nop
nop
nop
nop
nop
nop
nop
clrwdt
decfsz delay1
goto nsecloop_a
return

nsec1a:
call nsec_a
decfsz delay2
goto nsec1a
return

msec250a:
movlw D'250'
movwf delay2
goto nsec1a


This is just a sample code to generate delays

If you move the value into the delay2 register and following the above procedure, you will get almost correct delays...
If you are a perfectionist, you need to consider the delays which are taken for the each instructions too....
 
  • Like
Reactions: psb64

    psb64

    points: 2
    Helpful Answer Positive Rating

psb64

Newbie level 4
Joined
Jun 5, 2011
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Sri Lanka
Activity points
1,325
I thank everyone who helped me with this project for their help and contribution.:smile:
 

d@nny

Full Member level 5
Joined
May 28, 2011
Messages
246
Helped
11
Reputation
22
Reaction score
11
Trophy points
1,298
Activity points
3,238
; Delay = 7.56e+007 seconds
; Clock frequency = 4 MHz

; Actual delay = 75600000 seconds = 75600000000000 cycles
; Error = 0 %

cblock
d1
d2
d3
d4
d1_4
d1_5
endc

;75599999999990 cycles
movlw 0xD7
movwf d1
movlw 0x2A
movwf d2
movlw 0xF0
movwf d3
movlw 0x00
movwf d4
movlw 0x4A
movwf d1_4
movlw 0x06
movwf d1_5
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto $+2
decfsz d4, f
goto $+2
decfsz d1_4, f
goto $+2
decfsz d1_5, f
goto Delay_0

;10 cycles
goto $+1
goto $+1
goto $+1
goto $+1
goto $+1
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top