|
Skrevet av Jens Christoffersen
|
|
onsdag 28. desember 2011 14:13 |
|
Dette programmet styrer en PM4222-09 step motor.
/*
Program: main.c
Description: Drive steppermotor
PIC: 16 F 628
IDE: MPLAB 8.63
Compiler: Hi - Tech C v9.80
Date: 12 / 2011
Author: jc - nerdegutta
WEB: www.nerdegutta.org
PIC PINS
RA2_ 1 -| P |- 18 RA1
RA3_ 2 -| I |- 17 RA0
RA4_ 3 -| C |- 16 RA7
RA5_ 4 -| 1 |- 15 RA6
VSS_ 5 -| 6 |- 14 VDD
RB0_ 6 -| F |- 13 RB7
RB1_ 7 -| 6 |- 12 RB6
RB2_ 8 -| 2 |- 11 RB5
RB3_ 9 -| 8 |- 10 RB4
Using RA0, RA1, RA2 as input to trigger RB0 - RB3
Using RB0 - RB3 to generate signal for the ULN 2003
RA0 - start slow
RA1 - start fast
RA2 - stop
RA3 - PIC running LED
RA6 - LED slow
RA7 - LED fast
*/
#include "<htc.h>"
#define _XTAL_FREQ 4000000
#define slowSleep 20000
#define fastSleep 10000
#define turnoffwait 200
/* Configuration */
__CONFIG (WDTDIS &
PWRTEN &
MCLREN &
BOREN &
LVPDIS &
DATUNPROT &
UNPROTECT &
INTIO);
/* Prototyping */
void startSlow();
void startFast();
/* Global variables */
/* Functions */
void startSlow()
{
PORTA = 0b01001000; // turning onn PIC running LED & fast slow
while (1)
{
//Step 1
PORTB = 0b00001001; // RB0 & RB3 high
__delay_us(slowSleep);
//Step 2
PORTB = 0b00001100; // RB2 & RB3 high
__delay_us(slowSleep);
//Step 3
PORTB = 0b00000110; // RB1 & RB2 high
__delay_us(slowSleep);
//Step 4
PORTB = 0b00000011; // RB0 & RB1 high
__delay_us(slowSleep);
if (RA2)
{
PORTA = 0b00001000; // turning on PIC running LED
PORTB = 0b00000000; // turning off all PINS
__delay_ms(turnoffwait);
break;
}
}
}
void startFast()
{
PORTA = 0b10001000; // turning onn PIC running LED & fast LED
while (1)
{
//Step 1
PORTB = 0b00001001; // RB0 & RB3 high
__delay_us(fastSleep);
//Step 2
PORTB = 0b00001100; // RB2 & RB3 high
__delay_us(fastSleep);
//Step 3
PORTB = 0b00000110; // RB1 & RB2 high
__delay_us(fastSleep);
//Step 4
PORTB = 0b00000011; // RB0 & RB1 high
__delay_us(fastSleep);
if (RA2)
{
PORTA = 0b00001000; // turning on PIC running LED
PORTB = 0b00000000; // turning off all PINS
__delay_ms(turnoffwait);
break;
}
}
}
void main()
{
TRISA = 0b00000111; // RA0-RA2 input, RA3-RA7 output
TRISB = 0b00000000; // Setting all bits on port b to output
CMCON = 0x07; // disabeling comparator
PORTA = 0b00001000;
while (1) {
if (RA0)
{
startSlow();
}
if (RA1)
{
startFast();
}
}
}
|
|
Sist oppdatert onsdag 28. desember 2011 14:14 |