Continue to Site

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.

  • 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.

help with programing 16f690

Status
Not open for further replies.

Kingdom Man

New Member
Hello all! I am just a new beginner and am very exited about all this programming, I have been working through some problems and have made some success on my own but now I am looking for some help.
Here is my code, I know its super simple to you guys, but like I said Im learning...

#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
org 0

start
banksel PORTA
clrf PORTA ;Init PORTA
banksel ANSEL ;Bank 2
clrf ANSEL ;digital I/O
bsf STATUS,RP0 ;Select register page 1
clrf TRISC ;set I/O pins on PORTC to output
movlw b'00000001'
movwf TRISA ;set I/O pin RA0 to input
bcf STATUS,RP0 ;back to register page 0
movlw b'00001111'
movwf PORTC ;set i/o pins RC0-RC3 high
goto run
run:
btfsc PORTA,0 ;skip next if PORTA high
movlw b'00001111'
movwf PORTC ;set all portc high
btfss PORTA,0 ;skip next if PORTA low
movlw b'00000000'
movwf PORTC ;set i/o pins RC0-RC3 low
goto run ;keep checking input status

end

this code works for setting all my output pins high on port c when the input on port a is in a certain state High or low, but what I want to learn how to do now is set the input pin to monitor a thermistor, and I want to be able to learn how to set different setpoints for my output. for instance if I want to trigger my outputs high when the input see`s 2 volts how do i do this?
 
One way is to use one of the analog comparators and the programmable voltage reference, the other is to measure it with the A/D converter.
The comparator is how I'd do it.
 
Kingdom Man

I am still waiting for you to answer my email - I have said I will help you with this, but I can't help if you don't or wont talk to me!
 


So I have been going over what you guys discussed there, and I am wondering how I can set up the code to set the output high at 1v and set it low again at .8v. I want to be able to set up my trigger points kinda like you did in your code, but now quite sure what the commands are for what I want to do. I only need to learn how to trigger one output based on an input voltage, similar to what you have done in your code
PHP:
	CBLOCK 		0x020
	endc
	#define _1VOLT 0x33
	#define _2VOLT 0x66
	#define _2_5VOLT 0x7F
	#define _3VOLT 0x99
	#define _4VOLT 0xCC
	#define _5VOLT 0xFF
I'm guessing there would be a better way for me to set up the voltage scaling if Im just wanting to have two set points in the voltage range??
 
Last edited:
Kingdom Man

I am still waiting for you to answer my email - I have said I will help you with this, but I can't help if you don't or wont talk to me!

Hey how are you, I have emailed you twice, I dont know what happened? If you have another email let me know or we can talk on private message...
 
You would check for .8 volts like this
#define _.8VOLT 0x28
It's Decimal 5.5 for 10th of volt to 255 for 5volts
 
You would check for .8 volts like this
#define _.8VOLT 0x28
It's Decimal 5.5 for 10th of volt to 255 for 5volts

Hey do you think you could give me a full program that would set up all the ports and adc converter and whatever else is needed to monitor an input and trigger an output? I am still very new, and I do feel that I am learning as I have successfully done some simple programs, but I am not good enough to know what to do with what you gave me there.
If you could just write a program that would simply set my output pin high when it hits any given voltage (eg: .8 volts) I could then go through all the commands and see what you did, and learn all the meanings of the different commands. I would really appreciate the help, thanks
Dan
 
I post it for the 16f690 mostly all you do is the include and some fuses
 
Here have a look I didn't try it but it should work
Code:
	list		p=16f690		; list directive to define processor
	#include	<P16F690.inc>		; processor specific variable definitions
	errorlevel -302 ; Turn off banking message
                            ; known tested (good) code

	__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)


	CBLOCK 		0x020
	endc
	#define _.8VOLT 0x28
	#define _1VOLT 0x33
	#define _2VOLT 0x66
	#define _2_5VOLT 0x7F
	#define _3VOLT 0x99
	#define _4VOLT 0xCC
	




	ORG			0x000		; processor reset vector
  	goto		init		; go to beginning of program


	ORG			0x004	    ; interrupt vector location

init:
     bsf       STATUS,RP0     ; select Register Page 1
     movlw     0xFF
     movwf     TRISA          ; Make PortA all input
     clrf      TRISC          ; Make PortC all output
     movlw     0x10           ; A2D Clock Fosc/8
     movwf     ADCON1
     bcf       STATUS,RP0     ; back to Register Page 0

     bcf       STATUS,RP0     ; address Register Page 2
     bsf       STATUS,RP1     
     movlw     0xFF           ; we want all Port A pins Analog
     movwf     ANSEL
     bcf       STATUS,RP0     ; address Register Page 0
     bcf       STATUS,RP1
     clrf	   ADCON0
     movlw     0x01
     movwf     ADCON0         ; configure A2D for Channel 0 
MainLoop:
     call	adcdelay		  ;delay to charge cap
     bsf       ADCON0,GO      ; start conversion
     btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
     goto      $-1
	
    movf     ADRESH,w       ; Copy the display to the LEDs
	movwf	 PORTC
	goto      MainLoop
    movlw 	_1VOLT
    subwf	 ADRESH, w
	BNC 	LessThan1V                ; Branch if less than 1V

	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_2VOLT
    subwf	 ADRESH, w
	BNC 	LessThan2V                ; Branch if Between 1V and 2V
	
	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_2_5VOLT
    subwf 	ADRESH, w
	BNC 	LessThan2_5V             ; Branch if Between 2V and 2.5V

	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_3VOLT
    subwf	 ADRESH, w
	BNC 	LessThan3V                ; Branch if Between 2.5V and  3V
	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_4VOLT
    subwf 	ADRESH, w
	BNC 	LessThan4V                ; Branch if Between 3V and 4V
	movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_.8VOLT
    subwf 	ADRESH, w
	BNC 	LessThan.8V                ; Branch if Between 3V and 4V
    goto      MainLoop
LessThan.8V:
	bsf 	PORTC,0
	return 
LessThan1V:
	bsf		PORTC,1
	return
LessThan2V:
	bsf		PORTC,2					; lights led thats in volt range 
	return
LessThan2_5V: 
	bsf		PORTC,3 
	return
LessThan3V:
    bsf		PORTC,4	  
	return	
LessThan4V:
	bsf		PORTC,5
	return


   
adcdelay:
	 nop					  ;1 cycle

	 return					  ;4 cycles (including call)
	
     end
 
Thanks Burt, but is there any way you can show me how to trigger one output with one variable voltage input? this is two complicated for me, I need to see just the basics of one output being triggered, hope you understand?
 
Here have a look I didn't try it but it should work

I did some playing around with the code you gave me, and I now understand how to use this code to set different on and off points:
PHP:
 #include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

	CBLOCK 		0x020
	endc
	#define _.8VOLT 0x28
	#define _1VOLT 0x33
	#define _2VOLT 0x66
	#define _2_5VOLT 0x7F
	#define _3VOLT 0x99
	#define _4VOLT 0xCC
	
	ORG			0x000		; processor reset vector
  	goto		init		; go to beginning of program


	ORG			0x004	    ; interrupt vector location

init:
     bsf       STATUS,RP0     ; select Register Page 1
     movlw     0xFF
     movwf     TRISA          ; Make PortA all input
     clrf      TRISC          ; Make PortC all output
     movlw     0x10           ; A2D Clock Fosc/8
     movwf     ADCON1
     bcf       STATUS,RP0     ; back to Register Page 0

     bcf       STATUS,RP0     ; address Register Page 2
     bsf       STATUS,RP1     
     movlw     0xFF           ; we want all Port A pins Analog
     movwf     ANSEL
     bcf       STATUS,RP0     ; address Register Page 0
     bcf       STATUS,RP1
     clrf	   ADCON0
     movlw     0x01
     movwf     ADCON0         ; configure A2D for Channel 0 
MainLoop:
     call	adcdelay		  ;delay to charge cap
     bsf       ADCON0,GO      ; start conversion
     btfss     ADCON0,GO      ; this bit will change to zero when the conversion is complete
     goto      $-1
	
    movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_.8VOLT
    subwf	 ADRESH, w
	BNC 	LessThan.8VOLT                ; Branch if less than 1V


    movlw    _1VOLT
    subwf	 ADRESH, w
	BNC 	 LessThan1VOLT                ; Branch if Between 1V and 2V
	

    movlw 	_2VOLT
    subwf 	ADRESH, w
	BNC 	LessThan2VOLT             ; Branch if Between 2V and 2.5V


    movlw 	_2_5VOLT
    subwf	 ADRESH, w
	BNC 	LessThan2_5VOLT               ; Branch if Between 2.5V and  3V


    movlw   _3VOLT
    subwf 	ADRESH, w
	BNC 	LessThan3VOLT               ; Branch if Between 3V and 4V


    movlw 	_4VOLT
    subwf 	ADRESH, w
	BNC 	LessThan4VOLT                ; Branch if Between 3V and 4V
    goto      MainLoop

LessThan.8VOLT:
	bcf 	PORTC,0
	return 
LessThan1VOLT:
	bsf		PORTC,0
	return
LessThan2VOLT:
	bcf		PORTC,0					; lights led thats in volt range 
	return
LessThan2_5VOLT: 
	bsf		PORTC,0
	return
LessThan3VOLT:
    bsf		PORTC,0	  
	return	
LessThan4VOLT:
	bsf		PORTC,0
	return


   
adcdelay:
	 nop					  ;1 cycle

	 return					  ;4 cycles (including call)
	
     end

But now I am trying to figure out how to set the output to stay high until it gets to the desired off voltage input, for instance: let say I want to trigger a fan to come on at the input of 1v and I want the fan to go off when the input reaches .8v how could this be done?
 
With this part
Code:
 movf     ADRESH,w       ; Copy the display to the LEDs
    movlw     _.8VOLT
    subwf     ADRESH, w
    BNC     LessThan.8VOLT                ; Branch if less than 1V 
LessThan.8VOLT:
    bcf     PORTC,0          [COLOR="Red"] ;set to bsf to turn off [/COLOR] ;This was turning on a led you can 
    return             ;set to do what you want
Just change the BCF to BSF to turn off the fan at .8 volts on PORTC,0
and set the 1volt to turn on PORTC,0
 
Last edited:
Thanks so much for taking the time to help me Burt! I will go through this and see if I can make some sense of it. BTW what does BNC do? what does Branch if less than mean?
also what does the command: subwf mean?
 
I cant seem to figure out a way to keep my output high, what I mean is that when I put a BSF @ 1v it sets the pin high at 1v but then as soon as I go anybit lower than 1v on the input it clears the bit and the pin goes low. What I am trying to achieve is this: when the input hits 1v fan turns on, and stays on until it gets below say, .8volts
Can I write somthing like this:
PHP:
 movf     ADRESH,w       ; Copy the display to the LEDs
    movlw 	_.8VOLT
    subwf	 ADRESH, w
	BNC 	MoreThan.8VOLT                ; Branch if less than 1V
MoreThan.8VOLT:
	bsf 	PORTC,0
	return
I tried this but it doesn't seem to work for me??
 
Last edited:
Just use the two LessThan.8VOLT: and LessThan1VOLT: or you have to set all above 1 volt to turn on the same pin
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top