1
\$\begingroup\$

I am struggling with the simplest program. I am working with PIC microcontroller and am trying to get the LED to blink with MCC configure in MPLAB. There aren't many steps involved in this yet I can't figure out why my LED is not flashing. The steps, according to this video MCC melody Timer0 PLIB driver: 100ms Timer - Youtube are:

1)Open up MCC melody

2)select Timer0 from Device resources>drivers>Timer panel and add it enter image description here

3)select Fosc/4 (internal oscillator) and enable interrupt

enter image description here

enter image description here

4)configure a pin for output blinking

enter image description here

enter image description here

5)Hit "Generate Code" and then create a pointer to Timer0 struct and pass Timer_call_back_10us (this is the time that falls into the range set by Timer0 in MCC melody from step 3) into argument of TimeoutCallbackRegister. toggle LED pin RA5.

6)enable global interrupt

#include "mcc_generated_files/system/system.h"
const struct TMR_INTERFACE *Timer= &Timer0;
/*
Main application
*/
void Timer_Callback_10us(void){
LED_Toggle();
 }
 int main(void)
 {
 SYSTEM_Initialize();
 Timer->TimeoutCallbackRegister(Timer_Callback_10us);
 // If using interrupts in PIC18 High/Low Priority Mode you need to 
 enable the Global High and Low Interrupts 
 // If using interrupts in PIC Mid-Range Compatibility Mode you need to 
 enable the Global and Peripheral Interrupts 
 // Use the following macros to: 

 // Enable the Global Interrupts 
 INTERRUPT_GlobalInterruptEnable(); 

 // Disable the Global Interrupts 
 //INTERRUPT_GlobalInterruptDisable(); 

 // Enable the Peripheral Interrupts 
 INTERRUPT_PeripheralInterruptEnable(); 

 // Disable the Peripheral Interrupts 
 //INTERRUPT_PeripheralInterruptDisable(); 


 while(1)
 {
 }    
 }

The code for the timer struct callback register comes from the timer0.c created after generating the MCC melody code. Here it is:

#include <xc.h>
#include "../tmr0.h"

volatile uint8_t timer0ReloadVal;

const struct TMR_INTERFACE Timer0 = {
.Initialize = Timer0_Initialize, 
.Start = NULL,
.Stop = NULL,
.PeriodCountSet = Timer0_PeriodCountSet,
.TimeoutCallbackRegister = Timer0_OverflowCallbackRegister,
.Tasks = NULL
};

static void (*Timer0_OverflowCallback)(void);
static void Timer0_DefaultOverflowCallback(void);

void Timer0_Initialize(void)
{
// PS 1:2; PSA not_assigned; TMRSE Increment_hi_lo; TMRCS FOSC/4; mask 
the nWPUEN and INTEDG bits
OPTION_REG = (uint8_t)((OPTION_REG & 0xC0) | (0xD8 & 0x3F));

// TMR 0; 
TMR0 = 0x0;

// Load the TMR value to reload variable
timer0ReloadVal = 0;

//Set default callback for TMR0 overflow interrupt
Timer0_OverflowCallbackRegister(Timer0_DefaultOverflowCallback);

//Clear Interrupt flag before enabling the interrupt
INTCONbits.TMR0IF = 0;

//Enable TMR0 interrupt.
INTCONbits.TMR0IE = 1;

}

uint8_t Timer0_Read(void)
{
uint8_t readVal;

readVal = TMR0;

return readVal;
}

void Timer0_Write(size_t timerVal)
{
// Write to the Timer0 register
TMR0 = (uint8_t) timerVal;
}

void Timer0_Reload(void)
{
// Write to the Timer0 register
TMR0 = timer0ReloadVal;
}

void Timer0_PeriodCountSet(size_t periodVal)
{
timer0ReloadVal = (uint8_t) periodVal;
}

void Timer0_OverflowISR(void)
{

//Clear the TMR0 interrupt flag
INTCONbits.TMR0IF = 0;

TMR0 = timer0ReloadVal;
if(Timer0_OverflowCallback)
{
  Timer0_OverflowCallback();
}
}

void Timer0_OverflowCallbackRegister(void (* CallbackHandler)(void))
{ 
Timer0_OverflowCallback = CallbackHandler;
}

