-4

Please, where is the error in code (with 16F628)?

OUT1 - LED1 OUT2 - LED2

Flashing only LED1 (OUT1). The counter is not working (bad integer?). HW is okay... My code:

#include "PIC16F628.h"
#include "pic.h"

#pragma config FOSC = INTOSCIO

#define OUT1 RB1
#define OUT2 RB2

#define _XTAL_FREQ 4000000

//******************************************

void main()
{
    int x;
    TRISB1=0;
    TRISB2=0;
    OUT1=0;
    OUT2=0;

    if (x < 10)
    {
        x++;
        OUT1=1; 
         __delay_ms(1000);

        OUT1=0; 
         __delay_ms(1000);
    } 

    else if (x >= 10)
    {       
         x = 0;
        OUT2=1; 
         __delay_ms(1000);

        OUT2=0; 
         __delay_ms(1000);             
    }    
}

And in this code blink only OUT2:

#include "PIC16F628.h"
#include "pic.h"
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"

#pragma config FOSC = INTOSCIO

#define OUT1 RB1
#define OUT2 RB2

#define _XTAL_FREQ 4000000

uint8_t x = 10;

//************************

void main()
{
    TRISB1=0;
    TRISB2=0;
    OUT1=0;
    OUT2=0;


    while(1)
    {  
        if (x < 10)
        {
            x++;
            OUT1=1; 
             __delay_ms(1000);

            OUT1=0; 
             __delay_ms(1000);
        } 

        else if (x >= 10)
        {       
             x = 0;
            OUT2=1; 
             __delay_ms(1000);

            OUT2=0; 
             __delay_ms(1000);             
        }  
    }   
}

Also in the debugger variable x is not change. Compiler is XC8.

  • Also never use signed integers when there's no need for it. Never use the `int` type when doing embedded programming, use the stdint.h types. – Lundin Jun 10 '16 at 11:49
  • Maybe you must define the frequency above the includes ? IIRC it works that way with dsPIC plib. – Marco van de Voort Jun 23 '16 at 15:20

1 Answers1

0

The variable x is not initialized. Give it an initial value.

Do you have any warning when you compile ? Because your compiler definitely should show a warning since you read the value of an uninitialized variable. If you're a beginner in C, make sure your compiler does display all the warnings and read them carefully. They are here for a reason.

You also have to place you if/else statement into an infinite loop if you want it to run more than once. Usually a very simple program in embedded c looks like this :

void main()
{
    //Initialization stuff

    while(1) {
        //main loop : Everything happens here forever and ever
    }
}

Except for applications using an RTOS but that's off-topic.

Try this main :

void main() {
    int x;
    TRISB1=0;
    TRISB2=0;
    OUT1=0;
    OUT2=0;

    while(1) {
        if (x < 10) {
            x++;
            OUT1=1; 
            __delay_ms(1000);

            OUT1=0; 
            __delay_ms(1000);
        } 

        else if (x >= 10) {       
            x = 0;
            OUT2=1; 
            __delay_ms(1000);

            OUT2=0; 
            __delay_ms(1000);             
        }    
    }
}
Tim
  • 1,853
  • 2
  • 24
  • 36
  • Compiler (XC-8) reports any warnings. – Tomáš Junek Jun 10 '16 at 11:27
  • I don't know this compiler but a similar code compiled with gcc gave me this warning : " 'x' could be used uninitialized in this function". Anyway is your code working better if you initialize x at the place you declare it ? Remember to always initialize a variable before to use it. Here it's very likely that you have a big negative value and x never reach 10. – Tim Jun 10 '16 at 11:31
  • What is the behavior you expect and what is the behavior you see ? Also can you enter debug mode and track the evolution of x or simply do step by step debugging ? By reading your code I understand that OUT1 will blink 10 times, then OUT2 will blink only once, then OUT1 will blink 10 times again, and so on. – Tim Jun 10 '16 at 11:44
  • Yes, OUT1 will blink 10 times, then OUT2 will blink only once... Variable x in debuger is unchanged (variable has a default, initialization value). – Tomáš Junek Jun 10 '16 at 12:34
  • Is this the full code ? If so it has to be placed in an infinite loop or it will only run once. Put the if/else statement in a while(1) loop. – Tim Jun 10 '16 at 12:36
  • Yes, this is full code. Blink only OUT 1 (x < 10). "main" is also loop. "while(1){...} is no change. If I wrrite "int x = 10;" before "main", blink only OUT 2 (x >= 10). Value x is not change. – Tomáš Junek Jun 10 '16 at 12:59
  • What makes you think main is a loop ? Main is a function, the first one executed by your program. It's not supposed to be a loop. See Microchip example here : http://ww1.microchip.com/downloads/en/DeviceDoc/02_Blink.zip . Anyway look at the code I gave you, I edited my post. – Tim Jun 10 '16 at 13:06