PIC12F675 programming help

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
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
 

tracecom

Joined Apr 16, 2010
3,944
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.
 

bance

Joined Aug 11, 2012
315
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.
 

Mark Sikkema

Joined Jul 27, 2013
17
Some template with code to make a LED flash on a PIC12F675.
Should give you a good start.
Rich (BB code):
;******************************************************************************
;   This file is a basic code template for object module code                 *
;   generation on the PIC12F675. 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 and linker (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: P12F675.INC                                              *
;                                                                             *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes:                                                                   *
;                                                                             *
;******************************************************************************

;------------------------------------------------------------------------------
; PROCESSOR DECLARATION
;------------------------------------------------------------------------------

     LIST      P=12F675              ; list directive to define processor
     #INCLUDE <P12F675.INC>          ; processor specific variable definitions

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the 
; .asm file. The lables following the directive are located in the respective 
; .inc file.  See the data sheet for additional information on configuration 
; word settings.
;
;------------------------------------------------------------------------------

    __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 

;------------------------------------------------------------------------------
; VARIABLE DEFINITIONS
;------------------------------------------------------------------------------

; example of using Shared Uninitialized Data Section
INT_VAR     UDATA_SHR   0x20   
W_TEMP      RES     1             ; variable used for context saving 
STATUS_TEMP RES     1             ; variable used for context saving
CNT1        RES     1             ; variable used for delay
CNT2        RES     1             ; variable used for delay
CNT3        RES     1             ; variable used for delay

;------------------------------------------------------------------------------
; EEPROM INITIALIZATION
;
; The 12F675 has 128 bytes of non-volatile EEPROM, starting at address 0x2100
; 
;------------------------------------------------------------------------------

DATAEE    CODE  0x2100
    DE    "MCHP"          ; Place 'M' 'C' 'H' 'P' at address 0,1,2,3

;------------------------------------------------------------------------------
; OSCILLATOR CALIBRATION VALUE
;------------------------------------------------------------------------------

OSC       CODE    0x03FF

; Internal RC calibration value is placed at location 0x3FF by Microchip as
; a 0xADDLW K instruction, where the K is a literal value to be loaded into 
; the OSCCAL register.  

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

RESET_VECTOR  CODE    0x0000  ; processor reset vector
        GOTO    START         ; go to beginning of program

;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

INT_VECTOR    CODE    0x0004  ; 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 PROGRAM
;------------------------------------------------------------------------------

MAIN_PROG     CODE

START

;------------------------------------------------------------------------------
; OSCCAL RESTORE (not required if internal OSC is not used)
;------------------------------------------------------------------------------

        errorlevel -302
        BSF     STATUS,RP0    ; set file register bank to 1 
        CALL    0x3FF         ; retrieve factory calibration value
        MOVWF   OSCCAL        ; update register with factory cal value 
        BCF     STATUS,RP0    ; set file register bank to 0
        errorlevel +302

;------------------------------------------------------------------------------
; PLACE USER PROGRAM HERE
;------------------------------------------------------------------------------
        BANKSEL TRISIO
        CLRF    TRISIO          ; set to output
        BANKSEL ANSEL
        CLRF     ANSEL          ; set to digital I/O

        BANKSEL GPIO
main_loop
        MOVLW   0
        MOVWF   GPIO            ; set pin

        CALL    delay_1s

        MOVLW   1<<GPIO4 
        MOVWF   GPIO            ; unset pin

        CALL    delay_1s

        GOTO    main_loop
        GOTO $

;------1s Delay Routine-------------
delay_1s: 
        MOVLW   d'5' 
        MOVWF   CNT1
LOOP1:
        MOVLW   d'255'
        MOVWF   CNT2
LOOP2:
        MOVLW   d'255'
        MOVWF   CNT3
LOOP3:
        DECFSZ  CNT3,F 
        GOTO    LOOP3
        DECFSZ  CNT2,F
        GOTO    LOOP2
        DECFSZ  CNT1,F
        GOTO    LOOP1
        RETURN

        END                       ; directive 'end of program'
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
 

Attachments

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
Hi,

I have written this:

Rich (BB code):
#include<12F675.h>

#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2
#define GP3 PIN_A3
#define GP4 PIN_A4
#define GP5 PIN_A5

#fuses MCLR,NOPROTECT,NOWDT,INTRC

#use delay (clock=4000000)

void main()
  {
      while(TRUE)
        {
            output_low(GP0);//Sets the starting output value as logic zero(0V)
             if(input(GP1)==1&&input(GP2)==1)
               {
                output_high(GP0);
                delay_ms(200);
                output_low(GP0);
               }            
             if(input(GP1)==1&&input(GP3)==1)
               {
                   output_high(GP0);
                   delay_ms(100);
                   output_low(GP0);
                   delay_ms(50);
                   output_high(GP0);
                   delay_ms(100);
                   output_low(GP0);
               }
               }
}

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

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
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
 

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
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
 

tshuck

Joined Oct 18, 2012
3,534
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?
 

tracecom

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

Rich (BB code):
' Name        : Sima Code.pbp
' Compiler    : PICBASIC PRO Compiler 3
' Assembler   : MPASM
' Target PIC  : 8-pin PIC12F675
' Hardware    : CRH Breadboard
' Oscillator  : 4MHz internal
' Description : PICBASIC PRO program to flash an LED in two different patterns
    ' depending upon the state of three input pins

Define OSCCAL_1K 1  ' Calibrate internal oscillator

' Define variables
LED var GPIO.0  ' LED pin
SW1 var GPIO.1  ' Switch 1
SW2 var GPIO.2  ' Switch 2
SW3 var GPIO.4  ' Switch 3

#config
    __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_ON & _BODEN_OFF & _CP_OFF & _CPD_OFF
#ENDCONFIG

   ANSEL = 0            ' Set all digital
   CMCON = 7            ' Analog comparators off
   TRISIO = %00011110   'Sets GPIO 1, 2, 3 & 4 as inputs; rest as outputs

Low LED                 ' Turn off LED connected to GPIO.0
  
mainloop:
    if (SW1 = 1) and (SW2 = 1) then gosub twoseconds
    if (SW1 = 1) and (SW3 = 1) then gosub twopulses
    Goto mainloop       ' Go back to mainloop

twoseconds:
    high LED    ' Turn on LED connected to GPIO.0
    Pause 2000  ' Delay for 2 seconds
    low LED     ' Turn off LED connected to GPIO.0
    return

twopulses:
    high LED    ' Turn on LED connected to GPIO.0
    pause 1000  ' Delay for 1 second  
    low LED     ' Turn off LED connected to GPIO.0
    pause 500   ' Delay for .5 seconds
    high LED    ' Turn on LED connected to GPIO.0
    pause 1000  ' Delay for 1 second 
    low LED     ' Turn off LED connected to GPIO.0
    return

End
 

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
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:

tracecom

Joined Apr 16, 2010
3,944
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
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.
 
Top