Help with realtime clock and timer


Closed Thread
Results 1 to 21 of 21
  1. #1
    geselaw's Avatar
    geselaw Guest

    Cool Help with realtime clock and timer

    Hi

    I am working on this new project and I am new to PIC .I have problem with my code(of which some of it is collected from the web) .Every thing seems fine but it is not working. I have attached small part of the code that I have modified to test the real-time clock with the original setup. Could Any one please look it up and through any sugestion. If you are intersted, I could send the complet code with schematic.I am using Pic16f628A.
    Attached Files Attached Files
    Last edited by geselaw; - 29th September 2005 at 14:25. Reason: forget some thing

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi geselaw,

    Are you aware that it's set-up for a 32.768kz crystal on RB6 and RB7?
    RB6 and RB7 are also the ICSP data and clock lines, so you can't have both things connected at the same time.

    The Comparator and External interrupts are also enabled, but there isn't anything to handle them in the interrupt routine. If one of them causes an interrupt and the flag is not reset in the handler, you will be "continuously interrupting", which won't allow the rest of the program to do anything.

    Also, as shown, the Start: section will "Fall Thru" into a subroutine. But then it might only be that it's a portion of the program.
    <br>
    DT

  3. #3
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default Full code

    Hi Darrel

    I have attached the full code.Can you please look at the device setup,assembly and the real-time clock that I made using Timer1 with 32.678khz crystal.I really tried to make this code work.I have also attached My schematic.
    Attached Images Attached Images  
    Attached Files Attached Files

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Somehow I don't think you're going to drive your ACS108's like that! Have you read their Datasheet and noticed what it says there? I don't see a COMMON path for a NEGATIVE current to flow in order to turn them on.

  5. #5
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default Done

    Hi Melanie

    First of all, I would like to congratulate you for the great job that you are doing in this forum. It really helps guys like me with no experience with microcontroller.

    Yes I have seen that problem some time ago and corrected it in my design. But my problem right now is the code. The chip is not performing according to the program.Let me ask you a direct question.

    1. I used Timer 1 for real-time clock.Do you see any problem in that module ?
    2. I used Time var word and then I used high byte and low byte for hour and minute. Is that Ok?
    3. How about the assembly code. Do you think that could make the chips unstable. I tried my best not to live floating inputs.I also put a stabilizing capacitor near the chip b/n Vss and Vdd.I tried to do every thing by the book.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    > I tried to do every thing by the book.

    Time to consign that book to land-fill.

    Darrel has already said that your schematic is screwed. The best code in the world isn't going to make it run and it's pointless even looking at it. Go away, create a circuit that is likely to actually work, then at least when you write some code you've half a chance it might do something sensible. While we're at it, you're using a CAD program, so let's layout the drawing that's easy to read.

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    YES, I did say that. Many times infact! &nbsp; I just don't remember typing it.

    Did you bug my computer again Melanie?

    Actually, I found so many problems with the schematic, I didn't want to say anything, for fear I'd have to come up with ways to fix it.

    I am however, fairly impressed with the program. Every time I thought I found something wrong, a quick look at the datasheet proved that whomever wrote it, spent a lot of time looking at the datasheets too.

    You definately gotta work on that circuit though.
    <br>
    DT

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Oh yeah, I forgot,

    The only real problem I found in the program was that the Comparator Interrupt section is resetting the Enable bit instead of the Interrupt Flag.

    After the first time through, the comparator won't cause an interrupt anymore. But, it will keep the External Interrupt section from ever executing.<br>
    Last edited by Darrel Taylor; - 1st October 2005 at 20:17.
    DT

  9. #9
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default

    Thank you Darrel for your suggestion ,here is the new code.

    COMP_INT BSF _Charger ;porta.0 will be on.it will be used for charging the battery
    MOVF CMCON,F ;read and clear comparator mismatch condition
    bcf PIR1, CMIF ;clear comparator interrupt flag
    movf _hour,w ;*** read hour to w register
    addlw 008h ;*** add 8 to hour which is in the w register
    movwf _Bat_hour ;move the result to the Bat_hour var
    goto Unsave

    The schematic was never meant to be published ,it was only for reference.I know this is lame excuse so I am working on it and I will post it ASAP.In the mean time you said you don't like the circuit,good. Which part do you think need improvement/change. I don't ask people to do my leg work for me .All I am asking is for suggestion.

    I am a Network Engineer by profession never had formal education in electronics. All the small things I know ,I learned it from internet. I really need to make this project work .Please help.

  10. #10
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default New Schematic

    I have tried to make the schematic readable. The schematic that I posted last time was unreadable even to me ,and I found so many silly mistakes and tried to correct them. If you find any more mistakes ,it is just because I don't know better for the time being ,but I am hear to learn.

    I have also made minor correction under the "Start" Label

    Start:

    'Initial Battery charging (if not intiated yet by low battery from assembely interupt)
    if (charger = 1) and (bat_hour != 0) then 'This will stop the programe from adding another charging-
    goto System_Start '-hour if the battery is already being charged caused by low battery-
    endif '-in the begining of the programe
    bat_hour = hour + 8 '*** charge the battery no matter what at start
    charger = 1 'Charge pin on
    Attached Images Attached Images  

  11. #11
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Look, lets start with basics...

    1. Go download the Datasheet for your PIC...
    2. Go see where the Crystal IS SUPPOSED TO BE CONNECTED

    Come back and tell us which pins it has got to be nailed to, and then tell us why you insist on having it on RB6/7 in your schematic. This I've got to hear...

    And whilst were teaching schematics, you never, ever, ever draw lines or connections through a component! Go around it.

  12. #12
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Looks like he's using the internal osc with external crystal for Timer1, but there are still a TON of errors in that schematic.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  13. #13
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default

    As you could see in my program("@ device intrc_osc_noclkout" and "PCON.3=1") , I am using the built-in 4MHz internal OSC .The one you see on the schematic was the one that I used for the real time clock on Tmr1 (32.768khz). You will find below the device setup that I used for quick reference.

    Could you please tell me those errors that you saw in the schematic ? I some times have second thought about mixing the digital circuit with the main power (ACS108). I found an error in the schematic and corrected it.The collector part of the phototransistor was connected to the Gnd. It should have been connected to the +ve.The new schematic is attached.

    DEFINE INTHAND myint
    @ device intrc_osc_noclkout
    @ device wdt_off
    @ device lvp_off
    @ device pwrt_on
    @ device cpd_off
    @ device bod_off
    @ device mclr_off
    @ device PROTECT_OFF

    ' **************
    ' *Device setup*
    ' **************
    PIE1.6 = 0 'disable the comparator before mesing with cmcon & vrcon
    VRCON = %10001011 'Voltage Refernece enabled and voltage = 2.97
    pause 10 'for stabiliazing
    CMCON = %10000101 'Single comparator enabled RA0 & RA3 are free for I/O
    PAUSE 10
    PIR1.6 = 0 'clear residing interrupt flag
    PIE1.6 = 1 'enable comparator flag
    pcon.3=1 '1=Internal oscillator 4mhz ,0=37khz
    T1con =%00001111 ' Turn on Timer1 with prescaler = 1
    Pir1.0 = 0 'disable comp & timr1 int flag
    trisb = %00000001
    trisa = %10100110 'PortA 1&2 are analog input ,the rest are digital I/O
    PIE1.0 = 1 ' Enable TMR1 overflow and Comparator interrupt
    INTCON = %11010000 ' Enable global ,peripheral , external interrupts
    Attached Images Attached Images  
    Last edited by geselaw; - 5th October 2005 at 16:17. Reason: Add

  14. #14
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    geselaw,

    Got your PM. Been gone for a couple days.

    While I'm not real "keen" on the idea of tying AC neutral to VDD, I understand why you did it (for the ACS108's). And if all electricians were guaranteed to wire everything correctly, it may not be a problem. But one day your going to plug this thing into an outlet that's been wired backwards, and somebody's going to get a RUDE awakening when they touch it. Personally, I would use an optically isolated solid state relay, but then, that's just me.

    I see you've increased the resistance to the LED's in the interrupters good, but you didn't increase the ones going to the normal LED's. They'll not last very long with only 15 ohms.

    The collector side of the photo-transistors, after going through the sensitivity adjustment pots, connect to Vss. They need to go to VDD instead.

    The MCLR pin is connected to the 5V out from the 7805. The PIC will RESET when you are trying to run on battery power. It needs to be after the ICL7673. Also, what is R1?

    Hey Bruce, do you see anything else?
    <br>
    DT

  15. #15
    TK5EP's Avatar
    TK5EP Guest


    Did you find this post helpful? Yes | No

    Default Osc ?

    Should't the XTAL be wired between pin 15 and 16 ??

    My 2 cents,

  16. #16
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Very funny!!!

    No, he's using the internal oscillator, with a 32768 crystal on Timer1 for a RTC.
    <br>
    DT

  17. #17
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,

    Haven't gone over the whole schematic - it's pretty cramped & incredibly hard to follow on-screen, but, outside a few things already mentioned, RA4 is an O.C. output attempting to drive an NPN.

    I have no clue what's being controlled with those AC line switches, but I definitely would NOT connect any mains to my DC rails. What happens when one is turned on? With neutral tied to +5Vout and the other AC leg coming right through an AC switched load into the DC rails - it kinda looks like an accident waiting to happen to me...;o}

    I may be wrong. I've never used one of those AC switches, but seems kinda adventurous to me.

    Whole bunch of discreet resistors, pots, etc all with the same part reference #'s, 15-ohm LED limiting resistors (ouch), yada yada.

    I would have to print that one out, and use various colored markers to run it all of it down.

    Looks like a valiant effort, but looks like it may need a little tweaking before flippin the ON switch..}
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  18. #18
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel

    All the LED limiting resistors are 150 ohm(typing error).The VDD connection to the phototransistor was corrected and I have posted the new schematic "Scrolling_2" above.About the MCLR though, I thought by disabling it with "@ device mclr_off", I turned it to be a digital input. That is why I tied it to 7805 output so that it would tell the PIC when the main power is off (go to low_power mode to conserve battery energy). I also used (R1)10kohm as a pull down resistor on RA5 so that RA5 would not float when the main power is off. Does it make sense ?

    Hi Bruce

    The ACS108 is used for controlling 110v solenoid.I completely agree with you about using the ACS switch.In the nest version ,opto-isolated solid state really will be used.I have not paid attention to RA4. Thank you for pointing that it is an open drain output. I really need to drive that NPN. I don't know how but either I have to use RA4 with a pull up resistor or use RA2 as in my configuration (VRCON.6 =0) I have not used this port for any thing else. A close examination shows that this pin was configured as digital input (TRISA= %10100110). So I think this might be another problem in my program as this pin could have floated at any time.The problem with RA2 is that it says in the datasheet "The RA2 pin can be used as a simple D/A output with limited drive capability. Due to the limited drive capability, a buffer must be used in conjunction with the Voltage Reference output for external connections to VREF. "

    I really need your advise which way to go.

    Best Regards
    geselaw

  19. #19
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    >> "@ device mclr_off"

    OOPS, my fault. I should have seen that. Doh!

    For the RA4 thing. You could use a PNP transistor between +5V and the relay. Or use a PNP to drive the NPN, but that adds another part.
    <br>
    DT

  20. #20
    geselaw's Avatar
    geselaw Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks Darrel, I will try it out ASAP.

  21. #21
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    if i can suggest one thing.. i really love to use ULNXXX serie chip. you have 7 built-in transistor resistor and diode. Really handy, save a lot of wiring and parts AND at the end reduce the Bill of material. ULN2003 and how much other are really great.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. Clock with timer interrupt0
    By boban in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 4th November 2009, 14:59
  3. Single digit 7 Seg LED clock - PIC16F88
    By thirsty in forum Code Examples
    Replies: 4
    Last Post: - 17th July 2009, 08:42
  4. Clock using Instant Interrupts
    By PICpocket in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th February 2009, 21:43
  5. clock and date without using real timer
    By eepro in forum Off Topic
    Replies: 1
    Last Post: - 21st September 2008, 02:38

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts