PDA

View Full Version : PIC Advice For A Newbe



Mad Professor
07-06-2009, 08:49 AM
Good Day All.

As the title says I am looking for advice with regards to programming PIC's.

To date the only kind of PIC's I have worked with are called PicAxe (http://www.picaxe.co.uk).

Due to the design and build in software thease PIC chips are to slow for what I am after.

I am looking at building my own PIC based stepper motor driver board.

I am looking to use one PIC per axis total of 3 axis on my board.

Each axis will have the following inputs and outputs.

Inputs
Enable.
Step.
Dir.
Mode Select 1.
Mode Select 2.

Outputs
Coil A.
Coil B.
Coil C.
Coil D.
Fan.
Enable LED.
Step LED.
Dir LED.

So I am going to need a PIC chip with at least 5 inputs and 8/9 outputs.

I am only looking at driving the stepper motors as unipolar.

The mode select switches will be for Full, Wave, and Half Mode stepping, I don't want to go down the micro stepper route yet.

I will be using this driver board with the Mach3 software.

Here are the two related posted on the PicAxe forum.

3 Axis Stepper and Spindle Control Circuit Diagrams.
and
1-5us pulse capture.

I have made a little box to put the driver board into.
http://www.mad-professor.co.uk/Misc/CNC%20Control%20Box.jpg

Thanks for your time.

Best Regards.

HankMcSpank
07-06-2009, 10:28 AM
I can't really assist you with your PIC selection (though you've not really any specific h/w needs...eg PWM, USARTS etc ....so I'd imagine most of the PIC 18 range would suffice), but just wanted to chime in as another recent PIC novice/convert.

I'm using my PIC to control just one stepper motor - this stepper is used as a way of traversing enamelled wire onto a coil winding motor. Basically, the main DC motor has an encoder disk. The PIC counts the stripes from this disk & every x stripes (to work out how far it has travelled) it then sends out a pulse to the stepper.

I actually went the simple/lazy way & used a dedicated IC to hadle to stepper interfacing - a UCN5804.

You project sounds really interesting - good luck!

Mad Professor
07-06-2009, 11:09 AM
Thanks for your reply.

I did not want a boring front on my control box, yes it is a little over the top.

I was looking at the F16 / F18 range of the PIC's.

As you already said I don't need PWM, ADC, Ect.

I will be buying the PIC's from RS.

I am not going to buy any pic or parts to build the driver boards unto such time that I know I can build the board and program the PIC to do what I want / need, as I could safe the money to buy a prebuilt driver, but it's not liky to find a driver board with all the led and fan control I am looking for.

The picaxe's that I have been using upto now are programed in something that is very close to Basic.

With the picaxe chips you don't need to buy the software or magic programmer.

I have been looking around the internet and I see there are meny languages for the pic, Basic, C, C++, Pascal, Ect.

As the picaxe was based around the Basic language, so a quick google found "MELabs PICBASIC Compiler (http://www.picbasic.co.uk)" but this is a paid software,
PICBASIC Compiler - meLabs £68.94
and
PICBASIC Pro Compiler - melabs £195.44.

That seems like alot of money to splash out for possibly a one time use.

John S
07-06-2009, 11:17 AM
Wow....is that a psychopathic paint job or is the camera fubar? :D Watch out the video on the shelf isn't an omen ;)


(I know about the typo lol )


What video ? need to get these glasses sorted...........

.

Sparks
07-06-2009, 12:13 PM
Hi

Love the box and I think I know what you're trying to achieve with the pics, however I think you are over-complicating matters by trying to go that route. I think you may be better having a look at various stepper-drive IC datasheets eg: http://www.robotstorehk.com/5804.pdf these or others may provide the functionality you require already built-in.

As for your progression to Pics your 3 main choices of language would be basic, assembler and 'C'.

Regards

Dave

BillTodd
07-06-2009, 02:15 PM
MP, (got a name?)

