文件名: DSCF3696-1
这是源代码:
'///////////////////////////////////////////////////////////////////////////////
'/ /
'/ 用M8与LM2576制作可调数控开关电源 /
'/ 输出电压 0-30V /
'/ 最大输出电流 3.0A /
'/ 截止型过电流保护 0-3.0A 连续调节 /
'///////////////////////////////////////////////////////////////////////////////
$regfile = "m8def.dat"
$crystal = 8000000
Config Lcdpin = Pin , Db4 = Portb.2 , Db5 = Portb.3 , Db6 = Portb.4 , Db7 = Portb.5 , E = Portb.0 , Rs = Portd.2
Config Lcd = 16 * 2
'定义LED是16字符,2行
Config Adc = Single , Prescaler = Auto , Reference = Internal
'定义ADC为单次运行模式,转换时钟-自动,内部参考电压
Config Timer1 = Pwm , Pwm = 10 , Compare A Pwm = Clear Down , Prescale = 1
'PWM1A配置,用于数模转换,
Ddrb.1 = 1 'PWM1A输出端子
'定义所需要变量 ////////////////////////////////////////////////////////////////
Dim V_dc As Long , V_dc1 As Long , A_dc As Long , A_dc1 As Long
Dim V1_dc As Word , V2_dc As Word
Dim A1_dc As Word , A2_dc As Word , A As Word , C As Word , A1 As Word
Dim V1 As String * 5
Dim V2 As String * 5
Dim V3 As String * 5
Dim V4 As String * 5
Dim C1 As String * 4
Dim Va1 As Byte
Dim N As Byte , B2 As Byte , B3 As Byte , B4 As Byte , B5 As Byte
'初始化设置/////////////////////////////////////////////////////////////////////
Ddrd.3 = 0 '定义PD.3~PD.7为输入,上拉电阻有效 .按键初始化
Ddrd.4 = 0
Ddrd.5 = 0
Ddrd.6 = 0
Ddrd.7 = 0
Portd.3 = 1
Portd.4 = 1
Portd.5 = 1
Portd.6 = 1
Portd.7 = 1
Start Timer1
Start Adc
Cls
Cursor Off
Locate 1 , 14
Lcd "ON-"
A = 0
C = 3000
Ddrc.4 = 1 '初始化电源开关
Portc.4 = 0
'主程序/////////////////////////////////////////////////////////////////////////
Do
Pwm1a = A
N = 1
V1_dc = 0
V_dc = 0
V2_dc = 0
V_dc1 = 0
A1_dc = 0
A_dc = 0
A2_dc = 0
A_dc1 = 0
For N = 1 To 60 'ADC采样60次
V1_dc = Getadc(0) '电源输出电压
V_dc = V_dc + V1_dc 'ADC的原始值累加
A1_dc = Getadc(1) '电流300mA档
A_dc = A_dc + A1_dc
A2_dc = Getadc(2) '电流3A档
A_dc1 = A_dc1 + A2_dc
V2_dc = Getadc(3) '电源输入电压
V_dc1 = V_dc1 + V2_dc
Next
V_dc = V_dc / 60 '还原各测量值
A_dc = A_dc / 60
A_dc1 = A_dc1 / 60
V_dc1 = V_dc1 / 60
V_dc = 3040 * V_dc '标准3040
V_dc = V_dc / 1023
A_dc = 3120 * A_dc '标准3040
A_dc = A_dc / 1023
A_dc1 = 3040 * A_dc1
A_dc1 = A_dc1 / 1023
V_dc1 = 4025 * V_dc1
V_dc1 = V_dc1 / 1023
If A_dc>= C Then '截止型电流保护300mA档
Waitms 500
If A_dc>= C Then
Portc.4 = 1
Locate 1 , 14
Lcd "OC "
Gosub Mu
End If
End If
If A_dc1>= C Then '截止型电流保护3A档
Waitms 500
If A_dc1>= C Then
Portc.4 = 1
Locate 1 , 14
Lcd "OC "
Gosub Mu
End If
End If
If V_dc1 <= 1200 Then '输入电压范围低端保护
Cls
Lcd "Input U<12V"
Portc.4 = 1
Gosub Mu
End If
If V_dc1>= 3500 Then '输入电压范围高端保护
Cls
Lcd "Input U>35V"
Portc.4 = 1
Gosub Mu
End If
Gosub Lp_lcd
Gosub Lp_key
Loop
End
'显示处理///////////////////////////////////////////////////////////////////////
Lp_lcd:
V1 = Str(v_dc) '输出电压
Va1 = Len(v1) '读字符串
If Va1 <= 3 Then
V1 = Format(v1 , "0.00") '定位小数点
Else
V1 = Format(v1 , " 0.00")
End If
V2 = Str(a_dc) '电流300mA档
Va1 = Len(v2)
If Va1 <= 2 Then
V2 = Format(v2 , "0.0")
Else
V2 = Format(v2 , " 0.0")
End If
V3 = Str(a_dc1) '电流3A档
V3 = Format(v3 , "0.000")
V4 = Str(v_dc1) '输入电压
V4 = Format(v4 , " 0")
C1 = Str(c) '设定电流
C1 = Format(c1 , " 0")
Locate 1 , 8
Lcd " "
Locate 1 , 1
Lcd "U:" ; V1 ; "V"
Locate 2 , 1
Lcd "I:"
If A_dc> A_dc1 Then
Locate 2 , 8
Lcd " "
Locate 2 , 1
Lcd "I:" ; V2 ; "mA"
End If
If A_dc < A_dc1 Then
Locate 2 , 1
Lcd "I:" ; V3 ; "A"
End If
If A_dc = A_dc1 Then
Locate 2 , 3
Lcd "0.0 "
End If
'Locate 1 , 9
'Lcd V4
Locate 2 , 10
Lcd "<" ; C1 ; "mA"
Return
'按键处理//////////////////////////////////////////////////////////////////////
Lp_key:
If Pind.3 = 0 Then '启动
Waitms 20
If Pind.3 = 0 And Portc.4 = 1 Then
Portc.4 = 0 'Portc.4控制LM2576开/关
Locate 1 , 14
Lcd "ON "
Waitms 250
End If
End If
If Pind.3 = 0 Then '停止
Waitms 20
If Pind.3 = 0 And Portc.4 = 0 Then
Portc.4 = 1 '控制LM2576开/关
Locate 1 , 14
Lcd "OFF"
End If
End If
If Pind.4 = 1 Then B2 = 0 '电压+
If Pind.4 = 0 And Portc.4 = 0 Then
Waitms 20
If Pind.4 = 0 Then
If A < 1023 Then
A = A + 1
B2 = B2 + 1
If B2> 20 And A < 1013 Then
A = A + 10
End If
End If
End If
End If
If Pind.5 = 1 Then B3 = 0 '电压-
If Pind.5 = 0 And Portc.4 = 0 Then
Waitms 20
If Pind.5 = 0 Then
If A> 1 Then
A = A - 1
B3 = B3 + 1
If B3> 20 And A> 10 Then
A = A - 10
End If
End If
End If
End If
If Pind.6 = 1 Then B4 = 0 '电流+
If Pind.6 = 0 Then
Waitms 20
If Pind.6 = 0 Then
If C < 3000 Then
C = C + 1
B4 = B4 + 1
If B4> 20 And C < 2980 Then
C = C + 20
End If
End If
End If
End If
If Pind.7 = 1 Then B5 = 0 '电流-
If Pind.7 = 0 Then
Waitms 20
If Pind.7 = 0 Then
If C> 1 Then
C = C - 1
B5 = B5 + 1
If B5> 20 And C> 20 Then
C = C - 20
End If
End If
End If
End If
Return
'报警声音///////////////////////////////////////////////////////////////////////
Mu:
Sound Portc.5 , 100 , 1000
Waitms 20
Sound Portc.5 , 100 , 1000
Waitms 20
Sound Portc.5 , 100 , 1000
Return
////////////////////////////////////////////////////////////////////////////////
HEX文件:
(文件大小:4K) (原文件名: KG01.rar)
装配图:
原文件名:装配图.pdf)
PCB+热转印:
(原文件名: PCB+热转印.rar)
3