I tried adding DEFINE WRITE_INT 1 but still getting same results![]()
The T1CON.0 = 0 line is turning off the Timer.
So it never counts again.
At the start of the program, the SecondsChanged bit is set so that programs will display the initial 00:00:00.
So it stops counting before the first second passes.
<br>
DT
Some days I feel dumber than others and today is one of those!
I tried joining this code with my code and things are not going so well... I have a piezo beeping on the press of the swithes and when I added this code it is more like rigning than beeping and also I have a feeling I need something short and simple but I am trying to use something big and complex.
All I really need is to have a counter that counts the minutes up to 720 and then cycle to 0 and start over endlessly. I don't need an interupt as I can test compare by branching from the main loop (in one minute I have plenty of time to test) Accuracy is not really important + or - 10 minutes over 720 is ok. I am already using timer0 so I guess I need to use timer1 or timer2.
Do you have any simple and quick advice or link to suggest?
I feel I have taked already too much of your time and I apologize for that.
Mike
Hi Mike,
Welcome to My World!
A great clock this will not be, it does (should do) what you asked, you may need to juggle the numbers if you have a bunch of code to stuff in.Code:counter var byte minutes var word clear ' zero variables at start loop: PortB = %00000000 TRISB = %00000000 counter = counter + 1 Pause 1000 If counter >= 60 then minutes = minutes + 1 : counter = 0 if minutes >= 720 then gosub action goto loop Action: minutes = 0 Toggle PortB.1 ' change this to do what you need done return
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Thank you Joe,
Unfortunately the PAUSE 1000 within my main loop is making the response too slow.
This is my main loop ( added your example) when I press on any of the 6 switches it needs to react faster than a second later. This is why I am thinking I probably need to use timer1.
Code:Main: l=0: y=0 WHILE !T0IF : WEND ; wait for timer to overflow TMR0 = 193 ; load timer for 1ms T0IF = 0 ; reset the overflow flag IF (SW1=0) THEN Counters[0] = DelayTime : goto Channel1 IF (SW2=0) THEN Counters[1] = DelayTime : goto Channel2 IF (SW3=0) THEN Counters[2] = DelayTime : goto Channel3 IF (SW4=0) THEN Counters[3] = DelayTime : goto Channel4 IF (SW5=0) THEN Counters[4] = DelayTime : goto Channel5 IF (SW6=0) THEN Counters[5] = DelayTime : goto Channel6 FOR X = 0 to 5 ; cycle thru counters IF Counters(X) > 0 THEN ; if counter is counting Counters(X) = Counters(X) - 1 ; decrement counter IF Counters(X) = 0 THEN ; if counter timed out SELECT CASE X ; turn on the appropriate LED CASE 0 : if led1 = 1 then gosub StartCharge1 CASE 1 : if led2 = 1 then gosub StartCharge2 CASE 2 : if led3 = 1 then gosub StartCharge3 CASE 3 : if led4 = 1 then gosub StartCharge4 CASE 4 : if led5 = 1 then gosub StartCharge5 CASE 5 : if led6 = 1 then gosub StartCharge6 END SELECT ENDIF ENDIF NEXT X ;-------------------------------------------------/ ; update counter / ;-------------------------------------------------/ counter = counter + 1 Pause 1000 If counter >= 60 then minutes = minutes + 1 : counter = 0 ; trigger off if it is time if minutes >= 720 then minutes = 0 GOTO Main
Well, that in itself implies there are days when you don't feel that way ...
Only prior instances of feeling "Smart" can make you recognize the "dumb" when it comes around.
Let's see if we can't make tomorrow a little better.
At first I was trying to make it easy with an example Without Interrupts.
Then you found my interrupt program, and the Elapsed Timer.
Combining the two has no doubt created the headaches.
I've tried to figure out what you were doing from the previous posts, and failed.
If I could see what you had so far, I might be able to combine things using only DT_INTS.
Who knows?
<br>
DT
Ok so in many occasions I felt like just putting up all my code but then I felt embarassed knowing how many people wil laugh...
But here it is.
This is what I am trying to do:
I have 6 switches SWx, 6 LEDs LEDx and 6 Optoisolator (which will trigger TRIACS) TRx
Normal mode:
if I press SW1, it will turn on LED1 and wait 10 seconds before turning on TR1.
The 10 seconds is just to wait in case I wanted to get in program mode.
Program mode:
Happens if I keep SW1 pressed for about 5 seconds. LED1 will blink 11 times and the program is waiting for user input on SW1 during 5 seconds. each press of SW1 will blink LED1 once. after 3 seconds of inactivity of SW1, LED1 wil blink the amount of time SW1 was pressed during program mode. if pressed 5 times during program mode LED1 will blink 5 times and it will save value 5 in EEPROM address 0. After this, going back to normal mode, if I press SW1, LED1 will go high, 10 seconds later TR1 will go high.
If I press SW1 again LED1 and TR1 will go low. If I don't press SW1, LED1 and TR1 will go low after 5 hours (value in EEPROM)
My default time value is 3 hours, minimum is 1 and max is 12 (720 min.)
At this point my code is (seams to be) working perfectly with the exception that I don't know how to test when the time is up. of course I need to keep track of 6 TR.
I ment to narrow down my code using indexes for all ports but my testing didn't go as planned so I have everything repeated 6 times.
PLease don't laugh
my code is too long, the rest is in the next messageCode:CLEAR INCLUDE "AllDigital.pbp" ; Disable Analog functions DEFINE OSC 8 OSCCON = %01110001 SW1 VAR PORTB.0 SW2 VAR PORTB.1 SW3 VAR PORTB.2 SW4 VAR PORTB.3 SW5 VAR PORTB.4 SW6 VAR PORTB.5 LED1 VAR PORTC.0 LED2 VAR PORTC.1 LED3 VAR PORTC.2 LED4 VAR PORTC.3 LED5 VAR PORTC.4 LED6 VAR PORTC.5 PZ VAR PORTC.6 ; piezo (beep) TR1 VAR PORTA.0 TR2 VAR PORTA.1 TR3 VAR PORTA.2 TR4 VAR PORTA.3 TR5 VAR PORTA.4 TR6 VAR PORTA.5 SwStat Var bit ; used to control toggle of SWx L var byte ; count SWz time hold I VAR byte ; general use in for next Counters VAR WORD[6] ; 6 words, for 6 buttons ChargeTime VAR WORD[6] ; charging time in hours DelayTime CON 10000 ; 10 seconds T0IF VAR INTCON.2 ; TMR0 overflow flag X VAR BYTE Y var byte OPTION_REG = %01010100 ; WPU on, TMR0 1:32 prescaler, internal clk PAUSE 50 ; let everything settle ;------------------------------------------------------------------------/ ; First time use, check if Charging time is configured. / ; if not, set defaults to 3 hours. (EEPROM 0 to 5 =3 and 6=0x25 / ;------------------------------------------------------------------------/ read 6,y ; if not 0x25 then it is the first time if y <> $25 then for I = 0 to 5: write i,3: next i: write 6,$25 endif ;------------------------------------------------------------------------/ Main: l=0: y=0 WHILE !T0IF : WEND ; wait for timer to overflow TMR0 = 193 ; load timer for 1ms T0IF = 0 ; reset the overflow flag IF (SW1=0) THEN Counters[0] = DelayTime : goto Channel1 IF (SW2=0) THEN Counters[1] = DelayTime : goto Channel2 IF (SW3=0) THEN Counters[2] = DelayTime : goto Channel3 IF (SW4=0) THEN Counters[3] = DelayTime : goto Channel4 IF (SW5=0) THEN Counters[4] = DelayTime : goto Channel5 IF (SW6=0) THEN Counters[5] = DelayTime : goto Channel6 FOR X = 0 to 5 ; cycle thru counters IF Counters(X) > 0 THEN ; if counter is counting Counters(X) = Counters(X) - 1 ; decrement counter IF Counters(X) = 0 THEN ; if counter timed out SELECT CASE X ; turn on the appropriate LED CASE 0 : if led1 = 1 then gosub StartCharge1 CASE 1 : if led2 = 1 then gosub StartCharge2 CASE 2 : if led3 = 1 then gosub StartCharge3 CASE 3 : if led4 = 1 then gosub StartCharge4 CASE 4 : if led5 = 1 then gosub StartCharge5 CASE 5 : if led6 = 1 then gosub StartCharge6 END SELECT ENDIF ENDIF NEXT X GOTO Main
here is the rest...
Code:Channel1: pause 25 ; debounce if sw1 = 0 then if led1 = 0 or swstat = 1 then if led1 = 0 then sound pz,[123,10]: high led1 pause 300 swstat = 1 l=l+1 if l => 5 then for i = 1 to 11: toggle led1: sound pz,[123,10]: pause 50: next i goto Bprog1 else goto Channel1 endif else low led1: low tr1: sound pz,[123,10] pause 100 while sw1 = 0: wend endif endif swstat = 0 goto main Channel2: pause 25 ; debounce if sw2 = 0 then if led2 = 0 or swstat = 1 then if led2 = 0 then sound pz,[123,10]: high led2 pause 300 swstat = 1 l=l+1 if l => 5 then for i = 1 to 11: toggle led2: sound pz,[123,10]: pause 50: next i goto Bprog2 else goto Channel2 endif else low led2: low tr2: sound pz,[123,10] pause 100 while sw2 = 0: wend endif endif swstat = 0 goto main Channel3: pause 25 ; debounce if sw3 = 0 then if led3 = 0 or swstat = 1 then if led3 = 0 then sound pz,[123,10]: high led3 pause 300 swstat = 1 l=l+1 if l => 5 then for i = 1 to 11: toggle led3: sound pz,[123,10]: pause 50: next i goto Bprog3 else goto Channel3 endif else low led3: low tr3: sound pz,[123,10] pause 100 while sw3 = 0: wend endif endif swstat = 0 goto main Channel4: pause 25 ; debounce if sw4 = 0 then if led4 = 0 or swstat = 1 then if led4 = 0 then sound pz,[123,10]: high led4 pause 300 swstat = 1 l=l+1 if l => 5 then for i = 1 to 11: toggle led4: sound pz,[123,10]: pause 50: next i goto Bprog4 else goto Channel4 endif else low led4: low tr4: sound pz,[123,10] pause 100 while sw4 = 0: wend endif endif swstat = 0 goto main Channel5: pause 25 ; debounce if sw5 = 0 then if led5 = 0 or swstat = 1 then if led5 = 0 then sound pz,[123,10]: high led5 pause 300 swstat = 1 l=l+1 if l => 5 then for i = 1 to 11: toggle led5: sound pz,[123,10]: pause 50: next i goto Bprog5 else goto Channel5 endif else low led5: low tr5: sound pz,[123,10] pause 100 while sw5 = 0: wend endif endif swstat = 0 goto main Channel6: pause 25 ; debounce if sw6 = 0 then if led6 = 0 or swstat = 1 then if led6 = 0 then sound pz,[123,10]: high led6 pause 300 swstat = 1 l=l+1 if l => 5 then for i = 1 to 11: toggle led6: sound pz,[123,10]: pause 50: next i goto Bprog6 else goto Channel6 endif else low led6: low tr6: sound pz,[123,10] pause 100 while sw6 = 0: wend endif endif swstat = 0 goto main Bprog1: WHILE !T0IF : WEND TMR0 = 225 T0IF = 0 IF Counters(0) > 0 THEN Counters(0) = Counters(0) - 1 IF Counters(0) = 0 THEN if y = 0 then y = 3 if y > 12 then y = 12 write 0,y for I = 1 to y: high led1:sound pz,[123,10]:pause 50:low led1: pause 50: next i goto main endif if sw1 = 0 then pause 25 ;debouce if sw1 = 0 then Counters[0] = DelayTime high led1: sound pz,[123,10]: pause 25 y = y + 1: low led1 Counters[0] = DelayTime endif endif goto bprog1 Bprog2: WHILE !T0IF : WEND TMR0 = 225 T0IF = 0 IF Counters(1) > 0 THEN Counters(1) = Counters(1) - 1 IF Counters(1) = 0 THEN if y = 0 then y = 3 if y > 12 then y = 12 write 1,y for I = 1 to y: high led2:sound pz,[123,10]:pause 50:low led2: pause 50: next i goto main endif if sw2 = 0 then pause 25 ;debouce if sw2 = 0 then Counters[1] = DelayTime high led2: sound pz,[123,10]: pause 25 y = y + 1: low led2 Counters[1] = DelayTime endif endif goto bprog2 Bprog3: WHILE !T0IF : WEND TMR0 = 225 T0IF = 0 IF Counters(2) > 0 THEN Counters(2) = Counters(2) - 1 IF Counters(2) = 0 THEN if y = 0 then y=3 if y > 12 then y = 12 write 2,chargetime(2) for I = 1 to y: high led3:sound pz,[123,10]:pause 50:low led3: pause 50: next i goto main endif if sw3 = 0 then pause 25 ;debouce if sw3 = 0 then Counters[2] = DelayTime high led3: sound pz,[123,10]: pause 25 y = y + 1: low led3 Counters[2] = DelayTime endif endif goto bprog3 Bprog4: WHILE !T0IF : WEND TMR0 = 225 T0IF = 0 IF Counters(3) > 0 THEN Counters(3) = Counters(3) - 1 IF Counters(3) = 0 THEN if y = 0 then y = 3 if y > 12 then y = 12 write 3,y for I = 1 to y: high led4:sound pz,[123,10]:pause 50:low led4: pause 50: next i goto main endif if sw4 = 0 then pause 25 ;debouce if sw4 = 0 then Counters[3] = DelayTime high led4: sound pz,[123,10]: pause 25 y = y + 1: low led4 Counters[3] = DelayTime endif endif goto bprog4 Bprog5: WHILE !T0IF : WEND TMR0 = 225 T0IF = 0 IF Counters(4) > 0 THEN Counters(4) = Counters(4) - 1 IF Counters(4) = 0 THEN if y = 0 then y = 3 if y > 12 then y = 12 write 4,y for I = 1 to y: high led5:sound pz,[123,10]:pause 50:low led5: pause 50: next i goto main endif if sw5 = 0 then pause 25 ;debouce if sw5 = 0 then Counters[4] = DelayTime high led5: sound pz,[123,10]: pause 25 y = y + 1: low led5 Counters[4] = DelayTime endif endif goto bprog5 Bprog6: WHILE !T0IF : WEND TMR0 = 225 T0IF = 0 IF Counters(5) > 0 THEN Counters(5) = Counters(5) - 1 IF Counters(5) = 0 THEN if y = 0 then y = 3 if y > 12 then y = 12 write 5,y for I = 1 to y: high led6:sound pz,[123,10]:pause 50:low led6: pause 50: next i goto main endif if sw6 = 0 then pause 25 ;debouce if sw6 = 0 then Counters[5] = DelayTime high led6: sound pz,[123,10]: pause 25 y = y + 1: low led6 Counters[5] = DelayTime endif endif goto bprog6 StartCharge1: high TR1: return StartCharge2: high TR2: return StartCharge3: high TR3: return StartCharge4: high TR4: return StartCharge5: high TR5: return StartCharge6: high TR6: return
Like you said, you've only had PBP a couple weeks ...
Laughing would only be Rude.
Thanks for the code!
I think I can do something with it.
But first I'd like to point out the WORST Basic statement ever devised.It turns an amazingly fast processor into a "thumb twiddling idiot".Code:PAUSE xx
Spending most of it's time doing nothing.
If I were that chips Boss, I'd fire it.
Granted, there are a few instances when PAUSE is required. But if at all possible, avoid PAUSE at all costs.
Working on it ....
DT
If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
.
Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
.
There simply is no "Happy Spam" If you do it you will disappear from this forum.
Bookmarks