877A software simulates SPI communication to read and write 93C46

Publisher:chwwdchLatest update time:2017-12-10 Source: eefocusKeywords:877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

;Functional description:
;1. Make a left shift of a light, and build the 8 codes of the left shift into the TABLE.
;2. Take out the data from the TABLE and store it in 93C46 in 16-bit mode, address 00H-03H.
;3. Take out the data code in the address 00H-03H of 93C46 and store it in the RAM (30H)-(37H) of 877A, and output it to the RC port of 877A.
;When the power is cut off and then restored, the data code will not disappear.
;4. If the 93C46 used in this circuit is ATMEL or M IC RO Chip , or is not from this factory, the programming time will be different


;必须调整本程序的DELAY时间
;本实战的目的是让大家进上步熟悉SPI通信的时序,熟悉93C46的读写,会用软件模拟SPI通信
;硬件接法:
;1.93C46的CS接877A的RB1口;CLK接877A的RB2口;DI接877A的RB4口;DO接877A的RB5口;93C46的ORG端接VCC,使93C46工作于16位方式
;2.实验本实验须将MCD-DEMO实验板上的24CXX系列芯片先取下,在实验过程中不要按动同样接在RB口的按键,以免影响通信时序.
;3.实验板上拔码开关S1要置ON,其它拔码开关都可以关闭。
;PIC 单片机 学习网  陈学乾  http://www.pic16.com   讨论论坛:http://pic16.com/bbs/
;版权所有,转载请注明出处,并不能去掉或改变文件中的说明文字。
;程序文件名“MCD-93C46.ASM"
;程序清单如下:
;************************************
    LIST      P=16F877A, R=DEC
    include "P16F877A.inc"
;***********************************
__CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC;
;************************************ 定义查表偏移量
READ   EQU   0  ;读93C46
WRITE  EQU   1  ;写入93C46 
EWEN   EQU   2  ;93C46写入使能
EWDS   EQU   3  ;93C46写入禁止
;*************************************定义引脚位地址
CS     EQU   1
CLK    EQU   2
DI     EQU   4
DO     EQU   5
;*********************
ADR46  EQU   20H
F1     EQU   23H
F2     EQU   24H
F3     EQU   25H
F4     EQU   26H
F5     EQU   27H
F6     EQU   28H
F7     EQU   29H
;**********************
  ORG 000H
  NOP              ;放置一条ICD必需的空操作指令
  GOTO MAIN
  ORG 0008H
;******************************************************
TABLE
  ADDWF PCL ,1   ;查表,PORTC一个灯左移
  RETLW  01H
  RETLW  02H
  RETLW  04H
  RETLW  08H
  RETLW  10H
  RETLW  20H
  RETLW  40H
  RETLW  80H
;*******************************************************
TO9346
  MOVWF F1        ;将W的值送F1暂存
  BSF  PORTB ,CS          ;写入起始位1
  BSF  PORTB ,DI
  BSF  PORTB ,CLK
  CALL DELAY
  BCF  PORTB ,CLK
  CALL DELAY
  MOVFW F1
  ADDWF PCL ,1
  GOTO  SREAD    ;读
  GOTO  SWRITE   ;写
  GOTO  SEWEN    ;写使能
  GOTO  SEWDS    ;写禁止
;*****************************************************
MAIN
  MOVLW  00H
  MOVWF  PORTC            ;LED先全部熄灭
  MOVLW  20H
  MOVWF  PORTB            ;除DO外,其它通信口全送0

  BSF STATUS,RP0          ;定义RA口,RC口全部为输出
  MOVLW 20H
  MOVWF TRISB             ;RB口5脚为入,其它全为出
  CLRW
  MOVWF TRISC             ;RC口全为输出.
  MOVWF OPTION_REG        ;开启RB口内部弱上拉
  BCF STATUS,RP0
;****************************************
LOOP
  CLRF 21H ;code pointer
  CLRF ADR46 ;93C46 address 00H
  MOVLW 04H
  MOVWF 22H ;4 groups of 8 codes
