A
Arduino4mo ago
wtf

Receiving incorrect data through Serial

I'm trying to interact with UART using assembly, but I can't seem to get it to print the right data. the main things to look at are putc and puts. everything works fine only if I used one of them and not both in one program.
5 Replies
wtf
wtfOP4mo ago
Pastebin
.include "m328pdef.inc" .include "delay.inc" .cseg .or...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
WeeGee
WeeGee3mo ago
putc:
push r17

lds r17, UCSR0A
sbrs r17, UDRE0 ; Wait for empty transmit buffer
rjmp putc

sts UDR0, r16 ; Transmit byte

pop r17
ret
putc:
push r17

lds r17, UCSR0A
sbrs r17, UDRE0 ; Wait for empty transmit buffer
rjmp putc

sts UDR0, r16 ; Transmit byte

pop r17
ret
Looks like this is going to blow up your stack as you wait. Every time you wait here, you're going back to putc and re-pushing r17 to the stack would also make sense why you're only getting issues when you try to use both putc AND puts; I assume puts calls putc my suggestion would be this
putc:
push r17
putc_waitloop:
lds r17, UCSR0A
sbrs r17, UDRE0 ; Wait for empty transmit buffer
rjmp putc_waitloop ;jump to AFTER preserving r17

sts UDR0, r16 ; Transmit byte

pop r17
ret
putc:
push r17
putc_waitloop:
lds r17, UCSR0A
sbrs r17, UDRE0 ; Wait for empty transmit buffer
rjmp putc_waitloop ;jump to AFTER preserving r17

sts UDR0, r16 ; Transmit byte

pop r17
ret
note though that I haven't checked your code very far; this was the first thing that stuck out to me so idk if this is the root of the issue @wtf so just checked for clarification; you're NOT using putc to help implement puts for some reason; I'd suggest using the putc that's in puts to re-make putc, then have puts call putc seems that puts's character printing is at least somewhat working for printing Hello
wtf
wtfOP3mo ago
that's so obvious idk how I didn't catch that I'll give it a shot works there goes 1 day of debugging to a dumb mistake
WeeGee
WeeGee3mo ago
This is why things are typically broken down into parts like this; the whole point of making putc is so you only have to debug "print a single character to serial" once, then you write "print a string" in terms of "print a single character" and loops/conditionals I'd strongly suggest making your puts call putc so you don't have two separate snippets of code that print to serial (that's 2x the places for you to make such a hard to spot mistake)
wtf
wtfOP3mo ago
that's what I did. will keep that in mind for the future

Did you find this page helpful?