电工吧
◎ 电工吧 >> 设为首页 >> 加入收藏>> 联系方式 >> 投稿 >>留言 
网站首页 · 电工新闻 · 电工基础 · 电力技术 · 电子技术 · 电工图片 · 电工法规 · 电工图书 · 电工搜索 · 论坛交流
 
      最新电子论文
 · 谈谈C51的编程规范 
 · 单片机初学者不好掌握的概 
 · MC14541B制作可编程序定时 
 · AT89C系列单片机加解密原理 
 · 功能强大的时钟中断 
 · PIC系单片机与MCS-51系列单 
 · 有些51系统容易复位的解决 
 · 温度传感器ds1820的汇编程 
      推荐电子论文

 · 功能强大的时钟中断
 · DSP编程的几个关键问题
 · 寻址方式与指令系统
 · 数码管的显示一例
 · PIC单片机软件开发技巧 

  ■ 所在位置:首页>>电子技术>>单片机技术>>正文
 

PIC单片机的I2C应用程序1

PIC单片机的I2C应用程序1

; Functionality:
;
; This code implements the basic functions for an I2C slave device
; using the SSP module. All I2C functions are handled in an ISR.
; Bytes written to the slave are stored in a buffer. After a number
; of bytes have been written, the master device can then read the
; bytes back from the buffer.
;
; Variables and Constants used in the program:
;
; The start address for the receive buffer is stored in the variable
; 'RXBuffer'. The length of the buffer is denoted by the constant
; value 'RX_BUF_LEN'. The current buffer index is stored in the
; variable 'Index'.
;
;--------------------------------------------------------------------
;
; The following files should be included in the MPLAB project:
;
; an734.asm-- Main source code file
;
; 16f872.lkr-- Linker s cript file
; (change this file for the device
; you are using)
;---------------------------------------------------------------------
#include <p16f872.inc> ; Change to device that you are using.
;---------------------------------------------------------------------
;Constant Definitions
;---------------------------------------------------------------------
#define NODE_ADDR 0x02 ; I2C address of this node
; Change this value to address that
; you wish to use.
;---------------------------------------------------------------------
; Buffer Length Definition
;---------------------------------------------------------------------

#define RX_BUF_LEN 32 ; Length of receive buffer

;---------------------------------------------------------------------
; Variable declarations
;---------------------------------------------------------------------
udata

WREGsave res 1
STATUSsave res 1
FSRsave res 1
PCLATHsave res 1

Index res 1 ; Index to receive buffer
Temp res 1 ;
RXBuffer res RX_BUF_LEN ; Holds rec'd bytes from master
; device.

;---------------------------------------------------------------------
; Vectors
;---------------------------------------------------------------------

STARTUP code
nop
goto Startup ;
nop ; 0x0002
nop ; 0x0003
goto ISR ; 0x0004

PROG code

;---------------------------------------------------------------------
; Macros
;---------------------------------------------------------------------

memset macro Buf_addr,Value,Length

movlw Length ; This macro loads a range of data memory
movwf Temp ; with a specified value. The starting
movlw Buf_addr ; address and number of bytes are also
movwf FSR ; specified.
SetNext movlw Value
movwf INDF
incf FSR,F
decfsz Temp,F
goto SetNext
endm


LFSR macro Address,Offset ; This macro loads the correct value
movlw Address ; into the FSR given an initial data
movwf FSR ; memory address and offset value.
movf Offset,W
addwf FSR,F
endm

;---------------------------------------------------------------------
; Main Code
;---------------------------------------------------------------------

Startup
bcf STATUS,RP1
bsf STATUS,RP0
call Setup

Main clrwdt ; Clear the WDT
goto Main ; Loop forever.


;---------------------------------------------------------------------
; Interrupt Code

本新闻共3页,当前在第1页  [1]  [2]  [3]  

 
         相关文章

·
·
·
·
·
·

 
 
  关于本站 | 友情站点 | 联系方式 | 版权声明 | 电工技术服务与支持中心
Copyright© 2005-2006 Dg8.Com.CN ,All Rights Reserved
电工网 电工吧 电子吧