Once you reach that point tinker a little, not a lot. When you feel resistance take a break, never force yourself. Don't force yourself to write code you don't enjoy writing, find ways around it, at least initially. You need to avoid reinforcing the association between code and pain until you have a buffer of good experiences in your new domain.
It might be (probably will be) necessary to find a new career path in the meantime.
Good luck.
Everyone is recommending you find what you truly love - that sounds good and all, but life is not that simple (you got to make a living for example - what you "love" might not provide for your family) and more importantly there's no guarantee you'll love it forever or even next month. It's sound advice but it can get you chasing the highs (what you love) forever.
The issue might not be with what you do per se - perhaps its depression or unhappy environment (relationships, coworkers, job, family etc) or hormonal imbalance of some sort manifesting itself via work.
What has helped me is paying close attention to & managing depression. It's a multiplier for me for sure - I try to put in as much miles as possible on the good days (before she cometh again). Also doing a reset (whatever that means to you) once in a while also helps... a trip to Cuba a couple of years ago helped me get out of antidepressants for example.
@Rury mentioned psychology of flow and it reminded me of a book called Flow by Mihaly Csikszentmihalyi [0] - it's a little dated but it was helpful to me. Also checkout The War of Art [1]- it's basic but sometimes it's the reminders you need.
Good luck. You're not alone - my contacts on the profile if you want accountability partner of some sort.
[0] https://www.goodreads.com/book/show/117101.Flow [1] https://www.goodreads.com/book/show/14653803-the-war-of-art
That said, I'd also suggest perhaps looking at other factors that might be giving you headwinds. Almost anything that can affect your dopamine levels (drugs/food/sex), can subtly influence your motivation. For me personally, I feel quitting all sources caffeine/nicotine has improved my ability to remain focused on tasks.
Short answer is to not do it as a job and instead do it as a hobby.
I was rapidly approaching burnout in software related tech work. Was light on code, but I did write code fairly regularly. And I just hated all of it, but was also really good. Trapped.
I purged one day. Had a whole bunker running in my house. All those enterprise apps and lots of cool things I made to do the job and make magic sometimes. It was my edge and curse. All around me in the end.
Changed scale. For me, that meant going back to the roots, the stuff that would keep me up at night.
Went small. Bought some dev boards and suddenly I was back to fun problems. Making video display signals, drivers for hardware, sensors. Decoded to set up an 8 but workstation. Apple 2 /e decked out with a faster clock, some I/O cards... just to pick up electronics where I left off decades ago.
Found out I still love all that stuff, but also found out I was sick of enterprise software.
I love embedded. Hardware, purpose built software, that whole thing is fun and very different.
Time to really change up, new career, new people. All of it, and that was scary as hell. Still is, but in that good, rise to the challenge way.
Today, I am into additive manufacturing, mostly polymers until last year when I got started on metal. There is a startup in my future and a chance to make a big impact and I am super excited about it all. I love the people I work with too.
Now I range more widely, electronics, having a scope, soldering things, writing code, connecting to CNC machines, making parts, and helping others to make the challenging parts they need, and more are an every day deal. Hobby fun, plus a lot of well honed, transferrable skills turned into a new reality and I am learning a ton!
I also found out I love that. Being around others who I can grow with and helping them too. We all have skills and hard won knowledge we share and apply to the problems at hand.
If you dread it every day, whatever that is will just keep eating at you. It goes slower and can be held at bay when you work with great people. I did and it delayed all this for roughly half a decade.
In the end it was the same, just slower. But it hit hard anyway.
I had to have change.
Once I started, and I did that by taking a general management job well outside my comfort zone, but that did require my various domain knowledge be applied to assist and understand the people being managed.
I found out I am the servant leader type. Empower the people, keep the crap out of their lives and put the best tools and people in their hands to get great results, and a fair amount of hands on. Cool, but I wanted a bit more entrepreneurial experience. That led to being part of an additive company starting up.
Did all that on some good advice from a friend who could see where I needed to grow and where some skill gaps were. Turns out that GM work was just the thing I needed to weave all I had learned together and be able to join a small team building great things.
Start seeking. Take a jump, maybe two. I ended up doing two, and may well do one more to in the new gig to come.
Have people who know you and who care?
Listen to them.
Often, we can see this stuff in others and give good advice and wisdom. We also often can't see these same things in ourselves. Too close.
Worst case is you have to do what you are doing now to recover, pay bills save a little and then try again.