Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************/
- // Electro-mechanical Aeropress full code - Group 3 Bump N Grind
- // Zoe Bryant, James Hilton, Rob Hill, Kareem Machnouk
- // 12/01/18
- //*************************************************************************/
- // #pragma config FOSC = INTIO67
- // #pragma config WDTEN = OFF, LVP = OFF, MCLRE = OFF
- /* */
- #pragma config FOSC = INTIO67
- #pragma config WDTEN = OFF, LVP = OFF, MCLRE = OFF
- /**INCLUDES********************/
- #include "p18f45k20.h"
- #include <delays.h>
- /**SUBFUNCTIONS***************/
- void Step_Nema_17_FWD (void);
- void Step_Nema_17_BWD (void);
- int Nema_17_Steps = 0;
- void Step_Nema_23_DWN (void);
- void Step_Nema_23_UP (void);
- int Nema_23_Steps = 0;
- int limit17count = 0;
- int limit23count = 0;
- int Coffee_Choice = 0; //Used to determine Brew Time
- void PrintCoffee (void); //Prints Coffee Option (based on Coffee_Choice variable)
- int Delay_Repeats = 0; //How many times brew animation has run
- int Brew_Timer (void); //Sets number of repeats of animation
- void Frame_1 (void); //Frame 1 of loading animation
- void Frame_2 (void); //Frame 2 of loading animation
- int detectButtonPress(void);
- void LCD_Clear (void);
- void LCD_Custom (void);
- void LCD_init (void);
- int ADC_Convert(void);
- void ADC_Init(void);
- /**DEFINES*****************/
- #define CLEAR_SCREEN 0b00000001
- #define FOUR_BIT 0b00101100
- #define LINES_5X7 0b00111000
- #define CURSOR_BLINK 0b00001111
- #define CURSOR_RIGHT 0b00000110
- #define CURSOR_HIDE 0b00001100
- #define DATA_PORT LATD
- #define RS_PIN PORTDbits.RD6
- #define E_PIN PORTDbits.RD7
- int detectButtonPress(void)
- {
- int button1count = 0;
- int button2count = 0;
- int button3count = 0;
- int button4count = 0;
- int button5count = 0;
- int button6count = 0;
- while (button1count < 5 && button2count < 5 && button3count < 5 && button4count < 5 && button5count <5 && button6count < 5) {
- if (PORTAbits.RA4 == 1) {
- button1count = 0;
- } else {
- button1count++;
- }
- if (PORTAbits.RA1 == 1) {
- button2count = 0;
- } else {
- button2count++;
- }
- if (PORTAbits.RA2 == 1) {
- button3count = 0;
- } else {
- button3count++;
- }
- if (PORTAbits.RA3 == 1) {
- button4count = 0;
- } else {
- button4count++;
- }
- Delay10TCYx(25); // delay 250 cycles or 1ms.
- }
- if (button1count >= 5)
- {
- return 1;
- }
- if (button2count >= 5)
- {
- return 2;
- }
- if (button3count >= 5)
- {
- return 3;
- }
- if (button4count >= 5)
- {
- return 4;
- }
- }
- void Delay5milli(void) //Suitable delay for LCD
- {
- Delay1KTCYx(2);
- }
- void SetAddr(unsigned char DDaddr)
- {
- DATA_PORT &= 0xf0; // Write upper nibble
- DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
- RS_PIN = 0; // Set control bit
- Delay5milli();
- E_PIN = 1; // Clock the cmd and address in
- Delay5milli();
- E_PIN = 0;
- DATA_PORT &= 0xf0; // Write lower nibble
- DATA_PORT |= (DDaddr&0x0f);
- Delay5milli();
- E_PIN = 1; // Clock the cmd and address in
- Delay5milli();
- E_PIN = 0;
- }
- void WriteCmd(unsigned char cmd)
- {
- DATA_PORT &= 0xf0;
- DATA_PORT |= (cmd>>4)&0x0f;
- RS_PIN = 0; // Set control signals for command
- Delay5milli();
- E_PIN = 1; // Clock command in
- Delay5milli();
- E_PIN = 0;
- DATA_PORT &= 0xf0; // Lower nibble interface
- DATA_PORT |= cmd&0x0f;
- Delay5milli();
- E_PIN = 1; // Clock command in
- Delay5milli();
- E_PIN = 0;
- }
- void WriteChar(char data)
- {
- DATA_PORT &= 0xf0;
- DATA_PORT |= ((data>>4)&0x0f);
- RS_PIN = 1; // Set control bits
- Delay5milli();
- E_PIN = 1; // Clock nibble into LCD
- Delay5milli();
- E_PIN = 0;
- DATA_PORT &= 0xf0; // Lower nibble interface
- DATA_PORT |= (data&0x0f);
- Delay5milli();
- E_PIN = 1; // Clock nibble into LCD
- Delay5milli();
- E_PIN = 0;
- }
- void WriteString(const rom char *buffer)
- {
- while(*buffer) // Write data to LCD up to null
- {
- Delay5milli();
- WriteChar( *buffer); // Write character to LCD
- buffer++; // Increment buffer
- }
- return;
- }
- /**MAIN***********************/
- void main (void) //main body
- {
- //SET UP
- TRISD = 0b00000000; //sets PORTd
- LATD = 0b00000000; //turns off PORTd outputs, good start position
- TRISC = 0b00000000; // PORTC bits to output (0)
- ANSEL = 0;
- ANSELH = 0;
- LCD_init();
- LCD_Custom();
- while (1) //looping section of main
- {
- LCD_Clear();
- WriteString("Aeropress");
- SetAddr (0xc0);
- WriteString("Welcome");
- while (detectButtonPress() != 1) //Hang until button 1 is pressed
- {
- }
- LCD_Clear();
- WriteString("Callibrating");
- limit17count = 0;
- limit23count = 0;
- while (limit17count <= 5)
- {
- Step_Nema_17_BWD();
- if (PORTAbits.RA7 == 1)
- {
- limit17count = 0;
- }
- else
- {
- limit17count++;
- }
- }
- while (limit23count <= 5)
- {
- Step_Nema_23_UP();
- if (PORTAbits.RA5 == 1)
- {
- limit23count = 0;
- }
- else
- {
- limit23count++;
- }
- }
- LCD_Clear();
- WriteString("Press SW1 ");
- SetAddr (0xc0);
- WriteString("To Start ");
- while (detectButtonPress() != 1) //Hang until Button 1 is pressed
- {
- }
- PrintCoffee();
- do{ //Change menu options untill button 1 is pressed
- switch (detectButtonPress()){
- case 2:
- Coffee_Choice = (Coffee_Choice+1) % 4; //cycle option up
- PrintCoffee();
- break;
- case 3:
- Coffee_Choice = (Coffee_Choice-1) % 4; //cycle option down
- PrintCoffee();
- break;
- }
- } while (detectButtonPress() != 1);
- LCD_Clear();
- WriteString("Moving Coffee ");
- SetAddr (0xc0);
- WriteString("Chamber Forwards ");
- for (Nema_17_Steps=0; Nema_17_Steps < 325; Nema_17_Steps++)
- {
- Step_Nema_17_FWD(); //Steps Forward 1
- }
- LCD_Clear();
- WriteString("Insert Coffee ");
- SetAddr (0xc0);
- WriteString("& Water ");
- while (detectButtonPress() != 1) //wait till button 1 is pressed
- {
- }
- limit17count = 0;
- while (limit17count <= 5)
- {
- Step_Nema_17_BWD();
- if (PORTAbits.RA7 == 1)
- {
- limit17count = 0;
- }
- else
- {
- limit17count++;
- }
- }
- //ADC check that Coffee chamber is homed properly
- ADC_Init();
- ADC_Convert();
- while (ADRESH < 0b01100000) //if further than ~5cm away Print Warning Screen
- {
- LCD_Clear();
- WriteString("Warning! Add");
- SetAddr (0xc0);
- WriteString("Brew Chamber");
- ADC_Convert();
- }
- LCD_Clear();
- WriteString("Brewing");
- Delay_Repeats == 0;
- for (Nema_23_Steps=0; Nema_23_Steps < 250; Nema_23_Steps++) //steps to plunge limit
- {
- Step_Nema_23_DWN(); //Steps Forward 1
- }
- for (Delay_Repeats=0; Delay_Repeats < Brew_Timer(); Delay_Repeats++) //repeat number of times equal to Brew_Timer value
- {
- Frame_1();
- Delay10KTCYx(16); //1s delay accounting for time to print
- Frame_2();
- Delay10KTCYx(16); //same
- }
- LCD_Clear();
- WriteString("Plunging");
- for (Nema_23_Steps=0; Nema_23_Steps < 2600; Nema_23_Steps++) //steps to plunge limit
- {
- Step_Nema_23_DWN(); //Steps Forward 1
- }
- LCD_Clear();
- WriteString("Coffee Ready");
- SetAddr (0xc0);
- WriteString("Remove Mug");
- while (detectButtonPress() != 1)
- {
- }
- /*LCD_Clear();
- WriteString("Remove Cap");
- SetAddr (0xc0);
- WriteString("Add Waste Bin");
- while (detectButtonPress() != 1)
- {
- }
- for (Nema_23_Steps=0; Nema_23_Steps < 300; Nema_23_Steps++) //steps till lower limit
- {
- Step_Nema_23_DWN(); //Steps Forward 1
- }*/
- LCD_Clear();
- WriteString("Press SW4");
- SetAddr (0xc0);
- WriteString("To Reset");
- while (detectButtonPress() != 4)
- {
- }
- for ( ; Nema_23_Steps != 0; Nema_23_Steps--)
- {
- Step_Nema_23_UP();
- }
- }
- }
- void Step_Nema_17_FWD(void) //moves Nema 17 stepper motor forwards 1
- {
- LATCbits.LATC2 = 0; //Anticlockwise
- LATCbits.LATC3 = 1;
- Delay1KTCYx(5);
- LATCbits.LATC3 = 0;
- Delay1KTCYx(5);
- }
- void Step_Nema_17_BWD(void) //moves Nema 17 stepper motor backwards 1
- {
- LATCbits.LATC2 = 1; //Clockwise
- LATCbits.LATC3 = 1;
- Delay1KTCYx(5);
- LATCbits.LATC3 = 0;
- Delay1KTCYx(5);
- }
- void Step_Nema_23_DWN(void) //moves Nema 23 stepper motor down 1
- {
- LATCbits.LATC0 = 0; //Anticlockwise
- LATCbits.LATC1 = 1;
- Delay1KTCYx(5);
- LATCbits.LATC1 = 0;
- Delay1KTCYx(5);
- }
- void Step_Nema_23_UP(void) //moves Nema 23 stepper motor up 1
- {
- LATCbits.LATC0 = 1; //Clockwise
- LATCbits.LATC1 = 1;
- Delay1KTCYx(5);
- LATCbits.LATC1 = 0;
- Delay1KTCYx(5);
- }
- void LCD_init (void) //initialises the LCD
- {
- WriteCmd ( 0x02 ); // sets 4bit operation
- WriteCmd ( CLEAR_SCREEN);
- WriteCmd ( FOUR_BIT & LINES_5X7 ); // sets 5x7 and multiline operation.
- WriteCmd ( CURSOR_HIDE ); // hides cursor
- WriteCmd ( CURSOR_RIGHT ); // moves cursor right
- }
- void LCD_Clear (void) //clears the LCD screen
- {
- SetAddr (0);
- Delay1KTCYx(5);
- WriteCmd ( CLEAR_SCREEN);
- Delay1KTCYx(5); //delay
- }
- int Brew_Timer (void) //use coffee selection value to set how many times animation with timer repeats
- { //can be 1, 3, 4 or 5min. Animation is 1sec a frame, 2 frame animation
- switch(Coffee_Choice){
- case 0:
- return 30;
- break;
- case 1:
- return 90;
- break;
- case 2:
- return 120;
- break;
- case 3:
- return 150;
- break;
- }
- }
- void PrintCoffee (void) //Prints current coffee selection
- {
- switch(Coffee_Choice){
- case 0:
- LCD_Clear();
- WriteString("1min brew");
- break;
- case 1:
- LCD_Clear();
- WriteString("3min brew");
- break;
- case 2:
- LCD_Clear();
- WriteString("4min brew");
- break;
- case 3:
- LCD_Clear();
- WriteString("5min brew");
- break;
- }
- }
- void LCD_Custom (void) //defines custom LCD characters
- {
- WriteCmd (0x40);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteChar (0b00010000);
- WriteChar (0b00001111);
- WriteChar (0b00000111);
- WriteChar (0b00000111);
- WriteChar (0b00000011);
- WriteChar (0b00000001);
- WriteCmd (0x48);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteChar (0b00000010);
- WriteChar (0b00011110);
- WriteChar (0b00011100);
- WriteChar (0b00011100);
- WriteChar (0b00011000);
- WriteChar (0b00010000);
- WriteCmd (0x50);
- WriteChar (0b00000001);
- WriteChar (0b00000010);
- WriteChar (0b00000110);
- WriteChar (0b00000100);
- WriteChar (0b00001100);
- WriteChar (0b00001000);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteCmd (0x58);
- WriteChar (0b00010000);
- WriteChar (0b00001000);
- WriteChar (0b00001100);
- WriteChar (0b00000100);
- WriteChar (0b00000110);
- WriteChar (0b00000010);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteCmd (0x60);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteChar (0b00001000);
- WriteChar (0b00001100);
- WriteChar (0b00000100);
- WriteChar (0b00000110);
- WriteChar (0b00000010);
- WriteChar (0b00000001);
- WriteCmd (0x68);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteChar (0b00000010);
- WriteChar (0b00000110);
- WriteChar (0b00000100);
- WriteChar (0b00001100);
- WriteChar (0b00001000);
- WriteChar (0b00010000);
- WriteCmd (0x70);
- WriteChar (0b00000001);
- WriteChar (0b00000010);
- WriteChar (0b00000110);
- WriteChar (0b00000111);
- WriteChar (0b00001111);
- WriteChar (0b00001111);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- WriteCmd (0x78);
- WriteChar (0b00010000);
- WriteChar (0b00001000);
- WriteChar (0b00001100);
- WriteChar (0b00011100);
- WriteChar (0b00011110);
- WriteChar (0b00011110);
- WriteChar (0b00011111);
- WriteChar (0b00011111);
- }
- void Frame_1 (void) //prints frame 1 of timer animation (using 4 custom characters)
- {
- LCD_Clear();
- //printf("%d", Delay_Repeats , exp(Delay_Repeats));
- SetAddr (0x07);
- WriteChar (0);
- SetAddr (0x08);
- WriteChar (1);
- SetAddr (0xc7);
- WriteChar (2);
- SetAddr (0xc8);
- WriteChar (3);
- }
- void Frame_2 (void) //prints frame 2 of timer animation (using 4 custom characters)
- {
- LCD_Clear();
- //printf("%d", Delay_Repeats , exp(Delay_Repeats));
- SetAddr (0x07);
- WriteChar (4);
- SetAddr (0x08);
- WriteChar (5);
- SetAddr (0xc7);
- WriteChar (6);
- SetAddr (0xc8);
- WriteChar (7);
- }
- void ADC_Init(void)
- {
- // initialize the Analog-To-Digital converter.
- // First, we need to make sure the AN0 pin is enabled as an analog input
- // as the demo board potentiometer is connected to RA0/AN0
- // Don't forget that RB0/AN12 must be digital!
- ANSEL = 0; //turn off all other analog inputs
- ANSELH = 0;
- ANSELbits.ANS0 = 1; // turn on RA5 analog
- // Sets bits VCFG1 and VCFG0 in ADCON1 so the ADC voltage reference is VSS to VDD
- ADCON1 = 0;
- //
- // ADFM = 0 so we can easily read the 8 Most Significant bits from the ADRESH
- // Special Function Register
- ADCON2 = 0b00111000;
- // Select channel 0 (AN0) to read the An0 voltage and turn on ADC
- ADCON0 = 0b00000001;
- }
- int ADC_Convert(void)
- { // start an ADC conversion and return the 8 most-significant bits of the result
- ADCON0bits.GO_DONE = 1; // start conversion
- while (ADCON0bits.GO_DONE == 1); // wait for it to complete
- return ADRESH; // return high byte of result
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement