PIC單片機(jī)報警程序如何設(shè)計(jì)?
PIC單片機(jī)采用精簡指令集,例如對于PIC16F716單片機(jī),只有35條單字節(jié)指令。要用這么少的指令實(shí)現(xiàn)復(fù)雜的控制或計(jì)算,顯然要在程序設(shè)計(jì)上多做文章,以下就程序設(shè)計(jì)需要注意的問題談一些看法。 美國微芯公司開發(fā)的CMOS工藝PIC系列單片機(jī),特別是采用內(nèi)置第二代Flash存儲器的微控制器在快速應(yīng)用方面具有獨(dú)到之處。PIC單片機(jī)系列內(nèi)大都包含運(yùn)算器、存儲器、A/D、PWM、輸入和輸出I/O、通信等常用接口。自由靈活的定義功能可以適應(yīng)不同的控制要求。
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單片機(jī)的源程序,除了源程序的開始處需要嚴(yán)格的列表指令外,還須注意源程序中字母符號的大小寫規(guī)則,否則在PC機(jī)上匯編程序時不會成功。在源程序中都會使用偽指令I(lǐng)NCLUDE。這條指令將列表中指定的單片機(jī)文件(在MPLAB中)瀆入源程序作為源程序的一部分,所以凡是MPLAB中有關(guān)該單片機(jī)已有的寄存器在源程序中無須再用賦值指令(EQU)賦值,這就使所建立的源程序大為簡化。
此外,由于有了偽指令I(lǐng)NCLUDE,所以根據(jù)MPLAB軟件中的格式,在源程序中的操作數(shù)凡是涉及MPLAB已規(guī)定的寄存器名稱的,其字母一律只能大寫,不能小寫。其余操作碼、符號字母可任意大小寫,但0x中的X應(yīng)小寫。否則匯編不會成功。鑒于上述原因,為了書寫方便,在使用MPLAB軟件時,PIC單片機(jī)的源程序均用大寫字母為宜(0x例外)。
以下舉例有關(guān)機(jī)器人控制的實(shí)現(xiàn)。控制部分采用PIC16F7X系列單片機(jī),運(yùn)用匯編語言編程,運(yùn)行速度較快,能夠達(dá)到系統(tǒng)的要求。
pic單片機(jī)源程序如下:
#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單片機(jī)的編程中采用合適的方法,可以使整個程序運(yùn)行穩(wěn)定,而且程序空間的使用也將有所減小,避免了調(diào)試中的Bug。其實(shí),我們公布的技術(shù)指標(biāo)在這方面并沒有任何犧牲,只是工藝上的原因,我們留的余量減少了。請大家注意不要認(rèn)為PIC的片子抗干擾能力強(qiáng),在電路設(shè)計(jì)時就一點(diǎn)不考慮應(yīng)有的抗干擾措施。