START
  MOVLW EWEN ;write enable
  CALL TO9346
  MOVFW 21H ;load code pointer
  CALL TABLE ;get code from TABLE
  MOVWF F5 ;store in "write register"
  INCF 21H ,1 ;get next code
  MOVFW 21H
  CALL TABLE ;get code from TABLE
  MOVWF F4
  MOVLW WRITE           
  CALL TO9346 ;write data
  MOVLW EWDS
  CALL TO9346 ;write disable
  INCF 21H ,1 ;get next code
  INCF ADR46 ,1 ;get next address
  CALL DELAY1
  DECFSZ 22H ,1 ;Until writing four addresses
  GOTO START
  MOVLW 30H ;Store in 877A's RAM first address
  MOVWF FSR
  CLRF ADR46 ;93C46 address 00
  MOVLW 04H
  MOVWF 22H ;Read 93C46's four addresses, 8 codes
;********************
A1
  MOVLW READ
  CALL TO9346 ;Read the data in the address
  MOVFW F5
  MOVWF INDF ;Store the read data in 877A's RAM
  INCF FSR ,1
  MOVFW F4
  MOVWF INDF
  INCF ADR46 ,1 ;Read the next address
  INCF FSR ,1
  DECFSZ 22H ,1 ;Until reading four addresses
  GOTO A1
A2
  MOVLW 08H
  MOVWF 22H
  MOVLW 30H ;RAM 30h-37H 8 codes in total
  MOVWF FSR
OUTPUT
  MOVFW INDF
  MOVWF PORTC ; output result to PORTC
  CALL DELAY1   
  INCF FSR ,1
  DECFSZ 22H ,1
  GOTO OUTPUT
  GOTO A2
;****************************
SREAD
  MOVLW 80H
  ADDWF ADR46 ,0 ; 6-bit address plus two-bit operation code, 10XXXXXX read instruction
  CALL SDT46 ; write operation code and address
  CALL RDT46 ; read high data
  MOVWF F5 ; store in F5
  CALL RDT46 ; read low address              
  MOVWF F4 ; store in F4
  GOTO EX9346
;******************************
SWRITE
  MOVLW 40H
  ADDWF ADR46 ,0 ; 6-bit address plus two-bit operation code 01XXXXXX write instruction
  CALL SDT46 ;Write opcode and address
  MOVFW F5 ;Load data
  CALL SDT46 ;Write data
  MOVFW F4 ;Load data
  CALL SDT46 ;Write data
  GOTO EX9346 
;******************************
SEWEN
  MOVLW 30H ;Write opcode 0011XXXX Write enable instruction
  CALL SDT46
  GOTO EX9346
;******************************
SEWDS
  CLRW ;Write 0000XXXX write disable instruction
  CALL SDT46
;******************************
EX9346
  BCF PORTB ,CS ;Clear CS to 0 when finished
  RETURN
;******************************
SDT46:
  MOVWF F2 ;Send the data to be written to F2
  MOVLW 08H ;Write 8-bit data
  MOVWF F3
SD1:
  RLF F2 ,1
  BSF PORTB ,DI
  BTFSS STATUS ,C
  BCF PORTB ,DI
  BSF PORTB ,CLK
  CALL DELAY
  BCF PORTB ,CLK
  CALL DELAY
  DECFSZ F3 ,1
  GOTO SD1
  RETURN
;******************************
RDT46
  MOVLW 08H ;Read 8-bit data
  MOVWF F3
RD1
  BSF PORTB ,CLK
  CALL DELAY
  BCF PORTB ,CLK
  CALL DELAY
  BSF STATUS ,C
  BTFSS PORTB ,DO
  BCF STATUS ,C
  RLF F2 ,1
  DECFSZ F3 ,1
  GOTO RD1
  MOVFW F2 ;Send the read data to W
  RETURN
;********************************
DELAY
  MOVLW 1FH ;CLK timing delay
  MOVWF F7
  DECFSZ F7 ,1
  GOTO $-1
  RETURN
;***********************************
DELAY1 ;Delay
  MOVLW .20
  MOVWF F4
D1
  MOVLW .40
  MOVWF F5
D2
  MOVLW .248
  MOVWF F6
  DECFSZ F6 ,1
  GOTO $-1
  DECFSZ F5 ,1
  GOTO D2
  DECFSZ F4 ,1
  GOTO D1
  RETURN
;************************************************
       end ;End of source program
;*********************************************************


Keywords:877A Reference address:877A software simulates SPI communication to read and write 93C46

Previous article:pic16f877a read and write program for 24c01
Next article:877A software simulates I2C communication to read and write 24C02

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号