basics of programming for PICs

Discussion in 'Embedded Systems and Microcontrollers' started by vane, Jul 15, 2008.

  1. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    OK, i am getting a pic programmer for my birthday ( here ) and wondered if anyone could give me the basics. I want to try and make a strobe circuit and wondered if someone could give me the basic code things for like, input 1 on, input 2 on input 1 off etc in the most common coding languages, i have had a bit of experience coding in c#. the pic programmer i am getting comes with a bit of software but i don't know what it is called.

    thanks for any help you can give
     
    Last edited: Jul 15, 2008
  2. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
  3. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    the website says it comes with a PIC16F627, what are the codes for that?
     
  4. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    This is an example using the Hitech PICC Lite compiler:

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>    /* always here for PICC compiliers */
    3.  
    4. //Configuration macro ... may need to change for different PICs
    5.  
    6.  __CONFIG(FCMDIS & IESODIS & BORDIS & UNPROTECT & MCLRDIS & PWRTEN & WDTDIS & INTIO);
    7.  
    8.  
    9. int i;
    10. main()
    11. {
    12.     PORTA = 0;
    13.     CMCON0 = 7;
    14.     ANSEL = 0;
    15.     TRISA4 = 0;   //set to output
    16.     TRISA5 = 0;   //set to output
    17.     while (1 == 1)
    18.     {
    19.         for (i = 0; i < 25000; i++);
    20.         RA4 = RA4 ^ 1;  //toggle pin RA4
    21.     }
    22. }
    23.  
    24.  
    This runs on a PICKit 1 w/PIC16F684 but should also be OK for a '627 (excpet the CONFIG line. You can try deleting all except the WDTDIS.)
     
  5. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,347
  6. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    I have got the PIC Microcontroller programmer and have assembled it (soldered the parts onto the board) and have successfully loaded the example, i then recoded it to do a cylon eye sequence but that is as far as i have got, this is with the PIC16F627
     
  7. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    Am i right about saying the PIC16F627 has 14inputs/outputs?
     
  8. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    Technically it has 16 but if you plan to program it on the board then you have 15 I/O + MCLR/RA5/VPP Input (pin 4)
     
  9. vane

    Thread Starter Active Member

    Feb 28, 2007
    181
    0
    do you know how i do something if one of the sample buttons is pressed? they are the RA0, RA1, RA2 and RA3

    here is the sample code they include to light up LD1 to LD6 (the sample LEDs)
    Code ( (Unknown Language)):
    1. ;**************************************************************************
    2. ;*           VELLEMAN   High-Q KIT  K8048/VM111 SAMPLE SOFTWARE           *
    3. ;**************************************************************************
    4. ;*           DEMO1: Simple Running light for testing LD1...LD5            *
    5. ;*       DEMO1: Program for testing K8048/VM111 with PIC16F627!       *
    6. ;**************************************************************************
    7. ;*          (C) VELLEMAN Components,2003  All rights reserved             *
    8. ;**************************************************************************
    9. ;*          Hardw. Rev: P8048'1          Softw. Rev:  1.20                *
    10. ;*          OSC.......: XT 4MHz Max.     POWER.....:  12V DC              *
    11. ;**************************************************************************
    12.  
    13. W                            EQU     H'0000'
    14. F                            EQU     H'0001'
    15.  
    16. ;----- Register Files------------------------------------------------------
    17.  
    18. INDF                         EQU     H'0000'
    19. TMR0                         EQU     H'0001'
    20. PCL                          EQU     H'0002'
    21. STATUS                       EQU     H'0003'
    22. FSR                          EQU     H'0004'
    23. PORTA                        EQU     H'0005'
    24. PORTB                        EQU     H'0006'
    25.  
    26. INTCON                       EQU     H'000B'
    27. OPTION_REG                   EQU     H'0081'
    28. TRISA                        EQU     H'0085'
    29. TRISB                        EQU     H'0086'
    30. CMCON                        EQU     H'001F'
    31.  
    32. ;----- STATUS Bits --------------------------------------------------------
    33. IRP                          EQU     H'0007'
    34. RP1                          EQU     H'0006'
    35. RP0                          EQU     H'0005'
    36. NOT_TO                       EQU     H'0004'
    37. NOT_PD                       EQU     H'0003'
    38. Z                            EQU     H'0002'
    39. DC                           EQU     H'0001'
    40. C                            EQU     H'0000'
    41.  
    42. ;==========================================================================
    43. ;
    44. ;       RAM Definition
    45. ;
    46. ;==========================================================================
    47.  
    48.     __MAXRAM H'01FF'
    49.     __BADRAM H'07'-H'09', H'0D', H'13'-H'14', H'1B'-H'1E'
    50.     __BADRAM H'87'-H'89', H'8D', H'8F'-H'91', H'93'-H'97', H'9E'
    51.     __BADRAM H'105', H'107'-H'109', H'10C'-H'11F', H'150'-H'16F'
    52.     __BADRAM H'185', H'187'-H'189', H'18C'-H'1EF'
    53.  
    54. ;==========================================================================
    55. ;
    56. ;       Configuration Bits
    57. ;
    58. ;==========================================================================
    59.  
    60. _BODEN_ON                    EQU     H'3FFF'
    61. _BODEN_OFF                   EQU     H'3FBF'
    62. _CP_ALL                      EQU     H'03FF'
    63. _CP_75                       EQU     H'17FF'
    64. _CP_50                       EQU     H'2BFF'
    65. _CP_OFF                      EQU     H'3FFF'
    66. _DATA_CP_ON                  EQU     H'3EFF'
    67. _DATA_CP_OFF                 EQU     H'3FFF'
    68. _PWRTE_OFF                   EQU     H'3FFF'
    69. _PWRTE_ON                    EQU     H'3FF7'
    70. _WDT_ON                      EQU     H'3FFF'
    71. _WDT_OFF                     EQU     H'3FFB'
    72. _LVP_ON                      EQU     H'3FFF'
    73. _LVP_OFF                     EQU     H'3F7F'
    74. _MCLRE_ON                    EQU     H'3FFF'
    75. _MCLRE_OFF                   EQU     H'3FDF'
    76. _ER_OSC_CLKOUT               EQU     H'3FFF'
    77. _ER_OSC_NOCLKOUT             EQU     H'3FFE'
    78. _INTRC_OSC_CLKOUT            EQU     H'3FFD'
    79. _INTRC_OSC_NOCLKOUT          EQU     H'3FFC'
    80. _EXTCLK_OSC                  EQU     H'3FEF'
    81. _LP_OSC                      EQU     H'3FEC'
    82. _XT_OSC                      EQU     H'3FED'
    83. _HS_OSC                      EQU     H'3FEE'
    84.  
    85.     __CONFIG        _BODEN_ON & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _XT_OSC
    86.  
    87. ;==========================================================================
    88. ;       Variable Definition
    89. ;==========================================================================
    90. TIMER1      EQU H'20'       ;Used in delay routine
    91. TIMER2      EQU H'21'       ; " "   "  
    92. PATERN      EQU H'22'       ;Pattern data for effect's
    93.  
    94.         ORG 0       ;Reset vector address
    95.         GOTO    RESET       ;goto RESET routine when boot.
    96.  
    97.  
    98. ;       *********************************************
    99. ;       *  Example of a delay routine               *
    100. ;       *********************************************
    101.  
    102. DELAY_ROUTINE   MOVLW   D'255'         ;54 Generate approx 10mS delay at 4Mhz CLK
    103.                 MOVWF   TIMER2
    104. DEL_LOOP1       MOVLW   D'255'         ;60 
    105.                 MOVWF   TIMER1
    106. DEL_LOOP2       DECFSZ  TIMER1,F
    107.                 GOTO    DEL_LOOP2
    108.                 DECFSZ  TIMER2,F
    109.                 GOTO    DEL_LOOP1
    110.         RETLW   0
    111.  
    112.  
    113. ;          **********************************
    114. ;              **  RESET :  main boot routine  **
    115. ;              **********************************
    116.  
    117. RESET       MOVLW   B'00000111' ;Disable Comparator module's
    118.         MOVWF   CMCON
    119.         ;
    120.         BSF STATUS,RP0  ;Switch to register bank 1
    121.                     ;Disable pull-ups
    122.                     ;INT on rising edge
    123.                     ;TMR0 to CLKOUT
    124.                     ;TMR0 Incr low2high trans.
    125.                     ;Prescaler assign to Timer0
    126.                     ;Prescaler rate is 1:256
    127.         MOVLW   B'11010111' ;Set PIC options (See datasheet).
    128.         MOVWF   OPTION_REG  ;Write the OPTION register.
    129.         ;
    130.         CLRF    INTCON      ;Disable interrupts
    131.         MOVLW   B'11000000'
    132.         MOVWF   TRISB       ;RB7 & RB6 are inputs.
    133.                     ;RB5...RB0 are outputs.
    134.         MOVLW   B'11111111' ;all RA ports are inputs
    135.         MOVWF   TRISA
    136.         BCF STATUS,RP0  ;Switch Back to reg. Bank 0
    137.         CLRF    PORTB      
    138.         ;
    139.  
    140. EFFECT_1    MOVLW   B'00000001' ;Activate LD1 (RB0)
    141.         MOVWF   PORTB
    142.         CALL    DELAY_ROUTINE
    143.         MOVLW   B'00000010' ;Activate LD2 (RB1)
    144.         MOVWF   PORTB
    145.         CALL    DELAY_ROUTINE
    146.         MOVLW   B'00000100' ;Activate LD3 (RB2)
    147.         MOVWF   PORTB
    148.         CALL    DELAY_ROUTINE
    149.         MOVLW   B'00001000' ;Activate LD4 (RB3)
    150.         MOVWF   PORTB
    151.         CALL    DELAY_ROUTINE
    152.         MOVLW   B'00010000' ;Activate LD5 (RB4)
    153.         MOVWF   PORTB
    154.         CALL    DELAY_ROUTINE
    155.         MOVLW   B'00100000' ;Activate LD6 (RB5)
    156.         MOVWF   PORTB
    157.         CALL    DELAY_ROUTINE
    158.         GOTO    EFFECT_1
    159.         END
    160.  

    If i have breached any copyright thingumydoo-da s for releasing it i'm sorry :)
     
Loading...