cancel
Showing results for 
Search instead for 
Did you mean: 

hello sir i have a problem, with the stm8s208. i use the i2c bus.

debruyne.marc
Associate II

hello sir

i use the i2c bus on a stm8s208.

there is just connected 2 pcf8574.

the crystal i used is 8Mhz, and a i2c clock divider of 4

one pcf8574 is connected to dipswitched , adress is 42

the second pcf8574 is connected to led's, adress is 40

individualy its working. both.

so i can read, the switches, and i can send information so the leds are lighting up.

even i read first the switches and then i go to the led sending routine ,and put then a 'break" in the stv development its still ok.

but, now, if i jump back to read the switches, , i can't read my switches, its stops in the "pcf8574 start routine" of the led's.

another information, the same board, the same adressing, id working with the arduino i2c bus.

so,my board is "functionaly" ok.

send the file in addembler here with

can you tell me or give me some clues,what i did wrong in the programming

many thanks

marc debruyne

3 REPLIES 3
debruyne.marc
Associate II

hello

well, its sometimes necesary, to try to solve it all yourself , after a bit of search, it works with just adding a delay of 100ms

now it works ok

bye

marc debruyne

Cristian Gyorgy
Senior III

Hello Marc!

Usually I'm glad to see people programing in assembler, but the code listed by you it's a total disaster - I hope it's not discouraging!

Some thoughts:

  • Use register definitions taken from the files of whatever compiler you want, it's much better than to define the addresses yourself. A mistake here can take months of debugging to find it!
  • why do you write these instructions: ?? You don't need them, the SP register has the right address after reset.
	; initialize SP
	ldw X,#$stack_segment_end
	ldw SP,X
  • you only have to initialize the I2C peripheral once, not before every transaction;
  • this is your init code:
  bres  I2C_CR1,#0
	mov	I2C_TRISER,#$10
	mov	I2C_CCRL,#$A0   ;was 46
	mov	I2C_CCRH,#$28
	mov	I2C_ITR,#$02
	mov	I2C_FREQR,#$01  ;was 11
	mov	I2C_OARH,#$40
  mov I2C_OARL,#$A0
  bset  I2C_CR1,#0
;  bset  I2C_CR2,#2
;  bset  I2C_CR2,#0
	ret

, now let me see: you write to a reserved location in I2C_CCRH (bit 5), the values you write in I2C_CCRL makes no sense (very low SCL frequency); you write the own address registers and I guess from your description that your MCU is a master.

  • what's with all the "nop" instructions in your code? I've written in total more than 100KB of assembled code and never had to use a nop instruction!
  • try to use interrupts!
  • you say it works with a 100 ms delay! This sounds painful for a MCU. Maybe anyone could take it as normal if it came from a Microsoft software, but in the MCU world you should avoid delays.

Goodbyte!

hello cristian.
waauw, thank you sir, for the answer.
realy glad you have given a comment, on my programming in assembler.
but first of all, i am since 1975, programming in assembler, 8051, 6502,tms380,cop8,68hc11, atmega2560,and at last, because i think, the stm8s is one of the best single chip processors.
it has it all,just pity, its not on a 100pin.and there is not mutch "examples" available on the stm8s in assembler.
now regarding the language,you must know i just try to write, a bit logical,and not as with compilers do, working with the "stack".
i put a nop in,because i had problems with timing.
i use the pcf8574, and the mcp23008,well, i use a crystal, of 14,7..mhz, well, it can not work, at all, with that frequency together, thats why i must use a 4x divider of the clock for i2c. and with spi is that too slow. so i must before i go to a spi routine set the clock divider to 0.
with the i2c-crrh i just tryed if my pcf8574 could work.
yes, i know you are right, i must use interupts, for my i2c routine. so i know or i know if the sending go trough.
yes, it realy works, with just adding a 100ms delay, because, i think the 2 pcf8574, are not answering, after a few times i switch the dip switches.
and now its working, just a bit with a delay, but it can work maybe with a lesser delay.
again ,many thanks for the answer
marc debruyne
Op 21/07/2020 om 18:52 schreef ST Community: