CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

#use delay Not working when including a .c file

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
apakSeO



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

#use delay Not working when including a .c file
PostPosted: Thu Jan 05, 2017 9:14 am     Reply with quote

PIC is: PIC18F46J50 as a plug-in module on an Explorer 8 demo board
CCS version is: 5.065 of the PCWHD compiler
IDE is MPLAB X v3.40

The goal is to use the internal 8MHz oscillator as a clock source into the PLL which will eventually generate a 48MHz system clock. I monitor the system clock on REFO, which is mapped to pin RB2. RB2 is setup to output the system clock, which I have monitored on an oscilloscope.



I've been able to generate xc8 compatible code without any problems, both including this .c file and also without it.

My MPLAB project starts simple: a single main.c file, shown below.

Code:

#device PIC18F46J50 ICD=TRUE     
#use delay(internal=8MHz, clock=48MHz)   

#include <18F46J50.h>

#define InitTrisB   0b11000000      // Buttons on B6 & B7
#define InitTrisC   0b00000000      // LEDs on    C2 & C6 

// Get access to REFO for debugging purposes
#BYTE REFOCON = getenv("SFR:REFOCON") // More portable;  show in Window->PIC memory views->SFR
#BIT   RODIV0  = REFOCON.0
#BIT   RODIV1 = REFOCON.1
#BIT   RODIV2 = REFOCON.2
#BIT   RODIV3 = REFOCON.3
#BIT   ROSEL  = REFOCON.4
#BIT   ROSSLP = REFOCON.5
#BIT   ROON   = REFOCON.7

void main(void)
{
    set_tris_b(InitTrisB);
    set_tris_c(InitTrisC);
   
    ROON = 1; // output REFO clock on RB2
     
    output_c(0b01000000);
   
    while(1)
    {
        // Flash some LEDs so I know something is happening
        output_toggle(PIN_C2);
        output_toggle(PIN_C6);
        delay_ms(500);
    }
   
}


This code compiles without error and runs fine. The #use delay() directive works as expected, I can set the clock frequency to 8,16,24, and 48MHz no problems.

Evil or Very Mad However Evil or Very Mad ...

As soon as I include a certain .c file in the project, everything goes haywire. The perplexing thing about this .c file is that there is nothing inside of it having anything at all to do with clock frequency, or anything that as far as I can tell would matter to the CCS compiler.

This .c file is part of a debounce buttons library described here:
https://github.com/tcleg/Button_Debouncer ( I am using the C version )

The working version of my project looks like this, containing only the single .c file:


When I add the file button_debounce.c to the MPLAB project, the clock frequency maxes out at 8MHz. No matter what I try, I cannot get the CCS compiler to set the clock frequency higher than 8MHz. CCS will allow me to set the clock below 8MHz ( 4,2,1 MHz work OK )The broken version of my project looks like this: In main.c, I use the directive #include "button_debounce.h" :


I've verified the .c file is the problem; when I right-click on the file and select "Exclude file from current configuration", the clock-setting problems go away.

Another bit of information: I was forced to add another #device PIC18F46J50 statement to the top of the button_debounce.c file. This is because the compiler errors out: ".../drivers/stdint.h: Error#128: A #DEVICE required before this line" The button_debounce.h file includes stdint.h in its text. Perhaps this is the problem? I don't see how.

Anyone care to chime in on a possible cause of this problem?
temtronic



Joined: 01 Jul 2010
Posts: 9216
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 05, 2017 9:34 am     Reply with quote

OK, I can't find the debounce.c file you talk about, though the link tosses me to another sight....
please post the 'debounce.c' code segment in you post
it may NOT be CCS C compatible....

I like to help but make it easy for us old guys !

Jay
edit...

this..
#device PIC18F46J50 ICD=TRUE
#use delay(internal=8MHz, clock=48MHz)

#include <18F46J50.h>

is wrong order


#device PIC18F46J50 ICD=TRUE
#include <18F46J50.h>
#use delay(internal=8MHz, clock=48MHz)

may be correct(not on PICeng PC)

also
ICD=TRUE _may_ cause 'problems' as it'll set fuses/registers to make itself work not necessarily what YOU want !

Jay
apakSeO



Joined: 07 Dec 2016
Posts: 60
Location: Northeast USA

View user's profile Send private message

PostPosted: Thu Jan 05, 2017 10:33 am     Reply with quote

Here's the button_debounce.c file:

Code:

