XC8 Pic16f1574 always executes the else of if-else statement

Thread Starter

Travm

Joined Aug 16, 2016
363
I'm at a loss here.
I can get the IF statement to work, but as soon as I add an else statement, it just always executes the else statement.

The seven segment code works fine, but i'm sure is super inefficient, but one thing at a time i guess.

Code:
/**
  Generated Main Source File

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

  Summary:
    This is the main file generated using MPLAB(c) Code Configurator

  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information :
        Product Revision  :  MPLAB(c) Code Configurator - 3.15.0
        Device            :  PIC16F1574
        Driver Version    :  2.00
    The generated drivers are tested against the following:
        Compiler          :  XC8 1.35
        MPLAB             :  MPLAB X 3.20
*/

/*
    (c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this
    software and any derivatives exclusively with Microchip products.

    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
    WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
    PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
    WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.

    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
    BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
    FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
    ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
    THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.

    MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
    TERMS.
*/

#include "mcc_generated_files/mcc.h"

/*
                         Main application
*/
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();


    
      

    while (1)
    {
        // Add your application code
       unsigned int i;
      for (i=1200; i>125; i-=10)
        {
      
       PWM1DCL = i;
       PWM1DCH = (i >> 8);
       PWM1LDCON = 0x80;
       __delay_ms(10);
        }
      
      for (i=125; i>10; i-=10)
        {
      
       PWM1DCL = i;
       PWM1LDCON = 0x80;
       __delay_ms(10);
        }
       for (i=5; i>1; i--)
        {
      
       PWM1DCL = i;
       PWM1LDCON = 0x80;
       __delay_ms(25);
        }
    
       for (i = 1; i < 5; i++)
        {
      
       PWM1DCL = i;
       PWM1LDCON = 0x80;
       __delay_ms(25);
        }
       for (i = 10; i < 125; i+=10)
        {
      
       PWM1DCL = i;
       PWM1LDCON = 0x80;
       __delay_ms(10);
        }
       for (i = 135; i < 1200; i+=10)
        {
      
       PWM1DCL = i;
       PWM1DCH = (i >> 8);
       PWM1LDCON = 0x80;
       __delay_ms(10);
        }
    
     if(i>135)
       {
       SEG7A_SetLow();
       SEG7B_SetLow();
       SEG7C_SetLow();
       SEG7D_SetLow();
       SEG7E_SetHigh();
       SEG7F_SetLow();
       SEG7G_SetLow();
     }
     else;
       { 
       SEG7A_SetHigh();
       SEG7B_SetLow();
       SEG7C_SetLow();
       SEG7D_SetHigh();
       SEG7E_SetHigh();
       SEG7F_SetHigh();
       SEG7G_SetHigh();
       } 
    
    }
}
/**
End of File
*/
in this case, the 7 segment is displaying the else case, even though i > 135. If i remove the else statement, the if statement executes properly.
 

dannyf

Joined Sep 13, 2015
2,197
Code:
       PWM1LDCON = 0x80;
once your pwm module is set-up, there is no reason for you to continue to set the LDA bit.

You really should read the datasheet.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
once your pwm module is set-up, there is no reason for you to continue to set the LDA bit.
This is wrong. LDA bit must be reset everytime the duty cycle is changed, otherwise it isn't actually loaded into the pwm module.
A helpful person in this forum helped me with that.

to quote the datasheet
"23.4 Reload Operation
Four of the PWM module control register pairs and one
control bit are double buffered so that all can be
updated simultaneously. These include:
• PWMxPHH : PWMxPHL register pair
• PWMxDCH : PWMxDCL register pair
• PWMxPRH : PWMxPRL register pair
• PWMxOFH : PWMxOFL register pair
• OFO control bit
When written to, these registers do not immediately
affect the operation of the PWM
. By default, writes to
these registers will not be loaded into the PWM operating
buffer registers until after the arming conditions are
met. The arming control has two methods of operation:
• Immediate
• Triggered
The LDT bit of the PWMxLDCON register controls the
arming method. Both methods require the LDA bit to be
set.
All four buffer pairs will load simultaneously at the
loading event.

Code:
  else;