static void Timer0_DefaultOverflowCallback(void)
{
//Add your interrupt code here or
//Use Timer0_OverflowCallbackRegister function to use Custom ISR
}

7)I downloaded the program to the PIC

enter image description here

8)I got the following error

The configuration is set for the target board to supply its own power 
but no voltage has been detected on VDD. Please ensure you have your 
target powered up and try again.
Connection Failed.

To get around this error. I simply opened MPLAB IPE

I clicked power settings and made sure the PICkit5 votlage was set

enter image description here

then I clicked connect

![enter image description here

The following message (sorry. there wasn't an error here) showed up

enter image description here

edit: i changed the prescalar to 1:256 (16ms) and the LED is still not blinking

\$\endgroup\$
9
  • \$\begingroup\$ Hello ... We don't know what you have or what you've tried or what you do see. It would really help to have a bit more information: do you have a circuit diagram of what you're using, or even a photo of the circuit? Do you get any behaviour at all? If so, what? \$\endgroup\$
    – jonathanjo
    Commented Jul 16 at 15:50
  • \$\begingroup\$ The LED isn't flashing so there is no behavior. \$\endgroup\$
    – xcaliber99
    Commented Jul 16 at 15:54
  • \$\begingroup\$ is the LED connected correctly? \$\endgroup\$
    – jsotola
    Commented Jul 16 at 16:24
  • \$\begingroup\$ please remove the pictures of code and replace them with code text \$\endgroup\$
    – jsotola
    Commented Jul 16 at 16:25
  • 7
    \$\begingroup\$ Your eyes will not interpret a 10 µs (or 64 µs) LED toggle interval as flashing. \$\endgroup\$
    – Jens
    Commented Jul 16 at 16:39

1 Answer 1

0
\$\begingroup\$

"8)I got the following error

The configuration is set for the target board to supply its own power but no voltage has been detected on VDD. Please ensure you have your target powered up and try again. Connection Failed."

You really should have a power supply for your target board. If you insist to powering your target board through the PICkit 5, do the following:

  • On the left panel, select the project, then right-click and select Properties
  • Select the PICkit 5
  • On Option categories, select Power
  • Check the box for "Power target circuit from PICkit 5

16ms is still pretty fast. You may need to slow it down more. Which PIC are you using?

I think this line: Timer->TimeoutCallbackRegister(Timer_Callback_10us); should be: Timer0_OverflowCallbackRegister(Timer_Callback_10us);

\$\endgroup\$
5
  • \$\begingroup\$ If you look at my entire post, you will notice I followed the powering steps you listed. I mispoke though. I meant I got the following message - it wasn't an error, it showed that it worked. \$\endgroup\$
    – xcaliber99
    Commented Jul 16 at 19:58
  • \$\begingroup\$ Also, doesn't it do the same thing? The struct is defined as: const struct TMR_INTERFACE Timer0 = { .Initialize = Timer0_Initialize, .Start = NULL, .Stop = NULL, .PeriodCountSet = Timer0_PeriodCountSet, .TimeoutCallbackRegister = Timer0_OverflowCallbackRegister, .Tasks = NULL }; by creating a pointer called Timer you can use the access operator to access whats inside the struct right? \$\endgroup\$
    – xcaliber99
    Commented Jul 16 at 20:06
  • \$\begingroup\$ I don't think so. Honestly, I don't see anywhere in the code where they actually use that structure. If you can see it let me know. They ARE using Timer0_OverflowCallback in the Timer0_OverflowISR(void) function, though. \$\endgroup\$
    – StevenH
    Commented Jul 16 at 21:09
  • \$\begingroup\$ they defined it at the top of the second code file as a member of a struct Timer0 ".TimeoutCallbackRegister = Timer0_OverflowCallbackRegister, " and then in the main file, there is a pointer to the struct, Timer0, that gets created called Timer. Timer accesses the member in the struct and passes the funciton Timer_Callback_10us to toggle the LED. this is how i understood it. i could be wrong. \$\endgroup\$
    – xcaliber99
    Commented Jul 16 at 21:51
  • \$\begingroup\$ Right. I saw those, but I don't see it or any of its members being used anywhere. \$\endgroup\$
    – StevenH
    Commented Jul 16 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.