2008-11-15
回路図。Timer1用の水晶は時計用の32.768kHz、10進数では半端な数値ですが、「2の16乗の半分」と2進数の世界では切りのいい数値です。通常のクロックは20MHzのセラロック。
基板パターン、ご参考まで。
発振子以外はLED1個だけの回路です。Sleepモードにはいると3μA程度の電流になります。普段は5mA程度です。Sleepに入る前にLEDは消灯しないと消費電力は下がりません。「寝るときは電気を消して」ということです。Sleepモードでは電源間に470μF程度のコンデンサを入れておけば5分程度の停電に耐えることが出来ます。もちろん電圧検知のためのICと、コンデンサに蓄えた電力が電源回路経由で消費されないように逆流防止のダイオードを入れておく必要があります。
ブレッドボードでも問題なく動作するようです。コンデンサは22pFを使用
実験回路全景
0001 '-------------------------------------------------------------------------------
0002 ' TIMER1 Sleep
0003 '-------------------------------------------------------------------------------
0004 program TIMER1_SLEEP
0005
0006 symbol PORT_OUTPUT = 0
0007 symbol PORT_INPUT = 1
0008
0009 symbol LED_TRIS = TRISB
0010 symbol LED_PORT = PORTB
0011 symbol LED_BIT = 0
0012
0013 symbol LED_ON = 1
0014 symbol LED_OFF = 0
0015
0016 dim led as byte
0017
0018 '===============================================================================
0019 ' Interrupt routine
0020 '===============================================================================
0021 sub procedure interrupt
0022 if PIR1.TMR1IF = 1 then
0023 PIR1.TMR1IF = 0
0024 led = LED_OFF
0025 end if
0026
0027 if PIR1.CCP1IF = 1 then
0028 PIR1.CCP1IF = 0
0029 led = LED_ON
0030 end if
0031 end sub
0032
0033
0034 '*******************************************************************************
0035 ' Program main
0036 '*******************************************************************************
0037 main:
0038 led = LED_OFF
0039 LED_TRIS.LED_BIT = PORT_OUTPUT
0040
0041 '---- port A digital INPUT-OUTPUT (comparetor off)--------
0042 CMCON.CM2 = 1
0043 CMCON.CM1 = 1
0044 CMCON.CM0 = 1
0045
0046 '---- disable port B pull-up -------------
0047 OPTION_REG.NOT_RBPU = 1
0048
0049 '---- timer1 config ----------------------
0050 T1CON.T1CKPS0 = 0 ' Timer1 1:1 prescale
0051 T1CON.T1CKPS1 = 0 ' Timer1 1:1 prescale
0052 T1CON.T1OSCEN = 1 ' Timer1 Oscillator enable
0053 T1CON.TMR1CS = 1 ' external clock select
0054 T1CON.TMR1ON = 1 ' Timer1 on
0055 T1CON.NOT_T1SYNC = 1 ' async
0056
0057 '---- timer1 interrupt -------------------
0058 INTCON.PEIE = 1 ' Enable Peripheral interrupt
0059 PIE1.TMR1IE = 1 ' Enable TMR1 interrupt
0060 PIE1.CCP1IE = 1 ' Enable CCP1 interrupt
0061 PIR1.TMR1IF = 0 ' Clear interrupt flag
0062 PIR1.CCP1IF = 0 ' Clear interrupt flag
0063 CCP1CON.CCP1M3 = 1 ' Compare mode
0064 CCP1CON.CCP1M2 = 0 ' Compare mode
0065 CCP1CON.CCP1M1 = 1 ' Compare mode
0066 CCP1CON.CCP1M0 = 0 ' Compare mode
0067 CCPR1L = 0x00
0068 CCPR1H = 0x80
0069 INTCON.GIE = 1 ' Enable interrupts
0070
0071 while(TRUE)
0072 if led = LED_ON then
0073 LED_PORT.LED_BIT = LED_ON
0074 else
0075 LED_PORT.LED_BIT = LED_OFF
0076 end if
0077 wend
0078 end.
0079
0080 '----- E N D O F P R O G R A M ---------------------------------------------
0081
0001 '-------------------------------------------------------------------------------
0002 ' TIMER1 Sleep
0003 '-------------------------------------------------------------------------------
0004 program TIMER1_SLEEP
0005
0006 symbol PORT_OUTPUT = 0
0007 symbol PORT_INPUT = 1
0008
0009 symbol LED_TRIS = TRISB
0010 symbol LED_PORT = PORTB
0011 symbol LED_BIT = 0
0012
0013 symbol LED_ON = 1
0014 symbol LED_OFF = 0
0015
0016 dim led as byte
0017
0018 '===============================================================================
0019 ' Interrupt routine
0020 '===============================================================================
0021 sub procedure interrupt
0022 if PIR1.TMR1IF = 1 then
0023 PIR1.TMR1IF = 0
0024 if led = LED_ON then
0025 led = LED_OFF
0026 else
0027 led = LED_ON
0028 end if
0029 end if
0030 end sub
0031
0032
0033 '*******************************************************************************
0034 ' Program main
0035 '*******************************************************************************
0036 main:
0037 led = LED_OFF
0038 LED_TRIS.LED_BIT = PORT_OUTPUT
0039
0040 '---- port A digital INPUT-OUTPUT (comparetor off)--------
0041 CMCON.CM2 = 1
0042 CMCON.CM1 = 1
0043 CMCON.CM0 = 1
0044
0045 '---- disable port B pull-up -------------
0046 OPTION_REG.NOT_RBPU = 1
0047
0048 '---- timer1 config ----------------------
0049 T1CON.T1CKPS0 = 0 ' Timer1 1:1 prescale
0050 T1CON.T1CKPS1 = 0 ' Timer1 1:1 prescale
0051 T1CON.T1OSCEN = 1 ' Timer1 Oscillator enable
0052 T1CON.TMR1CS = 1 ' external clock select
0053 T1CON.TMR1ON = 1 ' Timer1 on
0054 T1CON.NOT_T1SYNC = 1 ' async
0055
0056 '---- timer1 interrupt -------------------
0057 INTCON.PEIE = 1 ' Enable Peripheral interrupt
0058 PIE1.TMR1IE = 1 ' Enable TMR1 interrupt
0059 PIR1.TMR1IF = 0 ' Clear interrupt flag
0060 INTCON.GIE = 1 ' Enable interrupts
0061
0062 while(TRUE)
0063 if led = LED_ON then
0064 LED_PORT.LED_BIT = LED_ON
0065 else
0066 LED_PORT.LED_BIT = LED_OFF
0067 end if
0068
0069 asm
0070 sleep
0071 end asm
0072 wend
0073 end.
0074
0075 '----- E N D O F P R O G R A M ---------------------------------------------
0076
スリープモードの実際の使用例はタイマーの製作を見てください。