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.

Writing code for a PIC 16f690

Status
Not open for further replies.

Kingdom Man

New Member
Hello, I dont know if I am posting in the right place or even asking the right questions, but here it is anyway. I am just starting to play around with learning how to program the PIC Microchip possessors, I a extremely small bit of code that I am trying to add a function to: what I want the code to do is turn one of my A Port pins to a input and then drive one of my Port C outputs high when the input is high. I hope this makes sense, as I am just learning here...
Here is some code I have been messing with, if anyone could help me with generating a new set of instructions for what i am wanting it to do, I am hoping that through looking at what you have done I can learn something!

#include <p16F690.inc>
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
org 0
Start:
BSF STATUS,RP0 ;select Register Page 1
clrf TRISC ;make make port C outputs
BCF STATUS,RP0 ;back to Register Page 0
movlw 1 ;turn on LED
movwf PORTC ;on port C

Goto $ ;wait
End
 
Hi,

I could give you the code to run what you want but you are at the stage where you must learn the basics yourself, without sounding mean.

You really need to use a book or tutorial to guide you though these early steps,
have a look at the Sticky at the top of this forum.
You can do a lot worse than Nigels site

Do you have a programmer/starter board - they usually have beginners tutorials with them ?
 
Hi,

Thats great, - but do come back on for help if you get stuck on a particular point - everyone does - beginner or experienced, we all have difficulty understanding Microchips wonderfully clear datasheets :confused:
 
Kingdom man do you have a pickit2 it has a small guide and code you can try that show
you how it all works you can get it here https://www.electro-tech-online.com/custompdfs/2009/07/Low20Pin20Count20User20Guide2051556a.pdf And if you download this file it will install the code for the lesson all you need to try it is four leds four 220 ohm resistors for the leds a button switch and a pot it has the circuit in the pdf
at the end you can use a bread board to set it up on if you don't have a pickit2 and LPC demo board https://ww1.microchip.com/downloads/en/DeviceDoc/Pk2-Starter Kit Lessons (b).zip Also take a look at the datasheet it shows you how to get started to
 
Last edited:
The code you have posted simply outputs a high on Port C bit 0. To read Port a and indicate the result on port C you could simply do,
Code:
Loop
		btfss	PORTA,0		;skip if port A high
		bcf	PORTC,0		;else set Port C low
		btfsc	PORTA,0		;skip if port A low
		bsf	PORTC,0		;else set Port C high
		goto	Loop		;repeat forever

However, this will not work on that particular chip as Port A has anologue pins. To turn off the ADC function you need to clear the ANSEL register that is located in bank 2.

The final code should look like,
Code:
#include	<p16F690.inc> 
		__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
		org	0
Start:
		bsf	STATUS,RP0	;select Register Page 1
		clrf	TRISC		;make make port C outputs
		bcf	STATUS,RP0	;back to Register Page 0
		bsf	STATUS,RP1	;page 2
		clrf	ANSEL		;turn off ADC
		bcf	STATUS,RP1	;back to page 0
		movlw	1		;turn on LED 
		movwf	PORTC		;on port C
Loop
		btfss	PORTA,0		;skip if port A high
		bcf	PORTC,0		;else set Port C low
		btfsc	PORTA,0		;skip if port A low
		bsf	PORTC,0		;else set Port C high
		goto	Loop		;repeat forever
		end

Whilst handing over code on a plate may be seen by some as bad practice, in the case of pics there are so many pitfalls that getting example code to play with is normally the best starting point.

Mike.
 
Many pitfalls that getting example code to play with is normally the best starting point.
That's for sure with a 16f690 PORTC is anologue to it's not a easy chip to setup. if you haven't use any pic much I was just going to post him some thing all most like that but with banksel for register switching
I'll post it any way He may have a pickit2 with the LPC demo Board
Code:
	#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
    banksel TRISA
    movlw   b'00001000'   	;Set RA3 as input
    movwf   TRISA         	;and set as outputs
    banksel TRISC         	;Bank 0
    clrf    TRISC		;make ouputs
    banksel PORTC
    clrf    PORTC
    goto    run
run:
     btfss  PORTA,3		;skip next if PORTA high
     bsf    PORTC,0	        ;set PORTC High
     btfsc  PORTA,3		;skip next if PORTA low
     bcf    PORTC,0	        ;set PORTC Low
     goto   run			;keep checking the switch
	
 end
This works with the LPC demo Board If you have one if not the circuit in my first post if you want to try the lessons
 
Last edited:
You guys are awesome! Thanks so much, I will keep studying all these example codes and also reading up on any other sources I can, I cant thank you enough for this help as I am very eager to learn. Also I am so new to this, I have no training and this is my first attempt to learn this. And yes I have the PIC Kit 2, any other advice on documents to read for learning this would be great!
 
Hey Guys, is there a list of all the commands for these Processors, like the command and a definition? Also How would I go about learning how to set an input on one of the pins that could watch a variable 0-5 v signal, and then trigger an output based on a certain voltage on the input pin? I'm guessing there is only a certain amount of resolution when monitoring a variable input? I guessing 0-255?? Let me know if I'm totally wrong, as I am very new to this stuff.
 
Hi guys,

I have just bought the pickit2 pic16f690 starter kit with the lpc demo board.

I have ran into a wall with getting the push button working. In the lessons that are included with the kit there is a reversible project that will or is supposed to reverse the 4 flashing leds direction with the push of the button. The button doesn't do a thing.

Also with the code above in post #8 supplied by be80be the button doesn't do anything either.

I have found though that when I jumper Vdd to RA3 it works! So, the pic must always be seeing a low on RA3 if the button is pressed or not. Has anyone ran into this problem? I am not sure what to do but it is bugging me. Help please.
 
Hi have you checked jumper JP5 if it is cut the switch will not work.
The jumper pulls up the RA3 to Vdd threw a 10K when the switch is pressed it shorts RA3
to ground
 
The jumper is installed. On the jumper the two pins are traced together. The potentiometer seems to work without problems as well.

Thanks for the quick response :)
 
Problem solved.

I was using mplab to program the chip and I guess it wasnt supplying high enough voltage for RA3.

Programming the chip with the other software is working like a charm.

Do you use mplab to program the pics or the other software?
 
I use mplab ide to write code in. But most times I use the pickit2 software for programming

What happens in MpLab is you don't release the chip from reset after programming.
It's holding Mclr Low and then your program will not run.
 
Last edited:
Hi guys,

I have just bought the pickit2 pic16f690 starter kit with the lpc demo board.

I have ran into a wall with getting the push button working....

I have found though that when I jumper Vdd to RA3 it works! So, the pic must always be seeing a low on RA3 if the button is pressed or not. Has anyone ran into this problem? I am not sure what to do but it is bugging me. Help please.

I was going to add this to your other post in the section, and then found your comments here.

There is a little procedure you can follow that will eliminate this problem with the PICkit2 and LPC board.

When you are in MPLAB IDE, and have your chip (16F690) and programmer(PICkit 2) selected, click on the toolbar box "programmer". In the pulldown list, at the bottom, is settings. Click there, and you will see a box like in the attachment to this post. Check the box named [3 state on "Release from Reset"]. You will only need to do this one time.

This should eliminate any further problems with using the pushbutton on the LPC board. Since you are using HiTech C, be sure to include MCLRDIS in your configuration statement.
 

Attachments

  • please release me.JPG
    please release me.JPG
    33.2 KB · Views: 461
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top