nice code.
Thanks, considering i'm self taught, and started in my spare time only a few months ago, with nearly 0 programming experience (I can manipulate visual basic macros in Excel as needed, yay for me).
Everything i know about the C language presently comes from;
1. Microchip wikidot, and
2. Helpful people on this forum (hint, not you).

Danny, I'm giving you the benefit of doubt here, but if you really have nothing helpful to add I may have to explore whether this forum has block features.
You should read my posts, they are much shorter than any data sheet.
I have read the data sheet (several actually, and even tried to understand the XC8 manual), beginning to end, many sections several times.

Why does the internet have to be full of trolls? Seriously.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
I wouldn't call him a troll......a bit of a smartarse that time, but troll, nah wouldn't go that far.

Why does the internet have to be full of trolls ?

Hmmmm interesting thought that. My thoughts, people are basically (insert bad word here) and when not face to face tend to show it more.

What I find more interesting is that people will spend more words on those that aren't being helpful than those that are.

Read the datasheet........well there does seem to be a lot of questions, really simple ones, that are easily answered by reading the data sheet.

The more you program the less time you'll spend chasing those face palm errors. Just wait till you get to using pointers.....and forget to initialise them. Not that I'd admit to doing that recently.ahem......
He has posted entirely unhelpfully in almost every thread I have made.

Troll.

It is possible, especially while learning, to read the datasheet, sometimes two or ten times and not fully understand what you're missing. Seriously, I'm thinking everyone who just says "read the datasheet" without offering any sort of help, even what section or bit of information might have been overlooked or missed, should be just ignored.

As for giving more words to less helpful people, it's just come to a point as if this guy has been following me around while I'm trying to learn things, talking out his backside. Even to the point he told me to read the datasheet because i was doing something that had to be done BECAUSE THE DATASHEET SAID.

Three or four people so far have been tremendous help when I have become stuck. I hope they appreciate that few words often mean more than many when i say thanks.

I am sure this forum has more helpful people I just felt the need to call a spade a spade.

This guy has been trolling me, I am not going to search this forum to see if he is actually not stupid.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
How do you think hobbyists got by pre internet ? Datasheets usually came in big expensive books that were rare as hens teeth, usually if you could get hold of one it was quite a few years old and a little worse for wear.

For us younger guys, pre teens, it was off to the local library to read, most books were quite a few years out of date, and magazines, well you were lucky to get hold of.

But we managed, we read and we read, we made stuff on breadboards, perf boards or vero boards, damn we even used self tappers of scrap bits of timber. Most of this was without too many friends that had similar interests. So you can see why it's often the datasheets are relied on.

You honestly think you're that interesting as for someone to spend time giving you a hard time ? Pull your head in and start acting like an adult, and if you do think you have a serious issue, talk to the admins.

And yes I did fight and die in 3 world wars and had my legs blown off 4 times and had to walk to school 15 miles on my hands in the snow cos we couldn't afford shoes.

(my apologies to any & all vets)
I don't think we're having the same conversation.

you should read my posts also.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
We are, I was addressing the whole datasheet issue & pointing out that the datasheets are so much more accessible.
.
That's exactly the bit that leads me to believe we aren't in the same conversation. But enough about datasheets.

I always google 5-6 different search terms before bothering to post anything to a forum asking a question. I have found some help on stack exchange, but often it is too specific and doesn't apply to my scenario.

This standard C compiler idea sounds interesting...
What do you recommend? GCC?

Microchip MPlabX seems pretty good, but I don't have a benchmark. Would never have thought about doing that.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
The PC I have set aside for development will run Linux, but I'm working from Windows until I get my bench built and the PC parts put together. Need to find a display somewhere too.

I quit Linux years ago because i got fed up with updates breaking my system every second month. Figure it's worth another go now that windows is as bad at that or worse.
Makes sense that it would be a more practical development system. I dislike most Microsoft tools I've had to use, outside the basic office suite.

Never thought about GitHub either, perhaps when I start making software that actually does stuff I'll look into that.

Some decent info, thanks.
 

Thread Starter

Travm

Joined Aug 16, 2016
363
My first go at Linux was Gentoo, it worked but was more configuring than I cared to do. Ubuntu had me for ease of install. Faster than windows.
But every update broke something.
Every time. Usually not hard to fix, but it got old real fast.
 
Top