Difference between revisions of "Coding Standards"
From Wiki
(→Test) |
|||
| Line 1: | Line 1: | ||
| − | = | + | = 8051 Coding Standards = |
| − | + | == Formatting == | |
| + | |||
| + | == Example == | ||
| + | === Header File === | ||
<syntaxhighlight lang=c> | <syntaxhighlight lang=c> | ||
| − | // | + | #ifndef _C_CODING_STANDARDS_H_ |
| − | void | + | #define _C_CODING_STANDARDS_H_ |
| − | + | ||
| − | + | //------------------------------------------------------------------------------------ | |
| − | + | // Global CONSTANTS | |
| + | //------------------------------------------------------------------------------------ | ||
| + | #define TRUE 1 | ||
| + | #define FALSE 0 | ||
| + | |||
| + | //#define SOME_DEFINE | ||
| + | #define SOME_OTHER_DEFINE | ||
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Macros | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | #define LED_INVERT() LED_IO = !LED_IO //Macros should be made with '()' so that they look like regular functions (the last line does not need to be followed by a semicolon) | ||
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Structures | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | struct struct_name | ||
| + | { | ||
| + | unsigned char character; | ||
| + | unsigned int integer; | ||
| + | }; | ||
| + | |||
| + | union random_union | ||
| + | { | ||
| + | struct struct_name name; | ||
| + | unsigned char bytes[sizeof(struct_name)]; | ||
| + | }; | ||
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Function Prototypes | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | void function_name(unsigned char parameters); | ||
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Externs | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | extern bit variable_name; | ||
| + | unsigned char fixed_data_loaction; | ||
| + | unsigned char idata fixed_idata_location; | ||
| + | |||
| + | unsigned char bdata bitaddressable_char; | ||
| + | extern bit bitaddressable_char_bit0; | ||
| + | extern bit bitaddressable_char_bit1; | ||
| + | extern bit any_random_name; | ||
| + | |||
| + | #endif //_C_CODING_STANDARDS_H_ | ||
| + | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | === Code File === | ||
<syntaxhighlight lang=c> | <syntaxhighlight lang=c> | ||
| − | #include | + | #pragma userclass (code = CODING_STANDARDS) |
| − | + | #pragma userclass (const = CODING_STANDARDS) | |
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Includes | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | #include "file.h" | ||
| + | |||
| + | #ifdef SOME_DEFINE | ||
| + | #include "other_file.h" | ||
| + | #endif | ||
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Global Variables | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | // All variables are completely lower-case and delimited with underscores | ||
| + | bit variable_name; | ||
| + | unsigned char other_variable_name; | ||
| + | unsigned long big_ol_variable; | ||
| + | unsigned char fixed_data_location _at_ 0x32; //Cleared automatically on reboots | ||
| + | unsigned char idata fixed_idata_location _at_ 0x80; //Not cleared on warm reboots | ||
| + | |||
| + | unsigned char bdata bitaddressable_char; | ||
| + | sbit bitaddressable_char_bit0 = bitaddressable_char^0; | ||
| + | sbit bitaddressable_char_bit1 = bitaddressable_char^1; | ||
| + | sbit any_random_name = bitaddressable_char^7; | ||
| + | |||
| + | unsigned char code code_array[2] = {0x00, 0xFF}; | ||
| + | unsigned char code big_code_array[6] = | ||
{ | { | ||
| − | + | 0x01, | |
| − | + | 0x00, | |
| + | 0x01, | ||
| + | 0x00, | ||
| + | 0x02, | ||
| + | 0x00 | ||
| + | }; | ||
| + | |||
| + | //------------------------------------------------------------------------------------ | ||
| + | // Function Name - Optional But Recommended Description | ||
| + | //------------------------------------------------------------------------------------ | ||
| + | void function_name(unsigned char parameters) using 2 //All function names are completely lower-case and delimited with underscores | ||
| + | { | ||
| + | unsigned char i; | ||
| + | |||
| + | // A line comment is formatted like this | ||
| + | if(variable_name && !other_variable_name) | ||
| + | variable_name = FALSE; | ||
| + | |||
| + | if(!variable_name || (other_variable_name > 8)) | ||
| + | { | ||
| + | variable_name = TRUE; | ||
| + | fixed_idata_location = 0x0F; //TODO correct a random bug or add a certain feature | ||
| + | } | ||
| + | |||
| + | #ifdef SOME_DEFINE | ||
| + | do | ||
| + | { | ||
| + | other_variable_name--; //Code comment | ||
| + | }while(other_variable_name < 5); | ||
| + | |||
| + | #ifndef SOME_OTHER_DEFINE | ||
| + | for(i = 8; i; i--) | ||
| + | { | ||
| + | other_variable_name++; | ||
| + | fixed_idata_location = ((other_variable_name&0x01) ? 0xFF : 0x80); | ||
| + | } | ||
| + | #endif //!SOME_OTHER_DEFINE | ||
| + | #endif //SOME_DEFINE | ||
| + | |||
| + | #if defined SOME_DEFINE && !defined SOME_OTHER_DEFINE | ||
| + | while(fixed_idata_location&0xF8) //>= 8 | ||
| + | { | ||
| + | fixed_idata_location += 0x0A; | ||
| + | LED_INVERT(); //TODO DEBUG Don't forget to remove this before releasing to the public | ||
| + | } | ||
| + | #elif | ||
| + | |||
| + | #endif //SOME_DEFINE && !SOME_OTHER_DEFINE | ||
| + | |||
| + | switch(other_variable_name) | ||
| + | { | ||
| + | case 0: | ||
| + | case 1: | ||
| + | other_variable_name += 0x80; | ||
| + | break; | ||
| + | default: | ||
| + | other_variable_name = 0x00; | ||
| + | } | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 16:33, 11 August 2017
8051 Coding Standards
Formatting
Example
Header File
#ifndef _C_CODING_STANDARDS_H_
#define _C_CODING_STANDARDS_H_
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define TRUE 1
#define FALSE 0
//#define SOME_DEFINE
#define SOME_OTHER_DEFINE
//------------------------------------------------------------------------------------
// Macros
//------------------------------------------------------------------------------------
#define LED_INVERT() LED_IO = !LED_IO //Macros should be made with '()' so that they look like regular functions (the last line does not need to be followed by a semicolon)
//------------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------------
struct struct_name
{
unsigned char character;
unsigned int integer;
};
union random_union
{
struct struct_name name;
unsigned char bytes[sizeof(struct_name)];
};
//------------------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------------------
void function_name(unsigned char parameters);
//------------------------------------------------------------------------------------
// Externs
//------------------------------------------------------------------------------------
extern bit variable_name;
unsigned char fixed_data_loaction;
unsigned char idata fixed_idata_location;
unsigned char bdata bitaddressable_char;
extern bit bitaddressable_char_bit0;
extern bit bitaddressable_char_bit1;
extern bit any_random_name;
#endif //_C_CODING_STANDARDS_H_Code File
#pragma userclass (code = CODING_STANDARDS)
#pragma userclass (const = CODING_STANDARDS)
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include "file.h"
#ifdef SOME_DEFINE
#include "other_file.h"
#endif
//------------------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------------------
// All variables are completely lower-case and delimited with underscores
bit variable_name;
unsigned char other_variable_name;
unsigned long big_ol_variable;
unsigned char fixed_data_location _at_ 0x32; //Cleared automatically on reboots
unsigned char idata fixed_idata_location _at_ 0x80; //Not cleared on warm reboots
unsigned char bdata bitaddressable_char;
sbit bitaddressable_char_bit0 = bitaddressable_char^0;
sbit bitaddressable_char_bit1 = bitaddressable_char^1;
sbit any_random_name = bitaddressable_char^7;
unsigned char code code_array[2] = {0x00, 0xFF};
unsigned char code big_code_array[6] =
{
0x01,
0x00,
0x01,
0x00,
0x02,
0x00
};
//------------------------------------------------------------------------------------
// Function Name - Optional But Recommended Description
//------------------------------------------------------------------------------------
void function_name(unsigned char parameters) using 2 //All function names are completely lower-case and delimited with underscores
{
unsigned char i;
// A line comment is formatted like this
if(variable_name && !other_variable_name)
variable_name = FALSE;
if(!variable_name || (other_variable_name > 8))
{
variable_name = TRUE;
fixed_idata_location = 0x0F; //TODO correct a random bug or add a certain feature
}
#ifdef SOME_DEFINE
do
{
other_variable_name--; //Code comment
}while(other_variable_name < 5);
#ifndef SOME_OTHER_DEFINE
for(i = 8; i; i--)
{
other_variable_name++;
fixed_idata_location = ((other_variable_name&0x01) ? 0xFF : 0x80);
}
#endif //!SOME_OTHER_DEFINE
#endif //SOME_DEFINE
#if defined SOME_DEFINE && !defined SOME_OTHER_DEFINE
while(fixed_idata_location&0xF8) //>= 8
{
fixed_idata_location += 0x0A;
LED_INVERT(); //TODO DEBUG Don't forget to remove this before releasing to the public
}
#elif
#endif //SOME_DEFINE && !SOME_OTHER_DEFINE
switch(other_variable_name)
{
case 0:
case 1:
other_variable_name += 0x80;
break;
default:
other_variable_name = 0x00;
}
}