Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.33 KB | None | 0 0
  1. /***********************************************************/
  2. // Electro-mechanical Aeropress full code - Group 3 Bump N Grind
  3. // Zoe Bryant, James Hilton, Rob Hill, Kareem Machnouk
  4. // 12/01/18
  5. //*************************************************************************/
  6. // #pragma config FOSC = INTIO67
  7. // #pragma config WDTEN = OFF, LVP = OFF, MCLRE = OFF
  8. /*                                                                          */
  9.  
  10. #pragma config FOSC = INTIO67
  11. #pragma config WDTEN = OFF, LVP = OFF, MCLRE = OFF
  12.  
  13. /**INCLUDES********************/
  14. #include "p18f45k20.h"
  15.  
  16. #include <delays.h>
  17.  
  18.  
  19. /**SUBFUNCTIONS***************/
  20. void Step_Nema_17_FWD (void);
  21. void Step_Nema_17_BWD (void);
  22. int Nema_17_Steps = 0;
  23.  
  24. void Step_Nema_23_DWN (void);
  25. void Step_Nema_23_UP (void);
  26. int Nema_23_Steps = 0;
  27.  
  28. int limit17count = 0;
  29. int limit23count = 0;
  30.  
  31. int Coffee_Choice = 0; //Used to determine Brew Time
  32. void PrintCoffee (void); //Prints Coffee Option (based on Coffee_Choice variable)
  33. int Delay_Repeats = 0; //How many times brew animation has run
  34. int Brew_Timer (void); //Sets number of repeats of animation
  35.  
  36.  
  37. void Frame_1 (void); //Frame 1 of loading animation
  38. void Frame_2 (void); //Frame 2 of loading animation
  39.  
  40. int detectButtonPress(void);
  41.  
  42. void LCD_Clear (void);
  43. void LCD_Custom (void);
  44.  
  45. void LCD_init (void);
  46.  
  47. int ADC_Convert(void);
  48. void ADC_Init(void);
  49.  
  50. /**DEFINES*****************/
  51.  
  52. #define CLEAR_SCREEN    0b00000001
  53. #define FOUR_BIT        0b00101100
  54. #define LINES_5X7       0b00111000
  55. #define CURSOR_BLINK    0b00001111
  56. #define CURSOR_RIGHT    0b00000110
  57. #define CURSOR_HIDE     0b00001100
  58.  
  59. #define DATA_PORT  LATD
  60. #define RS_PIN     PORTDbits.RD6
  61. #define E_PIN      PORTDbits.RD7
  62.  
  63.  
  64. int detectButtonPress(void)        
  65. {
  66.     int button1count = 0;
  67.     int button2count = 0;
  68.     int button3count = 0;
  69.     int button4count = 0;
  70.     int button5count = 0;
  71.     int button6count = 0;
  72.  
  73.     while (button1count < 5 && button2count < 5 && button3count < 5 && button4count < 5 && button5count <5 && button6count < 5) {
  74.         if (PORTAbits.RA4 == 1) {
  75.             button1count = 0;
  76.         } else {
  77.             button1count++;
  78.         }
  79.    
  80.         if (PORTAbits.RA1 == 1) {
  81.             button2count = 0;
  82.         } else {
  83.             button2count++;
  84.         }
  85.    
  86.         if (PORTAbits.RA2 == 1) {
  87.             button3count = 0;
  88.         } else {
  89.             button3count++;
  90.         }
  91.        
  92.         if (PORTAbits.RA3 == 1) {
  93.             button4count = 0;
  94.         } else {
  95.             button4count++;
  96.         }
  97.        
  98.    
  99.         Delay10TCYx(25);    // delay 250 cycles or 1ms.
  100.     }
  101.    
  102.     if (button1count >= 5)
  103.     {
  104.         return 1;
  105.     }
  106.  
  107.     if (button2count >= 5)
  108.     {
  109.         return 2;
  110.     }
  111.  
  112.     if (button3count >= 5)
  113.     {
  114.         return 3;
  115.     }
  116.  
  117.     if (button4count >= 5)
  118.     {
  119.         return 4;
  120.     }
  121.    
  122. }
  123.  
  124.  
  125. void Delay5milli(void)                          //Suitable delay for LCD
  126. {
  127.  
  128. Delay1KTCYx(2);                                
  129. }
  130.  
  131.  
  132. void SetAddr(unsigned char DDaddr)
  133. {
  134.         DATA_PORT &= 0xf0;                      // Write upper nibble
  135.         DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
  136.                        
  137.         RS_PIN = 0;                             // Set control bit
  138.         Delay5milli();
  139.         E_PIN = 1;                              // Clock the cmd and address in
  140.         Delay5milli();
  141.         E_PIN = 0;
  142.  
  143.         DATA_PORT &= 0xf0;                      // Write lower nibble
  144.         DATA_PORT |= (DDaddr&0x0f);
  145.  
  146.         Delay5milli();
  147.         E_PIN = 1;                              // Clock the cmd and address in
  148.         Delay5milli();
  149.         E_PIN = 0;
  150. }
  151.  
  152. void WriteCmd(unsigned char cmd)
  153.     {
  154.         DATA_PORT &= 0xf0;
  155.         DATA_PORT |= (cmd>>4)&0x0f;          
  156.         RS_PIN = 0;                             // Set control signals for command
  157.         Delay5milli();
  158.         E_PIN = 1;                              // Clock command in
  159.         Delay5milli();
  160.         E_PIN = 0;
  161.  
  162.        
  163.         DATA_PORT &= 0xf0;                      // Lower nibble interface
  164.         DATA_PORT |= cmd&0x0f;
  165.         Delay5milli();
  166.         E_PIN = 1;                              // Clock command in
  167.         Delay5milli();
  168.         E_PIN = 0;
  169.     }
  170.  
  171. void WriteChar(char data)
  172. {
  173.         DATA_PORT &= 0xf0;
  174.         DATA_PORT |= ((data>>4)&0x0f);
  175.  
  176.         RS_PIN = 1;                             // Set control bits
  177.         Delay5milli();
  178.         E_PIN = 1;                              // Clock nibble into LCD
  179.         Delay5milli();
  180.         E_PIN = 0;
  181.  
  182.            
  183.         DATA_PORT &= 0xf0;                      // Lower nibble interface
  184.         DATA_PORT |= (data&0x0f);
  185.        
  186.         Delay5milli();
  187.         E_PIN = 1;                              // Clock nibble into LCD
  188.         Delay5milli();
  189.         E_PIN = 0;
  190. }
  191.  
  192.  void WriteString(const rom char *buffer)    
  193. {        
  194.         while(*buffer)                          // Write data to LCD up to null
  195.         {
  196.           Delay5milli();
  197.           WriteChar( *buffer);                  // Write character to LCD
  198.           buffer++;                             // Increment buffer
  199.         }
  200.         return;
  201. }  
  202.    
  203. /**MAIN***********************/
  204. void main (void) //main body
  205. {
  206.  
  207. //SET UP  
  208.     TRISD  = 0b00000000;                        //sets PORTd
  209.     LATD   = 0b00000000;                        //turns off PORTd outputs, good start position      
  210.     TRISC = 0b00000000;     // PORTC bits to output (0)        
  211.     ANSEL = 0;
  212.     ANSELH = 0;
  213.                  
  214.     LCD_init();
  215.     LCD_Custom();
  216.    
  217. while (1) //looping section of main
  218. {
  219.     LCD_Clear();                        
  220.     WriteString("Aeropress");
  221.     SetAddr (0xc0);
  222.     WriteString("Welcome");
  223.  
  224.         while (detectButtonPress() != 1) //Hang until button 1 is pressed
  225.         {
  226.         }
  227.  
  228.         LCD_Clear();                        
  229.         WriteString("Callibrating");
  230.  
  231.         limit17count = 0;
  232.         limit23count = 0;
  233.        
  234.         while (limit17count <= 5)
  235.         {
  236.         Step_Nema_17_BWD();
  237.         if (PORTAbits.RA7 == 1)
  238.             {
  239.             limit17count = 0;
  240.             }
  241.             else
  242.             {
  243.                 limit17count++;
  244.             }
  245.         }
  246.  
  247.         while (limit23count <= 5)
  248.         {
  249.         Step_Nema_23_UP();
  250.         if (PORTAbits.RA5 == 1)
  251.             {
  252.             limit23count = 0;
  253.             }
  254.             else
  255.             {
  256.                 limit23count++;
  257.             }
  258.         }
  259.  
  260.         LCD_Clear();                              
  261.         WriteString("Press SW1   ");
  262.         SetAddr (0xc0);
  263.         WriteString("To Start    ");
  264.  
  265.         while (detectButtonPress() != 1) //Hang until Button 1 is pressed
  266.         {
  267.         }
  268.        
  269.         PrintCoffee();
  270.  
  271.         do{                             //Change menu options untill button 1 is pressed
  272.        
  273.         switch (detectButtonPress()){
  274.         case 2:
  275.         Coffee_Choice = (Coffee_Choice+1) % 4; //cycle option up
  276.         PrintCoffee();
  277.         break;
  278.        
  279.         case 3:
  280.         Coffee_Choice = (Coffee_Choice-1) % 4; //cycle option down
  281.         PrintCoffee();
  282.         break;
  283.         }  
  284.         }   while (detectButtonPress() != 1);  
  285.        
  286.  
  287.  
  288.         LCD_Clear();            
  289.         WriteString("Moving Coffee   ");
  290.         SetAddr (0xc0);
  291.         WriteString("Chamber Forwards   ");
  292.    
  293.         for (Nema_17_Steps=0; Nema_17_Steps < 325; Nema_17_Steps++)
  294.                 {
  295.                     Step_Nema_17_FWD();                 //Steps Forward 1
  296.                 }
  297.    
  298.         LCD_Clear();          
  299.         WriteString("Insert Coffee     ");
  300.         SetAddr (0xc0);
  301.         WriteString("& Water      ");
  302.    
  303.         while (detectButtonPress() != 1) //wait till button 1 is pressed
  304.         {
  305.         }
  306.    
  307.  
  308.     limit17count = 0;
  309.  
  310.     while (limit17count <= 5)
  311.         {
  312.         Step_Nema_17_BWD();
  313.         if (PORTAbits.RA7 == 1)
  314.             {
  315.             limit17count = 0;
  316.             }
  317.             else
  318.             {
  319.                 limit17count++;
  320.             }
  321.         }
  322.      
  323.     //ADC check that Coffee chamber is homed properly
  324.         ADC_Init();
  325.             ADC_Convert();
  326.             while (ADRESH < 0b01100000) //if further than ~5cm away Print Warning Screen
  327.             {
  328.                 LCD_Clear();        
  329.                     WriteString("Warning! Add");
  330.                 SetAddr (0xc0);
  331.                 WriteString("Brew Chamber");
  332.                 ADC_Convert();
  333.    
  334.             }
  335.  
  336.  
  337.         LCD_Clear();        
  338.         WriteString("Brewing");
  339.    
  340.         Delay_Repeats == 0;
  341.    
  342.         for (Nema_23_Steps=0; Nema_23_Steps < 250; Nema_23_Steps++) //steps to plunge limit
  343.             {
  344.                 Step_Nema_23_DWN();                 //Steps Forward 1
  345.             }
  346.        
  347.         for (Delay_Repeats=0; Delay_Repeats < Brew_Timer(); Delay_Repeats++) //repeat number of times equal to Brew_Timer value
  348.             {
  349.             Frame_1();
  350.             Delay10KTCYx(16);   //1s delay accounting for time to print
  351.             Frame_2();
  352.             Delay10KTCYx(16);   //same
  353.             }
  354.  
  355.         LCD_Clear();        
  356.         WriteString("Plunging");
  357.    
  358.         for (Nema_23_Steps=0; Nema_23_Steps < 2600; Nema_23_Steps++) //steps to plunge limit
  359.             {
  360.                 Step_Nema_23_DWN();                 //Steps Forward 1
  361.             }
  362.    
  363.         LCD_Clear();        
  364.         WriteString("Coffee Ready");
  365.         SetAddr (0xc0);
  366.         WriteString("Remove Mug");
  367.    
  368.         while (detectButtonPress() != 1)
  369.         {
  370.         }
  371.        
  372.         /*LCD_Clear();        
  373.         WriteString("Remove Cap");
  374.         SetAddr (0xc0);
  375.         WriteString("Add Waste Bin");
  376.  
  377.  
  378.         while (detectButtonPress() != 1)
  379.         {
  380.         }
  381.        
  382.         for (Nema_23_Steps=0; Nema_23_Steps < 300; Nema_23_Steps++) //steps till lower limit
  383.             {
  384.                 Step_Nema_23_DWN();                 //Steps Forward 1
  385.             }*/
  386.  
  387.         LCD_Clear();        
  388.         WriteString("Press SW4");
  389.         SetAddr (0xc0);
  390.         WriteString("To Reset");
  391.  
  392.         while (detectButtonPress() != 4)
  393.         {
  394.         }
  395.  
  396.         for ( ; Nema_23_Steps != 0; Nema_23_Steps--)
  397.                 {
  398.                 Step_Nema_23_UP();
  399.                 }
  400. }
  401. }
  402. void Step_Nema_17_FWD(void) //moves Nema 17 stepper motor forwards 1
  403. {
  404.     LATCbits.LATC2 = 0; //Anticlockwise
  405.     LATCbits.LATC3 = 1;    
  406.     Delay1KTCYx(5);
  407.     LATCbits.LATC3 = 0;
  408.     Delay1KTCYx(5);
  409. }
  410.  
  411. void Step_Nema_17_BWD(void) //moves Nema 17 stepper motor backwards 1
  412. {
  413.     LATCbits.LATC2 = 1; //Clockwise
  414.     LATCbits.LATC3 = 1;    
  415.     Delay1KTCYx(5);
  416.     LATCbits.LATC3 = 0;
  417.     Delay1KTCYx(5);
  418. }
  419.  
  420. void Step_Nema_23_DWN(void) //moves Nema 23 stepper motor down 1
  421. {
  422.     LATCbits.LATC0 = 0; //Anticlockwise
  423.     LATCbits.LATC1 = 1;    
  424.     Delay1KTCYx(5);
  425.     LATCbits.LATC1 = 0;
  426.     Delay1KTCYx(5);
  427. }
  428.  
  429. void Step_Nema_23_UP(void) //moves Nema 23 stepper motor up 1
  430. {
  431.     LATCbits.LATC0 = 1; //Clockwise
  432.     LATCbits.LATC1 = 1;    
  433.     Delay1KTCYx(5);
  434.     LATCbits.LATC1 = 0;
  435.     Delay1KTCYx(5);
  436. }
  437.  
  438. void LCD_init (void) //initialises the LCD
  439. {
  440.     WriteCmd ( 0x02 );                          // sets 4bit operation
  441.     WriteCmd ( CLEAR_SCREEN);      
  442.     WriteCmd ( FOUR_BIT & LINES_5X7 );          // sets 5x7 and multiline operation.
  443.     WriteCmd ( CURSOR_HIDE );                  // hides cursor
  444.     WriteCmd ( CURSOR_RIGHT  );                 // moves cursor right  
  445. }
  446.  
  447.  
  448. void LCD_Clear (void) //clears the LCD screen
  449. {
  450.     SetAddr  (0);
  451.             Delay1KTCYx(5);
  452.             WriteCmd ( CLEAR_SCREEN);      
  453.             Delay1KTCYx(5);                            //delay  
  454. }  
  455.  
  456.  
  457. int Brew_Timer (void) //use coffee selection value to set how many times animation with timer repeats
  458. {                       //can be 1, 3, 4 or 5min. Animation is 1sec a frame, 2 frame animation
  459. switch(Coffee_Choice){
  460.     case 0:
  461.             return 30;
  462.     break;
  463.     case 1:
  464.             return 90;
  465.     break;
  466.     case 2:
  467.             return 120;
  468.     break;
  469.     case 3:
  470.             return 150;
  471.     break;
  472. }
  473. }
  474.    
  475.  
  476.  
  477.  
  478. void PrintCoffee (void) //Prints current coffee selection
  479. {
  480.  
  481. switch(Coffee_Choice){
  482. case 0:
  483.         LCD_Clear();                        
  484.         WriteString("1min brew");
  485. break;
  486. case 1:
  487.         LCD_Clear();                        
  488.         WriteString("3min brew");
  489. break;
  490. case 2:
  491.         LCD_Clear();                        
  492.         WriteString("4min brew");
  493. break;
  494. case 3:
  495.         LCD_Clear();                        
  496.         WriteString("5min brew");
  497. break;
  498. }
  499. }
  500.  
  501. void LCD_Custom (void) //defines custom LCD characters
  502. {
  503. WriteCmd (0x40);
  504. WriteChar (0b00011111);
  505. WriteChar (0b00011111);
  506. WriteChar (0b00010000);
  507. WriteChar (0b00001111);
  508. WriteChar (0b00000111);
  509. WriteChar (0b00000111);
  510. WriteChar (0b00000011);
  511. WriteChar (0b00000001);
  512.  
  513.  
  514. WriteCmd (0x48);
  515. WriteChar (0b00011111);
  516. WriteChar (0b00011111);
  517. WriteChar (0b00000010);
  518. WriteChar (0b00011110);
  519. WriteChar (0b00011100);
  520. WriteChar (0b00011100);
  521. WriteChar (0b00011000);
  522. WriteChar (0b00010000);
  523.  
  524. WriteCmd (0x50);
  525. WriteChar (0b00000001);
  526. WriteChar (0b00000010);
  527. WriteChar (0b00000110);
  528. WriteChar (0b00000100);
  529. WriteChar (0b00001100);
  530. WriteChar (0b00001000);
  531. WriteChar (0b00011111);
  532. WriteChar (0b00011111);
  533.  
  534. WriteCmd (0x58);
  535. WriteChar (0b00010000);
  536. WriteChar (0b00001000);
  537. WriteChar (0b00001100);
  538. WriteChar (0b00000100);
  539. WriteChar (0b00000110);
  540. WriteChar (0b00000010);
  541. WriteChar (0b00011111);
  542. WriteChar (0b00011111);
  543.  
  544. WriteCmd (0x60);
  545. WriteChar (0b00011111);
  546. WriteChar (0b00011111);
  547. WriteChar (0b00001000);
  548. WriteChar (0b00001100);
  549. WriteChar (0b00000100);
  550. WriteChar (0b00000110);
  551. WriteChar (0b00000010);
  552. WriteChar (0b00000001);
  553.  
  554. WriteCmd (0x68);
  555. WriteChar (0b00011111);
  556. WriteChar (0b00011111);
  557. WriteChar (0b00000010);
  558. WriteChar (0b00000110);
  559. WriteChar (0b00000100);
  560. WriteChar (0b00001100);
  561. WriteChar (0b00001000);
  562. WriteChar (0b00010000);
  563.  
  564. WriteCmd (0x70);
  565. WriteChar (0b00000001);
  566. WriteChar (0b00000010);
  567. WriteChar (0b00000110);
  568. WriteChar (0b00000111);
  569. WriteChar (0b00001111);
  570. WriteChar (0b00001111);
  571. WriteChar (0b00011111);
  572. WriteChar (0b00011111);
  573.  
  574. WriteCmd (0x78);
  575. WriteChar (0b00010000);
  576. WriteChar (0b00001000);
  577. WriteChar (0b00001100);
  578. WriteChar (0b00011100);
  579. WriteChar (0b00011110);
  580. WriteChar (0b00011110);
  581. WriteChar (0b00011111);
  582. WriteChar (0b00011111);
  583. }
  584.  
  585. void Frame_1 (void) //prints frame 1 of timer animation (using 4 custom characters)
  586. {
  587. LCD_Clear();  
  588. //printf("%d", Delay_Repeats , exp(Delay_Repeats));
  589. SetAddr  (0x07);
  590. WriteChar (0);
  591. SetAddr (0x08);
  592. WriteChar (1);
  593. SetAddr (0xc7);
  594. WriteChar (2);
  595. SetAddr (0xc8);
  596. WriteChar (3);
  597. }
  598.  
  599. void Frame_2 (void) //prints frame 2 of timer animation (using 4 custom characters)
  600. {
  601. LCD_Clear();  
  602. //printf("%d", Delay_Repeats , exp(Delay_Repeats));
  603. SetAddr  (0x07);
  604. WriteChar (4);
  605. SetAddr (0x08);
  606. WriteChar (5);
  607. SetAddr (0xc7);
  608. WriteChar (6);
  609. SetAddr (0xc8);
  610. WriteChar (7);
  611. }
  612.  
  613. void ADC_Init(void)
  614. {  
  615.     // initialize the Analog-To-Digital converter.
  616.     // First, we need to make sure the AN0 pin is enabled as an analog input
  617.     // as the demo board potentiometer is connected to RA0/AN0
  618.     // Don't forget that RB0/AN12 must be digital!
  619.  
  620.     ANSEL = 0;  //turn off all other analog inputs
  621.     ANSELH = 0;
  622.     ANSELbits.ANS0 = 1; // turn on RA5 analog
  623.  
  624.     // Sets bits VCFG1 and VCFG0 in ADCON1 so the ADC voltage reference is VSS to VDD
  625.  
  626.     ADCON1 = 0;
  627.    
  628.     //
  629.     // ADFM = 0 so we can easily read the 8 Most Significant bits from the ADRESH
  630.     // Special Function Register
  631.    
  632.     ADCON2 = 0b00111000;
  633.  
  634.     // Select channel 0 (AN0) to read the An0 voltage and turn on ADC
  635.     ADCON0 = 0b00000001;
  636.  
  637. }
  638.  
  639. int ADC_Convert(void)
  640.  
  641. { // start an ADC conversion and return the 8 most-significant bits of the result
  642.     ADCON0bits.GO_DONE = 1;             // start conversion
  643.     while (ADCON0bits.GO_DONE == 1);    // wait for it to complete
  644.     return ADRESH;                      // return high byte of result
  645. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement