• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic 12f683 question

Status
Not open for further replies.

fonddeath55

New Member
Can anyone give me a code to flash an led in mplab. that works with hex. And if not but you have an led code that works with somthing else can you tell me how it works.
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
I think this will do it,
Code:
;************************************************* ***********  
; Processor: PIC12F683 at 4 MHz using intosc  
;************************************************* ***********  
		#include "P12F683.INC" 
;
		__config _FCMEN_OFF & _IESO_OFF & _PWRTE_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF & _INTOSCIO
;

	cblock	0x20
del1
del2	
	endc

	org 0x00


	clrf	GPIO		;initialise all outputs
	clrf	CMCON0		; CMCON0 set GP0, GP1 to analog input, GP2 to I/O and 
	banksel	0x80		;select upper register bank
	movlw	0x2f
	movwf	TRISIO		;make GP4 output
	banksel	0x00		;select lower register bank

Loop
	bsf	GPIO,4		;set output GP4 high
	call	delay		;call the delay subroutine
	bcf	GPIO,4		;set output GP4 low
	call	delay		;call the delay subroutine
	goto	Loop		;go back and do it again

delay
	decfsz	del1,f		;decrement the low order delay register
	goto	delay		;go back if it hasn't reached zero
	decfsz	del2,f		;decrement the high order delay register
	goto	delay		;go back if it hasn't reached zero
	return			;return to where the subroutine was called

	end
I think this was posted here some time ago.

Mike.
 

fonddeath55

New Member
Nvm i set up the circuit and works great i learned alot from that i need an example because i read abunch of tuts and reading the datasheet but the tuts were for different chips so i didnt understand it. Thanks
 

fonddeath55

New Member
I think this will do it,
Code:
;************************************************* ***********  
; Processor: PIC12F683 at 4 MHz using intosc  
;************************************************* ***********  
		#include "P12F683.INC" 
;
		__config _FCMEN_OFF & _IESO_OFF & _PWRTE_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF & _INTOSCIO
;

	cblock	0x20
del1
del2	
	endc

	org 0x00


	clrf	GPIO		;initialise all outputs
	clrf	CMCON0		; CMCON0 set GP0, GP1 to analog input, GP2 to I/O and 
	banksel	0x80		;select upper register bank
	movlw	0x2f
	movwf	TRISIO		;make GP4 output
	banksel	0x00		;select lower register bank

Loop
	bsf	GPIO,4		;set output GP4 high
	call	delay		;call the delay subroutine
	bcf	GPIO,4		;set output GP4 low
	call	delay		;call the delay subroutine
	goto	Loop		;go back and do it again

delay
	decfsz	del1,f		;decrement the low order delay register
	goto	delay		;go back if it hasn't reached zero
	decfsz	del2,f		;decrement the high order delay register
	goto	delay		;go back if it hasn't reached zero
	return			;return to where the subroutine was called

	end
I think this was posted here some time ago.

Mike.
how does?
movlw 0x2f
movwf TRISIO ;make GP4 output
0x2f make g4 an output?
 

Pommie

Well-Known Member
Most Helpful Member
If you look at the binary representation of 0x2f it is 00101111. The direction (in/out) of GP4 is controlled by bit 4 (in red) which is zero. Any bits that are 1 are inputs, zeros are outputs.

Mike.
 

fonddeath55

New Member
Why are their 8 bits then because theirs only 5 pins? and doesnt that mean bit 1+2 are outputs too? then would this work too? 0E "1110" or EF "11101111" And which one is bit 0 then?
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
In binary the bits are numbered 76543210. There are 8 because it is an 8 bit processor, in the TRIS register only the lower 6 are used (0 to 5). There are 6 I/O pins because MCLR can be used as an input (GP3).

Mike.
 

fonddeath55

New Member
So tryed changed the output to gpio,0 but i dont know what i did wronge can you find whats wronge with it?
And whats this do and how to i change it? clrf CMCON0 ; CMCON0 set GP0, GP1 to analog input, GP2 to I/O and

;************************************************* ***********
; Processor: PIC12F683 at 4 MHz using intosc
;************************************************* ***********
#include "P12F683.INC"
;
__config _FCMEN_OFF & _IESO_OFF & _PWRTE_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF & _INTOSCIO
;

cblock 0x20
del1
del2
endc

org 0x00


clrf GPIO ;initialise all outputs
clrf CMCON0 ; CMCON0 set GP0, GP1 to analog input, GP2 to I/O and
banksel 0x80 ;select upper register bank
movlw 0xef
movwf TRISIO ;make GP4 output
banksel 0x00 ;select lower register bank

Loop
bsf GPIO,0 ;set output GP0 high
call delay ;call the delay subroutine
bcf GPIO,0 ;set output GP0 low
call delay ;call the delay subroutine
goto Loop ;go back and do it again

delay
decfsz del1,f ;decrement the low order delay register
goto delay ;go back if it hasn't reached zero
decfsz del2,f ;decrement the high order delay register
goto delay ;go back if it hasn't reached zero
return ;return to where the subroutine was called

end
 
Last edited:

be80be

Well-Known Member
This should work
Code:
;************************************************* *********** 
; Processor: PIC12F683 at 4 MHz using intosc 
;************************************************* *********** 
	#include "P12F683.INC" 
;
	__config _FCMEN_OFF & _IESO_OFF & _PWRTE_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF & _INTOSCIO
