PIC12f683 project not following delay instuction after pressing button

Thread Starter

ronnygrr

Joined Mar 30, 2024
5
I am an absolute beginner in the MCU field but kind of an intermediate in C language. For my Microcontrollers class, we were asked to build a dice simulator using LEDs, a button, and a PIC12f683. Here's a screenshot of the hardware I designed:

1711864604341.png

I am using SDCC to compile the C code and has been working fine so far, the example code we were given at the very beginning was an LED blinking every 100 time units. Now that I built all of that hardware I want to press the switch and turn the LEDs ON for a second and then turn them OFF but they are remaining ON all the time until I disconnect the switch. Here's the code I am using:

C:
#include <pic14/pic12f683.h>


// Pins are defined here for easier further manipulation
#define one 0b00000001
#define two 0b00000010
#define three 0b00000011
#define four 0b00000110
#define five 0b00000111
#define six 0b00010110


// Functions preamble
void delay (unsigned char tiempo);


// Main fucntion
void main(void)
{
    TRISIO = 0b00001000;  // All pins as outputs
    GPIO = 0x00;  // All pins DOWN
    ANSEL = 0x00;


    unsigned char time = 10;}

    // Matrix to store dice numbers in function of the LED configuration

    unsigned char  led_matrix[6] = {one, two, three, four, five, six};


    while (1)
    {
        do
        {
            GPIO = led_matrix[5];
            delay(time);
            GPIO = 0x00;
        } while (GP3 == 1);
    }
    
}
void delay(unsigned char tiempo)
{
    unsigned char i;
    unsigned char j;
    for(i=0;i<tiempo;i++)
      for(j=0;j<1275;j++);
}
 

Georg_B

Joined Apr 18, 2022
13
Code:
unsigned char i;
unsigned char j;
for(i=0;i<tiempo;i++)
for(j=0;j<1275;j++);
Unsigned char value can be 0 - 255. If the value is 255 and you add one to it, the result will be 0. The "j<1275" will be always true, you have created an endless loop. Change

Code:
unsigned char j;
to

Code:
unsigned short j;
Unsigned short can be 0 - 65535.
 

MrChips

Joined Oct 2, 2009
31,088
Your “do while (GP3 == 1)“ loop is the reason for this behaviour.
You need to replace this with a mechanism that triggers on a state change from 0 to 1 only.
 

Thread Starter

ronnygrr

Joined Mar 30, 2024
5
Your “do while (GP3 == 1)“ loop is the reason for this behaviour.
You need to replace this with a mechanism that triggers on a state change from 0 to 1 only.
I did that, here's my new while loop:

C:
unsigned char previous = 0;

    // Matriz para almacenar los números del dado en función de los LEDs
    unsigned char  led_matrix[6] = {one, two, three, four, five, six};

    while (1)
    {
        if (previous == 0 && GP3 == 1)
        {
            GPIO = led_matrix[5];
            delay(time);
            GPIO = 0x00;
        }

        previous = GP3; // Update previous state
        
    }
But now the LEDs are not turning on, I don't know what's going on, it might be something with the registers but I'm not sure, any thoughts? And thanks for your reply :)
 

MrChips

Joined Oct 2, 2009
31,088
One of the tips and techniques for successful program design is not to begin by writing code.
Instead of writing code, write out the steps in plain language and don't use computer or programming jargon. Following that, try to draw a flowchart of the process, again without using computer jargon.

For example:

Detect that key changed state from 0 to 1:

Step 1: wait for key = 0
Step 2: delay for 50ms
Step 3: wait for key = 1
Step 4: delay for 50ms
Step 5: confirm that key = 1

The above is not necessarily the correct steps. It is simply an example of writing what is known as pseudo-code.
 

MrAl

Joined Jun 17, 2014
11,725
I am an absolute beginner in the MCU field but kind of an intermediate in C language. For my Microcontrollers class, we were asked to build a dice simulator using LEDs, a button, and a PIC12f683. Here's a screenshot of the hardware I designed:

View attachment 318857

I am using SDCC to compile the C code and has been working fine so far, the example code we were given at the very beginning was an LED blinking every 100 time units. Now that I built all of that hardware I want to press the switch and turn the LEDs ON for a second and then turn them OFF but they are remaining ON all the time until I disconnect the switch. Here's the code I am using:

C:
#include <pic14/pic12f683.h>


// Pins are defined here for easier further manipulation
#define one 0b00000001
#define two 0b00000010
#define three 0b00000011
#define four 0b00000110
#define five 0b00000111
#define six 0b00010110


// Functions preamble
void delay (unsigned char tiempo);


// Main fucntion
void main(void)
{
    TRISIO = 0b00001000;  // All pins as outputs
    GPIO = 0x00;  // All pins DOWN
    ANSEL = 0x00;


    unsigned char time = 10;}

    // Matrix to store dice numbers in function of the LED configuration

    unsigned char  led_matrix[6] = {one, two, three, four, five, six};


    while (1)
    {
        do
        {
            GPIO = led_matrix[5];
            delay(time);
            GPIO = 0x00;
        } while (GP3 == 1);
    }
   
}
void delay(unsigned char tiempo)
{
    unsigned char i;
    unsigned char j;
    for(i=0;i<tiempo;i++)
      for(j=0;j<1275;j++);
}
I see a problem with the 'j' loop also, it looks like if you define 'j' to be an unsigned char, it can only go from 0 to 255. When it gets to the part that tests 'j' to see if it is below 1275, it's ALWAYS going to be below 1275 so it will never exit the loop.

I can never figure out why people do not read all the posts when there are only a few.
 
Top