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

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

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

PIC单片机的I2C应用程序2

 

; This source code provides a demonstration of the MSSP peripheral
; on the PIC16F87x MCU.
;*********** The subroutines for EEPROM *****************
;
; EE_Random_Read ; EEPROM Random address Read from EEPROM
; EE_SEQU_Read ; EEPROM Sequential Read form EEPROM
; EE_Ack_Check ; Polling current status of EEPROM
; EE_Page_Write ; Page Write function for EEPROM
; EE_Byte_Write ; Write a byte to EEPROM with address setting
;
;*********** The subroutines for I2C *****************
;
; Init_I2C_Master ; Initial I2C Module for Master Mode , 7-bit address
; StartI2C ; Send a START Condition !!
; StopI2C ; Send s STOP Condition
; RstartI2C ; Send a Repeat Start conditional to I2C
; Non_Ack ; Send a Non-acknowledge signal to I2C
; An_Ack ; Send a acknowledge signal to I2C
; Sebd_Byte ; Send a byte to I2C bus
; RecI2C ; Enable Read a byte form slave device
; I2C_Done : Wait the I2C completed the currect process
;
;*****************************************************************************************

list p=16f877a
#include

;
;***************************************
;
CBLOCK 0x20

I2C_Ctrl
I2C_Addr
I2C_Data
I2C_Page_Length
D_Count
ENDC
cblock 0x40
I2C_Page_Buffer:8
I2C_SEQU_Buffer:8
endc
;
;***************************************

w_temp EQU 0x72
status_temp EQU 0x73
pclath_temp EQU 0x74
;
;***************************************

;
EEPROM_CMD equ 0xA0 ; Device adress of Slave Point
EE_Read equ .1
EE_Write equ .0

;
#define SCL PORTC,3 ; I2C SCL pin
#define SDA PORTC,4 ; I2C SDA pin

;

;********************************************
; Locates startup code @ the reset vector
;********************************************
Reset_Addr
org 0x00
nop
goto Main_Init
;
org 0x04
goto ISR
;
;***************************************************************************
;**** The Start Address of ISR is 0x004
;**** \"PUSH\" & \"POP\"  PIC16F877 ΤSHARE BANK PIC
;***************************************************************************
ISR
Push movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,W
movwf pclath_temp
;
; Put your interrupt code here
;
Pop movf pclath_temp,W
movwf PCLATH
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
;
retfie ; return from interrupt


;----------------------------------------------------------------------

Main_Init
pagesel Init_I2C_Master ; Set PAGE to PCLATH Register
call Init_I2C_Master ; Init the MSSP for I2C Master
;
banksel I2C_Ctrl
movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
movwf I2C_Ctrl
;
;
Main
;
;----------------------------------------------------------------------
Test_Page_RW
movlw 0x08
movwf I2C_Page_Length
movlw I2C_Page_Buffer
_Fill_RAM movwf FSR
movwf INDF
incf FSR,W
decfsz I2C_Page_Length,F
goto _Fill_RAM
;
Test_Page_Write
; banksel I2C_Ctrl
movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
movwf I2C_Ctrl
movlw 0x10 ; Select EEPROM location at 0x00
movwf I2C_Addr
movlw .8
movwf I2C_Page_Length
call EE_Page_Write
;
call EE_Ack_Check
;
banksel I2C_Ctrl
movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
movwf I2C_Ctrl
movlw 0x10
movwf I2C_Addr
movlw .8
movwf I2C_Page_Length
call EE_SEQU_Read
;
goto $
;
;--------------------------------------------------------
Test_1byte_RW

banksel I2C_Ctrl
movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
movwf I2C_Ctrl
movlw 0x00 ; Select EEPROM location at 0x00
movwf I2C_Addr
movlw 0xAA ; Write data 0x5A to location 0x00 of EEPROM
movwf I2C_Data
call EE_Byte_Write
;
Test2 call EE_Ack_Check ; Polling Acknowledge for next access
;
banksel I2C_Data
movlw 0x00 ; Clear I2C data buffer
movwf I2C_Data
;
Test3
banksel I2C_Ctrl
movlw EEPROM_CMD ; Load EEPROM command address @ 0xA0
movwf I2C_Ctrl
movlw 0x00
movwf I2C_Addr
call EE_Random_Read
;
goto $
;

;
;******************************************************
;* Random Read a Byte from EEPROM
;*
;* Input:
;* - I2C_Ctrl : Control Byte of EEPROM
;* - I2C_Addr : Location of EEPROM
;* Output:
;* - I2C_Data : Read Data from EEPROM

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

 
         相关文章

·
·
·
·
·
·

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