Advertisement
CHU2

Line Follower

Dec 20th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | Source Code | 0 0
  1. #include <msp430.h>
  2.  
  3. const int RPM = 1000;
  4. volatile int IR01;
  5. volatile int IR02;
  6.  
  7. void spin01();
  8. void spin02();
  9. void spinBoth();
  10.  
  11. /**
  12.  * main.c
  13.  */
  14. void main(void)
  15. {
  16.     WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
  17.  
  18.     P1OUT = 0x00;
  19.     P2OUT = 0x00;
  20.     P1REN = 0x00;
  21.     P2REN = 0x00;
  22.  
  23.     // declarations for IR
  24.     P1DIR &= ~0x01; // 1.0
  25.     P1DIR &= ~0x80; // 1.7
  26.  
  27.     // declarations for motor
  28.     P1DIR |= 0x04 | 0x08 | 0x10 | 0x20; // 1.2 | 1.3 | 1.4 | 1.5
  29.     P2DIR |= 0x40 | 0x20 | 0x10 | 0x08; // 2.6 | 2.5 | 2.4 | 2.3
  30.  
  31.     // void loop() {
  32.     while (1) {
  33.         IR01 = P1IN & 0x01;
  34.         IR02 = P1IN & 0x80;
  35.  
  36.         if (IR01 && IR02) {
  37.             // do nothing as na tritrigger ako pag both off an usa na spin la gihap
  38.         }
  39.         else if (IR01) {
  40.             spin01();
  41.         }
  42.         else if (IR02) {
  43.             spin02();
  44.         }
  45.         else {
  46.             spinBoth();
  47.         }
  48.     }
  49. }
  50.  
  51. void spin01() {
  52.     // 1st cycle
  53.     P1OUT |= 0x04;                  // high
  54.     P1OUT &= ~(0x08 | 0x10 | 0x20); // low
  55.     __delay_cycles(RPM);
  56.  
  57.     P1OUT |= 0x04 | 0x08;
  58.     P1OUT &= ~(0x10 | 0x20);
  59.     __delay_cycles(RPM);
  60.  
  61.     // 2nd cycle
  62.     P1OUT |= 0x08;
  63.     P1OUT &= ~(0x04 | 0x10 | 0x20);
  64.     __delay_cycles(RPM);
  65.  
  66.     P1OUT |= 0x08 | 0x10;
  67.     P1OUT &= ~(0x04 | 0x20);
  68.     __delay_cycles(RPM);
  69.  
  70.     // 3rd cycle
  71.     P1OUT |= 0x10;
  72.     P1OUT &= ~(0x04 | 0x08 | 0x20);
  73.     __delay_cycles(RPM);
  74.  
  75.     P1OUT |= 0x10 | 0x20;
  76.     P1OUT &= ~(0x04 | 0x08);
  77.     __delay_cycles(RPM);
  78.  
  79.     // 4th cycle
  80.     P1OUT |= 0x20;
  81.     P1OUT &= ~(0x04 | 0x08 | 0x10);
  82.     __delay_cycles(RPM);
  83.  
  84.     P1OUT |= 0x20 | 0x04;
  85.     P1OUT &= ~(0x08 | 0x10);
  86.     __delay_cycles(RPM);
  87. }
  88.  
  89. void spin02() {
  90.     // 1st cycle
  91.     P2OUT |= 0x40;                  // high
  92.     P2OUT &= ~(0x20 | 0x10 | 0x08); // low
  93.     __delay_cycles(RPM);
  94.  
  95.     P2OUT |= 0x40 | 0x20;
  96.     P2OUT &= ~(0x10 | 0x08);
  97.     __delay_cycles(RPM);
  98.  
  99.     // 2nd cycle
  100.     P2OUT |= 0x20;
  101.     P2OUT &= ~(0x40 | 0x10 | 0x08);
  102.     __delay_cycles(RPM);
  103.  
  104.     P2OUT |= 0x20 | 0x10;
  105.     P2OUT &= ~(0x40 | 0x08);
  106.     __delay_cycles(RPM);
  107.  
  108.     // 3rd cycle
  109.     P2OUT |= 0x10;
  110.     P2OUT &= ~(0x40 | 0x20 | 0x08);
  111.     __delay_cycles(RPM);
  112.  
  113.     P2OUT |= 0x10 | 0x08;
  114.     P2OUT &= ~(0x40 | 0x20);
  115.     __delay_cycles(RPM);
  116.  
  117.     // 4th cycle
  118.     P2OUT |= 0x08;
  119.     P2OUT &= ~(0x40 | 0x20 | 0x10);
  120.     __delay_cycles(RPM);
  121.  
  122.     P2OUT |= 0x08 | 0x40;
  123.     P2OUT &= ~(0x20 | 0x10);
  124.     __delay_cycles(RPM);
  125. }
  126.  
  127. void spinBoth() {
  128.     // 1st cycle
  129.     P1OUT |= 0x04;                  // high
  130.     P1OUT &= ~(0x08 | 0x10 | 0x20); // low
  131.  
  132.     P2OUT |= 0x40;
  133.     P2OUT &= ~(0x20 | 0x10 | 0x08);
  134.  
  135.     __delay_cycles(RPM);
  136.  
  137.     P1OUT |= 0x04 | 0x08;
  138.     P1OUT &= ~(0x10 | 0x20);
  139.  
  140.     P2OUT |= 0x40 | 0x20;
  141.     P2OUT &= ~(0x10 | 0x08);
  142.  
  143.     __delay_cycles(RPM);
  144.  
  145.     // 2nd cycle
  146.     P1OUT |= 0x08;
  147.     P1OUT &= ~(0x04 | 0x10 | 0x20);
  148.  
  149.     P2OUT |= 0x20;
  150.     P2OUT &= ~(0x40 | 0x10 | 0x08);
  151.  
  152.     __delay_cycles(RPM);
  153.  
  154.     P1OUT |= 0x08 | 0x10;
  155.     P1OUT &= ~(0x04 | 0x20);
  156.  
  157.     P2OUT |= 0x20 | 0x10;
  158.     P2OUT &= ~(0x40 | 0x08);
  159.  
  160.     __delay_cycles(RPM);
  161.  
  162.     // 3rd cycle
  163.     P1OUT |= 0x10;
  164.     P1OUT &= ~(0x04 | 0x08 | 0x20);
  165.  
  166.     P2OUT |= 0x10;
  167.     P2OUT &= ~(0x40 | 0x20 | 0x08);
  168.  
  169.     __delay_cycles(RPM);
  170.  
  171.     P1OUT |= 0x10 | 0x20;
  172.     P1OUT &= ~(0x04 | 0x08);
  173.  
  174.     P2OUT |= 0x10 | 0x08;
  175.     P2OUT &= ~(0x40 | 0x20);
  176.  
  177.     __delay_cycles(RPM);
  178.  
  179.     // 4th cycle
  180.     P1OUT |= 0x20;
  181.     P1OUT &= ~(0x04 | 0x08 | 0x10);
  182.  
  183.     P2OUT |= 0x08;
  184.     P2OUT &= ~(0x40 | 0x20 | 0x10);
  185.  
  186.     __delay_cycles(RPM);
  187.  
  188.     P1OUT |= 0x20 | 0x04;
  189.     P1OUT &= ~(0x08 | 0x10);
  190.  
  191.     P2OUT |= 0x08 | 0x40;
  192.     P2OUT &= ~(0x20 | 0x10);
  193.  
  194.     __delay_cycles(RPM);
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement