0
\$\begingroup\$

I am new to C programming. I want to program a running LED on my PIC24 (PIC24HJ256GP206)

I have 5 LED on my PORT G and have input PORTB (RB0)

My program should, if RB0 is grounded (I put jumper on my circuit) the LED will be running faster, else the LED will running slower.

But, there is problem , the LED is not running.

here my code :

// PIC24HJ256GP206 Configuration Bit Settings
// 'C' sourc#include <p24Hxxxx.h>e line config statements
#include <p24Hxxxx.h>
 int FBS __attribute__((space(prog), address(0xF80000))) = 0xCF ;
//_FBS(
// BWRP_WRPROTECT_OFF & // Boot Segment Write Protect (Boot Segment may be written)
// BSS_NO_FLASH & // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
// RBS_NO_RAM // Boot Segment RAM Protection (No Boot RAM)
//);
 int FSS __attribute__((space(prog), address(0xF80002))) = 0xCF ;
//_FSS(
// SWRP_WRPROTECT_OFF & // Secure Segment Program Write Protect (Secure segment may be written)
// SSS_NO_FLASH & // Secure Segment Program Flash Code Protection (No Secure Segment)
// RSS_NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)
//);
 int FGS __attribute__((space(prog), address(0xF80004))) = 0x7 ;
//_FGS(
// GWRP_OFF & // General Code Segment Write Protect (User program memory is not write-protected)
// GSS_OFF // General Segment Code Protection (User program memory is not code-protected)
//);
 int FOSCSEL __attribute__((space(prog), address(0xF80006))) = 0xFF78 ;
//_FOSCSEL(
// FNOSC_FRC & // Oscillator Mode (Internal Fast RC (FRC))
// IESO_OFF // Two-speed Oscillator Start-Up Enable (Start up with user-selected oscillator)
//);
 int FOSC __attribute__((space(prog), address(0xF80008))) = 0xFFFF ;
//_FOSC(
// POSCMD_NONE & // Primary Oscillator Source (Primary Oscillator Disabled)
// OSCIOFNC_OFF & // OSC2 Pin Function (OSC2 pin has clock out function)
// FCKSM_CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
//);
 int FWDT __attribute__((space(prog), address(0xF8000A))) = 0xFF7F ;
//_FWDT(
// WDTPOST_PS32768 & // Watchdog Timer Postscaler (1:32,768)
// WDTPRE_PR128 & // WDT Prescaler (1:128)
// WINDIS_OFF & // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
// FWDTEN_OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
//);
 int FPOR __attribute__((space(prog), address(0xF8000C))) = 0xFFFF ;
//_FPOR(
// FPWRT_PWR128 // POR Timer Value (128ms)
//);
 int FICD __attribute__((space(prog), address(0xF8000E))) = 0xFFDF ;
