 |
 |
| View previous topic :: View next topic |
| Author |
Message |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Mon Feb 27, 2012 8:44 am |
|
|
Please what is NVR in the ds1302 driver ?
| Code: | void rtc_write_nvr(BYTE address, BYTE data) {
write_ds1302(address|0xc0,data);
}
BYTE rtc_read_nvr(BYTE address) {
return(read_ds1302(address|0xc1));
|
I have not understand the use of the above section of the driver. |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Feb 27, 2012 11:35 am |
|
|
NVR. stands for Non-Volatile-Ram. The DS1302 data sheets tell you all about it.
Flashing LED 1/s. Every time seconds advances you turn LED on for 500ms, then off.
Decreasing to 00. Either change variables hour and min to signed and test for -1. Or test for zero first, (if zero set hour to 24 or min to 60) then decrement.
Mike |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
| Decreasing to 00 |
Posted: Tue Feb 28, 2012 4:23 am |
|
|
Hi princejude,
There are now THREE of us telling you that your system does NOT work as you think it does.
We don't have your board in front of us, but we are ALL saying that your logic is FLAWED.
We all believe that you are not testing with sufficient care and attention.
Simply test by repeat pressing either KEY2 or KEY4.
You should NEVER be able to DECREASE either hours or minutes to 00. (Inrease, YES. Decrease, NO.)
Your problem is not helped by your display-driver / key-test scheme.
When you press ANY key, your display freezes.
This freezing will make it more difficult to see what is going on.
However, it should still be possible to observe the fault in your system.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Tue Feb 28, 2012 5:25 am |
|
|
Yes Mike, you all are right about decreasing to 00. I tested the board yesterday and noticed it as follows:
Decreasing the hour result: 03,02,01,23,22,21......
Decreasing the min result: 03,02,01,59,58.........
00 was missing in the above two cases. I tried using less than (if(hour < 0) hour = 23;) as one of u suggested but it makes the time setting to display some wrong numbers such as 95 will decreasing hour.
Since I can still set the time to 00 using the INCREASE buttons, I don't think this is a major problem but it is also good for me to learn from it.
| Quote: | | Flashing LED 1/s. Every time seconds advances you turn LED on for 500ms, then off. |
I tried this your suggestion but it makes the 7-seg not to display properly(maybe makes it to freeze). I did something like this: | Code: | if(sec_zero == 1) {delay_ms(500); output_low(PIN_C4);}
if(sec_zero == 0) {delay_ms(500); output_high(PIN_C4);}
//sec_zero is bit 0 of sec register and PIN_C4 drives sec LEDs
| Am suspecting the delay_ms() function i used in this case may be it may not work under this condition.
Someone told me to put the 7-seg driver section in a timer interrupt to prevent the code from hanging/freezing while pressing the set buttons but I have not try that. |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
| delay_ms(xx) and other issues |
Posted: Tue Feb 28, 2012 6:47 am |
|
|
Hi princejude,
Good you've now got the point about decreasing to 00.
When I offered the hint about colon flashing, I told you to turn the LED on for 500ms.
I did not tell you HOW to do it, you were supposed to work that one out for yourself.
You've discovered that delay_ms(500) is NOT the way to do it. I will give you a guide later in this post.
I realise that it's not a major issue, but I would still prefer to be able to DECREASE to 00.
When you take code to clients, they expect it to work EXACTLY as described.
They lose confidence in you if you present them with quirky code, it's just not professional.
Like I said before, there are two ways round the problem:-
(1) Convert your variables to signed, then test for negative.
(2) Change the sequence of operations. (I.e. perform the decrement AFTER the IF, test NOT before it.)
Either method works, without hiccups, if you code correctly.
BOTH your freezing problems are being caused by delay_ms(xx).
I NEVER use it, except for crude, rough and ready testing in the very early stages of code development.
You have several alternatives, which include:-
(1) Convert ALL timings, except very short delay_us(), to interrupt driven.
(2) Continue with inline code but remove the delay_ms(>1) lines.
I prefer to go down the interrupt route.
It's a radical change, once you've done it you wonder why you didn't do it earlier.
To go down the inline route you rely on the fact that it takes time to go once round your loop.
At this stage you have two long delays, one in each of your Key-tests and the colon flasher.
Your key-test delay is there to deal with key-bounce and slow the PIC down to human speed.
Suppose you leave delay_ms(1) in your display routine.
You are going round your loop in roughly 6ms for the six delays, plus 100us, or so, for the rest of the code.
In other words you've already got a crude ~6ms tick.
To get the 500ms colon flasher:-
(a) When seconds advances, preset a colon_on_counter at 80, and turn on the colon LED.
(b) Every time round the loop decrement colon_on_counter.
(c) When colon_on_counter reaches zero, turn off the colon LED.
Do something similar for the key tests.
The down side to the inline approach is that the timings change as you add more code.
The up side is that it's easier to see what you're doing.
The down side to the interrupts is that it takes a lot more to get your head round.
The up side is that you can have loads of delays which are unaffected as you develop more code.
You pays your money and takes your choice.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Tue Feb 28, 2012 7:48 am |
|
|
Hi Mike,
Thanks for your wonderful comments.
| Quote: | | (1) Convert ALL timings, except very short delay_us(), to interrupt driven. |
and
| Quote: | To get the 500ms colon flasher:-
(a) When seconds advances, preset a colon_on_counter at 80, and turn on the colon LED.
(b) Every time round the loop decrement colon_on_counter.
(c) When colon_on_counter reaches zero, turn off the colon LED.
|
Pls can you show me some sample on how to do these in code. Thanks ones more. |
|
 |
dyeatman
Joined: 06 Sep 2003 Posts: 1985 Location: Norman, OK
|
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
| Which command turns your colon LED on? |
Posted: Tue Feb 28, 2012 9:02 am |
|
|
Hi princejude again,
I can't tell from this code which round you've got the colon LED wired.
| Code: |
if(sec_zero == 1) {delay_ms(500); output_low(PIN_C4);}
if(sec_zero == 0) {delay_ms(500); output_high(PIN_C4);}
//sec_zero is bit 0 of sec register and PIN_C4 drives sec LEDs
|
The idea was that whenever sec_zero changes you turn the colon LED on for half a second then turn it off, and keep it off.
What you're doing is turning the colon LED on/off roughly once every two seconds.
It turns on for one second, off for one second about 500 ms out of step with the seconds change, and probably skipping an occasional 500ms.
You're also locking things up for BOTH halves of each second whether odd or even with the delay_ms(500).
Result:- Little else can happen.
Every time your code encounters the 'if(sec_zero==0)' section it performs an action, even if it's been done earlier in the same second.
Same applies to the 'if(sec_zero==1)' section!
Once you've had the colon LED on for 500ms you should perform NO action until the start of the next second.
I don't normally give away code. You're supposed to work it out for yourself. It goes like this:-
You pose a problem or ask a question.
I ask you a fresh question to encourage you look at things a different way.
Hopefully, you try a new approach to your problem, make mistakes, and learn from those mistakes.
When you get stuck again, you ask a new question..........
I can remember a time before there were germanium transistors. Why should I impose my old ideas and ways on you?
Tell me which way up you've got your colon LED wired and I'll give you some more hints.
I did show kamillas how to drive his 7 segment display with interrupts in a recent post. The topic is titled '7 segment time'. I've not got round to working out how to drive the links!
As far as I'm aware the code is complete and compilable. You could have a look at that.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Wed Feb 29, 2012 8:52 am |
|
|
I am now trying to re-write the code using interrupts. I am using timer0 interrupt for 7-segment display. Will I use another interrupt(timer1) for the switch debounce ?
Right now with the 7-seg code on timer0 interrupt, It is only the tens digit of sec that is counting on the 7-seg. Though it not a complete code yet. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9658 Location: Greensville,Ontario
|
|
Posted: Wed Feb 29, 2012 9:13 am |
|
|
Mike..germanium transistors !! hmm..2N107... I actually have TTL cards from the first PDP-5 that was installed in Canada,eh ! And some 12AX7s as well....things were a LOT simpler then, though you did have to read and test...
princejude... do yourself a big,big favour and buy a DS1307 and get rid of the DS1302. You'll save a LOT of time AND get EASY RTC functions like an ISR AND 1Hz LED colon flashing for FREE !! |
|
 |
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Wed Feb 29, 2012 9:52 am |
|
|
Tubes, germanium transistors oh my !! I also have some boxes of vacuum tubes down stairs. How about this for "state of the art" memory that I keep for entertainment?
[img]
Photo removed.
[/img]
mikey
++++++++++++++++++++
Photo removed regretfully.
Reason: This is not a general electronics chat forum.
This is the CCS compiler forum.
A reminder to stay on topic.
- Forum Moderator
++++++++++++++++++++ _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Thu Mar 01, 2012 6:51 am |
|
|
| Quote: | Like I said before, there are two ways round the problem:-
(1) Convert your variables to signed, then test for negative.
(2) Change the sequence of operations. (I.e. perform the decrement AFTER the IF, test NOT before it.)
|
Hi Mike,
Thanks for this your comment, I changed the sequence as you said in no 2 above and I can now decrease to 00. |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Thu Mar 01, 2012 8:50 am |
|
|
| Quote: | Tell me which way up you've got your colon LED wired and I'll give you some more hints.
|
The colon LED is wired to pin c4 through 1k resistor. That is, One end of 1k to pin c4 and the other end to anode while the cathode to GND. Am testing with a single LED for now. |
|
 |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
| Interrupts |
Posted: Thu Mar 01, 2012 3:58 pm |
|
|
| Quote: |
I am now trying to re-write the code using interrupts. I am using timer0 interrupt for 7-segment display. Will I use another interrupt(timer1) for the switch debounce ?
|
There's no right way to do any task. You can go either way. I would use one interrupt only to deal with ALL timing related events.
So, with a ~1ms interrupt, I'd use it to control the timing of your 7 segment display on each interrupt, and as a timer/counter for key debounce etc, and timer/counter for colon LED.
That's my halfpenny's worth.
Mike |
|
 |
princejude
Joined: 07 Aug 2010 Posts: 72
|
|
Posted: Thu Mar 08, 2012 5:44 am |
|
|
Hello Mike, Thanks for that "7 segment time" thread you suggested to me earlier.
I have now re-written the 7-seg display section using timer0 interrupt and is working but I have not succeeded in using interrupt for the switch debounce or time setting section. My question is how can I eliminate the switch debounce delay using interrupt ?
Here is my latest code so far:
| Code: | #include "C:\Users\caleb\Documents\CLOCK_PROJECT\INT7.h"
// I am using 4MHz Crystal
#define RTC_SCLK PIN_C3
#define RTC_IO PIN_C5
#define RTC_RST PIN_C2
#define KEY1 PIN_C0
#define KEY2 PIN_C1
#define KEY3 PIN_E0
#define KEY4 PIN_E1
#include <ds1302.c>
BYTE const tab[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
byte day,mth,year,dow,hour,min,sec,sec_unit, sec_ten, min_unit,min_ten,hour_unit,hour_ten ; // temperature tens & units
int digit;
#bit sec_zero = sec.0
#int_TIMER0
void TIMER0_isr(void)
{
rtc_get_time( hour, min, sec );
sec_unit = sec %10;
sec_ten = sec /10;
min_unit = min %10;
min_ten = min /10;
hour_unit = hour%10;
hour_ten = hour/10;
digit++;
if (digit > 5) digit = 0;
if (digit == 0)
{
OUTPUT_a(0xff) ;
OUTPUT_d(tab[sec_unit]) ;
OUTPUT_a(0b11111110) ;
}
if (digit == 1)
{
OUTPUT_a(0xff) ;
OUTPUT_d(tab[sec_ten]);
OUTPUT_a(0b11111101) ;
}
if (digit == 2)
{
OUTPUT_a(0xff) ;
OUTPUT_d(tab[min_unit]) ;
OUTPUT_a(0b11111011) ;
}
if (digit == 3)
{
OUTPUT_a(0xff) ;
OUTPUT_d(tab[min_ten]);
OUTPUT_a(0b11110111) ;
}
if (digit == 4)
{
OUTPUT_a(0xff) ;
OUTPUT_d(tab[hour_unit]);
OUTPUT_a(0b11101111) ;
}
if (digit == 5)
{
OUTPUT_a(0xff) ;
OUTPUT_d(tab[hour_ten]);
OUTPUT_a(0b11011111) ;
}
}
void main()
{
rtc_init();
rtc_set_datetime(8,3,12,5,7,59);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
enable_interrupts(INT_TIMER0);
// enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
While (TRUE)
{
}
} |
|
|
 |
|
|
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
|