PIC18f4685 Project Help

Thread Starter

Quasimodem

Joined Dec 9, 2017
4
I'm trying to code an IR shooting gallery project where the 18f4685 microcontroller controls when the start button is pressed (and difficulty setting is chosen) to start a 30 second countdown, lighting LEDs when a target is hit, and sending each hit to the scoreboard. When the time is up, I want the program to stop registering scores and hits. Can anyone help see what I'm doing wrong?

C:
#pragma config MCLRE=OFF, WDT=OFF, LVP=OFF, OSC=IRCIO67, PBADEN=OFF
#include <xc.h>
#include <stdio.h>

#define _XTAL_FREQ 8000000   /* Set oscillator frequency to 8MHz */

#define switch1  PORTAbits.RA0  // Switch on RA0
#define switch2  PORTAbits.RA1  // Switch on RA1
#define switch3  PORTAbits.RA2  // Switch on RA2

#define led1    LATBbits.LATB0     // led1
#define led2    LATBbits.LATB1     // led2
#define led3    LATBbits.LATB2     // led3

#define score PORTBbits.RB3     // Scoreboard
#define timer PORTBbits.RB4     // 7-segment timer

#define diff1  PORTDbits.RD7  // Difficulty switch
#define start  PORTDbits.RD5  // Start button

int x=30;  /* Sets variable x=30 at the start of the function. This starts
              the count at 30 seconds for the countdown. */

