mrfunkyjay
New Member
Hi all. I have several requirements in My Project. And it requires Multitasking, that I don't have any background knowledge about this.
My requirements are:
1. Reading PWM Signal from Pin RA1 and RA2. Two PWM Signals came from STEERING and THROTTLE Channels of Radio Control Receiver Module.
2. From these two different signals, I would like to have the outputs to LEDs while turning the steering left, Left LED blinks and turning the steering right, Right LED blinks. As well as the throttle trigger, when it is pressed, several LED also blink.
3. The problem is, my code below doesn't have any multitasking, that is why, only steering or throttle LED function is executed at a time, NOT BOTH. But I WANT BOTH to be able to execute.
Hereby I have my whole C Language program, I wrote it with MPLAB IDE C18 Compiler and PICKIT2. My MCU is PIC18F4520.
I need your help, thanks a lot!
My requirements are:
1. Reading PWM Signal from Pin RA1 and RA2. Two PWM Signals came from STEERING and THROTTLE Channels of Radio Control Receiver Module.
2. From these two different signals, I would like to have the outputs to LEDs while turning the steering left, Left LED blinks and turning the steering right, Right LED blinks. As well as the throttle trigger, when it is pressed, several LED also blink.
3. The problem is, my code below doesn't have any multitasking, that is why, only steering or throttle LED function is executed at a time, NOT BOTH. But I WANT BOTH to be able to execute.
Hereby I have my whole C Language program, I wrote it with MPLAB IDE C18 Compiler and PICKIT2. My MCU is PIC18F4520.
I need your help, thanks a lot!
Code:
#include <p18f4520.h>
#include <delays.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
int i, iPulse1, iPulse2;
void vLeft(void);
void vRight(void);
void vFlashing1(void);
void vFlashing2(void);
/*Counts the length of a pulse on Pin RA1*/
int GetPulse1(){
T1CON=0b00000000; /*Init timer1*/
TMR1L=0;
TMR1H=0;
TRISAbits.TRISA1=1; /*Make this pin as an input*/
while(PORTAbits.RA1==1); /*Make pin low*/
while(PORTAbits.RA1==0); /*While pin is high*/
T1CONbits.TMR1ON=1; /*Start timer0*/
while(PORTAbits.RA1==1); /*When pin is low*/
T1CONbits.TMR1ON=0; /*Stop timer0*/
return TMR1H*256+TMR1L; /*return 16bit timer value*/
}
/*Counts the length of a pulse on Pin RA2*/
int GetPulse2(){
T3CON=0b00000000; /*Init timer0*/
TMR3L=0;
TMR3H=0;
TRISAbits.TRISA2=1; /*Make this pin as an input*/
while(PORTAbits.RA2==1); /*Make pin low*/
while(PORTAbits.RA2==0); /*While pin is high*/
T3CONbits.TMR3ON=1; /*Start timer0*/
while(PORTAbits.RA2==1); /*When pin is low*/
T3CONbits.TMR3ON=0; /*Stop timer0*/
return TMR3H*256+TMR3L; /*return 16bit timer value*/
}
void main(void)
{
TRISA = 0b00000111; /*Initialize RA0 as Input Port to read PWM Signal*/
TRISB = 0b00000000; /*Initialize Port B as Output Port to flash LEDs*/
ADCON1 = 15; /*Digital Input*/
PORTB = 0;
while(1)
{
iPulse1=GetPulse1();
if(iPulse1<7000){ /*If the Pulse Width is less than 1.4ms*/
vLeft(); /*Blinks left LED*/
}
if(iPulse1>8000){ /*If the Pulse Width is more than 1.6ms*/
vRight(); /*Blinks right LED*/
}
iPulse2=GetPulse2();
if(iPulse2<7000){ /*If the Pulse Width is less than 1.4ms*/
vFlashing1(); /*Flashing1*/
}
if(iPulse2>8000){ /*If the Pulse Width is less than 1.2ms*/
vFlashing2(); /*Flashing2*/
}
}
}
void vLeft(void)
{
for(i=0;i<1;i++)
{
LATBbits.LATB0 = 1;
Delay10KTCYx(200);
LATBbits.LATB0 = 0;
Delay10KTCYx(150);
}
}
void vRight(void)
{
for(i=0;i<1;i++)
{
LATBbits.LATB1 = 1;
Delay10KTCYx(200);
LATBbits.LATB1 = 0;
Delay10KTCYx(150);
}
}
void vFlashing1(void)
{
for(i=0;i<1;i++)
{
LATBbits.LATB2 = 1;
Delay10KTCYx(60);
LATBbits.LATB2 = 0;
Delay10KTCYx(60);
LATBbits.LATB3 = 1;
Delay10KTCYx(60);
LATBbits.LATB3 = 0;
Delay10KTCYx(60);
LATBbits.LATB4 = 1;
Delay10KTCYx(60);
LATBbits.LATB4 = 0;
Delay10KTCYx(60);
LATBbits.LATB5 = 1;
Delay10KTCYx(60);
LATBbits.LATB5 = 0;
Delay10KTCYx(60);
}
}
void vFlashing2(void)
{
for(i=0;i<1;i++)
{
LATBbits.LATB2 = 1;
Delay10KTCYx(30);
LATBbits.LATB2 = 0;
Delay10KTCYx(30);
LATBbits.LATB3 = 1;
Delay10KTCYx(30);
LATBbits.LATB3 = 0;
Delay10KTCYx(30);
LATBbits.LATB4 = 1;
Delay10KTCYx(30);
LATBbits.LATB4 = 0;
Delay10KTCYx(30);
LATBbits.LATB5 = 1;
Delay10KTCYx(30);
LATBbits.LATB5 = 0;
Delay10KTCYx(30);
}
}