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.

PIC16F627 read switch input from PORTA

Status
Not open for further replies.

r00x

New Member
Hi there,

Trying to get into this whole PIC programming thing but hit an immediate brick wall :D a frustratingly simple one at that, which might explain why it's largely undocumented.

I'm using a Microchip PIC16F627 and a Velleman K8048 Pic Programmer. I'm writing my software using the MPLAB IDE v8.40 and the Hi-Tech C Compiler, then just use the Velleman software to upload the HEX files.

My problem is I can't seem to read any input from RA0..RA3, the 16F627 pins on which the K8048 switches are wired to.

I've looked over the assembler demos that came with the K8048 in case I was missing a step or two - the datasheet for the PIC in question mentions analogue functionality on PORTA and I certainly want them to be running in digital. I wondered if everything was set to analogue by default, but there is nothing in the assembler demo files to suggest a change is needed.

In assembler, reading RA0 after setting TRISA to 0b11111111 was achieved using:

Code:
 BTFSC PORTA,SW1

Where SW1 was defined as hexadecimal '00'.

There's a lot of assembler there but I attempted to copy the gist of the input checking bits with the following bit of code. It's supposed to light an LED to make sure they're working, then light another if the switch input is detected:

Code:
#include <htc.h>
#define SW1 = 0x00;





void main(){
[INDENT]TRISA = 1;
TRISB=0;  //LEDs connected here

PORTB = 0x01; //just to prove LEDs operate (they do)

for(;;){
[INDENT]
if(PORTA == SW1){
[INDENT]
PORTB=0x02;
[/INDENT]
}
[/INDENT]
}
[/INDENT]
}


I'm clearly doing it wrong, I just can't figure out what right is. Is the comparison between incorrect datatypes? Do I address the ports (i.e. RA0) directly?(tried it) Been trying for hours now, reading datasheets, googling hi-tech compilers and reading RA0/PORTA/just about everything. I can't seem to find a single simple example of reading a switch input for a PIC16f6xx?!! :(

Anyway I've got PORTB outputs down and have been making pretty patterns for a while, but if anyone thinks it might be easier to use PORTB for input too, let me know! I only want two LED outputs and three switch inputs, I have about eight bidirectional ports to play with on PORTB if I understand the datasheet.

I'd be very grateful for any code snippets of a simple "light an LED when a button is pressed" program at this stage :)


EDIT: Just to clarify, the observed response of the PIC when running this program is to light that first LED and then sit there no matter how many switches I mash. ;)
 
Last edited:
You are comparing PortA to zero. PortA can never be zero as some bits are undefined (not selected as I/O) and read as one. If your button is on A2 you could do if((PORTA & 0b00000100) ==0) and that should work. If A3 then 0b00001000 etc.

Mike.
 
Hi Pommie, thanks for the response. I had a go at what you suggested but it still seems unresponsive. I know the Velleman board takes the pins high when the button is pressed, so the input I'm looking for would be a logical 1 I supposed.

The include file supplied by Hi-Tech winds up pointing to pic16f6x.h which defines the PORTA bits as "volatile bits", i.e:

Code:
volatile bit RA0    @ (unsigned)&PORTA*8+0;

Does this mean I could check RA0 directly as I attempted to before? If I were to check it directly, would I use a single bit 0b1/0b0 or hex? I'm beginning to think I need to explicitly set porta to digital since nothing seems to be working but I don't know how to do that in C.
 
Hi all,

Problem solved by entering the following in the code:

Code:
CMCON = 0x07;

Thus setting PORTA to digital :)

Porta pins can then be read via the PORTA variable or the individual pins when using the included header file, example:

Code:
if(RA0 == 0)
 
I have the same setup of the Velleman K8048 kit (PIC16F627) and am using MPlab with the Hitec C compiler. I am having problems getting the Config word right. How did you solve this problem?

error = No config word in file, or file does not match to the controller type.
 
In MPLAB you have the option of setting the configuration bits in the code or setting them in the menu system. In MPLAB look at your menu, select "configure" then "configuration bits". Or you could define them in code, here is an example for the PIC16F628a, there'll probably be some differences for the PIC16F627:
__CONFIG(HS & WDTDIS & MCLREN & PWRTDIS & BORDIS & LVPDIS & UNPROTECT);

Also, make sure you have selected the correct device before doing all this in "configure" "select device"
 
I'm not familiar with your PIC programmer so I can't say, but I'm assuming you can set your config in your programmer as well and those settings are matching the setting you picked in code or MPLAB.
 
Thanks a bunch

I have been trawling the internet and pic 16f627 spec and Hi-Tec Manual for the solution for this problem for 10 days! the complete program that works is for this chip is:
#include <htc.h>
#include <pic16f6x.h>
#define SW1 = 0x00;
# define _XTAL_FREQ 4000000
__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPEN & UNPROTECT);


void main()
{
CMCON = 0x07; //This sets all PORTA pins to digital!!!!!!
TRISA = 1;
TRISB = 0; //LEDs connected here
PORTB = 0x01; //just to prove LEDs operate (they do)
__delay_ms(1000);
for(;;)
{
//if(RA0 == 0)
if((PORTA & 0b00000001) !=0)
{
PORTB = 0x02;
}
}
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top