void main(void)
{

/* Input output settings*/

       TRISAbits.TRISA0 = 1;     // RA0 Input for taking input from IR sensor
       TRISAbits.TRISA1 = 1;     // RA1 Input for taking input from IR sensor
       TRISAbits.TRISA2 = 1;     // RA2 Input for taking input from IR sensor

       TRISDbits.TRISD7 = 1;     // RD7 Input for difficulty switch easy
       TRISDbits.TRISD6 = 1;     // RD6 Input for difficulty switch hard
       TRISDbits.TRISD5 = 1;     // RD5 Input for start button

       TRISBbits.TRISB0 = 0;     // RB0 Output LED
       TRISBbits.TRISB1 = 0;     // RB1 Output LED
       TRISBbits.TRISB2 = 0;     // RB2 Output LED
      
       TRISBbits.TRISB3 = 0;     // RB3 Output to scoreboard
       TRISBbits.TRISB4 = 0;     // RB4 Output to 7-segment timer

       CMCON = 0x07;             // Disable Comparator

       ADCON1bits.PCFG0 = 1;   // These 4 settings below determines the analog or digital input
       ADCON1bits.PCFG1 = 1;   // Making all the pins digital
       ADCON1bits.PCFG2 = 1;   // by setting them as 1111
       ADCON1bits.PCFG3 = 1;        

       ADCON0bits.ADON = 0;  //  Disabled ADC
  
    while(1)
    {

/* This will be the code for the difficulty switch easy mode */
        if (diff1 == 0); // If difficulty switch is low, it is in Easy mode
      


      
        if (start == 1);    // when start button is pressed
        led1 = 1;       // turn on target LEDs
        led2 = 1;
        led3 = 1;
      
        timer = 1;      // turn on 555 timer
      
TRISCbits.RC6=0;    // Sets TX Pin as an out which will be used for Serial Comm
OSCCONbits.IRCF2=1; // Set internal frequency to 8MHZ
OSCCONbits.IRCF1=1; // Set internal frequency to 8MHZ
OSCCONbits.IRCF0=1; // Set internal frequency to 8MHZ
OSCCONbits.SCS1=1;  // Sets chip to internal oscillation
T0CONbits.TMR0ON=0; // Stops Timer
T0CONbits.T08BIT=0; // Sets 16-bit
T0CONbits.T0CS=0;   // Specifies internal clock
T0CONbits.PSA=0;    // Enables Prescaler
T0CONbits.T0PS2=1;  // Sets Prescaler to 32
T0CONbits.T0PS1=0;  // Sets Prescaler to 32
T0CONbits.T0PS0=0;  // Sets Prescaler to 32
TMR0H=0x0B;         // Sets Preload value "H" for 1 second delay
TMR0L=0xDC;         // Sets Preload Value "L" or 1 second delay
T0CONbits.TMR0ON=1; // Starts Timer
SPBRG=51;           // Sets chip baud rate to 9.6kHZ
TXSTAbits.SYNC=0;   // Sets chip to asynchronous communication
TXSTAbits.TXEN=1;   // This enables serial communications
TXSTAbits.BRGH=1;   // Selects high speed baud rate
BAUDCONbits.BRG16=0;// Sets chip to 8-bit baud generator to 8 bits
RCSTAbits.SPEN=1;   // Enables the serial port bit
__delay_ms(500);    // Half second delay to allow screen to setup

while (x<=30 && x>0)// while x is between these parameters do what is below
            {
while (INTCONbits.TMR0IF==0);   /* While Timer interrupt overflow flag equals
                                  zero stay here*/
TMR0H=0x0B;         // Starts count at PRELOAD to get 1 second
TMR0L=0xDC;         // Starts count at PRELOAD to get 1 second
INTCONbits.TMR0IF=0;// Set overflow interrupt flag to zero
x=x-1;              // Sets a countdown from 30 to 0
            }
if (x < 1)      // when time reaches zero
{
    timer = 0;  // stop 555 timer
}
__delay_ms(500);
while(1);           // Stops the program here
    }
  
/* If you want the microcontroller to work at  1MHZ then
  * comment the three lines below */

OSCCONbits.IRCF0 = 1;        // set internal clock to 8 MHz
OSCCONbits.IRCF1 = 1;        // For Avoiding switch debouncing problem    
OSCCONbits.IRCF2 = 1;              


            if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 0;
                             if(led1 == 0)
                             {
                                 score = 1;}
                    }
                                     else {    }
            if(switch2 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 0;
                             if(led2 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
            if(switch3 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led3 = 0;
                             if(led3 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
          
            if(PORTB == 0x00 && x>0) { //If all LEDs are off
                                       //and time is not up
          
            }

while(1);
{
     /* This will be the code for hard mode */

        if (diff1 == 1); // If difficulty switch is high, it is in Hard mode
      
        if (start == 1);    // when start button is pressed
      
        led1 = 0;       // turn on target LEDs
        led2 = 1;
        led3 = 0;
      
        if(switch2 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 0;
                             if(led2 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
      
      
        led1 = 0;       // turn on target LEDs
        led2 = 0;
        led3 = 1;
      
        if(switch3 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led3 = 0;
                             if(led3 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
      
        led1 = 1;       // turn on target LEDs
        led2 = 0;
        led3 = 0;
      
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 0;
                             if(led1 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
      
        led1 = 0;       // turn on target LEDs
        led2 = 1;
        led3 = 0;
      
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 0;
                             if(led2 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
        led1 = 1;       // turn on target LEDs
        led2 = 0;
        led3 = 0;
      
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 0;
                             if(led1 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
        led1 = 0;       // turn on target LEDs
        led2 = 0;
        led3 = 1;
      
        if(switch3 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led3 = 0;
                             if(led3 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
        led1 = 0;       // turn on target LEDs
        led2 = 1;
        led3 = 0;
      
        if(switch2 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 0;
                             if(led2 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
      
        led1 = 1;       // turn on target LEDs
        led2 = 0;
        led3 = 0;
      
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 0;
                             if(led1 == 0)
                             {
                             score = 1;}
                    }
                                     else {    }
}

while(1);
     {   //Forever Loop
     }   //End While loop
  
  
       return;
}
 
Last edited by a moderator:

JohnInTX

Joined Jun 26, 2012
4,787
The while(1); at lines 107, 148, 259 will stop the program from going past those points.

Also, having the ';' at the if statements at 58 and 63 (maybe others) will not to the test like you expect. The semicolon terminates the if statement right there.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
You want something like this:
Code:
if (start == 1){   // when start button is pressed
        led1 = 1;       // turn on target LEDs
        led2 = 1;
        led3 = 1;
   
        timer = 1;      // turn on 555 timer
}
 

Thread Starter

Quasimodem

Joined Dec 9, 2017
4
Thanks very much, that's exactly the help I am looking for; another set of eyes to note typos. Working on fixing it now.
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
You realize there is such a thing a a debugger? The purpose of it is to help you debug your code. You should learn to use it. You are much closer to the code than anyone. Both literally and figuratively.

Teach a man to fish......
 

Thread Starter

Quasimodem

Joined Dec 9, 2017
4
I have edited the program to this, and it's still not working. I want it to start a 30 second countdown while initiating an external 555 timer for a countdown clock, light all LED targets, which turn off when a hit is registered, and sending a high to the scoreboard. If all the LEDS are off while time is not up, to light them again. When the time is up, I want the program to stop registering scores and hits, but keeping the scoreboard value. The game's "hard mode" I wanted to do a random pattern of LEDs to light and scored when hit, but have given up on that idea, and will settle for a pattern of targets to light as targets.

Code:
#pragma config MCLRE=OFF, WDT=ON, LVP=OFF, OSC=IRCIO67, PBADEN=OFF
#include <xc.h>
#include <stdio.h>

#define _XTAL_FREQ 8000000   /* Set oscillator frequency to 8MHz */

#define switch1  PORTAbits.RA0  // Sensor on RA0
#define switch2  PORTAbits.RA1  // Sensor on RA1
#define switch3  PORTAbits.RA2  // Sensor on RA2

#define led1    LATBbits.LATB0     // led1
#define led2    LATBbits.LATB1     // led2
#define led3    LATBbits.LATB2     // led3

#define score PORTBbits.RB3     // Scoreboard
#define timer PORTBbits.RB4     // 555 timer

#define diff1  PORTDbits.RD7  // Difficulty switch
#define start  PORTDbits.RD5  // Start button

int x=30;  /* Sets variable x=30 at the start of the function. This starts
              the count at 30 seconds for the countdown. */

void main(void)
{

/* Input output settings*/

       TRISAbits.TRISA0 = 1;     // RA0 Input for taking input from IR sensor
       TRISAbits.TRISA1 = 1;     // RA1 Input for taking input from IR sensor
       TRISAbits.TRISA2 = 1;     // RA2 Input for taking input from IR sensor

       TRISDbits.TRISD7 = 1;     // RD7 Input for difficulty switch easy
       TRISDbits.TRISD5 = 1;     // RD5 Input for start button

       TRISBbits.TRISB0 = 0;     // RB0 Output LED
       TRISBbits.TRISB1 = 0;     // RB1 Output LED
       TRISBbits.TRISB2 = 0;     // RB2 Output LED
     
       TRISBbits.TRISB3 = 0;     // RB3 Output to scoreboard
       TRISBbits.TRISB4 = 0;     // RB4 Output 555 timer

       CMCON = 0x07;             // Disable Comparator

       ADCON1bits.PCFG0 = 1;   // These 4 settings below determines the analog or digital input
       ADCON1bits.PCFG1 = 1;   // Making all the pins digital
       ADCON1bits.PCFG2 = 1;   // by setting them as 1111
       ADCON1bits.PCFG3 = 1;       

       ADCON0bits.ADON = 0;  //  Disabled ADC
 

OSCCONbits.IRCF2=1; // Set internal frequency to 8MHZ
OSCCONbits.IRCF1=1; // Set internal frequency to 8MHZ
OSCCONbits.IRCF0=1; // Set internal frequency to 8MHZ
OSCCONbits.SCS1=1;  // Sets chip to internal oscillation
T0CONbits.TMR0ON=0; // Stops Timer
T0CONbits.T08BIT=0; // Sets 16-bit
T0CONbits.T0CS=0;   // Specifies internal clock
T0CONbits.PSA=0;    // Enables Prescaler
T0CONbits.T0PS2=1;  // Sets Prescaler to 32
T0CONbits.T0PS1=0;  // Sets Prescaler to 32
T0CONbits.T0PS0=0;  // Sets Prescaler to 32
TMR0H=0x0B;         // Sets Preload value "H" for 1 second delay
TMR0L=0xDC;         // Sets Preload Value "L" or 1 second delay
T0CONbits.TMR0ON=1; // Starts Timer

//__delay_ms(500);    // Half second delay to allow screen to setup


/* If you want the microcontroller to work at  1MHZ then
  * comment the three lines below */

OSCCONbits.IRCF0 = 1;        // set internal clock to 8 MHz
OSCCONbits.IRCF1 = 1;        // For Avoiding switch debouncing problem   
OSCCONbits.IRCF2 = 1;             


 
    {
/* This will be the code for the difficulty switch easy mode */
        if (diff1 == 0) { // If difficulty switch is low, it is in Easy mode

        if (start == 1){    // when start button is pressed
        led1 = 0;       // turn on target LEDs
        led2 = 0;
        led3 = 0;
     
        timer = 1;      // turn on 555 timer
     
        /* Internal timer code for 30 second coundown */
     
while (INTCONbits.TMR0IF==0);   /* While Timer interrupt overflow flag equals
                                  zero stay here*/
TMR0H=0x0B;         // Starts count at PRELOAD to get 1 second
TMR0L=0xDC;         // Starts count at PRELOAD to get 1 second
INTCONbits.TMR0IF=0;// Set overflow interrupt flag to zero
x=x-1;              // Sets a countdown from 30 to 0
         
        }
        }
while (x<=30 && x>0)// while x is between these parameters do what is below
            {
            if(switch1 == 1)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 0;
                             if(led1 == 0)
                             {
                                 score = 0;}
                    }
                                     else {    }
            if(switch2 == 1)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 0;
                             if(led2 == 0)
                             {
                             score = 0;}
                    }
                                     else {    }
            if(switch3 == 1)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led3 = 0;
                             if(led3 == 0)
                             {
                             score = 0;}
                    }
                                     else {    }
         
            while(PORTB == 0xFF) { //If all LEDs are off
            }   
            }

    }
         
     /* This will be the code for hard mode */

        if (diff1 == 1) // If difficulty switch is high, it is in Hard mode
        {
        if (start == 1)    // when start button is pressed
        {
        led1 = 1;       // turn on target LEDs
        led2 = 0;
        led3 = 1;
        }
        if(switch2 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 1;
                             if(led2 == 1)
                             {
                             score = 0;}
                    }
                                     else {    }
     
     
        led1 = 1;       // turn on target LEDs
        led2 = 1;
        led3 = 0;
     
        if(switch3 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led3 = 1;
                             if(led3 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
     
        led1 = 0;       // turn on target LEDs
        led2 = 1;
        led3 = 1;
     
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 1;
                             if(led1 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
     
        led1 = 1;       // turn on target LEDs
        led2 = 0;
        led3 = 1;
     
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 1;
                             if(led2 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
        led1 = 0;       // turn on target LEDs
        led2 = 1;
        led3 = 1;
     
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 1;
                             if(led1 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
        led1 = 0;       // turn on target LEDs
        led2 = 0;
        led3 = 1;
     
        if(switch3 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led3 = 1;
                             if(led3 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
        led1 = 1;       // turn on target LEDs
        led2 = 0;
        led3 = 1;
     
        if(switch2 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led2 = 1;
                             if(led2 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
     
        led1 = 0;       // turn on target LEDs
        led2 = 1;
        led3 = 1;
     
        if(switch1 == 0)    //   On reading IR sensor value ON
                    {    //Turn led OFF
                             led1 = 1;
                             if(led1 == 1)
                             {
                             score = 1;}
                    }
                                     else {    }
        }
         


if (x == 0)      // when time reaches zero
{
    timer = 0;  // stop 555 timer
}
__delay_ms(500);


while(1);           // Stops the program here
 
return;
}
 

spinnaker

Joined Oct 29, 2009
7,830
It simply doesn't work. I've made some changes and submitted the new code below.
Is the timer not working? Are LEDs not lighting? Break your code into small pieces. First write the code to turn your LEDs on. Make sure that works. Then make sure you can read your I/R sensor. You can then move on to the timer. Make sure your timer flag is being set.

And LEARN TO USE YOUR DEBUGGER! Step through the code and figure out where the problem is located. When you have more details on the location of the problem post back.
 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
32,823
Thanks very much, that's exactly the help I am looking for; another set of eyes to note typos. Working on fixing it now.
Depending on the tools you are using, most modern C compilers do a pretty good job of throwing warnings on code that that where the code is compilable, but is probably not what was intended. Unfortunately, many people completely ignore compiler warnings because, after all, the code compiled. Never, ever, ever ignore compiler warnings. There will be, on rare occasion, a warning that can safely be ignored. When that is the case, make sure that you are satisfied that it really does not reflect a logic error and then see if you can rewrite your code so that the warning doesn't get thrown (this is usually possible). If you are going to have live with it, then comment the code to indicate what warning is being thrown and why it is safe to accept it.
 

WBahn

Joined Mar 31, 2012
32,823
It simply doesn't work. I've made some changes and submitted the new code below.
It sounds like you are making one of the classic code development mistakes. You have written the entire program without testing any of it. Then, when the thing doesn't work, you are left with a sea of code and little idea how to determine what part is causing the problem.

I'd recommend taking a step back and develop your code in little bits and pieces. First, make sure that you can respond to the start button -- perhaps by using it to simply toggle an LED. Then see if your countdown timer works, perhaps by having it turn off the LED when time expires. Go through and identify each task that needs to be done and develop the code that performs just that task and test it until you are satisfied that it is doing exactly what you want. Then start bringing them together to perform larger subtasks of the problem.

You'll almost certainly find that your total development time drops significantly if you will adopt an approach like this.
 

jayanthd

Joined Jul 4, 2015
945
I can help with your project. Give clear information about the working of the device and also your requirements. I will write a code for you using mikroC PRO PIC Compiler.
 
Top