PIC 18F14K50-I/P: How do you program this?  - Page 1
Forum section: electronics / general

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 17, 2017, 07:58:05 pm
content: I bought this velman k8090 Relay control kit. I actually thought i had an arduino like chip on it.Really cool board but how do you program it? The only thing I can find is a 139$ software, n button, thats just a gui. I was hoping for something like the arduino IDE where you can make it do something through code. I like the sound of the relays clicking and wanted to make it click through them. Totally useless but a good way to learn.Can you make the arduino control it, or would that be totally redundant?

author: hans
author_rank: Super Contributor
timestamp: March 17, 2017, 08:46:46 pm
content: Left of the PIC is a 5-pin ICSP connector.You can connect a PICKIT2/3 or perhaps ICD2/3 and program it via MPLAB IDE or MPLAB X.This is however not an Arduino. 8-bit and 16-bit PICs don't have a C++ compiler from Microchip or the community, so the odds of an Arduino port are near zero.

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 17, 2017, 09:09:55 pm
content: Quote from: hans on March 17, 2017, 08:46:46 pmCool, but what the hell does that mean? Why would you use those five pins instead of the USB port? That was why I bought it because I figured I could program it from the computer. Is MPLAB C language? learning C has all my brain power tied up right now, so if I have to learn a whole new language and go through all that again this thing is not of much use for me. Basically I need the thing to turn a light on and off at certain times of the day or so many hours apart if it doesn't have a real time clock..

author: Smokey
author_rank: Super Contributor
timestamp: March 17, 2017, 09:49:12 pm
content: Here you go....http://lmgtfy.com/?q=MPLABhttp://lmgtfy.com/?q=PICKIThttp://lmgtfy.com/?q=PIC+ICSPps.. smells like a troll to me.

author: hans
author_rank: Super Contributor
timestamp: March 17, 2017, 10:32:53 pm
content: Quote from: raspberrypi on March 17, 2017, 09:09:55 pmICSP stands for "In Circuit Serial Programming". This is how Microchip calls it for PICs. Atmel AVR's have ISP. Or most ARM chips having JTAG or SWD nowadays.The emphasis on "In circuit" is because DIP chips used to be programmed outside circuit in a ZIF socket of a programmer.QuoteBecause USB programming is not magic. Arduino contains USB to serial converter + an Arduino bootloader (specific to the microcontroller) to load your programs. The bootloader is a custom program so you don't have to program the chip via ICSP. Alot of effort has been done to make programming Arduino easy to pick up and use.ICSP does offer more debug functions like breakpoints and memory watches, if the tool you're using supports it.QuoteYes, which is similar to Arduino, but also largely it isn't. In Arduino you have access to libraries (which are actually C++) which are generally easy to use. There is not such a system in MPLAB, it's just bare metal.

author: Ian.M
author_rank: Super Contributor
timestamp: March 17, 2017, 10:43:23 pm
content: Velleman don't publish sourcecode or firmware and protect their preprogrammed chips, so if you want to hack it, the first thing you need to do to avoid bricking it is to get a spare PIC18F14K50 and carefully label and safely store the original, before connecting to the ICSP header.Then, to get it to run standalone, you'd need a current Microchip programmer (e.g. PICkit 3), MPLAB X (bloatware), XC8 and Microchip's MLA (a massively bloated collection of libraries and examples for hundreds of PICs and boards you don't have) to get the USB support + a month or two to wrestle with learning XC8 (not so bad, its a fairly 'vanilla' ANSI C89 cross-compiler) and Microchip's MLA USB stack (absolutely vile, with a learning curve like climbing the East face of Mount Everest). Its not helped by the fact the PIC18F14K50 isn't the latest and greatest so most of the tutorials and examples you can find for it are for Microchip's obsolete, incompatible and non-standard C18 C compiler.Hint: The nearest board with a PIC18F14K50 on it you'll find in the MLA is the now discontinuedDM164127 Low Pin Count USB

author: ludzinc
author_rank: Unknown
timestamp: March 17, 2017, 11:03:24 pm
content: Go straight to the source:http://www.vellemanusa.com/support/downloads/?code=K8090Velleman themselves have sw/drivers/manuals for the board.Took me 2 minutes of looking....

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 17, 2017, 11:23:41 pm
content: Quote from: Smokey on March 17, 2017, 09:49:12 pmWhat is that thing? Like an automated way to say "did you try googling it?"

