Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic18f1330 turning on and off leds using rc transmitter

Status
Not open for further replies.

mrfunkyjay

New Member
Just like the title, I would like to turn on several leds by pressing the 3rd channel (assigned) button on transmitter. This is done by catching the pulse from channel 3 output from receiver and do several logic inside pic18f1330. I have already my C language code, but my led goes on forever after i pressed my button(3ch). Please advice me, thank you very much.

Code:
#include <p18f1330.h>
#include <delays.h>
#pragma config OSC = INTIO2
#pragma config WDT = OFF

int cState;
void vStandby (void);
void vDimmer30 (void);
void vDimmer60 (void);
void vDimmer100 (void);
int iPulse;

/*Counts the length of a pulse on Pin RA1*/
int GetPulse(){
	T1CON=0b00000000;   /*Init timer1*/
	TMR1L=0;
	TMR1H=0;
	TRISAbits.TRISA1=1;   /*Make this pin as an input*/
	while(PORTAbits.RA0==1); /*Make pin low*/
	while(PORTAbits.RA0==0); /*While pin is high*/
	T1CONbits.TMR1ON=1;   /*Start timer1*/
	while(PORTAbits.RA0==1); /*When pin is low*/
	T1CONbits.TMR1ON=0;   /*Stop timer1*/
	return TMR1H*256+TMR1L;  /*return 16bit timer value*/
}

void main(void)
{
	OSCCONbits.IRCF2 = 1; /*Set OSC Frequency to 4 MHz*/
	OSCCONbits.IRCF1 = 1;
	OSCCONbits.IRCF0 = 0;
	OSCCONbits.IOFS = 1; /*Frequency Stable*/
	OSCCONbits.SCS1 = 0; /*Using Primary OSC*/
	OSCCONbits.SCS0 = 0;
	TRISA = 0b00000001; /*Initialize RA0 as Input Port to read PWM Signal*/
	TRISB = 0b00000000; /*Initialize Port B as Output Port to flash LEDs*/
	ADCON1 = 15; /*Digital Input*/
	PORTA = 0;
	PORTB = 0;

	while(1)
	{	
		iPulse = GetPulse();
		if (iPulse <= 1400)
		{
			cState = 0;
		}		
		if (iPulse >= 1600 && cState == 0)
		{
			vDimmer100();
			cState = 1;			
		}
		if (iPulse <= 1400 && cState == 1)
		{
			cState = 2;
		}
		if (iPulse >= 1600 && cState == 2)
		{
			vStandby();
			cState = 3;
		}
		if (iPulse <= 1400 && cState == 3)
		{
			cState = 0;
		}
	}
}

void vStandby (void)
{
	LATBbits.LATB0 = 0;
	LATBbits.LATB1 = 0;
	LATBbits.LATB4 = 0;
	LATBbits.LATB5 = 0;
}

void vDimmer30 (void)
{
	PTPERH = 0b00000011; /*Set Period PTPER to 255*/
	PTPERL = 0b11110000;
	PTCON0bits.PTMOD1 = 0; /*Free-Running Mode*/
	PTCON0bits.PTMOD0 = 0;
	PWMCON0bits.PWMEN2 = 0; /*PWM0,1,2,3 port as Output PWM*/
	PWMCON0bits.PWMEN1 = 1;
	PWMCON0bits.PWMEN0 = 1;
	PTCON0bits.PTCKPS1 = 0; /*Set Prescaler to 4*/
	PTCON0bits.PTCKPS0 = 1;
}

void vDimmer60 (void)
{
	PTPERH = 0b00001111; /*Set Period PTPER to 255*/
	PTPERL = 0b11110000;
	PTCON0bits.PTMOD1 = 0; /*Free-Running Mode*/
	PTCON0bits.PTMOD0 = 0;
	PWMCON0bits.PWMEN2 = 0; /*PWM0,1,2,3 port as Output PWM*/
	PWMCON0bits.PWMEN1 = 1;
	PWMCON0bits.PWMEN0 = 1;
	PTCON0bits.PTCKPS1 = 0; /*Set Prescaler to 4*/
	PTCON0bits.PTCKPS0 = 1;
}

void vDimmer100 (void)
{
	PTPERH = 0b11111111; /*Set Period PTPER to 255*/
	PTPERL = 0b11111111;
	PTCON0bits.PTMOD1 = 0; /*Free-Running Mode*/
	PTCON0bits.PTMOD0 = 0;
	PWMCON0bits.PWMEN2 = 0; /*PWM0,1,2,3 port as Output PWM*/
	PWMCON0bits.PWMEN1 = 1;
	PWMCON0bits.PWMEN0 = 1;
	PTCON0bits.PTCKPS1 = 0; /*Set Prescaler to 4*/
	PTCON0bits.PTCKPS0 = 1;
}

I planned to do dimming, but now i cannot proceed with the logic, help!!! :confused:
 
Maybe you need to turn off the pwm module or set its ON time to zero in vStandby. You are also making A1 an input in GetPulse even though you use A0.

BTW, why do you use block rem (/*...*/) when you should be using line rem (//...). Doing this stops you using block rem for its intended purpose.

Mike.
 
Maybe you need to turn off the pwm module or set its ON time to zero in vStandby. You are also making A1 an input in GetPulse even though you use A0.

BTW, why do you use block rem (/*...*/) when you should be using line rem (//...). Doing this stops you using block rem for its intended purpose.

Mike.

I have changed the vDimmer100() to LATBbits.LATB0 and B4 = 1;

so no PWM used, but still the leds wont go off when i press the button 2nd time. Is my logic correct?
 
is it possible to do the same thing, just know you use it on RGB led driver, with PWM? Not dimming the led.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top