E ovako, ovo je znaci ceo program sa nekim nacinom kontrolisanja PWM-a (zato sto je potrebno i brzina da se menja)
void init_ports(void); // initilaze the ports
void init_interrupt(); // initilize the interrupts
void Inttostr(int number, char *output); //function to convert intteger to string
signed int requested =0;
signed int error=0;
signed int actual=0;
char pwm_duty=0;
char txt1[6];
char txt2[6];
void main ()
{
CMCON = 0x07;
init_ports(); // initilaze the ports
init_interrupt(); // initilize the interrupts
LCD_Init(&PORTD); // initilaze the LCD, Mikro C function call
LCD_Cmd(LCD_CLEAR); //clear LCD
LCD_Cmd(Lcd_CURSOR_OFF); // Set the LCD cursor off
PWM_Init(1000); // initilaze the PWM, Mikro C function call
PWM_Start();
while(1)
{
// read direction of rotation <= ovako sam ja pokusao da kontrolisem smer, ali ne radi jer ne prima signal non stop!
if(PORTB.F2 == 1) //motor moves in right direction
{ PORTC.F0 = 1;
PORTC.F1 =0;
}
else //motor moves in left direction
{
PORTC.F1 = 1;
PORTC.F0 =0;
}
// display actual and requested speed on LCD
IntToStr(actual, txt1);
Lcd_out(1,1, "Act Speed= " ); // display actual speed on LCD row 1
Lcd_out(1,11,txt1);
IntToStr(requested, txt2);
LCD_out(2,1, "Req Speed= " ); //display requsted speed on LCD row 2
Lcd_out(2,11,txt2);
actual = 0; //clear INTO
Delay_ms(1000); // 1/2 sec. Mikro C function
// Read TMRO => int actual
error = (requested - actual)* 5;
// pwm_duty = char(error) + pwm_duty
pwm_duty = error + pwm_duty;
// new value old value
// output to pwm using Mikro C funcition
pwm_change_duty(pwm_duty);
}
}
void interrupt()
{
if(INT0IF == 1)
{
actual++;
INTCON.INT0IF = 0;
}
//increment
if( PORTB.F7 == 0 && INTCON.RBIF== 1)
{ requested = requested + 2;
INTCON.RBIF = 0;
}
// decrement
if(PORTB.F6 == 0 && INTCON.RBIF == 1)
{
requested = requested - 2;
INTCON.RBIF = 0;
}
PORTB = PORTB;
INTCON.RBIF = 0;
INTCON.INT0IF = 0;
}
void init_ports()
{
TRISB = 0xFF; //PORTB all inputs
TRISC = 0x00; //PORTC all outputs - using RC2 for PWM and RC0&RC1 for H brigde
TRISD = 0x00; //PORTD all outputs - output LCD on port D
}
void init_interrupt(void)
{
//setting the INTCON & INTCON2 register
INTCON.GIE = 1; // Enable interrupt
INTCON.INT0IE = 1; //Enable INTO
INTCON.INT0IF =0; //Clear INTO Flag
INTCON.RBIE = 1; //enable portB rb7..rb4
INTCON.RBIF = 0; //enable RB7..RB4
INTCON2.INTEDG0 = 0; // set the falling edge interrupt
INTCON2.RBPU = 0; //enable pull ups
}
Citat:
sander: Nema razloga da se tako nesto ne moze uraditi.
Ajde postavi svoj program pa da vidimo.