Super, Ive never used C on a PIC ill give it a whirlNot any more!!
I haven't got the external xtal yet though if that makes any odds?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Super, Ive never used C on a PIC ill give it a whirlNot any more!!
Ah, you've got one of Pete's kits (forum member 'Gecko').
Eight LEDs spread across two ports in that circuit will require a little bit more work in your new software (I wonder why he did that?).
What clock speed are you running for this demo, Ian?Simple C code (Its what I mainly work with!!!)
Running.....C:#include<XC.h> __PROG_CONFIG(1,0x3F0A); volatile unsigned char ledPwm[8]; // * different values volatile unsigned char PWM; void delayUs(int x) { x>>= 2; while(x--); } void delayMs(int x) // Millisecond ish { while(x--) delayUs(1000); } void interrupt ISR() { if(TMR0IF) { PWM++; // Check each level!! if(ledPwm[0] < PWM)RB0 = 0; if(ledPwm[1] < PWM)RB1 = 0; if(ledPwm[2] < PWM)RB2 = 0; if(ledPwm[3] < PWM)RB3 = 0; if(ledPwm[4] < PWM)RB4 = 0; if(ledPwm[5] < PWM)RB5 = 0; if(ledPwm[6] < PWM)RB6 = 0; if(ledPwm[7] < PWM)RB7 = 0; if(PWM > 64) { PWM = 0; // Frequency start PORTB = 0xFF; } TMR0 = 200; // Adjust frequency here.. TMR0IF = 0; } } void main() { unsigned char x,LED; TRISA0 = 0; TRISB = 0x0; OPTION_REG = 0; // no pre scale TMR0IE = 1; TMR0 = 200; // frequency GIE = 1; // interrupts on LED = 64; PORTB = 0x55; // start pattern while(1) { for(x=0;x<8;x+=2) // fill with arbitury values ledPwm[x] = LED; // for(x=1;x<8;x+=2) ledPwm[x] = 64 - LED--; if(LED < 1) LED = 64; delayMs(50); // Visualise time... } }
Yeah, four pins on PORTA and four pins on PORTB. It will require a pair of write operations to update the LEDs during each PWM 'step' which increases the PWM 'step' time.When you say 2 ports, is that an internal thing, because they are connected to 8 individual pins
It actually looks pretty good. You can see some "stair stepping" but that's to be expected during fade operations since linear PWM steps produce non-linear brightness levels. It's certainly better than the three coarse brightness levels the OP started out with...20Mhz.... The interrupt latencey is @ 8.4uS and is called every 25uS.. There is 33.6uS per step @ 2.15mS time base...
If I went to a rather (Coarser ) frequency ie.. 32 steps then I would get the 1khz I quoted...
This was only an example... I have used this before just to dim a control panel....
When I first ran it, I thought I'd programmed it incorrectly as it seemed to only have 16 steps.... But it was an optical error to the eye.... After I slowed the PWM refresh to 500mS I could see the difference..It actually looks pretty good. You can see some "stair stepping" but that's to be expected during fade operations since linear PWM steps produce non-linear brightness levels. It's certainly better than the three coarse brightness levels the OP started out with...
And after all that we are told he will only ever use one at a time.... 8:1 multiplexing and one hardware PWM would have done the job!!!.....
.
This is the reason for this line....The point here is that he can offer PWM on any generic IO pin...
void interrupt ISR()
{ static unsigned char step = 0;
static unsigned char toggle[64] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
PORTB ^= toggle[step]; // update channel outputs
toggle[step] = 0; // clear element for next period
step++; //
if(step == 64) // if end-of-period
{ step = 0; //
toggle[led[0]] |= 1; // insert LED 0 toggle bit
toggle[led[1]] |= 2; // insert LED 1 toggle bit
toggle[led[2]] |= 4; // insert LED 2 toggle bit
toggle[led[3]] |= 8; // insert LED 3 toggle bit
toggle[led[4]] |= 16; // insert LED 4 toggle bit
toggle[led[5]] |= 32; // insert LED 5 toggle bit
toggle[led[6]] |= 64; // insert LED 6 toggle bit
toggle[led[7]] |=128; // insert LED 7 toggle bit
toggle[0] ^= ~PORTB; // initialize first toggle element
} //
TMR0 = 210; // adjusts step size & frame rate
TMR0IF = 0; //
} //
netmgr,
Is the 16F628A socketed on that board?
that was the plan yes, then when it got to full brightness, leave it on full and fade in the next oneI'm curious if you were looking for a smooth fade similar to the one shown in the following video?