//*********************************************************************************
// State Button Debouncer - Platform Independent
//
// Revision: 1.6
//
// Description: Debounces buttons on a single port being used by the application.
// This module takes what the signal on a GPIO port is doing and removes
// the oscillations caused by a bouncing button and tells the application if
// the button(s) are debounced. This algorithm is robust against noise if the
// amount of allowable debouncing states is adequate. Below is an example of how
// the button debouncer would work in practice in relation to a single button:
//
// Real Signal:     0011111111111110000000000000011111111111111111110000000000
// Bouncy Signal:   0010110111111111010000000000001010111011111111110101000000
// Debounced Sig:   0000000000000011000000000000000000000000000001110000000000
//
// The debouncing algorithm used in this library is based partly on Jack
// Ganssle's state button debouncer example shown in, "A Guide to Debouncing"
// Rev 4. http://www.ganssle.com/debouncing.htm
//
// Revisions can be found here:
// https://github.com/tcleg
//
// Copyright (C) 2014 Trent Cleghorn , <trentoncleghorn@gmail.com>
//
//                                  MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//*********************************************************************************

//*********************************************************************************
// Headers
//*********************************************************************************
#include "button_debounce.h"

//*********************************************************************************
// Functions
//*********************************************************************************
void
ButtonDebounceInit(Debouncer *port, uint8_t pulledUpButtons)
{
    uint8_t i;
   
    port->index = 0;
    port->debouncedState = 0x00;
    port->changed = 0x00;
    port->pullType = pulledUpButtons;
   
    // Initialize the state array
    for(i = 0; i < NUM_BUTTON_STATES; i++)
    {
        port->state[i] = 0x00;
    }
}

void
ButtonProcess(Debouncer *port, uint8_t portStatus)
{
    uint8_t i;
    uint8_t lastDebouncedState = port->debouncedState;
   
    // If a button is high and is pulled down or
    // if a button is low and is pulled high, use a 1 bit
    // to denote the button has changed state. Else, a 0 bit
    // shows it is in a normal position.
    // Then, save the port status info into the state array
    port->state[port->index] = portStatus ^ port->pullType;
   
    // Debounce the buttons
    for(i = 0, port->debouncedState = 0xFF; i < NUM_BUTTON_STATES; i++)
    {
        port->debouncedState &= port->state[i];
    }
   
    // Check to make sure the index hasn't gone over the limit
    port->index++;
    if(port->index >= NUM_BUTTON_STATES)
    {
        port->index = 0;
    }
   
    // Calculate what changed.
    // If the switch was high and is now low, 1 and 0 xORed with
    // each other produces a 1. If the switch was low and is now
    // high, 0 and 1 xORed with each other produces a 1. Otherwise,
    // it is 0
    port->changed = port->debouncedState ^ lastDebouncedState;
}

uint8_t
ButtonPressed(Debouncer *port, uint8_t GPIOButtonPins)
{
    // If the button changed and it changed to a 1, then the
    // user just pressed it.
    return (port->changed & port->debouncedState) & GPIOButtonPins;
}

uint8_t
ButtonReleased(Debouncer *port, uint8_t GPIOButtonPins)
{
    // If the button changed and it changed to a 0, then the
    // user just released the button.
    return (port->changed & (~port->debouncedState)) & GPIOButtonPins;
}

uint8_t
ButtonCurrent(Debouncer *port, uint8_t GPIOButtonPins)
{
    // Current pressed or not pressed states of the buttons expressed
    // as one 8 bit byte. A 0 bit denotes the button is not pressed,
    // and a 1 bit denotes it is being pressed.
    return port->debouncedState & GPIOButtonPins;
}


I am adding this file to MPLAB's "Source Files" section. However when I do so CCS issues errors upon compilation: ".\stdint.h Error#128 A #DEVICE required before this line"

I'm not sure why this error occurs; I already have a #device statement on the 1st line of main.c. Why CCS or MPLAB complains about stdint.h I have no idea. stdint.h is included within the text of button_debounce.h
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 05, 2017 12:00 pm     Reply with quote

The traditional way is to have your main source file in Source Files
and that's all. Other .c files are #include'd in the main file.

This thread provides some other useful suggestions:
http://www.ccsinfo.com/forum/viewtopic.php?t=52943
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Thu Jan 05, 2017 12:09 pm     Reply with quote

and the reason is that if you put the source file into the project directly, MPLAB tries to compile this file.
Since it is an include file, not a standalone source, 'of course' this gives errors.
You can put it as an header file, and this then doesn't happen, or as PCM_Programmer says, just let the compiler include the files itself.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group