What I'm trying to do now is work out how to loop my program and how to run through the register setup procedure once only. for following did not work
Nope, because your for loop doesn't make any sense the way you have it. The structure of a for loop in C is:
for (
begin;
test;
step) {
body }
Which translates to:
begin;
while (
test) {
body;
step; }
... so your 'for(; setupconfig=0;)' will never execute because 'setupconfig=0' equals zero which means false.
However, I'm not sure why you want a for loop at all, especially over the setup code; the general layout of your code should look something like:
void main() {
/* Setup code here, will execute once at startup */
while (1) {
/* Code to execute repeatedly forever here */
}
}
So I'd put everything that's inside your for loop at the moment where I have the first comment, and then put the ADC reading and PWM duty cycle setting calls inside the while loop where I have the second comment.
Alternatively, if you wanted to try a loop to test out the PWM stuff, you could do something like this inside your while loop:
for (speed_fan = 64; speed_fan < 192; speed_fan++)
{
PWM1_set_duty(speed_fan);
/* Some sort of delay here so you can see the ramp;
* not sure what delay routines you might have,
* so adjust to suit!
*/
delay_ms(100);
}
... which will make it repeatedly ramp the PWM output from 25% up to 75% over the course of ~12 seconds, just so you can see it working.
Hmm, actually, your comment about the PWM library accepting 10-bit values is at odds with the documentation I found earlier, which says it only accepts 8-bit values! So you may need to change the start and end conditions in my example loop to suit -- but of course, as you're learning the language it's always a good idea to play around with examples anyway so you can get a feel for how it hangs together.