十年單片機開發方案公司深圳英銳恩分享PIC單片機控制LED點陣顯示屏。
本實驗在proteus軟件上模擬完成。
實驗器件:20片8X8LED點陣顯示屏,10片74LS164移位寄存器,10片74LS273數據鎖存器,2片74LS138譯碼器,1片PIC16F877A單片機。
proteus元件庫中中沒有16X80點陣顯示屏,所以用20片8X8LED點陣屏拼裝而成。
10片74LS164移位寄存器和10片74LS273數據鎖存器用于傳送和保存一行數據。
2片74LS138構成4選16譯碼器用于行選。
DELAY_REG EQU 0x20
LINE_NUM EQU 0x21 ;行號
BYTE_NUM EQU 0x22 ;字節號
WORD_NUM EQU 0x23 ;字號
;字號X32 + 行號X2 + 字節號 得查表偏移量。
BYTE_BIT_COUNT EQU 0x24 ;一個字節的位數,移位時用
SER EQU 0x07
SCK EQU 0x00
RCK EQU 0x01
;**********************************************************************
ORG 0x000 ; processor reset vector
nop ; nop required for icd
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
retfie ; return from interrupt
main
; remaining code goes here
BSF STATUS,RP0
CLRF TRISB
CLRF TRISC
CLRF TRISD
BCF STATUS,RP0
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF LINE_NUM
MAIN_LOOP:
CALL LOAD_A_LINE ;加載一行
MOVF LINE_NUM,W ;行選
MOVWF PORTC
BSF PORTB,RCK ;將一行數據送入鎖存器
NOP
NOP
BCF PORTB,RCK
CALL DELAY ;延時
INCF LINE_NUM ;下一行
BCF LINE_NUM,4 ;一共16行,模16
GOTO MAIN_LOOP
;加載一行
LOAD_A_LINE:
CLRF WORD_NUM ;字號清零
LOAD_A_WORD:
CLRF BYTE_NUM ;字節號清零
LOAD_A_BYTE:
BCF STATUS,C ;清除C標志位,為移位乘2做準備
RLF LINE_NUM,W ;每個字一行占兩個字節,所以乘2
ADDWF WORD_NUM,W ;下面WORD_NUM每次加32,不用再乘
ADDWF BYTE_NUM,W ;行號X2 + 字號X32 + 字節號
CALL ENCODE ;查表,取出對應的字節
MOVWF PORTD
MOVLW D'8'
MOVWF BYTE_BIT_COUNT ;一個字節有8位
LOAD_A_BIT:
BSF PORTB,SCK ;將PORTD最高位送入移位寄存器
NOP
NOP
BCF PORTB,SCK
RLF PORTD ;移位,準備將下一位送入移位寄存器
DECFSZ BYTE_BIT_COUNT ;循環8次
GOTO LOAD_A_BIT
INCF BYTE_NUM
BTFSS BYTE_NUM,1 ;BYTE_NUM==2?一行有兩個字節
GOTO LOAD_A_BYTE
MOVLW D'32'
ADDWF WORD_NUM ;一個字32個字節,每次加32,使用時不用再乘32
MOVF WORD_NUM,W
SUBLW D'160'
BTFSS STATUS,Z ;WORD_NUM==160?5個字160個字節
GOTO LOAD_A_WORD
RETURN
;延時
DELAY:
DECFSZ DELAY_REG
GOTO DELAY
RETURN
ENCODE:
ADDWF PCL
DT 0x3F,0xF8,0x21,0x08,0x29,0x28,0x25,0x48,0x25,0x88,0x3F,0xF8,0x01,0x00,0x01,0x00,
DT 0x3F,0xF8,0x01,0x00,0xFF,0xFE,0x00,0x00,0x48,0x84,0x44,0x42,0x84,0x42,0x00,0x00
DT 0x02,0x00,0x02,0x40,0x02,0x20,0x02,0x20,0x7F,0xFE,0x02,0x80,0x02,0x90,0x02,0x90,
DT 0x04,0xA0,0x04,0xC0,0x08,0x80,0x09,0x80,0x12,0x82,0x20,0x82,0x40,0x7E,0x00,0x00
DT 0x20,0x00,0x10,0x00,0x13,0xFC,0x00,0x40,0x88,0x40,0x48,0x40,0x50,0x40,0x10,0x40,
DT 0x10,0x40,0x20,0x40,0xE0,0x40,0x20,0x40,0x20,0x40,0x2F,0xFE,0x20,0x00,0x00,0x00
DT 0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x02,0x80,
DT 0x02,0x80,0x02,0x40,0x04,0x40,0x04,0x20,0x08,0x10,0x10,0x18,0x20,0x0E,0x40,0x04
DT 0x01,0x08,0x10,0x8C,0x0C,0xC8,0x08,0x90,0x7F,0xFE,0x40,0x04,0x8F,0xE8,0x00,0x40,
DT 0x00,0x80,0x7F,0xFE,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x02,0x80,0x01,0x00
END
;***** VARIABLE DEFINITIONS
w_temp EQU 0x7D ; variable used for context saving
status_temp EQU 0x7E ; variable used for context saving
pclath_temp EQU 0x7F ; variable used for context saving
; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
list p=16f877A ; list directive to define processor
#include ; processor specific variable definitions
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF
源程序:
(圖文源網絡,侵刪)