Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help in c language coding in pic18f452...

Status
Not open for further replies.

Waran

Newbie level 5
Joined
Jul 22, 2009
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Malaysia
Activity points
1,523
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.

 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
pic18f452

'else if (PORTD.F1 = 0)'

I think you mean

'else if (PORTD.F1 == 0)'

check what size argument the Delay_ms() takes?
If it is only a byte your max arg is 255.
 

Waran

Newbie level 5
Joined
Jul 22, 2009
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Malaysia
Activity points
1,523
pic 18f452

hi,

Thank you for helping.

I don't understand about the size argument of Delay_ms() ?
How to check the size of the argument?
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
speed + c language

look at the function prototype which will be in the header file.
In an 8-bit micro, most arguments are one byte.
for example

Function prototype:

void Delay_ms(unsigned char delay);

an unsigned char is only 8-bits wide, which has a maximum value of 255 or 0xff.
If it takes an unsigned int as an argument, you are ok as that is 16-bits wide,
65535 or 0xffff.

Also, if using Port A as a digital I/O, make sure you configure the ansel register.
As most Pics with an A/D default to Analog input on reset.
 

Waran

Newbie level 5
Joined
Jul 22, 2009
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Malaysia
Activity points
1,523
pic18f452 porta,4 output aan uit

Hi,

Can u redo my coding by inserting the neccesary coding?
By the way, I am not using PORT A as input/output pin. In fact, I am not using PORT A at all.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top