Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <msp430.h>
- const int RPM = 1000;
- volatile int IR01;
- volatile int IR02;
- void spin01();
- void spin02();
- void spinBoth();
- /**
- * main.c
- */
- void main(void)
- {
- WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
- P1OUT = 0x00;
- P2OUT = 0x00;
- P1REN = 0x00;
- P2REN = 0x00;
- // declarations for IR
- P1DIR &= ~0x01; // 1.0
- P1DIR &= ~0x80; // 1.7
- // declarations for motor
- P1DIR |= 0x04 | 0x08 | 0x10 | 0x20; // 1.2 | 1.3 | 1.4 | 1.5
- P2DIR |= 0x40 | 0x20 | 0x10 | 0x08; // 2.6 | 2.5 | 2.4 | 2.3
- // void loop() {
- while (1) {
- IR01 = P1IN & 0x01;
- IR02 = P1IN & 0x80;
- if (IR01 && IR02) {
- // do nothing as na tritrigger ako pag both off an usa na spin la gihap
- }
- else if (IR01) {
- spin01();
- }
- else if (IR02) {
- spin02();
- }
- else {
- spinBoth();
- }
- }
- }
- void spin01() {
- // 1st cycle
- P1OUT |= 0x04; // high
- P1OUT &= ~(0x08 | 0x10 | 0x20); // low
- __delay_cycles(RPM);
- P1OUT |= 0x04 | 0x08;
- P1OUT &= ~(0x10 | 0x20);
- __delay_cycles(RPM);
- // 2nd cycle
- P1OUT |= 0x08;
- P1OUT &= ~(0x04 | 0x10 | 0x20);
- __delay_cycles(RPM);
- P1OUT |= 0x08 | 0x10;
- P1OUT &= ~(0x04 | 0x20);
- __delay_cycles(RPM);
- // 3rd cycle
- P1OUT |= 0x10;
- P1OUT &= ~(0x04 | 0x08 | 0x20);
- __delay_cycles(RPM);
- P1OUT |= 0x10 | 0x20;
- P1OUT &= ~(0x04 | 0x08);
- __delay_cycles(RPM);
- // 4th cycle
- P1OUT |= 0x20;
- P1OUT &= ~(0x04 | 0x08 | 0x10);
- __delay_cycles(RPM);
- P1OUT |= 0x20 | 0x04;
- P1OUT &= ~(0x08 | 0x10);
- __delay_cycles(RPM);
- }
- void spin02() {
- // 1st cycle
- P2OUT |= 0x40; // high
- P2OUT &= ~(0x20 | 0x10 | 0x08); // low
- __delay_cycles(RPM);
- P2OUT |= 0x40 | 0x20;
- P2OUT &= ~(0x10 | 0x08);
- __delay_cycles(RPM);
- // 2nd cycle
- P2OUT |= 0x20;
- P2OUT &= ~(0x40 | 0x10 | 0x08);
- __delay_cycles(RPM);
- P2OUT |= 0x20 | 0x10;
- P2OUT &= ~(0x40 | 0x08);
- __delay_cycles(RPM);
- // 3rd cycle
- P2OUT |= 0x10;
- P2OUT &= ~(0x40 | 0x20 | 0x08);
- __delay_cycles(RPM);
- P2OUT |= 0x10 | 0x08;
- P2OUT &= ~(0x40 | 0x20);
- __delay_cycles(RPM);
- // 4th cycle
- P2OUT |= 0x08;
- P2OUT &= ~(0x40 | 0x20 | 0x10);
- __delay_cycles(RPM);
- P2OUT |= 0x08 | 0x40;
- P2OUT &= ~(0x20 | 0x10);
- __delay_cycles(RPM);
- }
- void spinBoth() {
- // 1st cycle
- P1OUT |= 0x04; // high
- P1OUT &= ~(0x08 | 0x10 | 0x20); // low
- P2OUT |= 0x40;
- P2OUT &= ~(0x20 | 0x10 | 0x08);
- __delay_cycles(RPM);
- P1OUT |= 0x04 | 0x08;
- P1OUT &= ~(0x10 | 0x20);
- P2OUT |= 0x40 | 0x20;
- P2OUT &= ~(0x10 | 0x08);
- __delay_cycles(RPM);
- // 2nd cycle
- P1OUT |= 0x08;
- P1OUT &= ~(0x04 | 0x10 | 0x20);
- P2OUT |= 0x20;
- P2OUT &= ~(0x40 | 0x10 | 0x08);
- __delay_cycles(RPM);
- P1OUT |= 0x08 | 0x10;
- P1OUT &= ~(0x04 | 0x20);
- P2OUT |= 0x20 | 0x10;
- P2OUT &= ~(0x40 | 0x08);
- __delay_cycles(RPM);
- // 3rd cycle
- P1OUT |= 0x10;
- P1OUT &= ~(0x04 | 0x08 | 0x20);
- P2OUT |= 0x10;
- P2OUT &= ~(0x40 | 0x20 | 0x08);
- __delay_cycles(RPM);
- P1OUT |= 0x10 | 0x20;
- P1OUT &= ~(0x04 | 0x08);
- P2OUT |= 0x10 | 0x08;
- P2OUT &= ~(0x40 | 0x20);
- __delay_cycles(RPM);
- // 4th cycle
- P1OUT |= 0x20;
- P1OUT &= ~(0x04 | 0x08 | 0x10);
- P2OUT |= 0x08;
- P2OUT &= ~(0x40 | 0x20 | 0x10);
- __delay_cycles(RPM);
- P1OUT |= 0x20 | 0x04;
- P1OUT &= ~(0x08 | 0x10);
- P2OUT |= 0x08 | 0x40;
- P2OUT &= ~(0x20 | 0x10);
- __delay_cycles(RPM);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement