New to pic programming, help needed please.

Thread Starter

bainsk8

Joined May 31, 2011
25
Thanks guys Eagle it is then. I am just about to place an order for the PK3, I did try earlier via the Microchip website but for some reason when I add it to the basket it does not do it and shows nothing, very strange. I will have to order it via a distributor I think.

Thanks again guy's, I am ready to go now. :D
 

onebird

Joined Mar 27, 2010
16
I might be a new member and all, but I should say that I really admired the way bainsk8 asks for help and advice. So clear, to the point and quite respectful.
@bainsk8, you have earned my respect. Just thought I should comment
 

Thread Starter

bainsk8

Joined May 31, 2011
25
Thank you onebird, that is most kind of you to say. I hope you found this thread of use and you also enjoy your stay here.

Just to keep all who helped up to date I have received a nice shiny PK3 with demo board and PIC18F45K20 on-board. I have managed to resist the temptation to try it out as I would like to get some other projects finished first.

I am currently in the process of producing a power supply that will turn my 12v wall adapter into a switchable 5v / 3.3v supply to my breadboard. I have also placed some parts on order to make a demo board for the PIC16F84A-04/P. I have also been reading/learning more about electrics, electrical related mathematical formulas and PIC's and feel things are moving along quite nicely. More so since the help I received here, so a big thanks again for that guy's. :D
 
Last edited:

Thread Starter

bainsk8

Joined May 31, 2011
25
Just a quick update, I have programmed my first PIC to flash a LED. Hoorah!
I am using the 16F84A as mentioned before.

It was not plain sailing and it took me 6 hours to debug it. I was using a 4Mhz external crystal with two 15pf capacitors in series as per spec. But the led would not flash, so I went through a process of elimination. I checked the software first, all seemed fine here so I then checked the code, again all seemed correct. Then I read about bad PIC's so I swapped it over and programmed that but still no flashing LED. After much head scratching and just about to give up I thought lets just play with the circuit to make sure its getting power and that all the wires are properly seated in the breadboard. The LED came on as I placed my hand over the crystal and started to flash very slowly, uh! What the, I thought, so I played with it a bit and discovered that if I placed a resistor from the 5v rail to the clock in pin on the PIC it flashed really fast.

My first question is why did the 4mhz quartz not work, could it be a wrong type or something?
Second question is why did my hand placed over the top of the crystal kind of bring it to life, albeit slowly?
And am I right in thinking that the resistor is now acting as the clock?

I find the whole thing most odd, but a great learning curve because I really studied the PIC data sheet very closely and I also now have a better understanding of the MPLAB software.

Be great if someone could answer the questions for me. :D
 
Last edited:

Thread Starter

bainsk8

Joined May 31, 2011
25
Hi Markd77, yes I am using the schematic below with the led on RA0 to ground and I also have a 1K resistor from 5v rail to MCLR.





The Code is:
Rich (BB code):
;**Set up the constants**

STATUS        equ 03h
TRISA        equ 85h
PORTA        equ 05h
CounterL    equ 0Dh
CounterH     equ 0Eh

;**Set up the port**

bsf    STATUS,5
bcf    TRISA,0
bcf    STATUS,5

;**Turn thr LED ON**

Start    bsf    PORTA,0

;**Delay Loop1**

Loop1    decfsz     CounterL,1
        goto     Loop1
        decfsz     CounterH,1
        goto     Loop1

;**Turn the LED OFF**
        
        bcf    PORTA,0

;**Delay Loop2**

Loop2    decfsz    CounterL,1
        goto    Loop2
        decfsz    CounterH,1
        goto     Loop2
        
        goto Start
        
        end
Thank you nerdegutta, that is a very interesting link I have book marked it and will take a better look at it later.
 

Thread Starter

bainsk8

Joined May 31, 2011
25
Thanks Markd77, I will give that a shot later this evening, I am just off out for the day. I see what it does it sets the external oscillator mode, watch dog time off , but what does this part do mark?

PWRTE _ON & _CP_OFF

Thanks again. :D
 

ErnieM

Joined Apr 24, 2011
8,377
bainsk8 said:
My first question is why did the 4mhz quartz not work, could it be a wrong type or something?
Second question is why did my hand placed over the top of the crystal
kind of bring it to life, albeit slowly?
I dunno if it's the right crystal, you didn't tell us what crystal you used <grin>. I've found that crystals are perhaps fussier then something called a "resonator" which is not quite as accurate as a crystal but much cheaper and already has the caps built in (its a 3 terminal device). That is what I usually use.

Now moving your hand is actually significant. Your hand has capacitance and perhaps you are adding the additional 3.8294516 pF the crystal needs to oscillate. Check and recheck the wiring of that area, it is a bit critical. Try to keep the wires very short too.

BTW, the caps from the oscillator can go to either Vss or Vdd (ignore the flame war that might follow me saying this. I get flamed whenever I say it but it is solid AC analysis and I ship product this way every day).

Finally, getting your first LED to light after only 6 hours is actually way above average, especially since you built your own board too.

Congratulations!
 

Markd77

