Pic16f870

Discussion in 'Programmer's Corner' started by EricFa, Dec 29, 2010.

  1. EricFa

    Thread Starter New Member

    Dec 29, 2010
    3
    0
    Is there a place where I can find Sample Code for the PIC16F870 from Microchip? Or maybe a tutorial with examples? :confused:
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You can use the template which should be found here which can be a help.
    C:\Program Files\Microchip\MPASM Suite\Template\code

    Otherwise just search for PIC16F sample code and make any required changes (some of the registers will be different or in different banks).
    Should help you get used to the Simulator in MPLAB IDE (if that's what you are using).
     
  3. EricFa

    Thread Starter New Member

    Dec 29, 2010
    3
    0
    I already searched with Google, but it only finds Samples for the PIC 16F84 and not the PIC16F870. :(:(:mad:
    Is there any way I can find people, that have experience in programming the PIC 16F870?
     
  4. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Sure plenty of us have PIC experience. Lots & lots of books and tutorials are out there.
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Problem is there are hundreds of PICs so there are not too many examples for the less popular ones.
    For simple programs it is not too hard to change them from one PIC to the other. If you make the basic changes like anything which refers to the device number then compliing it and reading the errors should guide you in the right direction.
    If you have problems, post code, circuit diagram and error reports and we should be able to help.
    Ones that you should find lots of examples for are 16F628, 16F877.
    16F84 is a bit older and has some differences to the modern PICs so I'd avoid code for that one.
     
  6. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,347
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Do you want to program in C or assembler? Also do you have some demo board. Or are you just using a breadboard. And you do have a programmer (hardware)?
    Since a PIC MCU is a building block. It is hard to find general examples. I have some ideas what you can do. But I need to know more about your design limits.
     
  8. EricFa

    Thread Starter New Member

    Dec 29, 2010
    3
    0
    @ Markd77:
    What is the difference between the PIC16F870 and the 16F628 or 16F877?

    @ bertus:
    Thanks for the Link!

    @ t06afre:
    I have an USB & Serial Dual Mode Microchip PIC Programmer
    http://www.nbglin.com/usbb.htm
    I bought it via Ebay.

    I just want some code; someone must have programmed something with the PIC16F870.
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You forgot to tell us which programming language you want to use. I do not think you will get much help this way. So instead let us do like this. You find some projects on the net that you feel you feel match your skill level. If you want post the links here for our opinion. Then you can use this forum if you need help in porting them to your CPU.
     
  10. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    You haven't mentioned which language and compiler you use. So it makes things hard. If you specified that, someone could have just coded something. So, I'll just post a few examples:

    All of them are to blink a LED at RB0 (PORTB pin 0). Configuration: 4MHz crystal, XT OSC, Watchdog timer off, Power Up timer off, Code protect off, Brown out detect off, Low voltage program disabled, Data EE read protect off, Flash program write disabled, Background debug disabled. Config 0x3939

    mikroC:
    Code ( (Unknown Language)):
    1. void main() {
    2.      TRISB = 0;
    3.      PORTB = 0;
    4.      while (1){
    5.            PORTB.F0 = ~ PORTB.F0; //Invert RB0
    6.            delay_ms(500);
    7. /*Can also be written as:
    8.            PORTB.F0 = 1;
    9.            delay_ms(500);
    10.            PORTB.F0 = 0;
    11.            delay_ms(500);
    12. */
    13.      }
    14. }
    mikroBASIC:
    Code ( (Unknown Language)):
    1. program LEDBLINK
    2.  
    3. main:
    4.      TRISB = 0
    5.      PORTB = 0
    6.      while true
    7.            PORTB.B0 = not PORTB.B0 'Invert state at RB0
    8.            delay_ms(500)
    9.            'Can also be written as
    10.            'PORTB.B0 = 1
    11.            'delay_ms(500)
    12.            'PORTB.B0 = 0
    13.            'delay_ms(500)
    14.      wend
    15.  
    16. end.
    MPASM (assembler in MPLAB):
    Code ( (Unknown Language)):
    1.     LIST P=16F870
    2.     #INCLUDE <P16F870.INC>
    3.     __CONFIG 0x3939
    4. ;    CAN BE WRITTEN AS
    5. ;    __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _XT_OSC
    6.  
    7.     ORG 0x00
    8.     GOTO MAIN
    9.  
    10.     CBLOCK 0x20
    11.     d1
    12.     d2
    13.     d3
    14.     ENDC
    15.  
    16. DELAY
    17.             ;499994 cycles
    18.     movlw    0x03
    19.     movwf    d1
    20.     movlw    0x18
    21.     movwf    d2
    22.     movlw    0x02
    23.     movwf    d3
    24. Delay_0
    25.     decfsz    d1, f
    26.     goto    $+2
    27.     decfsz    d2, f
    28.     goto    $+2
    29.     decfsz    d3, f
    30.     goto    Delay_0
    31.  
    32.             ;6 cycles
    33.     goto    $+1
    34.     goto    $+1
    35.     goto    $+1
    36.  
    37. RETURN
    38.  
    39. MAIN
    40.     BANKSEL    TRISB ;GOTO BANK 1
    41.     CLRF    TRISB ;SET PORTB TO OUTPUT
    42.     BANKSEL    PORTB ;GOTO BANK 0
    43.     CLRF    PORTB ;CLEAR PORTB
    44.  
    45. START
    46.     BSF        PORTB,0 ;SET RB0
    47.     CALL    DELAY
    48.     BCF        PORTB,0 ;CLEAR RB0
    49.     CALL    DELAY
    50.  
    51.     GOTO    START
    52. END
    Hope this helps.
    Tahmid.
     
  11. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    The neat thing about the PIC processors, at least the 8-bit ones, is that the programming is the same for all of them. You just have to make adjustments for the features that each chip has or hasn't got. You need to get the data sheets for your particular unit to figure out what's possible on that chip.
     
Loading...