Skip to main content
Topic: Successful PIC programming (Read 5137 times) previous topic - next topic

Successful PIC programming

I've added a new protocol for programming Microchip PIC microcontrollers to the newterm branch (could be ported back to the old one). Besides this firmware you'll need some way of generating 13v which is applied to the MCLR/VPP pin (pic24/30 don't need it but they aren't tested yet). Feel free to (help me) add it.

I used a prototype (  http://dangerousprototypes.com/2009/12/ ... er-update/ ) that is provided by Ian, which uses a PWM signal to supply a 13v. The final HVP adapter (see also http://dangerousprototypes.com/forum/in ... opic=265.0 ) need some other initialization, but sending commands should be the same.

Code: [Select]
Bus Pirate v3
Firmware v4.2 (newterm-basic)  Bootloader v4.1
DEVID:0x0447 REVID:0x3043 (B5)
http://dangerousprototypes.com
HiZ> m
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. KEYB
9. LCD
10. PIC
x. exit(without change)

(1)> 10
Commandmode
1. 6b cmd/14b data
2. 4b cmd/16b data

(1)> 1
Delay in ms

(1)> 1
READY

There are 2 modes which can be used: a 6 bit mode and a 4  bit mode. The 6 bit is currently the only that is (a bit) tested. This mode is used by pic10, 12, 14, and 16.

I hooked up a pic10F222 to the adapter and buspirate. Used commando's in this example:

2   load data into buffer
4   read data from current pc
6   increment pc
8   program buffer to memory at pc
14  stop programming
9   erase memory

See document DS41266C for detailed information about programming the pic10F220/222 ( http://ww1.microchip.com/downloads/en/D ... 41266C.pdf )

reading and wiping the device:

Code: [Select]
PIC> g 100 50
Prescale:8
PR2:19
PWM active
PIC> W
POWER SUPPLIES ON
PIC> P
Pull-up resistors ON

Code: [Select]
PIC> s
PIC(BASIC)> list

100  REM DUMP PIC10F
110  FOR A=1 TO 518
120  START
130  SEND 4
140  STOP
150  PRINT  RECEIVE ;",";
160  START
170  SEND 6
180  NEXT A
190  END
66 bytes.
Ready.
PIC(BASIC)> run
4075,37,1029,2573,3322,49,3327,50,753,2570,2048,754,2570,2567,3264,2,3072,38,332
3,6,3076,422,2307,2579,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,40
95,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,3096,4095,4095,40
95,4095,3096,
Ready.
PIC(BASIC)> exit
Ready.
PIC> p
Pull-up resistors OFF
PIC> w
POWER SUPPLIES OFF

As we can see it is already programmed! the dumping starts at 0x3FF (config word), rolls over to 0x000. Then 0x1FF programwords follows, OSCCAL, 4 ID words and the backup OSSCAL (oscilator calibration value) (on page 2 there is a memory map)

Remember to unpower the device (p w) to reset the pc to 0x3FF. Alternatively you can increment the pc to 0x3FF, but we are lazy, aren't we?

Code: [Select]
PIC> W
POWER SUPPLIES ON
PIC> P
Pull-up resistors ON
PIC> [9]
CMD6 WRITE: 0x09
DTA
PIC> p
Pull-up resistors OFF
PIC> w
POWER SUPPLIES OFF

This will erase the device, except the ID bits and backup OSCCAL.

Let us check:

Code: [Select]
PIC> W
POWER SUPPLIES ON
PIC> P
Pull-up resistors ON
PIC> s
PIC(BASIC)> run
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,3096,
Ready.
PIC(BASIC)>

4095=0xfff and thus empty. Please note the 3096 at the end. which translates to 0xC18 (MOVLW 0x18).

Let us try and program the chip:

Code: [Select]
Bus Pirate v3
Firmware v4.2 (newterm-basic)  Bootloader v4.1
DEVID:0x0447 REVID:0x3043 (B5)
http://dangerousprototypes.com
HiZ> m 10 1 1
PIC (mod dly)=(6 1)
READY
PIC> g 100 50
Prescale:8
PR2:19
PWM active
PIC> W
POWER SUPPLIES ON
PIC> P
Pull-up resistors ON
PIC> s
PIC(BASIC)> list

100  REM PROGRAM PIC10F
110  FOR A=1 TO 24
120  START
130  SEND 2
140  STOP
150  READ B
160  SEND B
170  START
180  SEND 8
190  DELAY 2
200  SEND 14
210  SEND 6
210  STOP
220  NEXT A
230  START
240  FOR A=25 TO 512
250  SEND 6
260  NEXT A
270  SEND 2
280  STOP
290  READ B
300  SEND B
310  START
320  SEND 8
330  DELAY 2
340  SEND 14
350  STOP
360  END
1000  DATA 4075
2000  DATA 37,1029,2573,3322,49,3327
2010  DATA 50,753,2570,2048,754,2570
2020  DATA 2567,3264,2,3072,38,3323
2030  DATA 6,3076,422,2307,2579
3000  DATA 3096
283 bytes.
Ready.
PIC(BASIC)> run

Ready.
PIC(BASIC)> exit
Ready.
PIC> p
Pull-up resistors OFF
PIC> w
POWER SUPPLIES OFF
PIC> g
PWM disabled

line 1000 has the config word, 3000 has the OSCCAL vallue.

Now sit back and whoreship the blicking led:

Code: [Select]
PIC> W
POWER SUPPLIES ON
PIC>

Just for the curous among us, the pic10F source code (nothing special, just a delay and some gpio twiddling)

Code: [Select]
;
; blink.asm
;

; code originaly by farnell suppied with ther pic10f starter kit
; CvD:cleaned and commented a bit
;


list p=10F222
#include <P10F222.INC>

;__CONFIG_CP_OFF&_WDT_OFF&_MCLRE_ON&_INTRC_OSC

; vars
temp equ 0x10
delay1 equ 0x11
delay2 equ 0x12


; start
org 0x000

start movwf OSCCAL
bcf OSCCAL, FOSC4 ; no clock output
goto main

; subs needs to be in lower mem ( <0x0FF) as argument of call is 8 bits
; delay xx ms
delay movlw 0xFA
movwf delay1
movlw 0xFF
movwf delay2
delayl1 decfsz delay1,F
goto delayl2
retlw 0x00
delayl2 decfsz delay2,F
goto delayl2
goto delayl1

; main starts here
main movlw b'11000000' ; page 17 of the manual :)
option
movlw b'00000000' ; output all 0's (where tris=0)
movwf GPIO
movlw b'11111011' ; gp2=output
tris GPIO
mainl movlw b'00000100' ; GP2
xorwf GPIO,F ; toggle gp2
call delay ; delay a while
goto mainl ; jump back

end

Everything should be in the SVN (except the pic10f program). There is also a binmode on its way (somewhere this weekend I hope).

I'm aware there are still some bugs in this code (I *assume* also in the newterm branch ;)) Please test and post the results in here.

Re: Successful PIC programming

Reply #1
Fantastic demo. I'm breadboarding a 16f84 to do a first test.
Got a question? Please ask in the forum for the fastest answers.

Re: Successful PIC programming

Reply #2
Please check if the programming algo is the same. There are small differences between the pics. I got somewhere at home (I'm at work atm) most of the programming manuals. I'll post it later on.

BTW: nice topictittle ;P

Re: Successful PIC programming

Reply #3
I found my favorite 8bit micro on my desk, a 16f628a (missing one pin). I got the programming spec for it and I'm trying to figure it out now. I'm at the bottom of the learning curve at this point and reading this spec makes my eyes cross 8)
Got a question? Please ask in the forum for the fastest answers.

Re: Successful PIC programming

Reply #4
By quick looking at the sheet:

Code: [Select]
[4]r[6]

Will read a word and increment the pc, if you repeat this it will dump (eventually) the pic :D

I notice it had two types of programming mode, and a different way of entering programming mode (first apply 13v, then vcc and tie pgm low). See page 6.


Re: Successful PIC programming

Reply #6
Finally, I managed to do it too. Here are R+B in a terrible cell-phone photo. Thanks! It was nice to see some success.

Edit: I used the HVP adapter, and I entered the values by hand not the script. My program was just my own test that put a byte onto the GPIO port. I changed the color (byte) manually each time so I could tell if the changes took effect. IT was a little tedious, but now I understand it better.
Got a question? Please ask in the forum for the fastest answers.

Re: Successful PIC programming

Reply #7
congrats, ian!

Edit: did you made any changes to the design of the hvp adapter?