|
|
View previous topic :: View next topic |
Author |
Message |
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
Changes state in PIN-A1 times larger. |
Posted: Mon Apr 01, 2013 4:05 am |
|
|
Hii!
I have a program here that I have not gotten a positive result I intend to do!
It is the next program to low want to do the following as soon as I turn on the PIC.
For two days in Pin_A1 does nothing.
After two days Pin_A1 desire that the switching state is five by five hours, or 5 hours in the high state (1) and then 5 hours at low state (0) and changing state is 5 in 5 h during 10 days, after 10 days the state is Pin_A1 shut down until the PIC, is in this part of the code that I do not have any idea of making this state change and it's been a week to research, test, and so far no have a positive outcome!
I would like the staff to take a look at the code to be able to give help how should I resolve this problem.
Time does not have to be very precise so I'm using TIMER1.
The part that I doubt this checked, if someone has a better idea I want to do is welcome
I thank
Code: | #include <18F458.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOLVP
int16 segundos=0;
int8 intt=0;
#int_TIMER1
void timer1_isr(void)
{
set_timer1(3036);
++intt;
if(intt>=10)
{
Segundos++;
intt=0;
}
if(segundos==3600)
{
hora=hora +1;
Segundos=0;
}
If (hora==24)
{
Day=day+1;
hora =0;
}
}
void main(void)
{
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(true)
{
while(day<=2);
//***Here in this part where I have problem in making the code//****
output_toggle(PIN_A1);
}wihle( day<10)
output_low(PIN_A1);
//**********************************************************
}
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Apr 01, 2013 5:19 am |
|
|
Start at the beginning.
Your timing.
Now first, do a search in the forum, for why setting a timer 'to' a value is always going to be _very_ inaccurate. Why accept such inaccuracy if you don't have to?.
You have timer2, unused, so go for an accurate timing.
Then, why fiddle around with days, hours etc.. Just work in seconds.
You want times of 172800 seconds, then states of 18000 seconds repeated for a period of 864000 seconds.
So:
Code: |
#include <18F458.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOLVP
#define FIVE_HOURS 18000
#define DAY 86400
int32 clock1=DAY*2; //first period
int32 clock2=0;
#define TICKS_PER_SECOND 125
#int_TIMER2
void timer2_isr(void)
{
static int8 tick=TICK_PER_SECOND-1;
if (tick>0)
--tick;
else
{
tick=TICKS_PER_SECOND-1;
if (clock1>0)
--clock1;
if (clock2>0)
--clock2;
}
void main(void)
{
enum {FIRST_WAIT,FLASHING,STOP} main_state=FIRST;
enum {IDLE, PIN_ON} flash_state=IDLE;
setup_timer_2(T2_DIV_BY_16,249,10); //125 interrupts/second
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
while(true)
{
switch (main_state) {
case FIRST:
//here we just wait A1 off
output_low(PIN_A1);
if (clock1==0)
{
//state has ended
//set clock to 10 days
clock1=DAY*10;
main_state++;
}
break;
case FLASHING:
//Here we want to flash the output every 5 hours
switch (flash_state) {
case IDLE:
//first position - start flasher
clock2=FIVE_HOURS;
flash_state++; //move to flash pin
output_high(PIN_A1);
break;
case PIN_ON:
if (clock2==0)
{
//time has finished
clock2=FIVE_HOURS;
output_toggle(PIN_A1);
}
break;
} //end of 'flash' switch
//now check for the ten day timer
if (clock1==0)
{
output_low(PIN_A1);
main_state++;
flash_state-IDLE;
}
break;
case STOP;
//Stopped - do nothing forever....
break;
} //end of main switch
} //end of while loop
}
|
Unchecked. Just typed in.
However the point is to think of the different 'jobs' and treat each separately.
Wait for 2 days
Flash for 10 days
Stop
Then only the middle one needs do much, just flashing the output every five hours.
Just two 'state machines' inside one another controlled by two timers.
Best Wishes |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Mon Apr 01, 2013 1:30 pm |
|
|
hello Ttelmah !!
Thanks for helping, is undoubtedly a great help and very important to me, so I'm studying the code and understand the logic programming.
When compiled the code appeared some errors I was seeing then where was the error and made the correction.
What is not yet understood this was the variables "" main_state "" and "" flash_state "" along with "enum", if I can explain how it works, I've never worked with "enum" although I've seen its function
I'll test the code in phortboard and then post the result here
thank you very much |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Mon Apr 01, 2013 1:37 pm |
|
|
As I said 'untested'. Probably several typing errors.
Think of enum's as a way of using names instead of numbers. Rather like having a pair of defines for a boolean, as (say)
#define ON 0
#define OFF 1
Nice thing is that since the enum is a variable, and the values have names, the values will exist in a debugger. In higher level languages, they are also type safe, and unlike a define, can't accidentally be redefined.
Best Wishes |
|
|
fiasgardone
Joined: 19 Jan 2010 Posts: 71
|
|
Posted: Mon Apr 01, 2013 1:48 pm |
|
|
Ttelmah wrote: | As I said 'untested'. Probably several typing errors.
Think of enum's as a way of using names instead of numbers. Rather like having a pair of defines for a boolean, as (say)
#define ON 0
#define OFF 1
Nice thing is that since the enum is a variable, and the values have names, the values will exist in a debugger. In higher level languages, they are also type safe, and unlike a define, can't accidentally be redefined.
Best Wishes |
Hii! Ttelmah
Thanks for clarifying.
One more doubt, and these variables do not have to be declared before used, for example at the beginning of the program?
thank you |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Apr 01, 2013 2:30 pm |
|
|
as a 'helpful hint' when coding for long intervals....
...test using seconds and not hours or days !!!
ie: if the real world time is 5 hours, test using 5 seconds
Yeah, kinda obvious,but........
hth
jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|