Can't DEC PORTB on HC908JK3  - SOLVED :-) - Page 1
Forum section: electronics / general

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 01:13:15 am
content: Get this!LABELINC PORTBJMP LABELworks just fine. Generates a rising 8-bit value at the port pins.But....LABELDEC PORTBJMP LABELdoes absolutely nothing!!! No change in the port pins.Spent hours and hours last night trying to get a larger piece of software running until I narrowed it down to this.Is this a hardware bug in the micro???Just ignore the fact that I am not testing for limit values.

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 01:58:34 am
content: It gets worse.LABEL MOV PORTB,TEMPINC TEMPMOV TEMP,PORTBJMP LABELworks great.But...LABEL MOV PORTB,TEMPDEC TEMPMOV TEMP,PORTBJMP LABELdoesn't work!It's as if the DEC function has no effect.I'm ready to give up!

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 04:14:18 am
content: But wait, there's more...LABEL LDA PORTBINCASTA PORTBJMP LABELworks okay.But...LABEL LDA PORTBDECASTA PORTBJMP LABELdoes not work.Tried a different micro bought several years earlier form a different supplier. No difference.

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 04:30:34 am
content: and yet more...LABEL LDA PORTBADD #1STA PORTBJMP LABELworks okay.LABEL LDA PORTBSUB #1STA PORTBJMP LABELdoes not work.

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 05:11:29 am
content: Still anotherLABEL LDA PORTBADD #255STA PORTBJMP LABELdoes not work.If we start with say the value of 50 and add 255 and ignore the carry bit0011 0010 = 50+ 1111 1111 = 2551 0011 0001 = 49 (ignoring carry bit)Seems that you can't write a value to PORTB lower than what is already there.I'm open to suggestions though.

author: Nominal Animal
author_rank: Super Contributor
timestamp: June 07, 2020, 06:58:44 am
content: What aboutLABEL LDA TEMPDECASTA TEMPSTA PORTBJMP LABEL?

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 10:11:28 am
content: Quote from: Nominal Animal on June 07, 2020, 06:58:44 amYep, that one does work.Just can't figure out why the others don't work.Kinda shakes my faith in something I though was airtight. Surely others would have come up against this?

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 10:25:58 am
content: But...LABEL LDA PORTB (instead of TEMP)DECASTA TEMPSTA PORTBJMP LABELdoesn't work.Seems if PORTB is read and then a SUB or DEC occurs then it can't be written to.

author: mikerj
author_rank: Super Contributor
timestamp: June 07, 2020, 10:32:31 am
content: PORTB has pins muxed with the ADC, is the ADC properly configured to use the pins as GPIOs?

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 10:43:20 am
content: That's a good point. PORTB 0 is used as an AD input and 1-7 as outputs to an R2R D/A converter that gives a reference voltage to a power supply PWM chip.But then, why should the port be able to be INC'd but not DEC'd. If it was something to do with the A/D pin configuration I would expect it to affect both directions of count.

author: Nominal Animal
author_rank: Super Contributor
timestamp: June 07, 2020, 10:51:40 am
content: Quote from: Circlotron on June 07, 2020, 10:43:20 amThat's what happens, if the least significant bit is always read as 1.When you increment it, each time the low bit is already 1, and you essentially add 2 to the read value; that's why it increments.When you decrement it, you just clear the least significant bit; that's why it doesn't seem to change.In mathematical terms, the value of PORTB when read is((PORTB & 254) | 1). Then,((PORTB & 254) | 1) + 1 = (PORTB & 254) + 2but((PORTB & 254) | 1) - 1 = (PORTB & 254)

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 10:58:29 am
content: Aha!!!!!And here I've been yelling at the world and explaining the intricacies of assembly language to my non-technical wife in an effort to get things sorted.Nominal Animal, you got it.

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 11:08:52 am
content: Yep. Now the following works.LABELDEC PORTBJMP LABEL---------------PORTB,0 is an A/D input but I temporarily made the DDR all 1's so when PORTB is read during the read-modify-write, bit 0 (normally A/D input, but not right now) is read as whatever it is, not always as 1, and so it decrements as it should. Hooray...Edit -> so what i've done to my original code is read PORTB into a memory location, DEC it *twice* then write it back out to PORTB. Seeing I'm only using the upper 7 bits of PORTB there is no loss of resolution because of DECing it twice.

author: mikerj
author_rank: Super Contributor
timestamp: June 07, 2020, 05:08:15 pm
content: You're welcome.

author: Circlotron
author_rank: Super Contributor
timestamp: June 07, 2020, 10:46:15 pm
content: Quote from: mikerj on June 07, 2020, 05:08:15 pmSorry mikerj, I was getting mixed up between your post and Nominal Animal’s. Yours was definitely the turning point that got me on the road to understanding what was going on. Thank you so much.

author: Nominal Animal
author_rank: Super Contributor
timestamp: June 08, 2020, 11:09:47 am
content: Yup, mikerj's post was the turning point for me, too. I had no idea as to what it could be, and was still completely in the behaviour mapping stage, until he mentioned GPIO config, and you (Circlotron) mentioned bit 0 is an input. Then, within seconds, it all just clicked together.This sort of thing is exactly why I like teamwork.