• AVR Freaks

Hot!How to solve " function definition is not allowed here" error

Author
Vividhobby
New Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2016/11/12 19:25:43
  • Location: 0
  • Status: offline
2020/07/07 05:56:03 (permalink)
2 (1)

How to solve " function definition is not allowed here" error

Hello,
Olwase allow me to ask this kind of begginer's question.
I am trying to modify "micro C" program to fit MPLABX XC-8 (16F18875).
please fined two files attached. main function and library for ST7735TFT.
I add "main.c" function to "source file" directory, and "ST7735TFT.c" to Library directory.
After click to build, I got many " function definition is not allowed here" error such as 
 
void write_command(unsigned short cmd_){}
void write_data(unsigned short data_){}
void Rcmd1(){}  .....
 
Thank you for your kind advices to fif this.
 
Best Regards
 
 
 

Attachment(s)

Attachments are not available: Download requirements not met
#1

4 Replies Related Threads

    JPortici
    Super Member
    • Total Posts : 1114
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: How to solve " function definition is not allowed here" error 2020/07/07 06:27:12 (permalink)
    +2 (2)
    It's not a library (.lib compiled code). It's another source file
    #2
    davekw7x
    Entropy++
    • Total Posts : 1882
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: How to solve " function definition is not allowed here" error 2020/07/07 14:29:46 (permalink)
    +3 (3)
    Vividhobby
    ..begginer's question.



    My first thought is that maybe it's a bit much for a beginner.  But then, "it just happens" that I have recently completed a couple of display projects, one with an ILI9225 controller and one with an ST7735S controller, so, with things fresh in my so-called mind, I'm thinking maybe we can do something with your setup.

    For my projects I started with Arduino libraries (Adafruit, among others) and, after verifying operation on an Arduino, I converted the object-oriented C++ to down-to-earth XC8 programs for a PIC18F47K42.  (The programs had to accommodate different display sizes with "minor" changes, and that added a lot of stuff, but I won't get into that here.)

    Anyhow...

    Your project actually may not be a bad way to start, at least within the limitations of a fixed display size.  I made a "few changes" and got my little 0.96 inch 80x160 "mini" display to work.  Since it is smaller than yours, some of the test stuff ran off the edge of the screen, but I am satisfied with the display functions, at least as far as I observed.

    So, here goes:

    First things first---Here's the structure of your main code:


    void main() { // "Outer" main()
    //
    // 30-some-odd lines of stuff
    #include "ST7735TFT.c" // Has function definitions --- NOT VALID: we are inside a function!
    //
    // A few more lines of stuff
    //
    void testlines(unsigned int color) {
    // A function definition  ---  NOT VALID: we are inside a function!
    }
    //
    // Nine or ten more function definitions ---  NOT VALID: we are inside a function!

    void main() { // Huh? all of this stuff is inside your original main()

    // A lot of what should be a perfectly good sequence of test steps

    } // End of the  "inner" main()
     
    } // End of the very first "outer" main()


    Well: GET RID OF the "outer" main.

    Using as much of your code that I could, my main() looks like this


    // Start of main.c
    #include "mcc_generated_files/mcc.h"

    // ST7735 functions use SPI1_Write() to write to the display.
    // I'll define it to use the MCC-generated function
    //
    #define SPI1_Write(a) SPI1_ExchangeByte((uint8_t)a)

    // SPI TFT module control signal connections
    // Could use MCC pin manager and macro definitions to set these
    // low and hogh, but Ill leave them the way you had them so that
    // I don't have to change the TFT Functions
    //

    // Note that my module has a "RES" pin that is an active-low reset.
    // Tie it high or, better, connect to an output pin that you assert
    // briefly upon startup.
    #define TFT_SPI_HARDWARE                  // Hardware SPI module is used

    // My pin assignments for convenience on my HPC Curiosity
    #define TFT_CS   LATA3
    #define TFT_CS_Direction TRISA3
    #define TFT_DC   LATA1                     // output
    #define TFT_DC_Direction TRISA1
    #define TFT_RES  LATD0
    #define TFT_RES_DIR TRISD0
        
    // STT7735 functions use delay_ms() for various.  I'll define it
    // to be the compiler-supplied delay function
    #define delay_ms __delay_ms
    #include <string.h>
    #include "ST7735TFT.c"     // Include ST7735 TFT driver source file
        
    unsigned short k = 0;
    char *txt = "Hello World!";

    // NOTE: Since you will be storing a couple of digits in txt, it must
    // be an array, not a pointer to a const "string"
    // Initialize it with three zeros and never, ever write anything other than
    // txt1[0] and txt[1]
    //char *txt1 = "  ";
    char txt1[3] = {0};

    //  All of your test functions: testlines(), ..., tst()


    void main()
    {
        SYSTEM_Initialize();

        // Note that, on my system MCC doesn't enable the SPI module.
        // You can use the SPI1_Open() function, or you can simply enable
        // it here.  I'll keep it simple for starters
        SSP1CON1bits.SSPEN = 1;

        // My UART functions are interrupt-enabled.  If you don't have a UART
        // you can simply comment out the following three statements
        //
        INTERRUPT_GlobalInterruptEnable();
        INTERRUPT_PeripheralInterruptEnable();
        printf("\r\nCompiled on %s at %s\r\n", __DATE__, __TIME__);

        ST7735_TFT_Init(); // Initialize the TFT module
        fillScreen(ST7735_BLACK);
        tst();

        setTextWrap(0); // Turn off text wrap

    //  All of your test stuff...


    // I put in a couple of LEDs to show progress outside the
    // display module.  If you don't have LEDs defined in
    // MCC, just comment out the Toggle() statements
        while (1) {
            invertDisplay(1);
            LED1_Toggle();
            delay_ms(500);
            invertDisplay(0);
            LED2_Toggle();
            delay_ms(500);
        }
    } // End of main()



    The only changes I made in your ST7735.C file was to put in a reset sequence at the beginning of TFT initialization.  Oh, yeah, I used MCC to generate the SPI functions, so I didn't need to do anything with the original SPI initialization.  Almost forgot: I had to include <string.h> since strlen() was used in one of the functions.


    void ST7735_TFT_Init()
    {
        TFT_CS = 1;
        TFT_DC = 0;
        TFT_CS_Direction = 0;
        TFT_DC_Direction = 0;
    #ifdef TFT_RES
        TFT_RES = 0;
        __delay_ms(50);
        TFT_RES = 1;
    #endif

        // MC-generated code already initialized SPI
        //#ifdef TFT_SPI_HARDWARE
        //SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_LOW_2_HIGH);
        //#else
        //TFT_CLK = 1;
        //TFT_DATA = 0;
        //TFT_CLK_Direction = 0;
        //TFT_DATA_Direction = 0;
        //#endif
        
        Rcmd1();
        Rcmd2red();
        Rcmd3();
        write_command(ST7735_MADCTL);
        write_data(0xC0);
    }


    Test setup: PIC16F18875 plugged into a Curiosity HPC board.
    MPLABX version 5.40, XC8 version 2.20 (C99)


    Project setup:  If you add ST7735TFT.c to the project it will be compiled.  Then when you include it into your main file, it will be compiled again.  That's BAD.  "Normally" you would put function declarations (prototypes) in a separate header file and include that in your main file.  To keep from having to do that, for starters, I just included the .c file but DID NOT add it to the project settings.

    Now, including a .c file is Not Recommended in general, but it can work (for beginners).  After you get things working, then the "right thing" is to make a separate .h file with the function declarations (prototypes) and include that file in your main file.  Add the .c file to your project so that it gets compiled along with your main file and try it.

    Program notes: I used MCC to set up for 32 MHz internal oscillator and SPI mode 0 with clock = Fosc/4.  This gives an 8 MHz SPI clock, same as used in the Arduino UNO projects.  I ran my PIC at 3.3 Volts, and it works just "swell."

    Final note: to get 8 MHz out of SCK and SDO, you have to set the slew rate registers for minimum slew (this is not the default).  I did this in the Pin Module "Registers" window for SCK, SDO, TFT_CS, TFT_DC, and TFT_RES.  If you
    don't know what this means, it's time to hit the Data Sheet, as all good Beginners do.

    Good Luck!

    Regards,

    Dave

    Footnote: For my modules, the color renditions weren't consistent with the 7735_COLOR definitions.  If you get your display to "work," but RED turns out to be Yellow and you can't figure out what to do, let me know.  If yours turns out OK, then, well, never mind.


    post edited by davekw7x - 2020/07/07 14:38:01

    Attachment(s)

    Attachments are not available: Download requirements not met

    Sometimes I just can't help myself...
    #3
    Vividhobby
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2016/11/12 19:25:43
    • Location: 0
    • Status: offline
    Re: How to solve " function definition is not allowed here" error 2020/07/08 01:17:10 (permalink)
    0
    Dear Dave
     
    Thank you veru much for quick reply and advices on my question.
    I will check your message and program you advised deeply later.
     
    Thanks again.
     
    Vivid
     
    #4
    Vividhobby
    New Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2016/11/12 19:25:43
    • Location: 0
    • Status: offline
    Re: How to solve " function definition is not allowed here" error 2020/07/09 07:02:45 (permalink)
    0
    Hello Dave.
     
    I got working after some modification of my program. 
    Thsnk you very much for your kindness.
     
    Vivid
    #5
    Jump to:
    © 2020 APG vNext Commercial Version 4.5