I suggest you get a PICKIT2 and program in PIC assembler (I don't think the PIC is really suited to a HLL) with MPLAB. As to choice of PIC, there are many (too many?) that'll do the job, I suggest you pick one with more I/O and internal resources than you need (you're not buying millions, so the difference in cost between that and one that'll just do the job is pennies).

I know you're re-inventing the wheel, but hey, we all know we could improve on round...

Mad Professor
07-06-2009, 02:41 PM
Thanks for the info.

I see on the Microchipdirect web site: PICkit 2 Starter Kit (DV164120) (http://www.microchipdirect.com/productsearch.aspx?Keywords=DV164120) £35.45

Is this the best / cheapest place to buy one from?

I am just watching some PIC Programming videos on YouTube.

BillTodd
07-06-2009, 03:18 PM
Is this the best / cheapest place to buy one from?
how long is a piece of string?

I picked up a Pickit2 from Microchip with 16F887 development board in a promo last year for <£10 delivered, so keep your eyes open for promo's

They flog the PK2 programmer on its own for £25 + carriage + VAT.
http://www.microchipdirect.com/productsearch.aspx?Keywords=pickit+2

There are some very simple DIY PC comport programmers if you want to DIY. You can download the software (MPLAB 8.2 etc.) FOC from the Microchip web site.

[edit]

I think I have stepper driver code for 16 bit pics somewhere, if you want to pinch it.

Mad Professor
07-06-2009, 03:25 PM
Ok thanks.


how long is a piece of string?

That’s easy it's double half it's length.

I am just installing the MPLAB as I type.

I will keep my eyes open for a promo.

BillTodd
07-06-2009, 03:33 PM
Here you go:

It was designed around a 16C84 to drive a L6219 bipolar driver, but since it uses a step table, all you need to do is change the bits to suit your output.

You'll have to change the CPU type etc. to suit whatever PIC you use.

Ignore / delete the limit switch code.


;****************************************
; PIC Header
;****************************************
;(c) 1994 - 2008 W.K.Todd Electronics
;
;DATE:23/08/2008 (initialised 27/09/1998)
;FILE:STEPPER.ASM - L6219 STEPPER DRIVER
;VERSION:1.10 - interrupt driven
;
; Target processor = 16C84 (eeprom PIC)
; Assembler = MPASM
;
LIST P=16C84
#INCLUDE P16C84.INC

;EQUATES
STPCNT EQU 0x0C ;STEP COUNTER
FLAGS EQU 0x0D ;VARIOUS FLAG BITS
TIMER EQU 0x0E ;GP TIMER USED TO SLOW LOOP

;interrupt service temps
STATUS_TEMP EQU 0X2E
W_TEMP EQU 0X2F


;BIT ADDRESSES
DEBN EQU 0x00 ;DEBOUNCE FLAG
CLKIN EQU 0X00 ;CLK INPUT
DIRIN EQU 0x01 ;DIRECTION INPUT
XLIM EQU 0x00 ;LIMIT SWITCH INPUTS
YLIM EQU 0x01 ;
ZLIM EQU 0x02 ;
LIMIT EQU 0x03 ;LIMIT OUTPUT (ACTIVE HIGH)
BYPSWT EQU 0X04 ;LIMIT BYPASS SWITCH

;

ORG 0x000
GOTO START ;RESET VECTOR
NOP
NOP
NOP
;INTERRUPT VECTORS TO HERE
;
PUSH MOVWF W_TEMP ; Copy W to TEMP register,
SWAPF STATUS, W ; Swap status to be saved into W
MOVWF STATUS_TEMP ; Save status to STATUS_TEMP register
; Interrupt Service Routine
;test interrupt type
BTFSS INTCON,INTF ;test int0 flag
goto POPW

bcf INTCON,INTF ;clr flag
BTFSC PORTB,DIRIN ;TEST DIRECTION
GOTO CLKWIZ
;DO ANTI CLK TURN
DECF STPCNT,W ;DECR AND FETCH STEP COUNT
ANDLW 0x07 ;MODULO 7
MOVWF STPCNT ;SAVE
CALL STPTAB ;GET PHASE VALUES
MOVWF PORTB
GOTO POPW ;AND FINISH

;DO CLOCKWISE TURN
CLKWIZ INCF STPCNT,W ;INCR AND FETCH STEP COUNT
ANDLW 0x07 ;MODULO 7
MOVWF STPCNT ;SAVE
CALL STPTAB ;GET PHASE VALUES
MOVWF PORTB


POPW SWAPF STATUS_TEMP, W ; Swap nibbles in STATUS_TEMP register
; and place result into W
MOVWF STATUS ; Move W into STATUS register
; (sets bank to original state)
SWAPF W_TEMP, F ; Swap nibbles in W_TEMP and place result in W_TEMP
SWAPF W_TEMP, W ; Swap nibbles in W_TEMP
RETFIE
;
; TABLES
;Bits/function
;7 6 5 4 3 2 1 0
;Io1 I11 PH1 I12 I02 PH2 DIR STEP
STPTAB ADDWF PCL,f

;2/3 current, half step table
; RETLW 0xC8 ;STEP0
; RETLW 0x88 ;1
; RETLW 0x9C ;2
; RETLW 0x8C ;3
; RETLW 0xEC ;4
; RETLW 0xAC ;5
; RETLW 0xB8 ;6
; RETLW 0xA8 ;7

;full current, half step table
RETLW 0xC0 ;STEP0
RETLW 0x00 ;1
RETLW 0x1C ;2
RETLW 0x04 ;3
RETLW 0xE4 ;4
RETLW 0x24 ;5
RETLW 0x38 ;6
RETLW 0x20 ;7


;
; START OF PROGRAM
;
START BSF STATUS,RP0 ;select bank 1

MOVLW 0x03 ;RB0 +1 TO INPUT
MOVWF TRISB
MOVLW 0x17 ;RA0,1,2,4 TO INPUT
MOVWF TRISA
MOVLW 0XCF
MOVWF OPTION_REG ;set up interrupt
BCF STATUS,RP0

MOVLW 0X90
MOVWF INTCON ;enable int0
CLRF STPCNT
CLRF FLAGS

;MAIN LOOP
MAINLP

;TEST LIMIT SWITCHES
TLBPS BTFSS PORTA,BYPSWT ;TEST LIMIT BYPASS
GOTO BYPASS
MOVF PORTA,W ;GET LIMIT SWITCHES
ANDLW 0x07
BTFSS STATUS,Z
BSF PORTA,LIMIT ;SET LIMIT IF X,Y OR ZLIM IS HIGH
BTFSC STATUS,Z
BYPASS BCF PORTA,LIMIT ;CLEAR LIMIT OUTPUT IF SWITCHES ARE ALL LOW
GOTO MAINLP ;LOOP BACK


END

HankMcSpank
07-06-2009, 04:46 PM
FWIW, I bought the PIckit2 ...& think it's the best thing since the proverbial sliced (it even has a function that replicates the h/w normally needed to get your PIC interfaced with your PC serial comms wise ....normally you'd need to buy a signal level converter like a MAX232, but such capability is integral to the Pickit2 ...check out Uart Tool once you buy it & install the software).

Re languages, I started off thinking I'd learn assembly but alas, I spontaneously combusted when I tried to get something as simple as gettting an LED to light up!

PIC basic is much more digestible... I've got my PICs doing all sorts of stuff now ...eg just finished a program that carries out AtoD, then ultimately controls an AGC circuit based on the AtoD'ed incoming level etc ...I'd have had a beard like ZZ top trying to figure that lot out in Assembly!

John S
07-06-2009, 04:49 PM
.I'd have had a beard like ZZ top trying to figure that lot out in Assembly!

two Z axis ???:heehee:

.

irving2008
07-06-2009, 05:27 PM
I use a programmer that came, well the PCB did, with the Myke Predko's book Programming and Customizing the PIC Microcontroller. Can't recommend the book but it was cheap (well mine was) and with a downloaded copy of MPLAB and the C18 compiler I've been programming away in C happily. I've always thought C a better language to program hardware in than Basic (discuss*) but each to their own.

Unless you are happy as a pig in sh*t with a soldering iron and a scope (which, sadly, I am) I'd buy the PicKit or one of the other proprietary tools.

:)


