void main(void);
void Initialize_ADC(void);
void main()
{
unsigned char dig_input;
unsigned char frank;
Initialize_ADC();
while(1)
{
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
frank = 0x10;
while(frank != 0x00)
{frank -= 1;}
while (ADGO)
{}
frank = 0xFF;
while(frank != 0x00)
{frank -= 1;}
dig_input = ADRESH; //defines the digital input.
if (dig_input<128)
{
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0x00;
}
else
{
dig_input -= 128; //dig_input = dig_input-128
dig_input += dig_input; //multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
}
};
void Initialize_ADC()
{
TRISA = 0x01;
TRISC = 0x00;
PORTC = 0x00;
PR2 = 0x18;
T2CON = 0x05;
ADCON0 = 0x80; //clear ADCON0 A2D starts sampling again.
//Set ADCON1
ADCON1 = 0x00; // clear ADCON1 register.//
ADCON1 = 0x02; // set PCFG1 bit i.e AN0 is analogue input.
ADON = 1;
};
void main(){
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
While(1){
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
while (ADGO);
dig_input = ADRESH; //defines the digital input.
if (dig_input<128){ //if solar panel (Vin) to boost converter is less than 10V don't boost.
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else{
dig_input -= 128; //dig_input = dig_input-128
dig_input += dig_input; //multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for(i=0;i<1000;i++);
}
}
No, it's not just you. Some whitespace is very necessary as "punctuation" and to keep different sections of code separated visually. But too much of a good thing just sprawls the code unnecessarily long and you end up having to scroll up and down constantly to keep track of where it's going.Pommie said:Is it just me?
Torben said:That said, discussions over code formatting often wind up in the same hundred-post arguments that vi vs. emacs or mac vs. windows do.
void main()
{
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
While(1)
{
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
while (ADGO);
dig_input = ADRESH; //defines the digital input.
if (dig_input<128){ //if solar panel (Vin) to boost converter is less than 10V don't boost.
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else
{
dig_input -= 128; //dig_input = dig_input-128
dig_input += dig_input; //multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for(i=0;i<1000;i++);
}
if (a>b)
c = d;
c++;
if (a>b)
c = d;
a = p/2;
c++;
if (a>b)
{
c = d;
a = p/2;
}
c++;
if (a>b)
{
c = d;
}
c++;
Brian Hoskins said:People have even written books on it, for goodness sake. Sod that is my opinion - I'll keep guidelines in mind and I'm always happy to hear advice, but basically I'll pick a code writing method that I personally feel is the most sensible and stick to that.
Brian
Brian Hoskins said:My version;
<snip>
void main()
{
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
while (1)
{
// Read_ADC
ADGO = 1; // captures analogue voltage on pin.
while (ADGO) {}
dig_input = ADRESH; // defines the digital input.
// if solar panel (Vin) to boost converter is less than 10V don't boost.
if (dig_input < 128)
{
CCPR1L = 0x00; // sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else
{
dig_input -= 128; // dig_input = dig_input-128
dig_input += dig_input; // multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for (i = 0; i < 1000; i++) {}
}
}
if(a==b){
somert;
}
#pragma romdata
//Example heirarchy items.
// **********************************************************************************************
// The following arrays contain menu items for each level of menu heirarchy.
MenuItem menu1Items[] =
{
// TopLevel , Name , subMenu, handler
1 ,"Menu1-1" , 0 , handler,
1 ,"Menu1-2" , 0 , 0
};
//This represents the settings sub menu.
MenuItem menu2Items[] =
{
// TopLevel , Name , subMenu, handler
1 ,"Menu2-1" , 0 , 0,
1 ,"Menu2-2" , 0 , 0,
1 ,"Menu2-3" , 0 , 0
};
//base items. also: menu initialises by cha startup item to baseItems[0]
MenuItem menu0Items[] =
{
// TopLevel , Name , subMenu, handler
0 , "Menu1" , &menuLevels[1] , 0,
0 , "Menu2" , &menuLevels[2] , 0,
1 , "Menu3" , 0 , 0
};
//*********************************************************************************************
//End of item arrays
//The following array is required by menu system.
//It contains MenuLevel items, which describe a level that contains menu items.
//First item should be the base menu.
MenuLevel menuLevels[] = { // start index , array size , array
0 , sizeof(menu0Items) / sizeof (MenuItem), menu0Items,
0 , sizeof(menu1Items) / sizeof (MenuItem), menu1Items,
0 , sizeof(menu2Items) / sizeof (MenuItem), menu2Items,
};
Pommie said:I don't have a problem with any of the above examples. Looking at them, it appears my use of an open brace on the end of the line is frowned upon.
I.E.
Code:if(a==b){ somert; }
The vertical spacing is not the only problem. This from another recent post,
(my apologies to the original poster.)
<snip>
Mike.
void main()
{
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
while(1)
{
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
while (ADGO);
dig_input = ADRESH; //defines the digital input.
// Boost when (Vin) to boost converter is less than 10V (128)
if (dig_input<128)
{
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else
{
dig_input -= 128; // dig_input = dig_input-128
dig_input += dig_input; // multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for(i=0;i<1000;i++);
}
} // main()
aussiepoof said:The great thing about this topic is that *everyone* has an opinion and some of us are so indecisive we have two
This formatting allowed me to easily find the missing brace in Brian Hoskin's code posting - and no, I'm not impugning Brian, just pointing out why formatting is so important.
BTW: I improved the comment on the first if(), but the comments on the manipulations of dig_input leave a lot to be desired... comments ought to refer to the logic of the solution, not merely be a reiteration in English (or language of choice) of the code!!!
Maybe not when fresh, but many people work past the point where they should knock it off. All the examples here have been quite short. A few hundred or thousand lines of code is quite different. It is best to do what one can to avoided problems.Pommie said:3v0,
Surely, this is not a mistake that an experienced programmer would make.
...
Mike.
3v0 said:Many people work past the point where they should knock it off. All the examples here have been quite short. A few hundred or thousand lines of code is quite different. It is best to do what one can to avoided problems.
donniedj said:This is partly why I dislike C, difficult to read, susceptible to too much interpretation, and the written text becomes more effort consuming than the original reason for the code. It should solve problems and not add to it.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?