PIC - Help With 16f690 Blinking LED in MPLAB

Discussion in 'Homework Help' started by JBM, Feb 17, 2016.

  1. JBM

    Thread Starter New Member

    Feb 4, 2016
    15
    1
    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.
     
  2. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,699
    905
    What language?
    Is this a school assignment or for personal use?
    More details please?

    John
     
  3. JBM

    Thread Starter New Member

    Feb 4, 2016
    15
    1
    Its just for testing the circuit and its an assignment
     
  4. David Fowler

    New Member

    Feb 11, 2016
    25
    0
    How far have you got so far?
     
  5. nerdegutta

    Moderator

    Dec 15, 2009
    2,517
    785
    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. :)
     
  6. JBM

    Thread Starter New Member

    Feb 4, 2016
    15
    1
    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.

    Code (C):
    1.  
    2. // CONFIG
    3. #pragma config FOSC = HS  // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA4/OSC2/CLKOUT and RA5/OSC1/CLKIN)
    4. #pragma config WDTE = ON  // Watchdog Timer Enable bit (WDT enabled)
    5. #pragma config PWRTE = OFF  // Power-up Timer Enable bit (PWRT disabled)
    6. #pragma config MCLRE = OFF  // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
    7. #pragma config CP = OFF  // Code Protection bit (Program memory code protection is disabled)
    8. #pragma config CPD = OFF  // Data Code Protection bit (Data memory code protection is disabled)
    9. #pragma config BOREN = OFF  // Brown-out Reset Selection bits (BOR disabled)
    10. #pragma config IESO = OFF  // Internal External Switchover bit (Internal External Switchover mode is disabled)
    11. #pragma config FCMEN = OFF  // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
    12.  
    13. #define _XTAL_FREQ = 15000000
    14. void main(void)
    15. {
    16.   TRISC4 = 0x00;
    17. volatile unsigned int j = 0;
    18. volatile int k = 0;
    19. while(k < 10)
    20. {
    21.   PORTCbits.RC4 = 0xFF;
    22.   for(j=0;j<64000;j++);
    23.  
    24.   PORTCbits.RC4 = 0x00;
    25.   for(j=0;j<64000;j++);
    26.  
    27.   k++;
    28. }
    29. while(1);//wait forever
    30. }
    31.  
    Mod note: Added CODE-tags.
     
    Last edited by a moderator: Feb 18, 2016
  7. David Fowler

    New Member

    Feb 11, 2016
    25
    0
    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 :) ).
     
  8. JBM

    Thread Starter New Member

    Feb 4, 2016
    15
    1
    Please can someone help with the code without using external switching on and off
     
  9. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,810
    834
    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.
     
  10. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    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.
     
  11. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    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.
     
  12. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,998
    745
    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...
     
  13. David Fowler

    New Member

    Feb 11, 2016
    25
    0
    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.
     
Loading...