用attiny26和attiny24能DIY出可飞的无刷电调吗?
本贴完全不从商业方面考虑,因为适合做航模电调的AVR芯片无疑是Atmega8和Atmega48/88/168/328系列,而实际上可能Attiny26和Attiny24销售量太小,实际购买价格并不低于Atmega8和Atmega48,这里只是一个对硬件条件的小小挑战,纯业余爱好,不属于商业范畴,反正我就喜欢这样折腾自己。本贴许多指令以BASCOM为默认语言,如需要C格式表述,看官可提出,我在随后补充。刚开始折腾C,许多语句有可能用得不太规范,但意思应该能表达清楚。Attiny26基本参数:
FLASH容量:2K
RAM容量: 128
8位T/C: 2个,其中一个,T/C1有比较匹配中断及溢出中断,可利用之处是其预分频系数可以是 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8196,16384
硬件乘法器: 无
可用IO口数: 15(或16个,假如不要reset,刷写要用并口编程器)
Attiny24基本参数:
FLASH容量:2K
RAM容量: 128
8位T/C: 1个,有比较匹配中断及溢出中断
16位T/C: 1个,有比较匹配中断及溢出中断
硬件乘法器: 无
可用IO口数: 11(或12个,假如不要reset,刷写要用高压编程器) 2k全部用asm有可能办到。德国人网上有现成的程序哦。 现在把Attiny26L无刷电调实验图及源程序传上来,编译后约为1040字节,BASCOM的,使用中断软件产生PWM,中断里全部用汇编代码,过零检测是在PWM过程中软件过滤有效时间段,读取ACSR.ACO(即模拟比较输出值),而不用低通滤波电路。MOS管用NP复合管AO4606。单片机原先用DIP的封装,后来在实验中仅有的3片直插T26全部失效,两片是电路板下面有金属片,下载程序时阴差阳错被锁了,另一片是想试试堵转时力矩有多大,结果烧了MOS管,还烧掉了T26,只能找出贴片的,飞线焊上完成实验。AO4606的最大电流很小,驱动硬盘电机还行,对付航模电机有点吃力,所以程序里最大占空比限制为55%(140/255),电机上象征性的装一片7030桨。
程序只有基本的自启动转动功能,动力从0逐渐升到140,转动很稳定。剩下的容量还有将近1K,加上PPM接收和电流保护功能是够的,所以用T26自己做出可飞电调是能实现的,以下是实验程序的完整代码,和我自己写的用ATMEGA8及ATMEGA48的实用电调程序结构类似,但要简单一点。
程序源代码:
$regfile = "attiny26.dat"
$crystal = 8000000
$hwstack = 12
$swstack = 8
$framesize = 22
Osccal = &H9F 'RC 校正
Dim Pwmctl As Word , Pwmctladj As Byte , Pwmctlppm As Word , Pwmbit As Byte
Dim Widetemp As Byte , Wide As Byte , Widepre As Byte , Time0 As Byte, K1 As Byte , K2 As Byte , Tc1 As Byte
Dim Zerocross As Byte , Acset As Byte , Pole As Byte , Zccheck As Byte , J As Byte , K As Byte
Config Timer1 = Timer , Prescale = 4 'T26特有的时钟4分频,产生约8K的PWM波
Enable Interrupts
Enable Oc1a
Enable Ovf1
On Oc1a Isr_oc1a Nosave'中断进程,不用现场保护,用以提高进程运行速度
On Ovf1 Isr_ovf1 Nosave
Declare Sub Abon
Declare Sub Acon
Declare Sub Bcon
Declare Sub Baon
Declare Sub Caon
Declare Sub Cbon
Declare Sub Run
Ddrb = &B00111111
Ddra = 0
An Alias Portb.0 'A-
Bn Alias Portb.1 'B-
Cn Alias Portb.2 'C-
Ap Alias Portb.3 'A+
Bp Alias Portb.4 'B+
_cp Alias Portb.5 'C+
Set Acsr.acme 'ENABLE ACME
Ocr1a = 0 '预设最长换相时间
Wide = 65
Widetemp = Wide
Do '主程序
Abon
Acon
Bcon
Baon
Caon
Cbon
Loop
End
Sub Abon '各相运行过程
Time0 = 0
Admux = &B00000010 ' ADC2 端口
Acset = 1 '上升沿
Pole = &B00001010 '选通端口
Enable Ovf1 '开启中断
Ap = Pwmbit '根据此时PWM波情况,PWM波跨越换相后端口应该的状态
Bn = Pwmbit
Call Run
Disable Ovf1 '关中断
Portb = 0 '关断所有端口输出
End Sub
Sub Acon
Time0 = 0
Admux = &B00000001 'ADC1
Acset = 0 '下降沿
Pole = &B00001100
Enable Ovf1
Ap = Pwmbit
Cn = Pwmbit
Call Run
Disable Ovf1
Portb = 0
End Sub
Sub Bcon
Time0 = 0
Admux = &B00000000 'ADC0
Acset = 1
Pole = &B00010100
Enable Ovf1
Bp = Pwmbit
Cn = Pwmbit
Call Run
Disable Ovf1
Portb = 0
End Sub
Sub Baon
Time0 = 0
Admux = &B00000010
Acset = 0
Pole = &B00010001
Enable Ovf1
Bp = Pwmbit
An = Pwmbit
Call Run
Disable Ovf1
Portb = 0
End Sub
Sub Caon
Time0 = 0
Admux = &B00000001
Acset = 1
Pole = &B00100001
Enable Ovf1
_cp = Pwmbit
An = Pwmbit
Call Run
Disable Ovf1
Portb = 0
End Sub
Sub Cbon
Time0 = 0
Admux = &B00000000
Acset = 0
Pole = &B00100010
Enable Ovf1
_cp = Pwmbit
Bn = Pwmbit
Call Run
Disable Ovf1
Portb = 0
End Sub
Sub Run '主过程
Widepre = Widetemp '换相计时数
Shift Widepre , Right , 3 '换相检测预延时
J = Widetemp '过零检测次数
Shift J , Right , 1
If J >= 10 Then J = 10
If J < 2 Then J = 2
Zerocross = 0
Incr K 'K值,延时增大占空比
If K >= 40 Then
Incr Ocr1a
K = 0
End If
If Ocr1a >= 140 Then Ocr1a = 140 '限制最大占空比
K1 = Ocr1a - 20 '软件选取采样时间,滤掉干扰较大的时间段
K2 = Ocr1a + 30
Do
Loop Until Time0 >= Widepre '采样预延时
Do
Tc1 = Tcnt1
Zccheck = Acsr.aco
If Tc1 > 30 And Tc1 < K1 Or Tc1 > K2 And Tc1 < 245 Then
If Zccheck = Acset Then
Incr Zerocross
Else
If Zerocross >= 1 Then Decr Zerocross
End If
End If
Loop Until Zerocross >= J Or Time0 >= Wide
Widetemp = Time0 '实际过零时间
Time0 = 0
Widepre = Widetemp
If Widetemp >= 4 Then '换相延时计算,考虑了运算延时影响,经验值
Shift Widepre , Right , 4
Else
Widepre = 0
End If
Do '换相延时
Loop Until Time0 >= Widepre
End Sub
Isr_oc1a: '中断进程,程序中其它地方没用R4,R18,故不用POP、PUSH语句
IN r4, SREG '保存SREG
Clr R18
!out portb,r18
sts {pwmbit},r18
lds r18,{time0}
inc r18
sts {time0},r18 '中断计时
!Out Sreg , r4 '恢复SREG
Return
Isr_ovf1:
IN r4, SREG
lds r18,{pole} '适当端口置位
!out portb,r18
Ldi R18 , 1
sts {pwmbit},r18 '选通端口的状态,在PWM波跨越换相时,端口要处于正确状态
!Out Sreg , R4
Return
贴图
http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_690677RFS52Y.jpg
(原文件名:运行.jpg)
http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_690678Q619J0.jpg
(原文件名:正面.jpg)
http://cache.amobbs.com/bbs_upload782111/files_47/ourdev_690679B227U7.jpg
(原文件名:背面.jpg)
页:
[1]