PIC - Help With 16f690 Blinking LED in MPLAB

Thread Starter

JBM

Joined Feb 4, 2016
15
Hello people
I am new to pic programming
can anyone help me with writing code for led blinking using interrupts and timers for pic16f690 in mplab.
I am using a 15Mhz external oscillator.
 

nerdegutta

Joined Dec 15, 2009
2,684
MPLAB is the IDE you work in. Which compiler are you planning to use? And with that question we want to know if you are planning to program in C or Assembly. Which programmer do you have? PICkit2 or PICkit3 or some other. What does you breadboard or schematic look like?

and its an assignment
... and I'm moving this to Homework. :)
 

Thread Starter

JBM

Joined Feb 4, 2016
15
MPLAB is the IDE you work in. Which compiler are you planning to use? And with that question we want to know if you are planning to program in C or Assembly. Which programmer do you have? PICkit2 or PICkit3 or some other. What does you breadboard or schematic look like?


... and I'm moving this to Homework. :)
I am using XC8 compiler and planning to programm with C and using ICD8 programmer and this is the programm i wrote for LED blinking.

C:
// CONFIG
#pragma config FOSC = HS  // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA4/OSC2/CLKOUT and RA5/OSC1/CLKIN)
#pragma config WDTE = ON  // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF  // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF  // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF  // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF  // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF  // Brown-out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF  // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF  // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

#define _XTAL_FREQ = 15000000
void main(void)
{
  TRISC4 = 0x00;
volatile unsigned int j = 0;
volatile int k = 0;
while(k < 10)
{
  PORTCbits.RC4 = 0xFF;
  for(j=0;j<64000;j++);

  PORTCbits.RC4 = 0x00;
  for(j=0;j<64000;j++);
  
  k++;
}
while(1);//wait forever
}
Mod note: Added CODE-tags.
 
Last edited by a moderator:

David Fowler

Joined Feb 11, 2016
25
So I'm guessing that what you need to do is change the state of the LED every time the interrupt is fired? In that case, switching it on, waiting and then switching it off again isn't really going to work. Instead you're going to need to have the code somehow alternate between states.

You've got two options, either you can check the current state of RC4 and change it accordingly or a tidier option would be to use some bitwise logic (research XOR if you don't already know how to use it) to alternate the state of the pin.

I would suggest the first thing you do is change your code so that instead of explicitly switching on and off, you instead alternate its state (go on, use bitwise logic. Get your head around it now and it'll serve you well in the future :) ).
 

Thread Starter

JBM

Joined Feb 4, 2016
15
So I'm guessing that what you need to do is change the state of the LED every time the interrupt is fired? In that case, switching it on, waiting and then switching it off again isn't really going to work. Instead you're going to need to have the code somehow alternate between states.

You've got two options, either you can check the current state of RC4 and change it accordingly or a tidier option would be to use some bitwise logic (research XOR if you don't already know how to use it) to alternate the state of the pin.

I would suggest the first thing you do is change your code so that instead of explicitly switching on and off, you instead alternate its state (go on, use bitwise logic. Get your head around it now and it'll serve you well in the future :) ).
Please can someone help with the code without using external switching on and off
 

djsfantasi

Joined Apr 11, 2010
9,163
Please can someone help with the code without using external switching on and off
What do you mean by "external switching on and off"? I didn't see that in any posts? If you explain what you mean by this, I could better answer your question.
 

dannyf

Joined Sep 13, 2015
2,197
You never explained what you are trying to do and what you got done.

Your problem is likely that you didn't initiatebthe port to gpuo state - read the data sheet about how to so that.
 

dannyf

Joined Sep 13, 2015
2,197
The main loop structure is also totally wrong and the delay may be too short for visual blinking or have been optimized away.

Going back to the basic would be helpful.
 

Dodgydave

Joined Jun 22, 2012
11,302
To start with turn the wdt off, turn interrupts off, and then make an half second delay, set the port bit on, call delay, clr port bit, call delay,goto start, end...
 
ok lets start with the basics of this. I know you need to do this with interrupts, but worry about getting the logic working in a loop first. At the moment what you're doing is...

loop
{
Turn LED on
wait
turn LED off
wait
}

that isn't going to be any good when you're using an interrupt to change the state. What you're going to have to do it alternate the state of the LED.

loop
{
Alternate state
wait
}

I suggested two methods of doing that. Get it working in the loop and then you can look at using an interrupt. One step at a time young grasshopper.

I would also suggest using a delay or pause function rather than spinning the code in a tight loop, I know it's doing the same thing behind the scenes but it makes the code more readable and easier to tweak.
 
Top