Continue to Site

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 , pic16f877a mcu

Status
Not open for further replies.

yyyyaser

Newbie level 5
Joined
Oct 21, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,342
Help , pic16f877a , pwm for a motor

why is this code not working?

when set_point < feed_back , the if statement is executed.

but when set_point > feed_back , the it doesn't.

and it is working on the sw debugger but not on the breadboard.

//---------------------------------------------------------

unsigned int set_point , feed_back;
signed int current_duty;

void InitMain() {

PORTA = 0;
TRISA = 0xff; //PORT A FOR A/D CONVERSION

/*ADCON0 = 0x00000001; //PORT A WORKS AS AN ANALOG INPUT NOT DIGITAL */

PORTE = 0; // set PORTE to 0
TRISE = 0; // designate PORTE pins as output
PWM1_Init(20000); // Initialize PWM1 module at 20KHz

}

void main() {
InitMain();

ADC_Init();
PWM1_Start(); // start PWM1

while (1) {
set_point = ADC_Read(0);
feed_back = ADC_Read(1);

if(set_point<feed_back)
{
PORTE.B1=0;
PORTE.B2=1;
}

else if(set_point>=feed_back)

{
PORTE.B1=1;
PORTE.B2=0;
}

current_duty = set_point - feed_back;
current_duty = current_duty * (255.0/1023.0) ;
current_duty = abs( current_duty );

PWM1_Set_Duty(current_duty); // Set current duty for PWM1
}
}
 

try to change from "else if" to just "if" ---- at least i don´t ever use the "else if" option because of that weird errors all over...

;)

Code:
current_duty = abs( current_duty );

where do you have this routine working? it can come handy for me ^^
 
Last edited:

try to change from "else if" to just "if" ---- at least i don´t ever use the "else if" option because of that weird errors all over...

;)

Code:
current_duty = abs( current_duty );

where do you have this routine working? it can come handy for me ^^

well , I tried both if , else if , neither of them worked.

abs is is a standard library fn available in microC :] , returns the absolute value
 

Code:
void InitMain() {

ADCON1=0x00;
TRISA = 0xff; //PORT A FOR A/D CONVERSION

/*ADCON0 = 0x00000001; //PORT A WORKS AS AN ANALOG INPUT NOT DIGITAL */

PORTE = 0; // set PORTE to 0
TRISE = 0; // designate PORTE pins as output
PWM1_Init(20000); // Initialize PWM1 module at 20KHz

}

I think you need to config the ADCON1 aswell :S

what a pitty that is on MikroC (abs fn) i use hi-tech C i have to search to see if i have any of those already built in the compiler ^^
 

It finally worked ,I moved one output to a different port and it worked , but I don't understand why I can't use both pins on the same port .
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top