Three years since I last picked up a PIC. Great to see that this software and forum is still actively running. Well done to all those who have given up so much time on the project.
I'm trying to produce a motor controller, running from a battery, which knows the time-of-day (roughly). I have a test PCB board with two leds and a watch crystal, etc. I have found it hard to get any concrete information about this set-up and thought I would give a bit of information about some small success, before asking for help.
It may be interesting to others to know that the use of a watch crystal and two 15pF capacitors (Timer1) connected to the ICSP data and clock pins is working well. The timer1 runs fine and the chip programs without problems. One proviso, the timer will not run if I have the programming lead connected to the MOLEX 6 pin programming plug. I'm guessing that the extra capacitance of the lead, stops the oscillator from running. To ICSP the chip, I connect the programming lead to the MOLEX socket and start programming, then when it has verified successfully, I remove the programming lead and connect the battery. The timer then starts fine. The PCB I produced has a 0V guard ring which surrounds the two crystal pads and the two capacitor pads and the two pins of the MOLEX connector - I don't know whether this is significant, but it works.
If the software starts the watch crystal oscillator immediately, then, when the programming connector is inserted, I find that it will not program the PIC - I guess the oscillator upsets the programmer. The first line of the software is a wait 2 sec before starting the oscillator to give me a chance to set the programmer running.
I'm pretty new to all this, so I am chuffed with the result so far!!!
QUESTION
~~~~~~~~
The software below, works well as a clock UNTIL I include the sleep command. The program starts, the leds flash once, it goes to sleep OK (as far as my ammeter tells me) but never wakes up to flash again! If I remove the sleep command, the interrupt routine works fine and the led flashes every minute.
I have studied the 16F88 data sheet (obviously not well enough!) played with the T1CON bits, etc, etc. I think I want to be running an asynchronous timer1 for the interrupt-based clock and I think I'm expecting the 'normal' (awake) state of the PIC to be running on its internal 8MHz clock. I have the latest GC Basic and update loaded.
Any help or pointers in the right direction would be much appreciated.
Best wishes
'This program is interrupt driven on the timer1 watch crystal
'it flashes a led on port b.0 every minute
'it flashes a led on port a.3 once to show the program is running
'the timer1 is free running and its registers are never
'touched, just inspected
'Hardware settings
#chip 16F88, 8 'PIC 16F88 running at 8 MHz
#config MCLR = Off, Osc = INTRC_IO 'Turn off MCLR, select internal osc.
'WDT and LVP are disabled automatically
'Initialise
Dir PORTA.3 out 'led
dir portb.0 out 'led
Set porta.3 off
set portb.0 off
wait 2 s ' this gives the programmer time to start programming
' before the oscillator kicks in and upsets the ICSP programming
' ie gives me two seconds to start ICSP
Hour=0:Min=0:Sec=0:counter=0
InitTimer1 ExtOsc,PS1_1/1 'should overflow every 2 seconds
'T1CON = b'00001111'
On Interrupt Timer1Overflow Call IncTime
ClearTimer 1
TMR1L = 0 'Need to do this according to the PIC datasheet ?
TMR1H = 0 'I'm guessing these two are not needed!
StartTimer 1
'flash the portA led to say that timer 1 should have started
pulseout porta.3, 100 ms
flashled=true ' used later as a flag, to keep the one minute
' LED flash out of the interrupt routine
' first time round, it will immediately flash portB LED
'Main routine
main:
'flash portB LED if required
if flashled=true then
pulseout portb.0, 100 ms
flashled=false
end if
'then go to sleep and wait for the timer1 interrupt
'to wake the PIC
asm sleep
nop
goto main
'####################################
SUB IncTime
'interrupt routine called when timer1 overflows
' such a slow rate of call (every 2sec is the fastest :PS1_1/1)
' (every 16sec is the slowest:PS1_1/8)
' that quite a bit could be achieved in this interrupt
Counter ++
'flashled=true 'include this line to avoid having to wait a whole minute
If Counter = 30 Then '30 x 2sec has passed
Min ++
Counter = 0
flashled=true
If Min = 60 Then
Hour ++
Min = 0
If Hour = 12 Then
Hour = 0
End If
End If
End If
End Sub
'#####################################
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
mmmm……check to see if GIE is set, and in this case maybe skip the nop after sleep so the goto Main is executed prior to interrupt? The program counter is in never, never land, or it cycling through the 2 sec wait on startup.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your offering. Tried setting GIE=1 at a couple of different points and removing the nop also adding a for next loop after the sleep command to avoid the immediate goto main. No change. I did wonder whether the external oscillator stopped running once the sleep command had been issued and the rest of the PIC shut down, but my CRO shows a healthy 32kHz oscillation during its sleep!
The interrupt routine, and flashing of the leds continues to be perfect if I just remove the 'sleep' command - grrrr.
Is my problem something to do with the time needed to re-start the internal oscillator? I havn't got my head around the Data Sheet section "4.6.2 Clock Switching". I don't believe that I am using a 2-speed start up, should I be? Will the PIC default to happily waiting until the internal 8MHz clock powers up and is stable, before it runs my instructions?
4.6.2 CLOCK SWITCHING
Clock switching will occur for the following reasons:
....
• A wake-up from Sleep occurs due to an interrupt or
WDT wake-up and Two-Speed Start-up is enabled.
If the primary clock is XT, HS or LP, the clock will
switch between the INTRC and the primary system
clock after 1024 clocks (OST) and 8 clocks of the
primary oscillator. This is conditional upon the SCS
bits being set equal to ‘00’.
Note: Because the SCS bits are cleared on any
Reset, no clock switching will occur on a
Reset unless the Two-Speed Start-up is
enabled and the primary clock is XT, HS or
LP. The device will wait for the primary
clock to become stable before execution
begins (Two-Speed Start-up disabled).
Any thoughts?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Finally cracked the problem after hours of reading/experimenting!
The circuit now keeps time using the watch crystal+caps, connected to the Timer1terminals. It spends most of its time asleep, just waking up to flash a LED every minute. My (completely un-optimised) circuit draws around 2mA when it starts and falls to 0.15mA after 2 seconds have elapsed (ie when the sleep command kicks in).
As ever, the solution was simple but obscure to me! Failing to tell the PIC that I wanted the timer to run asynchronously was the problem I believe. The five lines below were what made the difference.
Hope this helps someone. Best wishes.
InitTimer1 ExtOsc,PS1_1 'should overflow every 2 seconds
ClearTimer 1
StartTimer 1
NOT_T1SYNC=1
On Interrupt Timer1Overflow Call IncTime
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Three years since I last picked up a PIC. Great to see that this software and forum is still actively running. Well done to all those who have given up so much time on the project.
I'm trying to produce a motor controller, running from a battery, which knows the time-of-day (roughly). I have a test PCB board with two leds and a watch crystal, etc. I have found it hard to get any concrete information about this set-up and thought I would give a bit of information about some small success, before asking for help.
It may be interesting to others to know that the use of a watch crystal and two 15pF capacitors (Timer1) connected to the ICSP data and clock pins is working well. The timer1 runs fine and the chip programs without problems. One proviso, the timer will not run if I have the programming lead connected to the MOLEX 6 pin programming plug. I'm guessing that the extra capacitance of the lead, stops the oscillator from running. To ICSP the chip, I connect the programming lead to the MOLEX socket and start programming, then when it has verified successfully, I remove the programming lead and connect the battery. The timer then starts fine. The PCB I produced has a 0V guard ring which surrounds the two crystal pads and the two capacitor pads and the two pins of the MOLEX connector - I don't know whether this is significant, but it works.
If the software starts the watch crystal oscillator immediately, then, when the programming connector is inserted, I find that it will not program the PIC - I guess the oscillator upsets the programmer. The first line of the software is a wait 2 sec before starting the oscillator to give me a chance to set the programmer running.
I'm pretty new to all this, so I am chuffed with the result so far!!!
QUESTION
~~~~~~~~
The software below, works well as a clock UNTIL I include the sleep command. The program starts, the leds flash once, it goes to sleep OK (as far as my ammeter tells me) but never wakes up to flash again! If I remove the sleep command, the interrupt routine works fine and the led flashes every minute.
I have studied the 16F88 data sheet (obviously not well enough!) played with the T1CON bits, etc, etc. I think I want to be running an asynchronous timer1 for the interrupt-based clock and I think I'm expecting the 'normal' (awake) state of the PIC to be running on its internal 8MHz clock. I have the latest GC Basic and update loaded.
Any help or pointers in the right direction would be much appreciated.
Best wishes
mmmm……check to see if GIE is set, and in this case maybe skip the nop after sleep so the goto Main is executed prior to interrupt? The program counter is in never, never land, or it cycling through the 2 sec wait on startup.
Thanks for your offering. Tried setting GIE=1 at a couple of different points and removing the nop also adding a for next loop after the sleep command to avoid the immediate goto main. No change. I did wonder whether the external oscillator stopped running once the sleep command had been issued and the rest of the PIC shut down, but my CRO shows a healthy 32kHz oscillation during its sleep!
The interrupt routine, and flashing of the leds continues to be perfect if I just remove the 'sleep' command - grrrr.
Is my problem something to do with the time needed to re-start the internal oscillator? I havn't got my head around the Data Sheet section "4.6.2 Clock Switching". I don't believe that I am using a 2-speed start up, should I be? Will the PIC default to happily waiting until the internal 8MHz clock powers up and is stable, before it runs my instructions?
Any thoughts?
Finally cracked the problem after hours of reading/experimenting!
The circuit now keeps time using the watch crystal+caps, connected to the Timer1terminals. It spends most of its time asleep, just waking up to flash a LED every minute. My (completely un-optimised) circuit draws around 2mA when it starts and falls to 0.15mA after 2 seconds have elapsed (ie when the sleep command kicks in).
As ever, the solution was simple but obscure to me! Failing to tell the PIC that I wanted the timer to run asynchronously was the problem I believe. The five lines below were what made the difference.
Hope this helps someone. Best wishes.