;

	cblock 0x20
	del1
	del2 
	endc

	org 0x00


	clrf GPIO 	;initialise all outputs
	movlw	7
	movwf	CMCON0
        BANKSEL ANSEL     ;
        CLRF ANSEL          ;digital I/O	
	banksel TRISO 	;select upper register bank
	movlw b'00000000'
	movwf TRISIO 	;setall output 
	banksel GPIO 	;select lower register bank
	movlw b'00000000';set all low
	movwf GPIO
	

Loop
	bsf GPIO,0 	;set output GP0 high
	call delay 	;call the delay subroutine
	bcf GPIO,0 	;set output GP0 low
	call delay 	;call the delay subroutine
	goto Loop 	;go back and do it again

	delay
	decfsz del1,f 	;decrement the low order delay register
	goto delay 	;go back if it hasn't reached zero
	decfsz del2,f	;decrement the high order delay register
	goto delay 	;go back if it hasn't reached zero
	return     	;return to where the subroutine was called
end
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
You have 0xef which is 11101111 which makes GP4 output. To make GP0 output you need 11111110 which is 0xfe. The CMCON value is wrong and should be loaded with 7.

So your code should be,
Code:
;************************************************* ***********  
; Processor: PIC12F683 at 4 MHz using intosc  
;************************************************* ***********  
		#include "P12F683.INC" 
;
		__config _FCMEN_OFF & _IESO_OFF & _PWRTE_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF & _INTOSCIO
;

	cblock	0x20
del1
del2	
	endc

	org 0x00


	clrf	GPIO		;initialise all outputs
	movlw	7
	movwf	CMCON0		; CMCON0 set GP0, GP1 to analog input, GP2 to I/O and 
	banksel	0x80		;select upper register bank
	clrf	ANSEL
	movlw	0xfe
	movwf	TRISIO		;make GP4 output
	banksel	0x00		;select lower register bank

Loop
	bsf	GPIO,0		;set output GP4 high
	call	delay		;call the delay subroutine
	bcf	GPIO,0		;set output GP4 low
	call	delay		;call the delay subroutine
	goto	Loop		;go back and do it again

delay
	decfsz	del1,f		;decrement the low order delay register
	goto	delay		;go back if it hasn't reached zero
	decfsz	del2,f		;decrement the high order delay register
	goto	delay		;go back if it hasn't reached zero
	return			;return to where the subroutine was called

	end
When you post code, if you type [code] before it and [/code] after it then it will keep it's formatting.

Mike.
 
Last edited:

be80be

Well-Known Member
Mike you know he may need to turn off ANSEL with a
Code:
        BANKSEL ANSEL ;
        CLRF ANSEL      ;digital I/O
 

fonddeath55

New Member
Heres a switch program i made but all it does is turn the led on can you find out whats wronge with it?

Code:
;************************************************* ***********  
; Processor: PIC12F683 at 4 MHz using intosc  
;************************************************* ***********  
		#include "P12F683.INC" 
;
		__config _FCMEN_OFF & _IESO_OFF & _PWRTE_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _CP_OFF & _CPD_OFF & _INTOSCIO
;

	cblock	0x20
del1
del2	
	endc

	org 0x00


	clrf	GPIO		;initialise all outputs
	clrf	CMCON0		; CMCON0 set GP0, GP1 to analog input, GP2 to I/O and 
	banksel	0x80		;select upper register bank
	movlw	0x01
	movwf	TRISIO		;make GP4 output
	banksel	0x00		;select lower register bank

Loop
		Bsf GPIO,1 ;led on
	
                             ;into the w register and then on the port 
 

;****Check if the switch is closed


BTFSC            9AH,0             ;Get the value from PORT A
                                                       ;BIT 0.  If it is a zero
call                   delay                    ;a zero, carry on as normal.
                                                       ;If is is a 1, then add an 
                                                       ;extra delay routine 

;****Add a delay 

call       delay 

;****Delay finished, now turn the LED off****

Bsf GPIO,1

;****Check if the switch is still closed

BTFSC                        9AH,0         ;Get the value from PORT A
                                                               ;BIT 0.  If it is a zero,
call                       delay                ;carry on as normal.
                                                               ;If is a 1, then add an 
                                                               ;extra delay routine  

;****Add another delay**** 

call       delay 

;****Now go back to the start of the program

                   goto                 Loop                 ;go back to Start and turn LED on again 


	
    
delay
	decfsz	del1,f		;decrement the low order delay register
	goto	delay		;go back if it hasn't reached zero
	decfsz	del2,f		;decrement the high order delay register
	goto	delay		;go back if it hasn't reached zero
	return			;return to where the subroutine was called

	end
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
A simple way would be,
Code:
	clrf	GPIO		;initialise all outputs
	movlw	7
	movwf	CMCON0		;CMCON0 set all digital
	banksel	0x80		;select upper register bank
	clrf	ANSEL
	movlw	0x2f
	movwf	TRISIO		;make GP4 output
	bsf	WPU,0		;turn on Weak pull up
	banksel	0x00		;select lower register bank

Loop	btfss	GPIO,0
	bcf	GPIO,4
	btfsc	GPIO,0
	bsf	GPIO,4
	goto	Loop
This will turn on an LED connected to GP4 when a switch on GP0 goes low. Note that the code turns on the WPU on GP0.

Mike.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top