*The main advantage for me - non-CNC related - is that there are a lot of C libraries out there for CANBus, TCP/IP stacks and on the latest PICs USB interfacing, all of which I have used - none of which are easy program in Basic. For the un-initiated CANBus is the network that talks to the engine management system on most modern cars... I did some projects linking that to a laptop via a wireless connection to monitor and adjust engine management parameters in real-time.

Mad Professor
07-06-2009, 05:43 PM
Thanks for eveyones input so far.

I have just had a quick google looking for the UCN8504B driver chips, could not find any UK suppliers, I did find a guy one eBay selling them, but wants £7.65 Each plus £3.75 for postage, so a min of £26.70 for 3 IC's.

Also wile I was on eBay I had a quick look at the prebuilt driver boards, most where from China/Hong Kong and only supports voltages upto 24volts.

I know going down the PIC route will cost more then a prebuilt driver, but at least once I have got the programmer I can use it for any other projects I might have.

BillTodd
07-06-2009, 06:34 PM
Free BASIC - http://gcbasic.sourceforge.net/

pavlo
08-06-2009, 01:26 AM
CLaNZeR has designed and built some great PIC programmers and software, hopefully he will add to this thread

HankMcSpank
01-07-2009, 02:14 PM
It looks like the site's spam filter isn't working that well!