//_FICD(
// ICS_PGD1 & // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
// JTAGEN_OFF // JTAG Port Enable (JTAG is Disabled)
//);
 void onled (int LedIdx)
 {
 //set output
 TRISGbits.TRISG1 = 0 ;//PIN60 //D01
 TRISGbits.TRISG0 = 0 ;//PIN61 //D02
 TRISGbits.TRISG13 = 0 ;//PIN64 //D03
 TRISGbits.TRISG14 = 0 ;//PIN62 //DO4
 TRISGbits.TRISG12 = 0 ;//PIN63 //D05
 //Off all the LED/ DO
 LATGbits.LATG1 = 0 ; //DO1 OFF
 LATGbits.LATG0 = 1 ; //DO2 OFF
 LATGbits.LATG13 = 1 ; //DO3 OFF
 LATGbits.LATG14 = 1 ; //DO4 OFF
 LATGbits.LATG12 = 1 ; //DO5 OFF
 //turn on based on LDIDX
 if (LedIdx == 0)
 LATGbits.LATG1 = 1 ;
 else if ( LedIdx == 1 )
 LATGbits.LATG0 = 0 ;
 else if ( LedIdx == 2 )
 LATGbits.LATG13 = 0 ;
 else if ( LedIdx == 3 )
 LATGbits.LATG14 = 0 ;
 else if ( LedIdx == 4 )
 LATGbits.LATG12 = 0 ;
 }
 int main (void)
 {
 int LED = 0;
 // Set Timer
 TMR1 = 0;
 PR1 = 5760 ; //5760 = 100ms // 28571 = 496ms
 //select clock souce souce as Tcy
 T1CONbits.TCS = 0 ;
 // Select pre scaler = 1:256
 T1CONbits.TCKPS = 2;
 //on timer
 T1CONbits.TON=1 ;
 // output = 0 : input = 1
 //set output
 TRISGbits.TRISG1 = 0 ;//PIN60 //D01
 TRISGbits.TRISG0 = 0 ;//PIN61 //D02
 TRISGbits.TRISG13 = 0 ;//PIN64 //D03
 TRISGbits.TRISG14 = 0 ;//PIN62 //DO4
 TRISGbits.TRISG12 = 0 ;//PIN63 //D05
 //Off all the LED/ DO
 LATGbits.LATG1 = 0 ; //DO1 OFF
 LATGbits.LATG0 = 1 ; //DO2 OFF
 LATGbits.LATG13 = 1 ; //DO3 OFF
 LATGbits.LATG14 = 1 ; //DO4 OFF
 LATGbits.LATG12 = 1 ; //DO5 OFF
 TRISBbits.TRISB0 = 1 ;
 //set all anolog pin to digital
 AD1PCFGL = 0xFFFF;
 while (1) // keep looping
 {
 if (PORTBbits.RB0 == 0 )
 {
 // Set Timer
 TMR1 = 0;
 PR1 = 5760 ; //5760 = 100ms // 28571 = 496ms
 //select clock souce souce as Tcy
 T1CONbits.TCS = 0 ;
 // Select pre scaler = 1:256
 T1CONbits.TCKPS = 2;
 //on timer
 T1CONbits.TON=1 ;
 if (IFS0bits.T1IF == 1 ) //SET FLAG TO 1
 {
 LATGbits.LATG0 = 1; //check timer
 IFS0bits.T1IF = 0 ;
 onled (LED);
 LED++;
 // Only have 5 LED
 if (LED >= 5)
 LED = 0 ;
 }
 }
 else
 {
 // Set Timer
 TMR1 = 0;
 PR1 = 5760 ; //5760 = 100ms // 28571 = 496ms
 //select clock souce souce as Tcy
 T1CONbits.TCS = 0 ;
 // Select pre scaler = 1:256
 T1CONbits.TCKPS = 2;
 //on timer
 T1CONbits.TON=1 ;
 if (IFS0bits.T1IF == 1 ) //
 { 
 LATGbits.LATG0 = 1; //check timer
 IFS0bits.T1IF = 0 ;
 onled (LED);
 LED++;
 // Only have 5 LED
 if (LED >= 5)
 LED = 0 ;
 }
 }
 }
 }
asked Aug 18, 2015 at 7:54
\$\endgroup\$
3
  • \$\begingroup\$ What do you mean by running led faster, do you mean blinking led at higher speed.? \$\endgroup\$ Commented Aug 19, 2015 at 4:10
  • \$\begingroup\$ Yes.The blinking led at higher speed. \$\endgroup\$ Commented Aug 19, 2015 at 9:13
  • \$\begingroup\$ You can use if else condition to check status of input RB0 and accordingly blink led at higher or slower speed \$\endgroup\$ Commented Aug 19, 2015 at 9:31

1 Answer 1

2
\$\begingroup\$

The main loop is continually resetting the timer, so the timer will never expire and set the flag.

In this case you should only restart the timer after the timer has expired and the timer flag has been set.

answered Aug 18, 2015 at 12:56
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.