author: Ian.M
author_rank: Super Contributor
timestamp: March 17, 2017, 11:26:54 pm
content: Quote from: ludzinc on March 17, 2017, 11:03:24 pmYes, if you want to run it tethered to a Windows PC.However:Quote from: raspberrypi on March 17, 2017, 07:58:05 pmIt looks like Raspberry wants to run it standalone. IMHO the easiest option would be to carefully remove the PIC, and safely store it, then cable it across to an Arduino via a short 20 way IDC cable with a 0.3" wide DIL IDC plug on each end to plug into the PIC socket and on the Arduino end, a 20 pin socket on a protoshield on the Arduino end to patch the Arduino I/O pins to and a RTC chip + crystal. The relay are simply controlled by 8 digital outputs and the buttons are two resistor ladders that need to go to analog inputs.

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 17, 2017, 11:34:15 pm
content: Quote from: ludzinc on March 17, 2017, 11:03:24 pmThanks. Due to my eye sight two minutes usually takes about 10 or just giving up in frustration. So some times I will use the forum to see if someone can spend two minutes to help me find the link. I don't know why I torture myself with electronics that are small or programing which has 0 tolerance for changing a : to a ; which looks exactly the same to me so instead I just retype until it works.What are the odds a of beginner just making a simple 12 hour on off timer out of this without taking the chip out or modifying the circuit? When I thought it was an arduino like device I had dreams of hooking it up to sensors and using all eight channels but that seems way too complicated. I really don't want to turn this thing into parts as it costs 70 bucks or just taking the switches out and plugging them into an arduino, but then its just a relay circuit.

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 17, 2017, 11:36:59 pm
content: Quote from: Ian.M on March 17, 2017, 11:26:54 pmSo wire the IO pins of the arduino into where the socket for the IC went?

