PIC单片机报警程序如何设计?
扫描二维码
随时随地手机看文章
PIC单片机采用精简指令集,例如对于PIC16F716单片机,只有35条单字节指令。要用这么少的指令实现复杂的控制或计算,显然要在程序设计上多做文章,以下就程序设计需要注意的问题谈一些看法。 美国微芯公司开发的CMOS工艺PIC系列单片机,特别是采用内置第二代Flash存储器的微控制器在快速应用方面具有独到之处。PIC单片机系列内大都包含运算器、存储器、A/D、PWM、输入和输出I/O、通信等常用接口。自由灵活的定义功能可以适应不同的控制要求。
pIC16c54警报声程序
#include“pic.h”
#definebeep_outRA2//音效输出
chartmr_buff;
charcycle_buff;
charcycle_set;
intchange_set;
intchange_count;
bitchange_flag;
voidmain(void)
{TRISA=0X0;
PORTA=0;
OPTION=4;
cycle_buff=52;
cycle_set=100;
change_set=196;
TMR0=0;
while(1)
{
if(TMR0==0)
{TMR0=(cycle_set-1)^0XFF;
beep_out=!beep_out;
change_count+=cycle_set;
if(change_count》change_set)
{change_count-=change_set;
if(change_flag)
{if(++cycle_set》195)
{change_flag=0;
}
}
else
{if(--cycle_set《52)
{change_flag=1;
}
}
}
}
}
}
编写PIC单片机的源程序,除了源程序的开始处需要严格的列表指令外,还须注意源程序中字母符号的大小写规则,否则在PC机上汇编程序时不会成功。在源程序中都会使用伪指令INCLUDE。这条指令将列表中指定的单片机文件(在MPLAB中)渎入源程序作为源程序的一部分,所以凡是MPLAB中有关该单片机已有的寄存器在源程序中无须再用赋值指令(EQU)赋值,这就使所建立的源程序大为简化。
此外,由于有了伪指令INCLUDE,所以根据MPLAB软件中的格式,在源程序中的操作数凡是涉及MPLAB已规定的寄存器名称的,其字母一律只能大写,不能小写。其余操作码、符号字母可任意大小写,但0x中的X应小写。否则汇编不会成功。鉴于上述原因,为了书写方便,在使用MPLAB软件时,PIC单片机的源程序均用大写字母为宜(0x例外)。
以下举例有关机器人控制的实现。控制部分采用PIC16F7X系列单片机,运用汇编语言编程,运行速度较快,能够达到系统的要求。
pic单片机源程序如下:
#include ;
#include
__config 0x3F4C ;_CPC_ON & _CPU_ON & _CPP_ON & _PWRTE_OFF & _WDT_ON & _FOSC_HS ; 0x3F4C
; RAM-Variable
CSR1 equ 0x20
CSR2 equ 0x21
STATUS_TEMP equ 0x2C
PCL_TEMP equ 0x2D
DE_TIME1 equ 0x22
DE_TIME0 equ 0x23
DE_TIME2 equ 0x24
DE_TIME3 equ 0x25
TIME_TMP0 equ 0x26
TIME_TMP1 equ 0x27
SOUND_D_TIME0 equ 0x28
SOUND_D_TIME1 equ 0x29
DE_TMEP0 equ 0x2A
DE_TMEP1 equ 0x2B
CON_SOUND equ 0x55
SOUND_B_TIME0 equ 0x56
SOUND_B_TIME1 equ 0x57
SOUND_D_TIME2 equ 0x58
CON_A_STAY equ 0x59
TR0_TIME equ 0x5A
SOUND_C_TIME1 equ 0x5B
SOUND_D_TIME3 equ 0x5C
SOUND_C_TIME0 equ 0x5D
W_TEMP equ 0x5E
; Program
Org 0x0000
; Reset-Vector
GOTO MAIN
Org 0x0004
; Interrupt-Vector
MOVWF W_TEMP
SWAPF STATUS,W
BCF STATUS,RP0
MOVWF STATUS_TEMP
MOVF PCLATH,W
MOVWF PCL_TEMP
GOTO Interrupt_0
Interrupt_0
BTFSC INTCON,T0IF
BTFSS INTCON,T0IE
GOTO Interrupt_exit
MOVF TR0_TIME,W
ADDWF TMR0,F
MOVLW 0x04
BCF INTCON,T0IF
BSF CON_SOUND,2
XORWF GPIO,F
CLRWDT
Interrupt_exit
MOVF PCL_TEMP,W
MOVWF PCLATH
SWAPF STATUS_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
RETFIE
;*****************************************
RAM_00
MOVLW 0x49
MOVWF CSR1
MOVLW 0x21
MOVWF CSR2
MAIN_RAM
INCF CSR2,F
MOVF CSR2,W
MOVWF FSR
CLRF INDF
DECFSZ CSR1,F
GOTO MAIN_RAM
CLRF CSR2
RETURN
MAIN
CALL RAM_00
BSF STATUS,RP0
;CALL LADR_0x03FF
;MOVWF SFR_0x10
CLRF STATUS
MOVLW 0xA7
CLRF SOUND_B_TIME0
CLRF SOUND_B_TIME1
CLRF SOUND_D_TIME2
CLRF CON_A_STAY
CLRF TR0_TIME
CLRF SOUND_C_TIME1
MOVWF SOUND_D_TIME3
MOVLW 0xFE
MOVWF SOUND_C_TIME0
CLRF STATUS
MOVLW 0x58
MOVWF TIME_TMP0
MOVLW 0x02
MOVWF TIME_TMP1
;CALL DELAY_0
MOVLW 0x07
MOVWF CMCON ;SFR_0x19 ; !!Warning: SFR_0x19 is Unimplemented
MOVLW 0x81
BSF STATUS,RP0
MOVWF TMR0
MOVLW 0x3B ;0x3B
MOVWF GPIO ;PORTA
BCF STATUS,RP0
CLRF GPIO ;PORTA
BSF INTCON,GIE
CLRF GPIO ;PORTA
GOTO SEARCH_KEY
WHILE_0
BTFSS CON_SOUND,5
GOTO WHILE_1
BCF CON_SOUND,0
CALL LADR_0x0154
SEARCH_KEY
CLRWDT
BCF STATUS,RP0
BTFSC GPIO ,4 ;BTFSC GPIO ,4 ;PORTA,4
GOTO SOUND_A_0
BTFSC GPIO,5
GOTO SOUND_B_0
BTFSC GPIO,0 ;PORTA,0
GOTO SOUND_C_0
BTFSC GPIO,1 ; PORTA,1
GOTO SOUND_D_0
BTFSS GPIO ,1 ;PORTA,1
GOTO WHILE_0
WHILE_1
CLRF GPIO ;PORTA
MOVLW 0xA7
BCF INTCON,T0IE
BCF CON_SOUND,5
BCF CON_SOUND,0
MOVWF SOUND_D_TIME3
BCF GPIO,2 ; PORTA,2
GOTO SEARCH_KEY
;********************************
SOUND_A_0
BTFSC CON_SOUND,0
GOTO SOUND_A_1
BSF INTCON,T0IE
BSF CON_SOUND,0
BCF CON_SOUND,5
SOUND_A_1
CALL SOUND_A_2
GOTO SEARCH_KEY
SOUND_A_2
BSF STATUS,RP0
MOVF TMR0,W
XORLW 0x81
BTFSC STATUS,Z
GOTO SOUND_A_3
MOVLW 0x81
MOVWF TMR0
SOUND_A_3
BCF STATUS,RP0
BTFSS CON_SOUND,2
RETURN
BCF CON_SOUND,2
INCFSZ CON_A_STAY,F
GOTO SOUND_A_5
SOUND_A_EXIT
MOVLW 0x3B
MOVWF TR0_TIME
RETURN
SOUND_A_5
MOVLW 0x02
SUBWF CON_A_STAY,W
BTFSC STATUS,C
GOTO SOUND_A_6
SOUND_A_51
MOVLW 0x76
MOVWF TR0_TIME
RETURN
SOUND_A_6
MOVLW 0x03
SUBWF CON_A_STAY,W
BTFSC STATUS,C
GOTO SOUND_A_7
SOUND_A_61
MOVLW 0x8F
MOVWF TR0_TIME
RETURN
SOUND_A_7
MOVLW 0x04
SUBWF CON_A_STAY,W
BTFSC STATUS,C
GOTO SOUND_A_8
SOUND_A_71
MOVLW 0x94
MOVWF TR0_TIME
RETURN
SOUND_A_8
MOVLW 0x05
SUBWF CON_A_STAY,W
BTFSC STATUS,C
GOTO SOUND_A_9
SOUND_A_81
MOVLW 0xA3
MOVWF TR0_TIME
RETURN
SOUND_A_9
MOVLW 0x06
SUBWF CON_A_STAY,W
BTFSS STATUS,C
GOTO SOUND_A_81
MOVLW 0x07
SUBWF CON_A_STAY,W
BTFSS STATUS,C
GOTO SOUND_A_71
MOVLW 0x08
SUBWF CON_A_STAY,W
BTFSS STATUS,C
GOTO SOUND_A_61
MOVLW 0x09
SUBWF CON_A_STAY,W
BTFSS STATUS,C
GOTO SOUND_A_51
CLRF CON_A_STAY
GOTO SOUND_A_EXIT
;********************************
SOUND_B_0
BTFSC CON_SOUND,0
GOTO SOUND_B_1
BSF INTCON,T0IE
BSF CON_SOUND,0
BCF CON_SOUND,5
CLRF SOUND_B_TIME0
CLRF SOUND_B_TIME1
SOUND_B_1
CALL SOUND_B_2
GOTO SEARCH_KEY
SOUND_B_2
BSF STATUS,RP0
MOVF TMR0,W
XORLW 0x81
BTFSC STATUS,Z
GOTO SOUND_B_3
MOVLW 0x81
MOVWF TMR0
SOUND_B_3
BCF STATUS,RP0
BTFSS CON_SOUND,1
GOTO SOUND_B_5
MOVLW 0x66
MOVWF TR0_TIME
BTFSS CON_SOUND,2
RETURN
BCF CON_SOUND,2
INCF SOUND_B_TIME0,F
BTFSC STATUS,Z
INCF SOUND_B_TIME1,F
DECF SOUND_B_TIME1,W
MOVLW 0x90
SOUND_B_4
BTFSC STATUS,Z
XORWF SOUND_B_TIME0,W
BTFSS STATUS,Z
RETURN
MOVLW 0x02
CLRF SOUND_B_TIME0
CLRF SOUND_B_TIME1
XORWF CON_SOUND,F
RETURN
SOUND_B_5
MOVLW 0x85
总之,在PIC单片机的编程中采用合适的方法,可以使整个程序运行稳定,而且程序空间的使用也将有所减小,避免了调试中的Bug。其实,我们公布的技术指标在这方面并没有任何牺牲,只是工艺上的原因,我们留的余量减少了。请大家注意不要认为PIC的片子抗干扰能力强,在电路设计时就一点不考虑应有的抗干扰措施。