Thanks for eveyones input so far.

I have just had a quick google looking for the UCN8504B driver chips, could not find any UK suppliers, I did find a guy one eBay selling them, but wants £7.65 Each plus £3.75 for postage, so a min of £26.70 for 3 IC's.


I'll hazard a guess you meant UCN5804B ...they are available in the UK. Here's where I bought mine from


http://www.cricklewoodelectronics.com/Cricklewood/product.php?productid=15150&cat=0&page=1 (it's a great little chip)

It's only saved you a couple of quid, but at least they're UK based (& a good company at that)

irving2008
01-07-2009, 05:03 PM
It looks like the site's spam filter isn't working that well!



I'll hazard a guess you meant UCN5804B ...they are available in the UK. Here's where I bought mine from


http://www.cricklewoodelectronics.com/Cricklewood/product.php?productid=15150&cat=0&page=1 (it's a great little chip)

It's only saved you a couple of quid, but at least they're UK based (& a good company at that)fixed the spam...

Ah, another devotee of Cricklewood Electronics...

tumutbound
02-07-2009, 02:25 AM
I started to play around with PICs a little while back and found the following suppliers good value for money.

Tech Toys (http://www.techtoys.com.hk) - development/test kits
Kee Electronics (http://www.keeelectronics.com/catalog/index.php) - mainly PIC programmers
Futurlec (http://www.futurlec.com.au) - lots of development/test kits for PIC's and Atmel stuff. Cheap electronic components too.

I'd avoid using BASIC and stick to C or assembler. There are a lot of free C libraries available from Microchip (http://www.microchip.com) plus of course the free MPLAB stuff.

Kwackers
23-08-2009, 07:29 PM
Worth looking at Mikroelectonika too http://www.mikroe.com/

I used to spend half my time breadboarding stuff and fiddling with wiring, now I just flick the switches on the development board to get hardware I'm interested in 'wired' up.
USB to PC for programming, debugging, ICD. A decent collection of on-board IO (leds, switches, 7 segment, GLCD, LCD, RS232, USB, PS2 etc) along with all ports brought out to headers and a collection of cheap IO boards that will connect to the headers (or make your own) all the way up to GPS, LAN, CAN etc etc.

They also have a range of compilers in various languages which all provide nice little libraries to use the peripherals making one's life very easy.
(If your projects are small then you can use the compilers in 'lite' mode for free). Want your stepper controller to have it's own web page on your network? (And who wouldn't :cool:) it's no problem...

Once your device is working as you expect simply copy the circuit for the peripherals you use into the final design (circuit diagrams are provided for all their peripherals).

I have of course no connection with the company, but having used PICs for a long time and in a lot of ways I've simply found their products to be top notch and saved me much time and effort.

Turbodisplay
06-12-2009, 10:14 AM
The best PICs to use are the 18F series as they are the most powerful, processors, with up to about 4K ram.
I would say the PIC18f4620 is the best processor to start with.
Thanks
Darren