Tag Archives: PIC

C Electronics LEDs Microcontrollers PIC RS-232 RS-485

PIC Micro Modbus Update 3 : The 18F27J53 Port & tinyLED

We got our first snow yesterday; it’s a little early in the year for this and even more odd was it stuck around. The Puget Sound usually keeps us over freezing point (mostly) but Mother Nature had other plans; It was a chilly 22 deg F outside. I haven’t fully insulated my workshop yet, so despite my heater doing all it could it was 55 deg F in the workshop this morning. I can only put in ~2 hours of comfortable work at temperature so work on the my Modbus project was slow. I’ve mostly gotten the project ported to the new micro with a few exceptions.

On the breadboard is the PIC 18F27J53 development board from @atomsoft - I put some of my other favorite development boards next to it for size comparison... I believe that 18F26K22 board is no longer available.. I think I still have one or two unused boards.. better save them for a rainy day or sell them off as collector items :)
On the breadboard is the PIC 18F27J53 development board from @atomsoft – I put some of my other favorite development boards next to it for size comparison… I believe that 18F26K22 board is no longer available.. I think I still have one or two unused boards.. better save them for a rainy day or sell them off as collector items 🙂

Whenever I start a microcontroller project I try to get the basics out of the way:

1. Port I/O set up correctly? Inputs, Ouputs, Disable A/D converters, etc.

  • I think I have a problem with this or perhaps it’s my OSCCON; I did some additional reading of the specification sheet and wrote down some notes to check yet. My UART is not working.. I’m using the EUSART1 which is the “easy” one.. no peripheral pin select to worry about

2. Oscillator running? Sure, probably but is it running at the right speed?

  • Well, yes.. it took some tweaking as the device I’m using has the PLL that takes the crystal oscillator from 12MHz to 96MHz /2 .. then an additional CPU divider from there. I missed the bit about the CPUDIV register at first.. I was running my CPU at 48MHz ..  that’s okay though because I caught it fast. I always through in a little heartbeat LED blinker in my main code to test to ensure my clock is running at the right speed. I was way off.. did my reading and found I hadn’t set the CPUDIV configuration correctly (for my assumed frequency)
#pragma config CPUDIV = OSC3_PLL3// CPU System Clock Postscaler (CPU system clock divide by 3 from 48MHz)
Okay, clock is good.. 100ms test: check!
Okay, clock is good.. 100ms test: check!
  • However, now with reading the Errata I’ve found there is a problem with the ESUART receive vs. transmitter baud rate generator and it’s recommended that I run my CPUDIV at the OSC1 (1:1) frequency.. so I’ll change that. It’s still not my problem though, On boot my micro transmits some test but that’s not happening according to my logic analyzer.

3. Make sure your IO actually works!

  • Once I think I have everything set up I usually set up some test LEDs and/or the UART.. I’ll send data to the console.. or if it’s basic.. just some LED blinking. I am doing both in this project..

So as of now I have my code to the point my oscillator is running at the frequency I want it to, of course that will have to change which is annoying because the built-in __delay functions with XC8 don’t like running so fast so I have to go and build my own. Or use timers…

My ESUART is not working.. but I have about a dozen odd items to check. I found in the errata that the RX side of the UART gets it baud rate from somewhere other than where the TX side gets. The workaround is keeping your CPUDIV at 0x03,  1:1. I can tell by my logic analyzer the TX isn’t happening.. my other guesses are the TX is at 3.3V because that’s what the micro runs at.. my UART to USB interface runs at 5V. I can set the micro up to run open collector and use a pull-up resistor. I also need to double-check my OSCCON and finally there was some other information that under some circumstances if the interrupts are enabled after the UART is, somehow, the UART gets disabled.. so I can shuffle some code around to figure that out. There are also a few other tidbits to double-check and test.

I’m not going to drop the code in because it doesn’t work all the way.. but you might be interested in just checking out the code itself. You can find it on my new pastebin at: http://pastebin.com/7z8dBMW4 . I recently created this account to shuffle code between my laptop inside and my workbench computer.

Some other tidbits:

I recently picked up the Breadboard Buddy 6.2 and some tinyLED leds from the AtomSoft store at Tindie. The breadboard buddy serial interface worked fine when testing it. I did have to download a driver for Windows 7 but the price is nice; it’s been loopback tested.. and the power supplies are right on (3.3, 5V). The tinyLED despite being cheap was surprisingly exciting. I am in love with these things because they save so much space on my breadboard. I have two green and four white.. rumor is there might be some 4x and 8x LEDs coming out as well! It’s probably obvious I love to develop on a breadboard.. everything off dev board usually runs at 100KHz or less (I2C)… so the capacitance isn’t an issue.

 

An up close shot with the AtomSoftTech tinyLEDs in the background. I think my LEDs were my favorite purchase of the month.. they are small and save me a ton of breadboard room. Note: this breadboard was a free "reject" because the alignment was wrong.. so the LEDs don't line up correctly. In standard boards they're straight. I ordered a new breadboard just to confirm this.. it'll be a bit; it's coming from China of course.
An up close shot with the AtomSoftTech tinyLEDs in the background. I think my LEDs were my favorite purchase of the month.. they are small and save me a ton of breadboard room. Note: this breadboard was a free “reject” because the alignment was wrong.. so the LEDs don’t line up correctly. In standard boards they’re straight. I ordered a new breadboard just to confirm this.. it’ll be a bit; it’s coming from China of course.

The weather looks better later into this week so hopefully I can get in a bit more shop time and wrap this up. I’ve determined I am not going to use this device other than testing integration so I’ll use some preset registers being faux digital inputs. It will be a slam dunk for anyone to add in some hardware polling and adding on to this to make a full blow controller. If I do decide to take this further I’ll consider making the USB interface some kind of firmware update to configure the I/O without downloading code from MPLABX.