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.

to How generate an interrupt after every 2.5 seconds.And toggle a led connected at GP0 pin of PIC PIC12F675 microcontroller.

Status
Not open for further replies.

khatus

Member level 3
Member level 3
Joined
Oct 7, 2017
Messages
64
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
1,754
Hello guys i want to generate an interrupt after every 2.5 seconds.And toggle a led connected at GP0 pin of PIC PIC12F675 microcontroller. I am using 8 MHz crystal

Here is my code


Code:
/*******************************************************************************
Program for, generate an interrupt after every 2.5 seconds.And toggle a led connected at GP0 pin of PIC PIC12F675 using PIC12F675
Program Written by_ khatus
MCU:PIC12F675; X-Tal: 8MHz(internal). Compiler: mikroC pro for PIC v7.6.0
Date: 10-6-2021;
*******************************************************************************/



unsigned int cnt;  // cnt varialbe declaration(global variable)
void timer_initialization() //function for timer initialization
{

OPTION_REG =0b00000111;
TMR0 = 61;//Timer0 preload value
INTCON.GIE=1; //Enables all unmasked interrupts
INTCON.T0IE=1; //Enables the TMR0 interrupt

}
void Interrupt()
{
 if(TMR0IF_bit)// if TMR0IF bit is set
 {
   cnt++;     //increment the counter
   TMR0IF_bit=0;// if TMR0IF bit is cleared
   TMR0 = 61; //preload timer0 with value

  }
}

void main()
{

TRISIO.TRISIO0=0; //set I/O
GPIO.F0 = 0x00;  //Initialyy all GPIO pin is HIGF
cnt = 0;  // Initialize cnt
timer_initialization();
  do {
    if (cnt >= 100) // if count greater than or equal to 100
    {
      GPIO.F0 = ~GPIO.F0;// Toggle GPIOF0 LEDs
      cnt = 0;// Reset cnt
    }
  } while(1);

}

But the led did not blink I can not configure the problem. Can any bode solve it for me??
 

Hi,

Don't know if this is the problem, but the variable "cnt" is modified in interrupt, but checked in main.
Thus you should declare it as volatile.

Klaus

Added:
"61" is a constant and used at least twice in your code, thus it shouid be declared as constant at the beginning of the code.
So it's better readable, it has a meaningful name and you may even calculate it at compile time.
It's more comfortable, less prone for errors, and does not increase code nor processing power.
 
Last edited:

Check out the note on the top right hand column of Page 19 of the data sheet.
The device you are using has GP0 and AN0 sharing the same pin. The default settings for all Microchip devices that have analog pins is for the pin to be in analog mode on power-on-reset.
Also any pin in analog mode will always read as '0' digitally (check out Figure 3-1 of the data sheet). Therefore when you toggle the GP0 pin, it will always read as '0', you set it to '1' and so that is what the pin will always show. (Strangely enough a pin in analog (input) mode will still respond to the digital output unless the TRIS bit makes the pin a digital input as well.)
Therefore, use the ANSEL register to set GP0 to digital mode.
(By the way, this is a trap that many new users of Microchip MCUs fall into.)
I agree with the above comment - 'cnt' must be declared volatile; read up on the purpose of the 'volatile' attribute if you don't understand why, or come back here with your question.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top