Waran
Newbie level 5
pic18f452 language
This is the coding I typed and compiled and no error is detected. But after I burned the coding in the chip and try out in the circuit, It didn't work. Is it because of circuit faulty or coding? Is it possible the coding is wrong after compiled and no error is detected?
void main(void)
{
TRISA = 0x00; //Initialize these Ports as Outputs
TRISC = 0x00; //This one too
TRISD = 0x00; //This one toooo
PORTA = 0x00; //Initialize PORTA...so the LED is off
PORTC.F0 = 0; //Brake Off
PORTD.F1 = 1; //Direction Forward
PORTD.F1 = 0; //Direction reverse
while(1)
{
if (PORTD.F1==1)
{
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001111; // 0x0F Full Speed
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
Delay_ms(500); //Pause 0.5 Seconds
PORTD.F0 = 0; //Turn Brake Off
}
else if (PORTD.F1 = 0)
{
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001111; // 0x0F (Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
Delay_ms(500); //Pause 0.5 Seconds
PORTD.F0 = 0; //Turn Brake Off
}
}
}
The function of this coding is to run a dc motor. If (PORTD.F1==1) is used, the dc motor will run in clockwise direction,pause for 2 seconds,and run in full speed. And it pause for 2 seconds,run at 3/4 speed and then pause for 2 seconds run at 1/2 speed and finally pause for 2 seconds and run at 1/4 speed.And then brake on,pause for 0.5 seconds and brake off.
The same procedure goes but in anti-clockwise direction if (PORTD.F1==0) is used. Below are the circuit I used for microcontroller connections.

This is the coding I typed and compiled and no error is detected. But after I burned the coding in the chip and try out in the circuit, It didn't work. Is it because of circuit faulty or coding? Is it possible the coding is wrong after compiled and no error is detected?
void main(void)
{
TRISA = 0x00; //Initialize these Ports as Outputs
TRISC = 0x00; //This one too
TRISD = 0x00; //This one toooo
PORTA = 0x00; //Initialize PORTA...so the LED is off
PORTC.F0 = 0; //Brake Off
PORTD.F1 = 1; //Direction Forward
PORTD.F1 = 0; //Direction reverse
while(1)
{
if (PORTD.F1==1)
{
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001111; // 0x0F Full Speed
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
Delay_ms(500); //Pause 0.5 Seconds
PORTD.F0 = 0; //Turn Brake Off
}
else if (PORTD.F1 = 0)
{
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001111; // 0x0F (Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00001011; //0X0B (3/4 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000101; //0x07 (1/2 Full Speed)
Delay_ms(500);
Delay_ms(500);
Delay_ms(500);
Delay_ms(500); //4 * 0.5 seconds = 2 Second Pause
PORTC = 0b00000011; //0x03 (1/4 Full Speed)
PORTD.F0 = 1; //Turn Brake On
Delay_ms(500); //Pause 0.5 Seconds
PORTD.F0 = 0; //Turn Brake Off
}
}
}
The function of this coding is to run a dc motor. If (PORTD.F1==1) is used, the dc motor will run in clockwise direction,pause for 2 seconds,and run in full speed. And it pause for 2 seconds,run at 3/4 speed and then pause for 2 seconds run at 1/2 speed and finally pause for 2 seconds and run at 1/4 speed.And then brake on,pause for 0.5 seconds and brake off.
The same procedure goes but in anti-clockwise direction if (PORTD.F1==0) is used. Below are the circuit I used for microcontroller connections.
