PIC12F675 programming help

Discussion in 'Programmer's Corner' started by koyaelektronic, Aug 1, 2013.

  1. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Hello forumers,

    I have been following this forum for a while now and I decided to join.
    Now my problem that I need the help with.
    I have a PIC12F675,and I need to program it to this:
    1 OUTPUT
    3 INPUTS
    IF INPUT 1 and INPUT 2 receive positive impulse(at the same time),OUTPUT releases 1 positive impulse that lasts 2 seconds.
    IF INPUT 1 and INPUT 3 receive positive impulse(at the same time),OUTPUT releases 2 positive impulses each lasting 1 second.
    Now,I have reached the limit of my knowledge of PIC circuits.I have built PICKit 2 clone,and I have installed PICKit software and MPLAB IDE.
    I have inserted the correct header,and now i need the code.
    For coding,i know nothing.
    So i was wondering if there are any members that can help me finish this little project of mine?

    Kind regards,
    Sima
     
  2. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    What programming language do you intend to use? Assembler, I guess.
     
  3. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Yes,assembler :D
     
  4. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    I can't code in assembler, but if all else fails, I could write the code in basic, and compile it to hex. Let's see if some assembler person comes along.
     
  5. bance

    Member

    Aug 11, 2012
    315
    34
    What you have asked for amounts to the first three lessons that would be learned as a complete beginner to PIC programming. Since you seem to have gone to an awful lot of trouble, building your own programmer and setting up a toolchain, etc. I suggest you spend an hour or two to work on some tutorials, here is a couple of links:-

    Gooligum

    Nigel Goodwin

    You are only trying to read a port. set a pin high and use a wait state, very simple to do....

    Good luck and have fun.

    HTH Steve.
     
  6. Mark Sikkema

    New Member

    Jul 27, 2013
    17
    2
    Some template with code to make a LED flash on a PIC12F675.
    Should give you a good start.
    Code ( (Unknown Language)):
    1. ;******************************************************************************
    2. ;   This file is a basic code template for object module code                 *
    3. ;   generation on the PIC12F675. This file contains the                       *
    4. ;   basic code building blocks to build upon.                                 *
    5. ;                                                                             *
    6. ;   Refer to the MPASM User's Guide for additional information on             *
    7. ;   features of the assembler and linker (Document DS33014).                  *
    8. ;                                                                             *
    9. ;   Refer to the respective PIC data sheet for additional                     *
    10. ;   information on the instruction set.                                       *
    11. ;                                                                             *
    12. ;******************************************************************************
    13. ;                                                                             *
    14. ;    Filename:      xxx.asm                                                   *
    15. ;    Date:                                                                    *
    16. ;    File Version:                                                            *
    17. ;                                                                             *
    18. ;    Author:                                                                  *
    19. ;    Company:                                                                 *
    20. ;                                                                             *
    21. ;                                                                             *
    22. ;******************************************************************************
    23. ;                                                                             *
    24. ;    Files required: P12F675.INC                                              *
    25. ;                                                                             *
    26. ;                                                                             *
    27. ;                                                                             *
    28. ;******************************************************************************
    29. ;                                                                             *
    30. ;    Notes:                                                                   *
    31. ;                                                                             *
    32. ;******************************************************************************
    33.  
    34. ;------------------------------------------------------------------------------
    35. ; PROCESSOR DECLARATION
    36. ;------------------------------------------------------------------------------
    37.  
    38.      LIST      P=12F675              ; list directive to define processor
    39.      #INCLUDE <P12F675.INC>          ; processor specific variable definitions
    40.  
    41. ;------------------------------------------------------------------------------
    42. ;
    43. ; CONFIGURATION WORD SETUP
    44. ;
    45. ; The 'CONFIG' directive is used to embed the configuration word within the
    46. ; .asm file. The lables following the directive are located in the respective
    47. ; .inc file.  See the data sheet for additional information on configuration
    48. ; word settings.
    49. ;
    50. ;------------------------------------------------------------------------------
    51.  
    52.     __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
    53.  
    54. ;------------------------------------------------------------------------------
    55. ; VARIABLE DEFINITIONS
    56. ;------------------------------------------------------------------------------
    57.  
    58. ; example of using Shared Uninitialized Data Section
    59. INT_VAR     UDATA_SHR   0x20  
    60. W_TEMP      RES     1             ; variable used for context saving
    61. STATUS_TEMP RES     1             ; variable used for context saving
    62. CNT1        RES     1             ; variable used for delay
    63. CNT2        RES     1             ; variable used for delay
    64. CNT3        RES     1             ; variable used for delay
    65.  
    66. ;------------------------------------------------------------------------------
    67. ; EEPROM INITIALIZATION
    68. ;
    69. ; The 12F675 has 128 bytes of non-volatile EEPROM, starting at address 0x2100
    70. ;
    71. ;------------------------------------------------------------------------------
    72.  
    73. DATAEE    CODE  0x2100
    74.     DE    "MCHP"          ; Place 'M' 'C' 'H' 'P' at address 0,1,2,3
    75.  
    76. ;------------------------------------------------------------------------------
    77. ; OSCILLATOR CALIBRATION VALUE
    78. ;------------------------------------------------------------------------------
    79.  
    80. OSC       CODE    0x03FF
    81.  
    82. ; Internal RC calibration value is placed at location 0x3FF by Microchip as
    83. ; a 0xADDLW K instruction, where the K is a literal value to be loaded into
    84. ; the OSCCAL register.  
    85.  
    86. ;------------------------------------------------------------------------------
    87. ; RESET VECTOR
    88. ;------------------------------------------------------------------------------
    89.  
    90. RESET_VECTOR  CODE    0x0000  ; processor reset vector
    91.         GOTO    START         ; go to beginning of program
    92.  
    93. ;------------------------------------------------------------------------------
    94. ; INTERRUPT SERVICE ROUTINE
    95. ;------------------------------------------------------------------------------
    96.  
    97. INT_VECTOR    CODE    0x0004  ; interrupt vector location
    98.         MOVWF   W_TEMP        ; save off current W register contents
    99.         MOVF    STATUS,w      ; move status register into W register
    100.         MOVWF   STATUS_TEMP   ; save off contents of STATUS register
    101.  
    102. ; isr code can go here or be located as a call subroutine elsewhere
    103.  
    104.         MOVF    STATUS_TEMP,w ; retrieve copy of STATUS register
    105.         MOVWF   STATUS        ; restore pre-isr STATUS register contents
    106.         SWAPF   W_TEMP,f
    107.         SWAPF   W_TEMP,w      ; restore pre-isr W register contents
    108.         RETFIE                ; return from interrupt
    109.  
    110. ;------------------------------------------------------------------------------
    111. ; MAIN PROGRAM
    112. ;------------------------------------------------------------------------------
    113.  
    114. MAIN_PROG     CODE
    115.  
    116. START
    117.  
    118. ;------------------------------------------------------------------------------
    119. ; OSCCAL RESTORE (not required if internal OSC is not used)
    120. ;------------------------------------------------------------------------------
    121.  
    122.         errorlevel -302
    123.         BSF     STATUS,RP0    ; set file register bank to 1
    124.         CALL    0x3FF         ; retrieve factory calibration value
    125.         MOVWF   OSCCAL        ; update register with factory cal value
    126.         BCF     STATUS,RP0    ; set file register bank to 0
    127.         errorlevel +302
    128.  
    129. ;------------------------------------------------------------------------------
    130. ; PLACE USER PROGRAM HERE
    131. ;------------------------------------------------------------------------------
    132.         BANKSEL TRISIO
    133.         CLRF    TRISIO          ; set to output
    134.         BANKSEL ANSEL
    135.         CLRF     ANSEL          ; set to digital I/O
    136.  
    137.         BANKSEL GPIO
    138. main_loop
    139.         MOVLW   0
    140.         MOVWF   GPIO            ; set pin
    141.  
    142.         CALL    delay_1s
    143.  
    144.         MOVLW   1<<GPIO4
    145.         MOVWF   GPIO            ; unset pin
    146.  
    147.         CALL    delay_1s
    148.  
    149.         GOTO    main_loop
    150.         GOTO $
    151.  
    152. ;------1s Delay Routine-------------
    153. delay_1s:
    154.         MOVLW   d'5'
    155.         MOVWF   CNT1
    156. LOOP1:
    157.         MOVLW   d'255'
    158.         MOVWF   CNT2
    159. LOOP2:
    160.         MOVLW   d'255'
    161.         MOVWF   CNT3
    162. LOOP3:
    163.         DECFSZ  CNT3,F
    164.         GOTO    LOOP3
    165.         DECFSZ  CNT2,F
    166.         GOTO    LOOP2
    167.         DECFSZ  CNT1,F
    168.         GOTO    LOOP1
    169.         RETURN
    170.  
    171.         END                       ; directive 'end of program'
    172.  
    173.  
    Get the datasheet, use MPLAB SIMulator, and put some breakpoints in the code. Like that you should be able to get the hang of this PIC.
    Be carefull with the 'Read Modify Write' (RMW) problem.

    Attached is is the complete workspace for MPLAB v8.91

    Good luck, Mark
     
  7. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Hi,

    I have written this:

    Code ( (Unknown Language)):
    1.  
    2. #include<12F675.h>
    3.  
    4. #define GP0 PIN_A0
    5. #define GP1 PIN_A1
    6. #define GP2 PIN_A2
    7. #define GP3 PIN_A3
    8. #define GP4 PIN_A4
    9. #define GP5 PIN_A5
    10.  
    11. #fuses MCLR,NOPROTECT,NOWDT,INTRC
    12.  
    13. #use delay (clock=4000000)
    14.  
    15. void main()
    16.   {
    17.       while(TRUE)
    18.         {
    19.             output_low(GP0);//Sets the starting output value as logic zero(0V)
    20.              if(input(GP1)==1&&input(GP2)==1)
    21.                {
    22.                 output_high(GP0);
    23.                 delay_ms(200);
    24.                 output_low(GP0);
    25.                }            
    26.              if(input(GP1)==1&&input(GP3)==1)
    27.                {
    28.                    output_high(GP0);
    29.                    delay_ms(100);
    30.                    output_low(GP0);
    31.                    delay_ms(50);
    32.                    output_high(GP0);
    33.                    delay_ms(100);
    34.                    output_low(GP0);
    35.                }
    36.                }
    37. }
    38.  

    And it doesnt work,my led at output is always on.
    Am i missing something
     
    Last edited by a moderator: Aug 2, 2013
  8. bance

    Member

    Aug 11, 2012
    315
    34
    That's 'C'......
     
  9. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Yep,switched to C because assembler is too difficult...
     
  10. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    Are you sure there's no hardware problem? Post your schematic.
     
  11. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Where's the initialization code? Oscillator selection, port direction? peripheral setup (if any)?
     
  12. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Nope no hardware problem,it just skips the IF statements,that is the source of my problem,it works the same with or without if statements
     
  13. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    blueroom,

    thats exactly what i need help with,i cant comprehend how to define ports to be input or output,and how to monitor the value that's coming to them

    regards
     
  14. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    could anyone atleast modify my code and put me on the right track?
    Basically what i am trying to achieve is:
    if 2 inputs(lets say GP1 and GP2) get +5V pulse on them,they trigger the first IF statement.
    next combination is GP1 and GP3,and they trigger the second IF statement.
    now i dont know how to express myself,and how to write IF statements for my needs,thats what I am basically trying to find out.
    In detail i am trying to:
    1)Impulse +5V on both GP1 and GP2 at the same time,triggers the Output impulse +5V on GP0 which lasts 2 seconds.
    2)Impulse +5V on both GP1 and GP3 at the same time,triggers 2 +5V impulses on GP0 which lasts 1 second each with half a second pause in between them.

    kind regards,
    sima
     
  15. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    What compiler is this? MikroC?

    Why don't you want to learn how to to this? Is this a one-off and you never expect to use PICs again?
     
  16. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    hi,

    nope this is ccs c compiler.
    Im just not into programming,so it will be one off for now.

    kind regards,
    sima
     
  17. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    I don't write C either, but here is some PicBasicPro code that runs.

    Code ( (Unknown Language)):
    1. ' Name        : Sima Code.pbp
    2. ' Compiler    : PICBASIC PRO Compiler 3
    3. ' Assembler   : MPASM
    4. ' Target PIC  : 8-pin PIC12F675
    5. ' Hardware    : CRH Breadboard
    6. ' Oscillator  : 4MHz internal
    7. ' Description : PICBASIC PRO program to flash an LED in two different patterns
    8.     ' depending upon the state of three input pins
    9.  
    10. Define OSCCAL_1K 1  ' Calibrate internal oscillator
    11.  
    12. ' Define variables
    13. LED var GPIO.0  ' LED pin
    14. SW1 var GPIO.1  ' Switch 1
    15. SW2 var GPIO.2  ' Switch 2
    16. SW3 var GPIO.4  ' Switch 3
    17.  
    18. #config
    19.     __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_ON & _BODEN_OFF & _CP_OFF & _CPD_OFF
    20. #ENDCONFIG
    21.  
    22.    ANSEL = 0            ' Set all digital
    23.    CMCON = 7            ' Analog comparators off
    24.    TRISIO = %00011110   'Sets GPIO 1, 2, 3 & 4 as inputs; rest as outputs
    25.  
    26. Low LED                 ' Turn off LED connected to GPIO.0
    27.  
    28. mainloop:
    29.     if (SW1 = 1) and (SW2 = 1) then gosub twoseconds
    30.     if (SW1 = 1) and (SW3 = 1) then gosub twopulses
    31.     Goto mainloop       ' Go back to mainloop
    32.  
    33. twoseconds:
    34.     high LED    ' Turn on LED connected to GPIO.0
    35.     Pause 2000  ' Delay for 2 seconds
    36.     low LED     ' Turn off LED connected to GPIO.0
    37.     return
    38.  
    39. twopulses:
    40.     high LED    ' Turn on LED connected to GPIO.0
    41.     pause 1000  ' Delay for 1 second  
    42.     low LED     ' Turn off LED connected to GPIO.0
    43.     pause 500   ' Delay for .5 seconds
    44.     high LED    ' Turn on LED connected to GPIO.0
    45.     pause 1000  ' Delay for 1 second
    46.     low LED     ' Turn off LED connected to GPIO.0
    47.     return
    48.  
    49. End
     
    koyaelektronic likes this.
  18. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Thanks a bunch man,is the code the same for PIC12F629?
    I have just tried it on F629 and it returns syntax error in line where ANSEL=0;
    I am out of F675's,i just broken the last one,while trying the code i written in C
     
    Last edited: Aug 2, 2013
  19. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    No, it would have to be modified for a 12F629. How do you plan to compile it?

    What happened to the 12F675? I thought there were no hardware errors.
     
  20. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Well,my battery went out on my laptop,and it heated up.Now it's unusable.No hardware errors.
     
Loading...