author: Ian.M
author_rank: Super Contributor
timestamp: March 18, 2017, 12:08:54 am
content: Yes. All the pinouts you need for the board end are on its schematic in the assembly manual.PIC18F14K50 socket do not connect pins:2 OSC13 OSC24 /MCLR17 Vusb18 D- (USB)19 D+ (USB)I wouldn't try stuffing wires direct into the PIC socket. For reliability and your sanity, you need a ribbon cable with connectors like this:http://www.ebay.com/itm/like/361773278535Be careful to get them the same way round and the same way up when you assemble the cable, using a vice and a cheap sacrificial 20 pin socket to protect the connector pins. That way you can simply put a 20 pin socket on a protoshield, and it will have the same pin numbering as the PIC for easy and reliable wiring to the Arduino.The relays will take quite a bit of power - you'd better put a 5V regulator on the protoshield fed by Arduino Vin, and feed PIC socket pin 1 from the regulator output rather than the Arduino's onboard 5V. The Arduino will then need 9V on its DC in jack. The other way would be to power the K8090 with 5V via its ICSP port and feed the Arduino via its 5V pin, in which case a Schottky diode and a 200mA polyfuse in series with the Arduino 5V pin are essential as it's unprotected when used as a power input, which is why the Arduino org. don't recommend doing so.If you want to do accurately timed stuff (like a 24H timeswitch) you'll need a RTC chip for the Arduino as they usually don't have an accurate crystal oscillator. If there's a smallish (typically 0.3" long) more or less rectangular metal can, probably with rounded ends marked '16.000' next to the Arduino ATmega328P, ignore this - you've got a clone with a real crystal - but the genuine board has a little silver 'flyspeck' of a ceramic resonator and doesn't keep accurate time on its own.

author: JPortici
author_rank: Super Contributor
timestamp: March 18, 2017, 12:29:21 am
content: what the freacking "ef"?i'll be more than happy to provide links on how to install MPLABX, XC8 (both free) and import a project that makes UART-controlled-relays for 139$it will make an awesome hourly rate for ~1h30m of workOr, if you neeed even more info that what was already written in the topic, feel free to ask

author: Ian.M
author_rank: Super Contributor
timestamp: March 18, 2017, 12:45:37 am
content: It already does USB controlled relays, with a MS Visual Studio 2008 DLL to interface to it. However to use it as a standalone 'Arduino-like' board isn't so easy as the PIC has to handle the USB communications + do whatever you want your user code to do.I'd go down the DIY '14K50 firmware route myself - with its on-board 12MHz crystal, it doesn't need a RTC, just a Schottky diode patched into the PIC power feed and a supercap to keep it running through any short power failures. However I already have the hardware and software toolchain and know how to use it.Wrestling with the MLA PIC18F14K50 CDC example to integrate his own user code is going to be quite some experience for our O.P. who is an embedded novice, and is still struggling with simple Arduino sketches. How many times do you think he'll have to reboot his PC because a bug in his PIC firmware has crashed the host USB stack?Hence my suggestion to non-destructively mod it to be an Arduino I/O board, to stay within the O.P's skills and comfort zone.

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 18, 2017, 02:06:37 am
content: Quote from: Ian.M on March 18, 2017, 12:08:54 amThe velmann manual reminds me of the manual to legos, it doesnt have any real info. So where else could I find the pin outs?Also is there still a +5v on the relay board if I take out the chip? Since the relays use so much power im feeding it 12VDC, no way an arduino could power all eight relays. The board comes on without the chip (but the buttons dont work so I'm not sure if the relays will come on with just a signal from arduino). If I have to make a new power supply for it I might as well just take the relays out and start from scratch.If the aduino and the board are on separate power supplies how can I feed +5 volts if they dont share a common ground? Or are you saying power the arduino from the relay board?Theres no easy way to program this so it can be a stand alone?Honestly I think it would be better to just build a complete power supply/relay circuit/arduino combination rather then try to get this board to work it really I'm just using its relays. Seems like that would be way harder then starting from scratch.Also the arduino has a 16MHz oval can xtal on it, so that will keep accurate time correct?The vellman also has a real crystal on it.

author: ebclr
author_rank: Super Contributor
timestamp: March 18, 2017, 02:11:09 am
content: https://pt.aliexpress.com/w/wholesale-pic-programmer.html?spm=2114.02010208.0.0.3ft2Bm&initiative_id=SB_20170317181038&site=bra&groupsort=1&SortType=total_tranpro_desc&g=y&SearchText=pic+programmer

author: Ian.M
author_rank: Super Contributor
timestamp: March 18, 2017, 03:22:15 am
content: @ebclr: Are you deliberately trolling? WTF would R.Pi need to use Programmer to Go mode for?@R.Pi,I hadn't spotted the Velleman board has a 12V power in, with a 7805 regulator. Yes it should be entirely satisfactory to power the Arduino from the +5V on Vdd (pin 1) of the PIC socket. PIC Vss (pin 20) needs to connect to Arduino Gnd. You should put a 200mA polyfuse between Vdd and the Arduino +5V to save your bacon if you accidentally short any Arduino pins. If you wire it that way, and don't plug +12V into the K8090, the buttons and LEDs will work if the Arduino has USB or external power, but the relays will be disabled - useful for testing.Velleman kit manuals are generally crap and usually don't cover the theory. Page 10 of theK8090 'Parts List'has the schematic - it tells you all you need to know but you have to work it out.First lets consider the buttons. Each button connects to ground and has a 1K pullup. The buttons are connected together in two banks of four, by aR-2R resistor ladder DAC. Switches 1-4 connect to PIC AN10 (Pin 13) and 5-8 to AN11 (Pin 12). The buttons have an equivalent source impedance as seen by the ADC of 100K - this is a bit on the high side for the Arduino ADC so a 0.047uF cap to ground on each of the two analog inputs used by the switches would be a good idea to prevent random noisepickup giving you phantom button pushes. Avoid using Arduino pins A4 or A5 as the Arduino will need them to talk to I2C devices like a RTC module.Just like an Arduino, the PIC has a 10 bit ADC converter, so to read the switches the Velleman firmware must be doing an ADC conversion for each bank and throwing away the bottom 6 bits. In Arduino code this would be:Code:switches=((analogRead(Sw5to8pin)+32)>>6)|(((analogRead(Sw1to4pin)+32)>>2)&0xF0);The +32 is for rounding - to make the decision point between two adjacent switch combo voltages half way between them. The >>6 lops off the low 6 bits for the first four switches. The >>2)&0xF0 also lops off the low 6 bits for the last four switches, but leaves the result four bits higher so it can be combined to make a single byte for all the switches.You'd then extract the state of a particular switch with the expressionCode:!(switches&(1<<(n-1)))which for n=1 to 8 will give you 1 if the corresponding switch is pressed, otherwise 0.The relays are on pins:Skt PIC I/O Description16 - RC0 - relay 115 - RC1 - relay 214 - RC2 - relay 37 - RC3 - relay 46 - RC4 - relay 55 - RC5 - relay 68 - RC6 - relay 79 - RC7 - relay 8Logic 1 = ON, logic 0 = OFF. Simply connect them to eight Arduino digital pins in order, and NOT using 0 or 1 as they are needed for the Arduino UART and control them with digitalWrite(n,level). They have series 1K resistors feeding into red LEDs and NPN transistor bases so they will about 2mA to 3mA from the I/O pin when on, easily within the Arduino's capabilities.If you want to check my conclusions, with the PIC removed, jumper pin 1 to any of the relay pin numbers (Skt) to turn that relay on, and hook a DMM on volts between pin 12 or 13 and pin 20 to see how the voltage changes as you press various combos of four buttons.

author: raspberrypi
author_rank: Frequent Contributor
timestamp: March 18, 2017, 05:01:54 am
content: Quote from: Ian.M on March 18, 2017, 03:22:15 amWith this set up when you press the button on the relay board the arduino reads them? Thats what the code is for? It almost sounded like you were saying put that first line of code into the relay board micro, which is no longer attached. or are you saying this is just how you emulate the switches on the relay board?

author: Ian.M
author_rank: Super Contributor
timestamp: March 18, 2017, 05:20:54 am
content: The two lines of code are for the Arduino. The first (assuming I've got it right) reads the ADC twice to get all the bits representing all eight switches into a single byte: switches. The second isnt a full line, its just an expression you could use in an if() which extracts the state of a single switch from the byte: switches.Connect a DMM on volts beween pin 13 (+) and 20 (-) and observe what happens as you press various combos of switches 1 to 4. If they are all up, you'll see a bit under 5V (check the voltage at pin 1 (Vdd)), the all up voltage should be 15/16 of Vdd, press them all and get 0V, and in between they are binary coded - SW4 subtracts Vdd/2, SW3, subtracts Vdd/4, Sw2 subtracts Vdd/8 and SW1 subtracts Vdd/16.