PDA

View Full Version : Input not working with PIC12F675



Henque19
- 14th January 2006, 19:49
I'm trying to get the hang of PIC programming, so I made a really simple circuit with two leds on port 1 and 2, and a switch on port 0 (connected so that pin 0 is high when the switch is open. The pin is connected to the positive terminal via a 10k resistor, and to ground via the switch).

The code segment I use is:
c VAR BYTE
...
c = PORTA
IF c.0 = 0 Then loop2

What have I done wrong? I have also tried the button command, and using pin 4 instead, still not working. Please help me!

Thanks
/Henrik

Melanie
- 14th January 2006, 21:05
> What have I done wrong?

Well, lets start by looking at the DATSHEET for the 12F675...

...there you will discover it hasn't got a PORTA, so...

c = PORTA

...isn't a valid statement.

Read the DATASHEET, find out what it's supposed to be, and try again... also don't forget (a) your TRIS statement, and (b) doublecheck to see if you need to turn off any any analogue functions on multiplexed pins...

Henque19
- 15th January 2006, 08:24
But I can use porta with outputs.

PORTA = 2
Pause 400
PORTA = 4
Pause 400

This makes the two leds turn on and off alternatively.

So if the pic doesnt have porta, why does this work?

I have defined pin 0 as input and pin 1 and 2 as outputs.

Input PORTA.0
Output PORTA.1
Output PORTA.2

And I'm a high school student, I don't have a degree in engineering, so I don't have a clue what the datasheet says.

Can you please explain that about disableing analogue functions on multiplexed pins?

Thanks again
/Henrik

mister_e
- 15th January 2006, 10:30
Why PORTA work?... O.K Let's open and look one section of the PBPPIC14.lib file located in the PBP folder.


.......
;************************************************* ***************
;* Fake PORT settings for 12C67x, 14C000 *
;************************************************* ***************

ifndef PORTA
PORTA EQU 5
endif
........

AND now if we open the 12F675 datasheet section 2.2.2 and we look the figure 2-2 on the left colums, we discover that GPIO is located @ 05H.... In the above snip, they create an alias to GPIO.

The usual way to write a program, is to use the correct name of the port... wich, in your case, is GPIO. your code will look like


GPIO = 2
Pause 400
GPIO = 4
Pause 400

--------------------------------------------------------------------------


I have defined pin 0 as input and pin 1 and 2 as outputs.

Input PORTA.0
Output PORTA.1
Output PORTA.2

It's one way to do it. You use the PBP function. You can also use the TRISIO register to do that... look section 3.0. your code will look


TRISIO.0=1 ' GPIO.0 As input
TRISIO.1=0 ' GPIO.1 As output
TRISIO.2=0 ' GPIO.2 As output


OR in one line


TRISIO=%111001 ' GPIO<2:1> as output, all the other as input

--------------------------------------------------------------------------


Can you please explain that about disableing analogue functions on multiplexed pins?

Look in section 6 and section 7. you'll discover the setting to use in CMCON and ANSEL register.
CMCON=%00000111
ANSEL=0

Now go in the FAQ and read the following thread wich discuss on HOW TO SET the PIC configuration fuses => http://www.picbasic.co.uk/forum/showthread.php?t=543

Do a search in this forum with 12F675 and you'll probably found some good example.

A really simple example here.



'
' Hardware configuration
' ======================
@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
' Internal Oscillator
' Disable watch dog timer
' Enable power up timer
' Disable MCLR function but leave this pin available as input
' Enable brown out detect

CMCON = %00000111 ' Disable analog comparator
ANSEL = 0 ' Disable A/D converter
TRISIO = %111001 ' Set GPIO<2:1> as output, the other as input

'
' Hardware connection
' ===================
SWITCH VAR GPIO.0
LED1 VAR GPIO.1
LED2 var GPIO.2

'
' ////////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'
' Program Start Here
'
' ////////////////////////////////|\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'
START:
IF SWITCH=1 THEN
LED1=1
LED2=0
ELSE
LED2=1
LED1=0
ENDIF
GOTO START

Henque19
- 15th January 2006, 11:27
Its working!
Thank you, you have given me a reason to live again :D

/Henrik