Library creating

Hi, i am having trouble with code, i have a raw code to test my connection between c2000 controller and encoder and now i just need to create .c and .h file to make it look more "beautiful", if there is someboody who can tell me how to do it i would be thankful the code for communication is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * \file main.c
 * \brief Main file for the project
 * \author Viktor
 * \date 4th March 2021
 *
 * Last update: 4th March 2021
 *
 */

#include <F2837xD_device.h>
#include "mcu.h"






/**
 * \brief Delay function defined in \a F2837xD_usDelay.asm
 */
extern void _F28x_usDelay(long LoopCount);
#define CPU_RATE 5.0L //!< Sets CPU rate for DELAY_US macro.
#define DELAY_US(A)  _F28x_usDelay(((((long double) A * 1000.0L) / (long double)CPU_RATE) - 9.0L) / 5.0L) //!< Sets CPU rate for DELAY_US macro.

#include"mcu.h"
#include"spi_communication.h"




unsigned int skusobnePole[10];
unsigned RxData[10];
unsigned PositionRaw = 0;
unsigned CRC_value = 0;
unsigned Error_bits = 0;

// main function
int main(void)
{
    unsigned i=0;

    // initialize MCU clocks
    mcu_initClocks();

    for (i=0; i<10; i++){
        RxData[i]=0;
    }

    // initialize SPI
    mcu_initSPI(500);


EALLOW;
//GpioCtrlRegs.GPBDIR.bit.GPIO58=1;
//GpioCtrlRegs.GPBDIR.bit.GPIO62=1;
EDIS;
    while(1){

        DELAY_US(200);

        RxData[0]= spi_transmitChar(0);
        RxData[1]= spi_transmitChar(0);
        RxData[2]= spi_transmitChar(0);
        RxData[3]= spi_transmitChar(0);
        PositionRaw = ((RxData[0] & 0x07) << 10) | (RxData[1] << 2) | (RxData[2] & 0xC0);
        Error_bits = (RxData[2] & 0x30) >> 4;
        CRC_value = (RxData[2] & 0x0F << 2) | ((RxData[3] & 0xC0) >> 6);











    }

	return 0;
}
Last edited on
Please read https://www.cplusplus.com/articles/jEywvCM9/ and edit your post for readability.
need to create .c and .h file to make it look more "beautiful"

"Beauty is in the eye of the beholder."

What do you mean by 'beautiful?' More consistent formatting of the source code? Better I/O when the app is executing?

"Better" formatting with your code by consistent indentation and hack'n'slashing on the excessive vertical whitespace:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * \file main.c
 * \brief Main file for the project
 * \author Viktor
 * \date 4th March 2021
 *
 * Last update: 4th March 2021
 *
 */

#include <F2837xD_device.h>
#include "mcu.h"

 /**
  * \brief Delay function defined in \a F2837xD_usDelay.asm
  */
extern void _F28x_usDelay(long LoopCount);
#define CPU_RATE 5.0L //!< Sets CPU rate for DELAY_US macro.
#define DELAY_US(A)  _F28x_usDelay(((((long double) A * 1000.0L) / (long double)CPU_RATE) - 9.0L) / 5.0L) //!< Sets CPU rate for DELAY_US macro.

#include"mcu.h"
#include"spi_communication.h"

unsigned int skusobnePole[10];
unsigned RxData[10];
unsigned PositionRaw = 0;
unsigned CRC_value   = 0;
unsigned Error_bits  = 0;

// main function
int main(void)
{
   unsigned i = 0;

   // initialize MCU clocks
   mcu_initClocks();

   for (i = 0; i < 10; i++)
   {
      RxData[i] = 0;
   }

   // initialize SPI
   mcu_initSPI(500);

   EALLOW;
   //GpioCtrlRegs.GPBDIR.bit.GPIO58=1;
   //GpioCtrlRegs.GPBDIR.bit.GPIO62=1;
   EDIS;
   while (1)
   {
      DELAY_US(200);

      RxData[0]   = spi_transmitChar(0);
      RxData[1]   = spi_transmitChar(0);
      RxData[2]   = spi_transmitChar(0);
      RxData[3]   = spi_transmitChar(0);
      PositionRaw = ((RxData[0] & 0x07) << 10) | (RxData[1] << 2) | (RxData[2] & 0xC0);
      Error_bits  = (RxData[2] & 0x30) >> 4;
      CRC_value   = (RxData[2] & 0x0F << 2) | ((RxData[3] & 0xC0) >> 6);
   }

   return 0;
}

(I can't even remotely test the code because I don't have the 3rd party library/libraries needed.)

There are apps available, standalone or online, that can format source code to look better. Some IDEs (Integrated Development Environment) have that capability 'built-in.' Visual Studio is one.
by more "beautiful" i ment that in the main.c there will be only one function which will do what is from 54 to 60 line in code, so i am asking how it can be done
Well I'd get rid of the globals, and put all those related variables in a struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct packet {
    unsigned RxData[10];
    unsigned PositionRaw;
    unsigned CRC_value;
    unsigned Error_bits;
};

void readpacket(struct packet *p) {
    p->RxData[0]   = spi_transmitChar(0);
    p->RxData[1]   = spi_transmitChar(0);
    p->RxData[2]   = spi_transmitChar(0);
    p->RxData[3]   = spi_transmitChar(0);
    p->PositionRaw = ((p->RxData[0] & 0x07) << 10) | (p->RxData[1] << 2) | (p->RxData[2] & 0xC0);
    p->Error_bits  = (p->RxData[2] & 0x30) >> 4;
    p->CRC_value   = (p->RxData[2] & 0x0F << 2) | ((p->RxData[3] & 0xC0) >> 6);
}

int main ( ) {
    struct packet p = { 0 };  // all members are 0
    // other init
    while ( 1 ) {
        DELAY_US(200);
        readpacket(&p);
    }
}

https://softwareengineering.stackexchange.com/questions/257541/separating-code-into-smaller-files-in-c

Found that link by doing a 'net search, here's the meta-search I used:
https://duckduckgo.com/?q=c+code+splitting+code+into+header+and+source+files&t=ffsb&ia=web

If'n that isn't helpful then I don't know what can be done since what you are trying to do isn't all that understandable.

The internet has a LOT of resources available, searching for what you want/need should be a first step. It is part of "asking questions the smart way."

How To: Ask Questions The Smart Way - C++ Articles
https://cplusplus.com/articles/jLzyhbRD/

Being a self-taught (and still learning) programming hobbyist I've had lots of questions how to do something across the years. Most times I've found what I'm looking for, or something close enough to work, by scraping the bowel of the interwebz.

The few times I couldn't find something on my own that worked I then began asking questions of others, starting off informing them what I had done in detail. More often than not answers were quick in coming.
Another meta-search that MIGHT be helpful:
https://duckduckgo.com/?q=c+code+creating+a+library&t=ffsb&ia=web
Topic archived. No new replies allowed.