Joined Sep 7, 2009
2,806
The template is a good start (config line needs changing), found in c:\program files\microchip\mpasm suite\template\code.
Pwrite is a startup timer that is supposed to make it more reliable.
CP is code protection, which means you can't read the chip and have to erase it to reprogram it. Only useful if you want to sell someone a PIC and for them to not know the code on it.
Rich (BB code):
;********************************************************************** 
;   This file is a basic code template for assembly code generation   * 
;   on the PIC16F84A. This file contains the basic code               * 
;   building blocks to build upon.                                    *   
;                                                                     * 
;   Refer to the MPASM User's Guide for additional information on     * 
;   features of the assembler (Document DS33014).                     * 
;                                                                     * 
;   Refer to the respective PIC data sheet for additional             * 
;   information on the instruction set.                               * 
;                                                                     * 
;********************************************************************** 
;                                                                     * 
;    Filename:        xxx.asm                                           * 
;    Date:                                                            * 
;    File Version:                                                    * 
;                                                                     * 
;    Author:                                                          * 
;    Company:                                                         * 
;                                                                     *  
;                                                                     * 
;********************************************************************** 
;                                                                     * 
;    Files Required: P16F84A.INC                                      * 
;                                                                     * 
;********************************************************************** 
;                                                                     * 
;    Notes:                                                           * 
;                                                                     * 
;********************************************************************** 
 
 
    list      p=16F84A            ; list directive to define processor 
    #include <p16F84A.inc>        ; processor specific variable definitions 
 
    __CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC 
 
; '__CONFIG' directive is used to embed configuration data within .asm file. 
; The lables following the directive are located in the respective .inc file. 
; See respective data sheet for additional information on configuration word. 
 
 
 
 
;***** VARIABLE DEFINITIONS 
w_temp        EQU     0x0C        ; variable used for context saving  
status_temp   EQU     0x0D        ; variable used for context saving 
 
 
 
 
 
 
 
 
;********************************************************************** 
        ORG     0x000             ; processor reset vector 
          goto    main              ; go to beginning of program 
 
 
        ORG     0x004             ; interrupt vector location 
        movwf   w_temp            ; save off current W register contents 
        movf    STATUS,w          ; move status register into W register 
        movwf    status_temp       ; save off contents of STATUS register 
 
 
; isr code can go here or be located as a call subroutine elsewhere 
 
 
        movf    status_temp,w     ; retrieve copy of STATUS register 
        movwf    STATUS            ; restore pre-isr STATUS register contents 
        swapf   w_temp,f 
        swapf   w_temp,w          ; restore pre-isr W register contents 
        retfie                    ; return from interrupt 
 
 
 
main 
 
; remaining code goes here 
 
 
 
 
 
 
 
 
 
 
        END                     ; directive 'end of program'
 

Thread Starter

bainsk8

Joined May 31, 2011
25
Thank you ErnieM, yes the crystal was a cheep one from the bay, (not 3 a terminal device) it did say about using capacitance in series of 15pf to 30pf. I did think that my hand was effecting capacitance so I tried two 15pf's in series = 30pf on each going to ground, but to no effect. I will have to remember about going to positive rail and give it a try, I'm sure there are some valid reasons behind using and not using an oscillator in this configuration and I am also sure its covered else where on this forum so hopefully there be no flaming done here. ;)

You say that it needs the additional capacitance of 3.8294516 pF, I take it this is what my hand was generating? Again this is interesting and I will have to take note of this in case I come across this again in future. It will hep me to get into the ballpark with adjustments.

Thank you for the kind words, like I have said earlier I have no experience with electrics and was very please to make my first project the other night. The project was to recycle a 12vDC 700ma wall adaptor to a regulated switchable 3.3v - 5v breadboard PSU using the LM317L regulator. I changed it slightly to include two led's to let me know what voltage it was switched to, so I don't antecedently fry anything I am working on. It was also the first time I have used a strip board, it took a bit of time working out where to put what where but I got there in the end. Here is a photo of it with PIC project.



I do plan to make a development board for this PIC, I would like to etch my own board for this just so I can gain some experience of the whole process.

The black wire near the led on the breadboard does nothing, I used it as the ground feed to the crystal.

Thanks again ErnieM and Markd77 for your help, its greatly appreciated.
 
Last edited:

Thread Starter

bainsk8

Joined May 31, 2011
25
Hi Markd77, rain has cancelled my day out which means I get to spend a bit more time with this. :D

Thank you for the heads up on the template code and location within the MPLAB file directory. There is so much to learn and I am finding myself bouncing between the PIC data sheet, MPLAB Help Files, my Electrics book, the internet and of cause this great forum.

I will get into the habit of using the template from now on, thank you again for this.

The link that nerdegutta posted earlier on is really good, I have just ordered a PIC16F628 and will give this a go after I have finished some more tutorials using the PIC16F84a

I wish I had got into PIC's and electronics earlier not only because it is very interesting but also because I would be much more advance with it as I can see the great potential of using PIC's.
 

Thread Starter

bainsk8

Joined May 31, 2011
25
Hi Markd77, just to let you know that

__config _XT_OSC & _WDT_OFF & _PWRTE _ON & _CP_OFF

indeed did fix the problem and now the crystal is working as it should. Thank you again for all your help. ;)
 
Top