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.

delay in pic c (12f675)

Status
Not open for further replies.

srikanthind

New Member
hi everyone, could someone help me out pls. I am new to pic c, am writing a small writing to check the delay and interrupts. but, it showing error for delay and interrupt is not working. i tried with for loop as well, but after for loop, the instruction is going to interrupt.

could some one tell me where have i done wrong.
the code is like below:

C:
#include<pic.h>
#include<htc.h>
//#include <delay.h>

int i;

void interrupt Isr(void)

{
  if (GPIF)

   { 
        if(GPIO3==0)
          GPIO=0B001111111;
   }
   GPIF=0;
} 
    

void main(void)
{
  ANSEL = 0b00000000;
	OPTION = 0b11010001;
	CMCON = 0b00000000;
    TRISIO= 0B00001000;

     GIE = 1;
	T0IE = 1;
	T0IF = 0;
    GPIF=0;
  IOCB=GPIO3;
      
  //GPIO=0B00000100;

  Delay(50);

for(i=0; i<5000;i++)
{
}

for(i=0; i<50000;i++);

  GPIO=GREEN;                   

      while(1)
       {}
             
}
[MODNOTE]Please use the "#" code tag[/MODNOTE]
 
Last edited by a moderator:
You use the built in delay routines

Add
Code:
#define __XTAL_FREQ 4000000 // or what speed your crystal is

At the top of you code ( below the includes )


Then you can access

Code:
__delay_ms(int);
__delay_us(int);
 
thank you mate..
_delay is working but, _delay_ms(); is not working. ( am using pic 12f675)

and is there any maximum number we can use as int. bcz it showing error after certain number..

thank you
You use the built in delay routines

Add
Code:
#define __XTAL_FREQ 4000000 // or what speed your crystal is

At the top of you code ( below the includes )


Then you can access

Code:
__delay_ms(int);
__delay_us(int);
 
Code:
#include<pic.h>
#include<htc.h>

#define __XTAL_FREQ 4000000
//#include <delay.h>


#define GREEN 0b00000110


void interrupt Isr(void)

{
  if (GPIF)

   { 
        if(GPIO3==0)

          GPIO=0B001111111;

   }
   GPIF=0;


} 
    

void main(void)
{

  ANSEL = 0b00000000;
	OPTION = 0b11010001;
	CMCON = 0b00000000;
    TRISIO= 0B00001000;

     GIE = 1;
	//T0IE = 1;
	T0IF = 0;
    GPIF=0;
    IOCB=GPIO3; 
      
  //GPIO=0B00000100;

  __delay_ms(5);

  __delay_us(50);

  __delay_ms(200);

GPIO |= (1<<5);
GPIO &= ~(1<<7);
  GPIO=GREEN;                   

      while(1)
       {}
             
}

Show me your code!!! I'll take a looksee...
 
C:
#include<htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(0x31C4);
 
#define GREEN 0b00000110

void interrupt Isr(void) 
	{
	if (GPIF)
		{ 
        if(GPIO3==0)
			GPIO=0B001111111;
		}
	GPIF=0;
	} 

void main(void)
	{
	ANSEL = 0b00000000;
	OPTION_REG = 0b11010001;
	CMCON = 0b00000000;
	TRISIO= 0B00001000;
	GIE = 1;
	//T0IE = 1;
	T0IF = 0;
    GPIF=0;
    IOCB=GPIO3; 
	//GPIO=0B00000100;
	__delay_ms(5);
	__delay_us(50);
	 __delay_ms(200);
 
	GPIO |= (1<<5);
	GPIO &= ~(1<<7);
	GPIO=GREEN;                   
	while(1)
       {}
 	}

Works fine.... Sorry there is one underscore before XTAL... You forgot the config....

htc.h calls pic.h so there is no need to include it...

The interrupt isn't working because its not switched on.. When swithed on it fires every 32μS... Is that what you wanted???
 
I turned it on using toie=1, but still not working and i need the interrupt when the button pressed.

Thank you

C:
#include<htc.h>
#define _xtal_freq 4000000
__config(0x31c4);
 
#define green 0b00000110

void interrupt isr(void) 
	{
	if (gpif)
		{ 
        if(gpio3==0)
			gpio=0b001111111;
		}
	gpif=0;
	} 

void main(void)
	{
	ansel = 0b00000000;
	option_reg = 0b11010001;
	cmcon = 0b00000000;
	trisio= 0b00001000;
	gie = 1;
	//t0ie = 1;
	t0if = 0;
    gpif=0;
    iocb=gpio3; 
	//gpio=0b00000100;
	__delay_ms(5);
	__delay_us(50);
	 __delay_ms(200);
 
	gpio |= (1<<5);
	gpio &= ~(1<<7);
	gpio=green;                   
	while(1)
       {}
 	}

works fine.... Sorry there is one underscore before xtal... You forgot the config....

Htc.h calls pic.h so there is no need to include it...

The interrupt isn't working because its not switched on.. When swithed on it fires every 32μs... Is that what you wanted???
 
What pin is the button on?????

Is it on pin3 ???


ALSO you have to read the GPIO port so the flag will clear..

You need to look at the timer interrupt... Is way too fast for any thing else to occur.... Either slow it down or turn it off.

This line IOCB = GPIO3... isn't good

Just IOC3 = 1 is good.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top