- 阅读权限
- 200
- UID
- 1
- 帖子
- 54
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 72 小时
- UID
- 1
- 帖子
- 54
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 72 小时
|
(转载请注明出自圣龙扬特-AVR电子网Http://www.avrbase.com)
MEGA8单片机的T/C2 是一个通用单通道8 位定时/ 计数器,其中一个重要特点是,允许使用外部的32.768 kHz 手表晶振作为独立的I/O 时钟源,接PB6、PB7。
Bascom-AVR设置语句结构如下:
Config Timer2 = Timer , Async = On/Off , Prescale = 1/8/32/64/64/128/256/1024
Async参数是指是否采用异步晶振。
当使用后勤工作步晶振时,要注意几点:
1、芯片主时钟要设置为使用内部RC;
2、主时钟必须比晶振高4 倍以上;
3、CKOPT熔丝位使能(即设置为0)。
第3条是否绝对笔者没有进一步验证,笔者在调试时,异步时针不工作,当把CKOPT设置为0后,工作正常。
下面把笔者的倒记时器源程序奉上,供参考。程序中,Prescale分频设置为64,即每0.5秒产生一次T2中断。
'-------------------------------------------------------------------------------
'Name : Timer2.bas
'Copyright : http://www.avrbase.com
'Micro : AtMega8
'Author : slyt/箫天
'-------------------------------------------------------------------------------
$regfile = "m8def.dat"
$crystal = 1000000
$hwstack = 40
$swstack = 40
$framesize = 64
'*******************************************************************************
Config Timer0 = Timer , Prescale = 64
On Timer0 Tim0_isr
Config Timer2 = Timer , Async = On , Prescale = 64
On Timer2 One_second
Config Clock = User
Config Int1 = Falling
On Int1 Button_down
'*******************************************************************************
Declare Sub Set_digital(byval Dat As Byte)
Declare Sub Play_music()
Declare Sub Power_off()
'*******************************************************************************
Dim Seconds As Long
Dim Set_seconds As Eram Long At 21
Dim Secd As Byte , Minutes As Byte , Hours As Byte
Dim Set_hours As Eram Byte At 11 , Set_minutes As Eram Byte At 12
Dim Segment As Word
Dim Seg_v As Byte
Dim Seg_index As Byte
Dim Seg_digital(4) As Byte
Dim Decrtime As Bit
Dim Key_down As Bit
Dim Key As Byte
Dim Key_time As Byte
'*******************************************************************************
'A-F
Ddrc = &HFF
'G
Ddrd.0 = 1
Key1 Alias Pind.1 : Ddrd.1 = 0 : Portd.1 = 1
Key2 Alias Pind.2 : Ddrd.2 = 0 : Portd.2 = 1
Key3 Alias Pind.4 : Ddrd.4 = 0 : Portd.4 = 1
Intk Alias Pind.3 : Ddrd.3 = 0 : Portd.3 = 1
V4 Alias Portd.6 : Ddrd.6 = 1
V3 Alias Portd.7 : Ddrd.7 = 1
V2 Alias Portb.0 : Ddrb.0 = 1
V1 Alias Portb.1 : Ddrb.1 = 1
Pp Alias Portb.2 : Ddrb.2 = 1
Spk Alias Portd.5 : Ddrd.5 = 0 : Spk = 0
Seg_v = &B00100010
Segment = &H4321
Key_down = 0
'*******************************************************************************
'initial original time
If Set_seconds = &HFFFFFFFF Then Set_seconds = 659 '10 minutes
Seconds = Set_seconds
'*******************************************************************************
Enable Int1
Enable Timer0
Enable Timer2
Enable Interrupts
'*******************************************************************************
Do
Idle
If Key_down = 1 Then
Disable Int1
Disable Timer2
If Key_time > 20 Then 'Waitms 20
V1 = 1 : Pp = 0
Key = Pind And &B11110 'get key status
Shift Key , Right , 1 'shift right 1 bit
Select Case Key
Case &B1010 'Key1 down
If Seconds < 43259 Then Seconds = Seconds + 60 'less than 12 hours , add one minute
Case &B0011 'Key3 down
If Seconds > 119 Then Seconds = Seconds - 60 'more than one minute
Case &B1001 'key2 down
Call Power_off 'powerdown mode
Case &B1111 'key up
Key_down = 0
Seconds = Seconds / 60
Seconds = Seconds * 60
Seconds = Seconds + 59
Set_seconds = Seconds 'write to EEPROM
Enable Int1
Enable Timer2
End Select
Key_time = 0
End If
End If
If Seconds < 60 Then 'time out
Disable Timer0
Disable Timer2
V1 = 1 : V2 = 1 : V3 = 1 : Pp = 1
Set_digital &B111111
Call Play_music
Spk = 0
Ddrd.5 = 0
If Key_down = 1 Then 'any key press
Key_down = 0
If Key = &B1001 Then 'key2 down
Waitms 200
Bitwait Intk , Set
Call Power_off 'powerdown mode
Else
Waitms 200
End If
Else
Waitms 100
Call Power_off 'powerdown mode
End If
Seconds = Set_seconds 'read seconds form eeprom
Enable Timer0
Enable Timer2
End If
'Three Bytes for Seconds, Minutes and Hour must follow each other in SRAM.
'The variable-name of the first Byte, that one for Second must be passed to the function.
Secd = Time(seconds)
Seg_digital(1) = Minutes Mod 10
Seg_digital(2) = Minutes / 10
Seg_digital(3) = Hours Mod 10
Seg_digital(4) = Hours / 10
Seg_index = Segment And &B1111
V1 = 0
V2 = 0
V3 = 0
V4 = 0
If Seg_digital(3) = 0 Then Seg_digital(3) = 10
If Seg_digital(4) = 0 Then Seg_digital(4) = 10
Seg_digital(seg_index) = Lookup(seg_digital(seg_index) , Digital)
Set_digital Seg_digital(seg_index)
V1 = Seg_v.1
V2 = Seg_v.2
V3 = Seg_v.3
V4 = Seg_v.4
Loop
'*******************************************************************************
Sub Set_digital(dat As Byte)
Portc = Dat
Portd.0 = Dat.6
End Sub
'*******************************************************************************
Sub Power_off()
V1 = 0 : Pp = 0 : Spk = 0
V2 = 0 : V3 = 0 : V4 = 0
Set_digital &H00
Do : Loop Until Key2 = 1
Waitms 50
Do : Loop Until Key2 = 1
Config Int1 = Low Level
Enable Int1
Powerdown
Waitms 200
Do : Loop Until Intk = 1
Config Int1 = Falling
Enable Timer2
End Sub
'*******************************************************************************
Sub Play_music()
Local I As Byte , J As Byte
Local Dp As Single
Local Duration As Word , Pulses As Word
Ddrd.5 = 1
For I = 1 To 10
Restore Musicdata
For J = 1 To 32
Read Duration
Read Pulses
Sound Spk , Duration , Pulses
If Key_down = 1 Then Exit Sub
Next
Waitms 500
Next
End Sub
'*******************************************************************************
'5 ms
Tim0_isr:
Timer0 = 180
Rotate Seg_v , Left '<- 00100010 <-
Rotate Segment , Right , 4 '-> 4321 ->
Incr Key_time
Return
'*******************************************************************************
'get here every second with a 32768 Hz xtal
One_second:
Toggle Pp
Toggle Decrtime
Seconds = Seconds - Decrtime
Return
'*******************************************************************************
Button_down:
Key = Pind And &B11110 'get key status
Shift Key , Right , 1 'shift right 1 bit
Key_down = 1
Key_time = 15
Return
'*******************************************************************************
Digital:
Data &H40 , &H79 , &H24 , &H30 , &H19 , &H12 , &H02 , &H78 , &H00 , &H10 , &HFF
'*******************************************************************************
'two tigers music
Musicdata:
Data 600% , 48% , 600% , 43% , 600% , 38% , 600% , 48%
Data 600% , 48% , 600% , 43% , 600% , 38% , 600% , 48%
Data 600% , 38% , 600% , 36% , 1200% , 32%
Data 600% , 38% , 600% , 36% , 1200% , 32%
Data 300% , 32% , 300% , 28% , 300% , 32% , 300% , 36%
Data 600% , 38% , 600% , 48%
Data 300% , 32% , 300% , 28% , 300% , 32% , 300% , 36%
Data 600% , 38% , 600% , 48%
Data 600% , 43% , 600% , 64% , 1200% , 48%
Data 600% , 43% , 600% , 64% , 1200% , 48%
|
|
|