MC9S08GB60 I2C and PCF8583 Real time clock

Started by JNB December 25, 2006
Hello

I try to use C+ that is new for me, and Codewarrior.

Running in circle since a couple of weeks with this simple clock, did
somebody got experiences and softwares that can manage IIC?. (with or
with out IT managment (I try both), I just need simple as possible,
as RCT seems to be)

Many thanks in advance.
--- In 6..., "JNB" wrote:

> I try to use C+ that is new for me, and Codewarrior.

Try C++, it's easier

> Running in circle since a couple of weeks with this simple clock, did
> somebody got experiences and softwares that can manage IIC?. (with or
> with out IT managment (I try both), I just need simple as possible,
> as RCT seems to be)

I will append my simple IIC code just for fun, but don't complain that
it is for HC12 and not for your MC9S08GB60 (you are asking the wrong
group). The two should be similar anyway.

I think you might have got some useful help though if you were asking
specific questions. All I got was that you are "Running in circle". I
thought that was what the main loop is supposed to do!

;;; Simple test of IIC port
;;;

PORTB = 0x01
DDRB = 0x03
IBAD = 0xe0
IBFD = 0xe1
IBCR = 0xe2
IBSR = 0xe3
IBDR = 0xe4
PTH = 0x260

M_ADDR = 0x02
S_ADDR = 0x01

;;; flags
IBMASTER = 0xa0
IBSLAVE = 0x80
TXRX = 0x10
IAAS = 0x40
IBB = 0x20
IBAL = 0x10
SRW = 0x04
IBIF = 0x02
RXAK = 0x01

.text
.globl _start
_start:
lds #_stack

;; init PORTB outputs
clr PORTB
;; enable IIC
movb #0xff,DDRB ; display LEDs on Dragon12
movb #0x57,IBFD
movb #M_ADDR<<1,IBAD
movb #IBSLAVE,IBCR
ldx #0
0: dbne x,0b ; simulate other tasks

;;; Scan switches, and service once when pressed
;;; (not debounced)
Scansw:
;; test switches
ldab sw ; prev sw value in B
movb PTH,sw ; update sw state
eorb sw ; set changed bits
comb ; changed bits are 0
orab sw ; filter out released buttons
bitb #1<<3 ; Sw2: IIC Send
beq Send
bitb #1<<2
beq iic_listen
bra Scansw

Send:
;; send something as master
bsr iic_wait_bus
bset IBCR,#IBMASTER|TXRX ; become master
;; send address (write mode)
ldab #S_ADDR<<1
bsr iic_tx
;; check for Nack
bitb #RXAK
bne 6f ; end if nobody listening
;; send data
ldab tdat
inc tdat
bsr iic_tx
6: movb #IBSLAVE,IBCR ; end transmission
bra Scansw

;;;
;;; Send byte from B to IIC (blocking)
;;;
iic_tx:
stab IBDR
bsr iic_wait
movb IBSR,PORTB ; echo trace
movb #IBIF,IBSR ; clear flg
rts

;;;
;;; Wait until IIC bus not busy
;;;
iic_wait_bus:
brset IBSR,#IBB,iic_wait_bus ; loop until bus available
rts

;;;
;;; Wait until IIC interrupt flg
;;; Returns IBSR status in B
;;;
iic_wait:
;; check for ready flg
brclr IBSR,#IBIF,iic_wait
ldab IBSR
stab PORTB ; echo trace
movb #IBIF,IBSR ; clear flg
;; check for err
bitb #IBAL
bne icc_err
rts

iic_listen:
movb #S_ADDR<<1,IBAD ; slave address
movb #IBSLAVE,IBCR ; slave mode
0: brclr IBSR,#IBIF,0b ; wait for next byte
movb #IBIF,IBSR ; clear flg
brclr IBSR,#IAAS,2f
;; is our addr
brset IBSR,#SRW,icc_respond
bclr IBCR,#TXRX
movw #0,inidx
2:
;; get single byte value
ldab IBDR
stab PORTB
ldx inidx
stab inbuf,x
inx
stx inidx
bra 0b

icc_err:
0: bgnd
bgnd
bra 0b

;;;
;;; Send response as slave
;;;
icc_respond:
bset IBCR,#TXRX ; set to output
0: bra 0b ; unimplemented

.data
;;;-----------------------------------
;;; Variables

sw: .byte 0xff
tdat: .byte 0x33
inidx: .word 0
inbuf: .space 5, 0
Thanks for your help and time.
Happy new year
Jnb
--- In 6..., "Jefferson Smith"

> I think you might have got some useful help though if you were asking
> specific questions. All I got was that you are "Running in circle". I
> thought that was what the main loop is supposed to do!

He, he, Computers are a bit like real life, they run in circles but keep getting interrupted.

Merry midwinter fest to yeah all.

Cheers,

Theo