Upravljanje steperom

Rasprava o PIC mikrokontrolerima, PIC projekti i drugo vezano za PIC-eve...

Moderators: pedja089, stojke369, [eDo], trax

Post Reply
enzo05
Posts: 9
Joined: 12-01-2012, 11:52

Upravljanje steperom

Post by enzo05 »

Pozdrav
Pokusavam napisati kod za upravljanje steperom u mikroC-u, ali ne radi sve kako treba pa ako moze pomoc oko toga:

Code: Select all

// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[]="Running";
char txt2[]="Stopped";

void runMotor()
{
     TRISB = 0;
     PORTB = 0;
     Lcd_Init();                     // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);            // Clear LCD display
     Lcd_Cmd(_LCD_CURSOR_OFF);       // Turn cursor off
     Lcd_Out(1,1,txt1);

     CMCON = 0x07;     // To turn off comparators
     ADCON1 = 0x06;    // To turn off analog to digital converters
     TRISD = 0;        // PORT D as output port
     PORTD = 0x0F;
     do
     {
       PORTD = 0b00000011;
       Delay_ms(100);
       PORTD = 0b00000110;
       Delay_ms(100);
       PORTD = 0b00001100;
       Delay_ms(100);
       PORTD = 0b00001001;
       Delay_ms(100);
     }while(1);
}

void stopMotor()
{
     TRISB = 0;
     PORTB = 0;
     Lcd_Init();                     // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR);            // Clear LCD display
     Lcd_Cmd(_LCD_CURSOR_OFF);       // Turn cursor off
     Lcd_Out(1,1,txt2);

     CMCON = 0x07;     // To turn off comparators
     ADCON1 = 0x06;    // To turn off analog to digital converters
     TRISD = 0;        // PORT D as output port
     PORTD = 0x0F;
     do
     {
       PORTD = 0b00000000;
       Delay_ms(100);
       PORTD = 0b00000000;
       Delay_ms(100);
       PORTD = 0b00000000;
       Delay_ms(100);
       PORTD = 0b00000000;
       Delay_ms(100);
     }while(1);
}

void main()
{
     runMotor();

     TRISC.F0 = 1;
     TRISC.F1 = 1;
     while(1)
     {
       if(Button(&PORTC,0,10,0))
       {
         runMotor();
       }
       if(Button(&PORTC,1,10,0))
       {
         stopMotor();
       }
     }
}
Shema iz proteus-a:
http://i67.tinypic.com/t8qjbk.png

Kad pokrenem simulaciju, motor se pocne okretati, ali kad pritisnem taster za zaustavljanje nista se ne desava, motor se i dalje vrti nakon toga.
Maki
Odlično uznapredovao
Odlično uznapredovao
Posts: 766
Joined: 02-07-2012, 12:54

Re: Upravljanje steperom

Post by Maki »

Ovdije je problem.

Code: Select all

do
{
PORTD = 0b00000011;
Delay_ms(100);
PORTD = 0b00000110;
Delay_ms(100);
PORTD = 0b00001100;
Delay_ms(100);
PORTD = 0b00001001;
Delay_ms(100);
}while(1);
Petlja će se izvršavat beskonačno, neće se vratiti u main.
Rješenje je unutar do-while petlje ubacit if da kad se stisne gumbić izađe iz petlje.
Npr.

Code: Select all

do
{
PORTD = 0b00000011;
Delay_ms(100);
PORTD = 0b00000110;
Delay_ms(100);
PORTD = 0b00001100;
Delay_ms(100);
PORTD = 0b00001001;
Delay_ms(100);
if(tu staviš gum/tipkalo s kojim gasiš) break; //break prekida izvođenje trenutne petlje, u tvom slučaju do-while
}while(1);
Edit:
Onda ti stopmotor funkcija više ne treba.
Post Reply