Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC12F629 Programming In XC8 Simple Hello World HELP

Status
Not open for further replies.

Andrew744

Newbie level 3
Joined
Dec 20, 2012
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,300
Dear Community,

Please help me in my time of need.

I'm trying to program a PIC12F629 micro controller to have 2 buttons, each button turns an led on when it's depressed. For some weird reason if I press both buttons down at the same time and let one go both led will stay on. When only one led should stay on.

Here's I schematic of my bread board. It's terrible so bear with me here.
23hkner.png



Code:
#include <htc.h> 
#define _XTAL_FREQ 4000000
 __CONFIG(MCLRE_OFF & CP_OFF & BOREN_OFF & WDTE_OFF & FOSC_INTRCIO & PWRTE_ON); 
 void main(){ 
    ANSEL=0; // Required for all digital I/O on PIC12F675 
    GPIO = 0b000000; // Preset output latches to all low 
    TRISIO = 0b001000; // All I/Os (except GPIO3 (/MCLR) as outputs 
    //CMCON =  0; //New For Input
while(1){ 

		if(GPIObits.GP2 == 1)
		{
			GPIObits.GP1 = 1;
		}
		else
		{
			GPIObits.GP1 = 0;
		}


		if(GPIObits.GP5 == 1)
		{
			GPIObits.GP4 = 1;
		}
		else
		{
			GPIObits.GP4 = 0;
		}
        }
}
 

It's hard to follow your "diagram", where does the 0.1uF cap go?
Probably, your issue is caused by lack of pulldown resistors.
 

kkkk... I'm sorry!!!
Try using a schematic program!!! Like Eagle or Kikad, etc...

U cannot use ANSEL in 12F629, use in 12F675!!!
U must use CMCON = 0x07; to disable comparators!!
GPIO3 OLNY WORK AS INPUT!!!!

I think is it:
PIC12F629.jpg

"
Code:
#include <htc.h>
#ifndef _XTAL_FREQ
 // Unless already defined assume 4MHz system frequency
 // This definition is required to calibrate __delay_us() and __delay_ms()
 #define _XTAL_FREQ 4000000
#endif

__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_OFF & CPD_OFF);
void main(void)
{
	OSCCAL	= 0x3ff;
	GPIO	= 0x00;
	CMCON	= 0x07;
	// BiTS  GPIO    76543210
	TRISIO	= 0b00101100; // GPIO3 ALWAYS IS INPUT!!! 1 = Input and 0 = Output
    while(1)
    {
		if(GPIO2 == 1)
		{
			GPIO1 = 1;
		}
		else
		{
			GPIO1 = 0;
		}
		if(GPIO5 == 1)
		{
			GPIO4 = 1;
		}
		else
		{
			GPIO4 = 0;
		}
	}
}
"
 
Last edited:
If you connect your switches to GND then you can use the internal pullups.

Keith
 

Thank You For The Replies Everyone. Much Appreciated. What do you mean debounce delays for the buttons?
 

For buttons you can't write just like this
Code:
 if(GPIO2 == 1)
		{
			GPIO1 = 1;
		}

You have to add delay like this.

Code:
 if(GPIO2 == 1)
	 {
                delay_ms(100);
                if(GPIO2 == 1)
	        {
		        GPIO1 = 1;
                }
	 }
 
For buttons you can't write just like this
Code:
 if(GPIO2 == 1)
		{
			GPIO1 = 1;
		}

You have to add delay like this.

Code:
 if(GPIO2 == 1)
	 {
                delay_ms(100);
                if(GPIO2 == 1)
	        {
		        GPIO1 = 1;
                }
	 }

Okay I will at that to my code. Thank You.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top