Maker Pro
Maker Pro

Simple PIC 18f45k20 Project


Jan 3, 2016
Jan 3, 2016
Hi Guys!

I am trying to create a very simple circuit using the PIC18f45k20 microcontroller whereby when power is applied to the PIC a welcome message appears on the LCD, when a button is pressed it sends a pulse to a relay to turn a motor on (Port RB4) When the button (RB4) is pressed again the motor stops running. I have attached the circuit diagram

I am coding in MPLab using the C18 compiler. I have attached the set up project window and I am a bit unsure how to finish the code.... Here's what I have got so far....

/** C O N F I G U R A T I O N   B I T S ******************************/

#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF                       // CONFIG1H
#pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30                        // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768                                    // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC       // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF                              // CONFIG4L   
#pragma config CPB = OFF, CPD = OFF                                         // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF                // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                            // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF           // CONFIG7L
#pragma config EBTRB = OFF                                                  // CONFIG7H

/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
#include "stdio.h"
#include "delay.h"
#include "LCD.h"

/** D E C L A R A T I O N S ******************************************/

void main (void)
    OSCCON = 0b01110010;        //8 MHz
    TRISD = 0b00000000;        // The Output to pin D is the Motor
    TRISC = 0b00000000;        // The Outputs to pin C go to the LCD
    TRISB = 0b00000000;        // The Outputs to pin B go to the LED's
    TRISA = 0b11111111;        // The Inputs to pin A come from the LDR's, SW1 and the FSR.

    stdout = _H_USER; //Set to user-defined output stream via _user_putc

    lcd_init(); //Initialize the LCD
    send_cmd(0x0C); //Turn off cursor

    set_cursor(1, 1); //Set cursor to row 1, column 1

    set_cursor(2, 1); //Set cursor to row 2, column 1
    printf("Press Go to crush Cans!");

    TRISD = 0b10111111;         // PORTD bit 6 to output (0);

    LATDbits.LATD6 = 1;        // Set LAT register bit 6 to turn on Motor

Any help would be incredible!!!


  • IMG_0856.JPG
    440.6 KB · Views: 102
  • project_window (1).png
    project_window (1).png
    13.8 KB · Views: 103


Jan 5, 2010
Jan 5, 2010

The program must have one large loop that goes over the following 4 states:

State 1:
Wait for the button to be pressed.

State 2:
Turn motor on.
Wait for button to be unpressed, and debounce it. (i.e. make sure it is still unpressed after 10 mS)

State 3:
Wait for button to be pressed.
Turn motor off.

State 4:
Wait for button to be unpressed and debounce.

Go back to state 1.

This assumes you do not need to do anything else while doing this. If you do it becomes a little more complicated.