View previous topic :: View next topic |
Author |
Message |
ak270
Joined: 22 Jun 2014 Posts: 5
|
24 hours timer |
Posted: Tue Jun 24, 2014 2:09 pm |
|
|
Hello
I'm new here, I have searched this forum like a long time, and now I want to start making a few small codes, I made this code, I want 24 in 24 hours PIN_A1 stay in the high state for 5 seconds, and do it 24 in 24 hours until turned off the power!
But this code does not work! This code was made with a sample of this forum!
Please can you check my code and correct me what this poorly done!
thank you very much
compiller Version 4.93
Code: |
#include <18F458.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOLVP
#define DAY 3600
int32 clock1=DAY; //first period
int16 clock2=0;
#define TICKS_PER_SECOND 125
#int_TIMER2
void timer2_isr(void)
{
static int8 tick=TICKS_PER_SECOND-1;
if (tick>0)
--tick;
else
{
tick=TICKS_PER_SECOND-1;
if (clock1>0)
--clock1;
}
}
void main(void)
{
int16 q;
Setup_adc_ports(AN0);
Setup_adc_ports(ADC_CLOCK_INTERNAL);
setup_timer_2(T2_DIV_BY_16,249,10); //125 interrupts/second
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(true)
{
output_low(PIN_A1);
if (clock1==0)
{
clock2=DAY*24;
--clock2;
}
if (clock2==0)
{
output_high(PIN_A1);
delay_ms(5000);
}
}
}
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Jun 24, 2014 2:51 pm |
|
|
Quote: | But this code does not work! This code was made with a sample of this forum! |
You don't tell us what works and what does not.
Is your PC running at the correct speed?
Can you do a 1 second flasher?
.....................
Why not do a 1s flash every 10s?
That way you won't have to wait so long to see if its working.
Then expand to once per minute/hour.
Mike
EDIT Maybe you could use a buzzer, then you don't have to watch for the flash. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9218 Location: Greensville,Ontario
|
|
Posted: Tue Jun 24, 2014 3:24 pm |
|
|
also... in the 'code library' there's a real nice software 'real time clock' program you should look at.
It makes it very easy to do what you want...
hth
jay |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Jun 24, 2014 5:24 pm |
|
|
Hi,
It seems to me that the OP has a a much more fundamental problem beyond the problems he has with his code. He apparently has no way to get any feedback or troubleshooting information from his code except for sitting back and waiting for LEDs to work as expected. This simple strategy *might* work (I emphasize 'might') for the simplest of programs, but for anything more complex, it's really hopeless and an exercise in frustration. Learn to use the debugger, or dedicate an I/O pin to create a software serial diagnostic port to read messages on your PC. If you plan to do any embedded development long term, you'll be way ahead of the game if you do this!
A couple of things really stand out in your post. They are:
1. You #fuses, and your #use delay are really reversed.
2. Your variable names are really poor. Names like 'Clock1' & 'Clock2' are really meaningless, and don't add anything to your understanding of what's going on with the code. How about 'SecsPerDay' or 'MinsPerHour' or other names that actually make some sense.
3. Your use of 'Setup_adc_ports' is wrong, it also seems unnecessary given your program description (which I could not follow anyway.....)
John |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Jun 25, 2014 6:59 am |
|
|
and out of curiosity
what does your .LST file show for this part of the code ??
Code: |
void timer2_isr(void)
{
static int8 tick=TICKS_PER_SECOND-1;
if (tick>0)
--tick;
.....
} |
|
|
|
ak270
Joined: 22 Jun 2014 Posts: 5
|
|
Posted: Wed Jun 25, 2014 1:11 pm |
|
|
Thanks for the comments,!
The problem is that the code is blocked, tried to make a simple code but does not work.
I think that is the problem with Timer2, but do not know where this error! so what I ask your help, although I tried all the ways I know
Any help is welcome
thank you |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9218 Location: Greensville,Ontario
|
|
Posted: Wed Jun 25, 2014 2:00 pm |
|
|
Time to go back to the basics !
Cut code for the simple '1Hz LED' program and CONFIRM an LED does indeed flash at a 1 Hz rate.
That MUST be confirmed before proceeding.....
Then show us your new entire code...
...and explain what does/doesn't 'work'.
hth
jay |
|
|
ak270
Joined: 22 Jun 2014 Posts: 5
|
|
Posted: Thu Jun 26, 2014 12:43 pm |
|
|
Hello guys!!
I made this code below for testing with the timer1 to flash the LED 3 seconds and works well.
I tests for longer times and for 24 hours, and even blocks!
What am I doing wrong?
Can help improve this code to tell 24hortas and connect the LED 5 second?
All help is welcome!
Thanks
Code: |
#include<18F452.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20M)
int16 data=0;
int16 ints=0;
#int_TIMER1
void timer1_isr(void)//100ms timer
{
set_timer1(3036);
++ints;
if(ints>=10)// 1sec
{
data++;
ints=0;
}
}
void main(void)
{
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(true)
{
if(data==3) //3 sec
{
output_high(PIN_A1);
delay_ms(5000);
output_low(PIN_A1);
data=0;
}
}
}
|
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Jun 28, 2014 8:45 am |
|
|
Which variable are you changing to create long delays?
What do you mean, when you say "I tests for longer times and for 24 hours, and even blocks! "
Where does the magic number 3036 come from?
Why have you not taken the hint and included some diagnostics to tell you what is going on?
Mike |
|
|
ak270
Joined: 22 Jun 2014 Posts: 5
|
|
Posted: Sun Jun 29, 2014 5:45 am |
|
|
Hello Mike thanks for the comment!
Quote: | What do you mean, when you say "I tests for longer times and for 24 hours, and even blocks! " |
What I really want is to make the timer1 count 1 second to do a head count after 24 hours put me to the high level (PIN_A1 or any other PIn) for 5 seconds per PIN_XX in low level counting back 24 hours a the PIN_XX at high for 5 seconds, and so continuously until turned off!
Quote: | Where does the magic number 3036 come from? |
Fout = Fosc/4/DIV/set_timer1
Exemple 1Hz:
1Hz = 20.000.000 / 4 / 8 / set_timer1 = 625.000 cycles
Maximum number of cycles the timer 1 (16 bits)
2^16 = 65.536
625.000 / 65.536 = 9,53 apox.10 overflow
625.000 / 10 = 62.500
65.356 - 62.500 = 3036 load the timer1 and wait 10 overflow
or
Fosc = 20.000.000 / 4 => cycles = 5.000.000 Hz => T=0.2Us per increment
Div = 8 => 1.6us per increment
Set timer = 3036
65.536 - 3.036 = 62.500 * 1.6uS = 0.1 seg
0.1 * 10 = 1 Seg
Quote: | Which variable are you changing to create long delays? |
I modified the code, but I am still having problems with errors in time!
Code: | #include<18F452.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20M)
int16 ints=0;
int32 day=86400;
#int_TIMER1 //100ms timer
void timer1_isr(void)
{
set_timer1(3036);
++ints;
if(ints>=10)//1seg
{
ints=0;
--day;
}
}
void main(void)
{
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(true)
{
if(day==0)
{
day=86400;
output_high(PIN_A1);
delay_ms(5000);
output_low(PIN_A1);
}
}// end while
}//end main
|
I know that this code may be better, but as I have yet to start in programming, I posted my questions here, so any help is welcome
I thank all |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9218 Location: Greensville,Ontario
|
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Sun Jun 29, 2014 8:01 am |
|
|
Quote: | I modified the code, but I am still having problems with errors in time! |
Quote: | I know that this code may be better, but as I have yet to start in programming, |
Are you working in Proteus? _________________ Google and Forum Search are some of your best tools!!!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9218 Location: Greensville,Ontario
|
|
Posted: Sun Jun 29, 2014 8:05 am |
|
|
man are 'we' going to look silly when he says yes......
after I've given him the link to a SOLUTION ...
sigh
jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Jun 29, 2014 11:04 am |
|
|
Yes, I can see where the magic number 3036 comes from.
You are assuming there is no delay in getting into your ISR!
I also want to know if you are using REAL hardware.
Mike |
|
|
ak270
Joined: 22 Jun 2014 Posts: 5
|
|
Posted: Sun Jun 29, 2014 2:48 pm |
|
|
Thanks for the help!
Guys, I'm doing tests on Protoboard!
I changed my 18F458 to 18F2550 because the other spoiled, I modified the code for my needs and it works, but I tested the code to PIN_A1 by 2 by 2 minutes (For testing) and makes only 8 in 8 minutes?
Read all posts of the topic and not found the solution, where is the error?
My modified code, to work 2 on 2 minutes (for testing)
help is welcome
I thank all
Code: |
#include <18F2550.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOLVP
//RTC variables
#define XTAL_FREQUENCY 20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4)// 1 clock tick = 1 instr. cycle = crystal frequency / 4
#define prev_second 120
int32 Ticker;
int32 Seconds=0;
void Initialize_RTC(void)
{
Ticker = TIMER1_FREQUENCY; // initialize clock counter to number of clocks per second
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
enable_interrupts( INT_TIMER1 ); // Start RTC
}
#int_TIMER1
void TIMER1_isr()
{
Ticker -= 65536; // Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{ Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
seconds++; // Increment number of seconds
}
}
////////////////////////////////////////////////////////////////////////////////
// Example program for using the RTC
////////////////////////////////////////////////////////////////////////////////
void main()
{
Initialize_RTC();
enable_interrupts( GLOBAL );
// loop forever
while(1)
{
if (seconds==prev_second )
{
seconds=0;
output_high(PIN_A1);
delay_ms(5000);
output_low(PIN_A1);
}
}
}
|
|
|
|
|