人生第一个程序

[复制链接]
 楼主| hhseng 发表于 2007-4-25 21:06 | 显示全部楼层 |阅读模式
我是初学PIC的,下面是我个人第一个程序,不过哪个延时子程序是在网上下的<br />请拍磚指教.<br /><br />;红绿蓝LED循環控制<br />&nbsp;&nbsp;LIST&nbsp;P=16F877A<br />&nbsp;&nbsp;INCLUDE&nbsp;&quot;P16F877A.INC&quot;<br />COUNTER&nbsp;EQU&nbsp;25H&nbsp;;定義25H为計數器<br />&nbsp;&nbsp;ORG&nbsp;0000H<br />&nbsp;&nbsp;NOP<br />主程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />&nbsp;&nbsp;BSF&nbsp;STATUS,5&nbsp;;选择体1<br />&nbsp;&nbsp;MOVLW&nbsp;0F8H&nbsp;;設置RC低3位为輸出<br />&nbsp;&nbsp;MOVWF&nbsp;TRISC<br />&nbsp;&nbsp;MOVLW&nbsp;04H&nbsp;;設置RB2为输入<br />&nbsp;&nbsp;MOVWF&nbsp;TRISB<br />&nbsp;&nbsp;BCF&nbsp;STATUS,5&nbsp;;选择体0<br />&nbsp;&nbsp;MOVLW&nbsp;00H&nbsp;;設置RC低3位初始化后为0<br />&nbsp;&nbsp;MOVWF&nbsp;PORTC<br />&nbsp;&nbsp;MOVLW&nbsp;0FFH&nbsp;;設置RB2位初始化后为1<br />&nbsp;&nbsp;CLRF&nbsp;COUNTER&nbsp;;計數器清0<br />CYC<br />&nbsp;&nbsp;CALL&nbsp;SWITCH&nbsp;;调用開關控制子程序<br />&nbsp;&nbsp;GOTO&nbsp;CYC&nbsp;;<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />按键控制子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />SWITCH<br />&nbsp;&nbsp;BTFSC&nbsp;PORTB,2&nbsp;;檢測RB2有否按下,有跳过下一条指令<br />&nbsp;&nbsp;GOTO&nbsp;BO&nbsp;;否,繼續檢測<br />&nbsp;&nbsp;PAGESEL&nbsp;TIME10MS&nbsp;;转入TIME10MS延时子程序页<br />&nbsp;&nbsp;CALL&nbsp;TIME10MS&nbsp;;调用TIME10MS延時子程序<br />&nbsp;&nbsp;PAGESEL&nbsp;SWITCH&nbsp;;转回SWITCH程序页<br />&nbsp;&nbsp;BTFSC&nbsp;PORTB,2&nbsp;;再次檢測RB2有否按下,有跳过下一条指令<br />&nbsp;&nbsp;GOTO&nbsp;BO&nbsp;;否,继续檢測<br />TOFF<br />&nbsp;&nbsp;BTFSS&nbsp;PORTB,2&nbsp;;檢測RB2有否放开,有跳过下一条指令<br />&nbsp;&nbsp;GOTO&nbsp;TOFF&nbsp;;否,繼續檢測<br />&nbsp;&nbsp;PAGESEL&nbsp;TIME10MS&nbsp;;转入TIME10MS延时子程序页<br />&nbsp;&nbsp;CALL&nbsp;TIME10MS&nbsp;;调用TIME10MS延時子程序<br />&nbsp;&nbsp;PAGESEL&nbsp;SWITCH&nbsp;;转回SWITCH程序页<br />&nbsp;&nbsp;BTFSS&nbsp;PORTB,2&nbsp;;再檢測RB2有否放开,有跳过下一条指令<br />&nbsp;&nbsp;GOTO&nbsp;TOFF&nbsp;;否,繼續檢測<br />&nbsp;&nbsp;INCF&nbsp;COUNTER&nbsp;;計數器加1为0跳过下一条指令&nbsp;<br />&nbsp;&nbsp;MOVLW&nbsp;03H&nbsp;;屏蔽計數器高6位<br />&nbsp;&nbsp;ANDWF&nbsp;COUNTER,W&nbsp;;檢測計數器<br />&nbsp;&nbsp;ADDWF&nbsp;PCL,F&nbsp;;根据計數器值狀態调用相應子程序<br />&nbsp;&nbsp;GOTO&nbsp;LED4<br />&nbsp;&nbsp;GOTO&nbsp;LED1<br />&nbsp;&nbsp;GOTO&nbsp;LED2<br />&nbsp;&nbsp;GOTO&nbsp;LED3<br />BO<br />&nbsp;&nbsp;NOP<br />&nbsp;&nbsp;RETURN<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />第1种亮灯模式子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />LED1<br />&nbsp;&nbsp;MOVLW&nbsp;04H&nbsp;;点亮RED&nbsp;LED<br />&nbsp;&nbsp;MOVWF&nbsp;PORTC&nbsp;<br />&nbsp;&nbsp;PAGESEL&nbsp;TIME1S&nbsp;;转入TIME1S延时子程序页<br />&nbsp;&nbsp;CALL&nbsp;TIME1S&nbsp;;延時1秒<br />&nbsp;&nbsp;PAGESEL&nbsp;LED1&nbsp;;转回LED1程序页&nbsp;<br />&nbsp;&nbsp;CALL&nbsp;SWITCH&nbsp;;檢測按键狀態<br />&nbsp;&nbsp;GOTO&nbsp;LED1&nbsp;;繼續返回点亮RED&nbsp;LED<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />第2种亮灯模式子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />LED2<br />&nbsp;&nbsp;MOVLW&nbsp;02H&nbsp;;点亮GREEN&nbsp;LED<br />&nbsp;&nbsp;MOVWF&nbsp;PORTC&nbsp;<br />&nbsp;&nbsp;PAGESEL&nbsp;TIME1S&nbsp;;转入TIME1S延时子程序页<br />&nbsp;&nbsp;CALL&nbsp;TIME1S&nbsp;;延時1秒<br />&nbsp;&nbsp;PAGESEL&nbsp;LED2&nbsp;;转回LED2程序页&nbsp;<br />&nbsp;&nbsp;CALL&nbsp;SWITCH&nbsp;;檢測按键狀態<br />&nbsp;&nbsp;GOTO&nbsp;LED2&nbsp;;繼續返回点亮GREEM&nbsp;LED<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />第3种亮灯模式子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />LED3<br />&nbsp;&nbsp;MOVLW&nbsp;01H&nbsp;;点亮BLUE&nbsp;LED<br />&nbsp;&nbsp;MOVWF&nbsp;PORTC<br />&nbsp;&nbsp;PAGESEL&nbsp;TIME1S&nbsp;;转入TIME1S延时子程序页<br />&nbsp;&nbsp;CALL&nbsp;TIME1S&nbsp;;延時1秒<br />&nbsp;&nbsp;PAGESEL&nbsp;LED3&nbsp;;转回LED3程序页&nbsp;<br />&nbsp;&nbsp;CALL&nbsp;SWITCH&nbsp;;檢測按键狀態<br />&nbsp;&nbsp;GOTO&nbsp;LED3&nbsp;;繼續返回点亮BLUE&nbsp;LED<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />第4种亮灯模式子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />LED4<br />&nbsp;&nbsp;INCF&nbsp;PORTC&nbsp;;順序点亮B,G,B+G,R,R+B,R+G<br />&nbsp;&nbsp;PAGESEL&nbsp;TIME1S&nbsp;;转入TIME1S延时子程序页<br />&nbsp;&nbsp;CALL&nbsp;TIME1S&nbsp;;延時1秒<br />&nbsp;&nbsp;PAGESEL&nbsp;LED4&nbsp;;转回LED4程序页&nbsp;<br />&nbsp;&nbsp;CALL&nbsp;SWITCH&nbsp;;檢測按键狀態<br />&nbsp;&nbsp;GOTO&nbsp;LED4&nbsp;;繼續順序点亮B,G,B+G,R,R+B,R+G<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />延時10MS子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />TIME10MS<br />&nbsp;&nbsp;MOVLW&nbsp;04H&nbsp;;<br />&nbsp;&nbsp;MOVWF&nbsp;23H<br />L1<br />&nbsp;&nbsp;MOVLW&nbsp;0FFH<br />&nbsp;&nbsp;MOVWF&nbsp;24H<br />L2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;DECFSZ&nbsp;24H<br />&nbsp;&nbsp;GOTO&nbsp;L2<br />&nbsp;&nbsp;DECFSZ&nbsp;23H<br />&nbsp;&nbsp;GOTO&nbsp;L1<br />&nbsp;&nbsp;RETURN<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />延時1S子程序<br />;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;<br />TIME1S<br />&nbsp;&nbsp;MOVLW&nbsp;06H<br />&nbsp;&nbsp;MOVWF&nbsp;20H<br />LOOP1<br />&nbsp;&nbsp;MOVLW&nbsp;0EBH<br />&nbsp;&nbsp;MOVWF&nbsp;21H<br />LOOP2<br />&nbsp;&nbsp;MOVLW&nbsp;0ECH<br />&nbsp;&nbsp;MOVWF&nbsp;22H<br />LOOP3<br />&nbsp;&nbsp;DECFSZ&nbsp;22H<br />&nbsp;&nbsp;GOTO&nbsp;LOOP3<br />&nbsp;&nbsp;CALL&nbsp;SWITCH<br />&nbsp;&nbsp;DECFSZ&nbsp;21H<br />&nbsp;&nbsp;GOTO&nbsp;LOOP2<br />&nbsp;&nbsp;DECFSZ&nbsp;20H<br />&nbsp;&nbsp;GOTO&nbsp;LOOP1<br />&nbsp;&nbsp;RETURN<br />&nbsp;&nbsp;END<br />&nbsp;&nbsp;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

50

主题

483

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部