szcharger:
就是模拟三路PWM(通过不同脉冲比较来实现)--下面为摇控此程序是七彩渐变控制程如有疑问可以相互沟通QQ:756074626 电话:13510453187 谭生==========原程序如下:;;================================================================================ R0 == 0X00 TCC == 0X01 PC == 0x02 R3 == 0X03 PORT5 == 0X05 PORT6 == 0X06 IOCF == 0X0F IOCE == 0X0E IOCD == 0X0D R3F == 0X0F ;不同型号修改 C == 0X00 DELAY_C1== 0X10 PWM_C1 == 0X12 PWM_C2 == 0X13 FLAG == 0X14 BA == 0X15 BR3 == 0X16 T_PARA == 0X17;变化周期 I_PARA == 0X18;状态 DIRECT_F== 0 COMP_F == 1 AC_FLAG == 2;=========================================================================== TIME_PARA1== 1 ;可修改的时间参数;============================================================================ ORG 0X0 JMP START ;============================================================================ ; ORG 0X8 ;中断服务程序;INTT: ; MOV BA,A; SWAP BA; SWAPA R3; MOV BR3,A; ;; MOV A,R3F ;;; JBS R3F,2;; JMP INT_RETURN; clr R3F; ; inc 0x1d ; MOV A,PWM_C2; SUB A,T_PARA; JBS 0X03,0X02; JMP INT_RETURN0; ; MOV A,@0; MOV PWM_C2,A; MOV PWM_C1,A; MOV A,@TIME_PARA1 ;所有记数中断开始,严格同步; MOV DELAY_C1,A; BS FLAG,DIRECT_F ; JMP INT_RETURN ;;INT_RETURN0: ; INC PWM_C2 ;;INT_RETURN: ; SWAPA BR3; MOV R3,A; SWAPA BA; RETI ;============================================================================ ;=====================================================================================;上电后程序通过读拨码开关的状态,决定颜色变化的周期,;以及开始点亮的初始状态;P61,P62,P63,决定点亮的初始状态,P65,P66,P67,决定点颜色变化的周期,P64另做选程序用(led,lamp),;P50,红,P51,绿,P52,蓝;===================================================================================== START: CALL INIT ;初始化 MOV A,@0X99 MOV T_PARA,A/* MOV A,PORT6 RRCA PORT6 MOV T_PARA,A AND A,@0X07 MOV I_PARA,A SWAPA T_PARA AND A,@0X07 CALL TRANS MOV T_PARA,A MOV T_PARA,A MOV A,I_PARA ADD PC,A NOP ;JMP LOOP_T00取消第一个渐暗的状态 JMP LOOP_T10 JMP LOOP_T20 JMP LOOP_T30 JMP LOOP_T40 JMP LOOP_T50 JMP LOOP_T60 JMP LOOP_T70 TRANS: ADD PC,A NOP RETL @0X33 RETL @0X66 RETL @0X99 RETL @0X0CC RETL @0X0FF RETL @0X0FF RETL @0X0FF RETL @0X0FF RETL @0X0FF */ LOOP_T00: MOV A,@0B00000000 MOV PORT5,A LOOP_T0: WDTC CALL DELAY CALL DETECT_AC JBS FLAG,DIRECT_F JMP LOOP_T0 BC FLAG,DIRECT_F LOOP_T10: MOV A,@0B00000000 MOV PORT5,A;============================================================;红色由暗渐亮,其他暗;============================================================ LOOP_T1: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT1 BS PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T1 BC FLAG,DIRECT_F JMP LOOP_T20 LT1: BC PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T1 BC FLAG,DIRECT_F ;============================================================;红的保持亮,绿色由暗渐亮;============================================================ LOOP_T20: MOV A,@0B00001000;1 MOV PORT5,A LOOP_T2: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT2 BS PORT5,1 JBS FLAG,DIRECT_F JMP LOOP_T2 BC FLAG,DIRECT_F JMP LOOP_T30 LT2: BC PORT5,1 JBS FLAG,DIRECT_F JMP LOOP_T2 BC FLAG,DIRECT_F ;============================================================;绿色保持亮,红色色由亮渐暗;============================================================ LOOP_T30: MOV A,@0B00001010;1 MOV PORT5,A LOOP_T3: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT3 BC PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T3 BC FLAG,DIRECT_F JMP LOOP_T40 LT3: BS PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T3 BC FLAG,DIRECT_F ;============================================================;绿色保持亮,兰色色由暗渐亮;============================================================ LOOP_T40: MOV A,@0B00000010;0 MOV PORT5,A LOOP_T4: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT4 BS PORT5,2 JBS FLAG,DIRECT_F JMP LOOP_T4 BC FLAG,DIRECT_F JMP LOOP_T50 LT4: BC PORT5,2 JBS FLAG,DIRECT_F JMP LOOP_T4 BC FLAG,DIRECT_F ;============================================================;兰色保持亮,绿色由亮渐暗;============================================================ LOOP_T50: MOV A,@0B00000110;0 MOV PORT5,A LOOP_T5: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT5 BC PORT5,1 JBS FLAG,DIRECT_F JMP LOOP_T5 BC FLAG,DIRECT_F JMP LOOP_T60 LT5: BS PORT5,1 JBS FLAG,DIRECT_F JMP LOOP_T5 BC FLAG,DIRECT_F ;============================================================;兰色保持亮,红色由暗渐亮;============================================================ LOOP_T60: MOV A,@0B00000100;0 MOV PORT5,A LOOP_T6: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT6 BS PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T6 BC FLAG,DIRECT_F JMP LOOP_T70 LT6: BC PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T6 BC FLAG,DIRECT_F ;============================================================;红的保持亮,兰色色由亮渐暗;============================================================ LOOP_T70: MOV A,@0B00001100;1 MOV PORT5,A LOOP_T7: WDTC CALL DETECT_AC CALL DELAY MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT7 BC PORT5,2 JBS FLAG,DIRECT_F JMP LOOP_T7 BC FLAG,DIRECT_F JMP LOOP_T80 LT7: BS PORT5,2 JBS FLAG,DIRECT_F JMP LOOP_T7 BC FLAG,DIRECT_F JMP LOOP_T80;============================================================;红色由暗渐亮;============================================================ LOOP_T80: MOV A,@0B00001000;1 MOV PORT5,A LOOP_T8: WDTC CALL DELAY CALL DETECT_AC MOV A,PWM_C1 SUB A,PWM_C2 JBS 0X03,0 JMP LT8 BC PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T8 BC FLAG,DIRECT_F JMP LOOP_T00 LT8: BS PORT5,3 JBS FLAG,DIRECT_F JMP LOOP_T8 BC FLAG,DIRECT_F JMP LOOP_T00 ;======================================================= ;pwm_c1在T_PARA范围内记数,记满循环, 10个指令周期(不包括RET);======================================================= DELAY: DJZ DELAY_C1 JMP DEL0 MOV A,@TIME_PARA1 MOV DELAY_C1,A MOV A,PWM_C1 SUB A,T_PARA JBC 0X03,0X02 JMP DE50 NOP INC PWM_C1 RETDE50: MOV A,@0 MOV PWM_C1,A RET DEL0: NOP NOP NOP NOP NOP NOP NOP NOP RET INIT: mov a,@0;@0b00001011 CONTW IOW PORT5 ;端口5设为输出 MOV PORT5,A ; MOV FLAG,A MOV A,@00 IOW IOCF ;禁止中断 MOV A,@0X80 IOW IOCE ;关WDTE,P60为中断 MOV A,@0X0FF IOW PORT6 ;设为输入,中断进入 MOV A,@0X01 ;P60用外部上拉,其他使能内部上拉 IOW IOCD MOV A,@TIME_PARA1 MOV DELAY_C1,A CLRA MOV PWM_C1,A MOV PWM_C2,A RET ;============================================================================== ;检测交流电的波形下降延,此子程序15个指令周期(不包括ret),各分支执行时间相等;============================================================================== DETECT_AC: JBS FLAG,AC_FLAG JMP PORT60IS0 PORT60IS1: JBC PORT6,0 ;检测底电平是否到 JMP RET_DELAY0 BC FLAG,AC_FLAG;底电平到, MOV A,PWM_C2 SUB A,T_PARA JBS 0X03,0X02 JMP DETECT1 MOV A,@0 MOV PWM_C2,A MOV PWM_C1,A MOV A,@TIME_PARA1 ;所有记数中断开始,严格同步 MOV DELAY_C1,A BS FLAG,DIRECT_F RET DETECT1: INC PWM_C2 NOP NOP NOP NOP NOP NOP RET PORT60IS0: JBS PORT6,0 JMP RET_DELAY BS FLAG,AC_FLAG NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP RET RET_DELAY: NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP RET RET_DELAY0: NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP RET END