gabosssss
Newbie level 5

Hello, I'm new here and I want to tell you I'm glad I found this forum. At first view, there are many people here able to help guys with problems (like me
)
So my problem is this: I have to move a robot, for the begining, using PWM from dsPIC30F4011. I did a program in MPLAB C30, my board is working, my servo motors are moving and I managed to move the wheels when I send something from PC on UART (and stop them when I send again).
I a blocked now, for some hours, because I want to modify this program so when I send "w", "a", "s", "d" from PC, the wheels move "forward", "turn left", "backward", "turn right" (something like PC games). I hope you all understood my problem. My english is not the best. Ask me any details, I'll answer as fast as I can. Thanks in advance!
This is my program so far:
#include "p30f4011.h"
_FOSC(CSW_FSCM_OFF & XT); // Disable clock monitor, external 8MHz oscillator
_FWDT(WDT_OFF); // Disable Watch-Dog Timer.
_FBORPOR(PBOR_OFF & MCLR_EN & PWRT_OFF & PWMxH_ACT_HI); // Brown out reset disabled, enable MCLR reset pin, turn off power-up timer, PWMxH acts high
_FGS(CODE_PROT_OFF); // Disable Code Protection
#define FOSC 8000000 // 8Mhz
#define FCY (FOSC / 4) // 1 inscruction cycle = 4 clock cycles
#define q 8000000
int i,j;
void initserial(double baudr);
void initPWM();
int main(void)
{
int f1;
initserial(9600);
Init_PWM(); // Initialize PWM module
while(1) // Loop forever
{
while(!U1STAbits.URXDA){}
f1=U1RXREG;U1TXREG=f1;
PDC1 = 2000; //forward
PDC2 = 1000; //forward (the servos are positioned somehow mirrored)
while(!U1STAbits.URXDA){}
f1=U1RXREG;U1TXREG=f1;
PDC1 = 1515; //center value for first servo
PDC2 = 1520; //center value for second servo
}//while
}//main
void initserial(double baudr)
{
U1MODE=0;//-- set up the UART
U1STA=0;//-- set up the UART
U1MODEbits.ALTIO=1;
U1BRG=(int)((q-16*baudr)/(16*baudr));
U1MODEbits.UARTEN=1;//--enable the UART
U1STAbits.UTXEN=1;//--enable transmision
}
void Init_PWM(void)
{
PTCONbits.PTEN = 0; // Timer Enable bit: DISABLE MCPWM
PWMCON1bits.PEN1H = 1; // PWM1H (pin 37) is enabled for PWM output
PWMCON1bits.PEN2H = 1; // PWM2H (pin 35) is enabled for PWM output
PTCONbits.PTCKPS = 1;// Input Clock Prescale bits: 1:4
PTCONbits.PTOPS = 0; // Output Clock Postscale bits: 1:1
PTCONbits.PTSIDL = 1; // Stop in Idle Mode: YES
PTCONbits.PTMOD = 0; // Mode Select bits: Free Running Mode
PTCONbits.PTEN = 1; // Timer Enable bit: ENABLE MCPWM
PTPERbits.PTPER = 11250; // Period Value bits
}
So my problem is this: I have to move a robot, for the begining, using PWM from dsPIC30F4011. I did a program in MPLAB C30, my board is working, my servo motors are moving and I managed to move the wheels when I send something from PC on UART (and stop them when I send again).
I a blocked now, for some hours, because I want to modify this program so when I send "w", "a", "s", "d" from PC, the wheels move "forward", "turn left", "backward", "turn right" (something like PC games). I hope you all understood my problem. My english is not the best. Ask me any details, I'll answer as fast as I can. Thanks in advance!
This is my program so far:
#include "p30f4011.h"
_FOSC(CSW_FSCM_OFF & XT); // Disable clock monitor, external 8MHz oscillator
_FWDT(WDT_OFF); // Disable Watch-Dog Timer.
_FBORPOR(PBOR_OFF & MCLR_EN & PWRT_OFF & PWMxH_ACT_HI); // Brown out reset disabled, enable MCLR reset pin, turn off power-up timer, PWMxH acts high
_FGS(CODE_PROT_OFF); // Disable Code Protection
#define FOSC 8000000 // 8Mhz
#define FCY (FOSC / 4) // 1 inscruction cycle = 4 clock cycles
#define q 8000000
int i,j;
void initserial(double baudr);
void initPWM();
int main(void)
{
int f1;
initserial(9600);
Init_PWM(); // Initialize PWM module
while(1) // Loop forever
{
while(!U1STAbits.URXDA){}
f1=U1RXREG;U1TXREG=f1;
PDC1 = 2000; //forward
PDC2 = 1000; //forward (the servos are positioned somehow mirrored)
while(!U1STAbits.URXDA){}
f1=U1RXREG;U1TXREG=f1;
PDC1 = 1515; //center value for first servo
PDC2 = 1520; //center value for second servo
}//while
}//main
void initserial(double baudr)
{
U1MODE=0;//-- set up the UART
U1STA=0;//-- set up the UART
U1MODEbits.ALTIO=1;
U1BRG=(int)((q-16*baudr)/(16*baudr));
U1MODEbits.UARTEN=1;//--enable the UART
U1STAbits.UTXEN=1;//--enable transmision
}
void Init_PWM(void)
{
PTCONbits.PTEN = 0; // Timer Enable bit: DISABLE MCPWM
PWMCON1bits.PEN1H = 1; // PWM1H (pin 37) is enabled for PWM output
PWMCON1bits.PEN2H = 1; // PWM2H (pin 35) is enabled for PWM output
PTCONbits.PTCKPS = 1;// Input Clock Prescale bits: 1:4
PTCONbits.PTOPS = 0; // Output Clock Postscale bits: 1:1
PTCONbits.PTSIDL = 1; // Stop in Idle Mode: YES
PTCONbits.PTMOD = 0; // Mode Select bits: Free Running Mode
PTCONbits.PTEN = 1; // Timer Enable bit: ENABLE MCPWM
PTPERbits.PTPER = 11250; // Period Value bits
}