Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

I2C using PIC12F1840

Status
Not open for further replies.

Yobortsa

Member
Ok, I give up. Anybody know how to get I2C working on a PIC12F1840? It appears to be different to the PIC16 series, therefore Microchip Application Note 734 doesn't work.

The datasheet for the 12F1840 is very difficult to understand - I have trouble distinguishing between what I need to do and what the hardware does. I've tried all sorts of combinations and can't get it working. I've tried Slave send and Slave receive to no avail. At the moment, I figure Slave send should be easier so that's what I'm trying to get working below.

The data sheet isn't clear to me - are interrupts required for successful operation? I think not. What needs to be done to initialise I2C. Should the TRIS register be set to input or output before enabling I2C?

At the moment, using the code below, the other side is indicating it received a NACK response. I have 10k pullups on both SCL and SDA to 5V and have tried from 10 to 400kHz.

Here is the current code:

Code:
	LIST	P=12F1840

	#include	<p12f1840.inc>
	errorlevel	-302

	__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOREN_OFF & _CP_OFF & _CPD_OFF & _CLKOUTEN_OFF
	__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_ON & _BORV_19 & _LVP_OFF

;---------------------------------------------------------------------
;Constant Definitions
;---------------------------------------------------------------------

#define NODE_ADDR 8 ; I2C address of this node

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

	cblock 0x20

Temp			; Storage for SSPSTAT important bits to identify state
I2Cdata			; Incoming data

	endc

;---------------------------------------------------------------------
; Program code
;---------------------------------------------------------------------

	org	0x0000

;	RESET

; Configuration from Data Sheet, p. 113:
	BANKSEL	PORTA				;
	CLRF	PORTA				; Init PORTA
	BANKSEL	LATA				; Data Latch
	CLRF	LATA				;
	BANKSEL	ANSELA				;
	CLRF	ANSELA				; digital I/O
	MOVLW	B'00001110'			; Set RA<3:1> as input, others output
	banksel	TRISA
	MOVWF	TRISA				;

	movlw	NODE_ADDR			; Set our I2C node address
	banksel	SSP1ADD
	movwf	SSP1ADD

;	movlw	B'00000000'			; was 0x36 ; Setup SSP module for 7-bit
;	banksel	SSPCON3
;	movwf	SSPCON3				; address, slave mode

;	banksel	SSPCON2
;	clrf	SSPCON2			; Set to 00000000 by default

	movlw	B'00100110'			; was 0x36 ; Setup SSP module for 7-bit
	banksel	SSP1CON1
	movwf	SSP1CON1				; address, slave mode

;	banksel	SSPSTAT
;	clrf	SSPSTAT

; Start of SSP handler

	banksel	PORTA
	bsf		PORTA,0

MainLoop:
	banksel	SSP1STAT			; Check status register for I2C
	btfss	SSP1STAT,R_NOT_W	; If we have read, go process...
	goto MainLoop				; Otherwise loop until read request comes in
	banksel	PORTA				; Turn off LED if..
	bcf		PORTA,0				;   ..we get here
	banksel	SSP1BUF				; Change to SSP1BUF page
	movf	SSP1BUF,W			; Dummy read the address before we load our data
	movlw	b'11110000'			; This is our dummy data to send
	movwf	SSP1BUF				; Load the data to send and hope the hardware processes it

	goto	MainLoop

; ------------------------------------------------------------------------------
	end

Thanks in advance,

David
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top