Heres my code so far. It appears to work as needed, but thus far I have only tested it with a sim. I will try and put the hardware together tonight and test it in real time.
All comments welcome. I did this all software (no hardware PWM) as I thought it would be more fun.Code:' Name : RMTEO Contest 2.pbp ' Compiler : PICBASIC PRO Compiler 2.6A ' Assembler : MPASM ' Target PIC : 16F1947 ' Hardware : RMTEO Contest ' Oscillator : internal ' Keywords : ' Description : PICBASIC PRO program to drive 4 LED's at independent speeds ' adjustable from 4 pot's. fade from 0-100-0% for each cycle ' ' ' ' ' Configure ports ' ' ANSELA = %00001111 'port A 0-3 analog 4-7 digital ANSELE = %00000000 'port E all digital ANSELF = %00000000 'port F all digital ANSELG = %00000000 'port G all digital CM1CON0 = %00000000 'No comparaters CM1CON1 = %00000000 CM2CON0 = %00000000 CM2CON1 = %00000000 ' preload latches before setting direction LATB = %00000000 LATC = %00000000 LATD = %00000000 'LED's 0 for red, 1 FOR GREEN should start red LATF = %00000000 ' low nibble is drive enable. 1 disables LATG = %00000000 ' I/O direction TRISA = %00001111'x,x,x,x,p3,p2,p1,p0 TRISB = %00000000'x,x,x,x,led3,led2,led1,led0 TRISC = %00000000'UNUSED TRISD = %00000000'UNUSED TRISE = %00000000'UNUSED TRISF = %00000000'UNUSED TRISG = %00000000'UNUSED 'REMOVE THIS LINE IF NOT SETTING THE CONFIGS IN CODE SPACE @ __config _CONFIG1,_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF @ __config _CONFIG2,_WRT_OFF & _VCAPEN_OFF & _PLLEN_ON & _STVREN_OFF & _LVP_OFF ;_BORV_25 '------------------Internal osc setup OSCCON = %11110000 'clock is 8 meg. turn on PLL for 32 meg clock DEFINE OSC 32 DEFINE NO_CLRWDT 1 'Variables 'Port assignments '------------------port B '---------------------words freqs1 var word freqs2 var word freqs3 var word freqs4 var word freq1 var word freq2 var word freq3 var word freq4 var word '---------------------Bytes TICKS VAR BYTE display VAR BYTE duty1 VAR BYTE duty2 VAR BYTE duty3 VAR BYTE duty4 VAR BYTE myflags var byte '---------------------Bits led1 var display.0 led2 var display.1 led3 var display.2 led4 var display.3 df1 var myflags.0 df2 var myflags.1 df3 var myflags.2 df4 var myflags.3 '---------------------INIT VARIABLE VALUE GOTO START MYINIT: BSR = 0 TICKS = 0 freqs1 = 128 freqs2 = 256 freqs3 = 512 freqs4 = 1023 duty1 = 0 duty2 = 0 duty3 = 0 duty4 = 0 display = 0 MYFLAGS = 0 '-------------------A/D setup adcon0 = %00000001 adcon1 = %11100000 define ADC_BITS 10 '-------------------10uS interrupt setup DEFINE INTHAND _TimeBase PIR1.1 = 0 PIE1.1 = 1 ; Enable Timer2 interrupts INTCON.6 = 1 ; enable PEIE INTCON.7 = 1 ; enable GIE T2CON = %00000100 ; Setup Timer2 AND SET PRESCALE TO 1:1. @32Mhz THE Interrupt PR2 = 39' FOR TMR2 WILL BE 10uSec RETURN '---------------------start of program START: GOSUB MYINIT MAIN: adcin 0,freqs1 '-------------------------led1 math if ticks>duty1 then led1 = 0 else led1 = 1 endif if freq1 > freqs1 then if df1 then duty1=duty1+1 else duty1=duty1-1 endif if duty1=100 then df1=0 if duty1=0 then df1 = 1 freq1 = 1 endif adcin 1,freqs2 '------------------------led2 math if ticks>duty2 then led2 = 0 else led2 = 1 endif if freq2 > freqs2 then if df2 then duty2=duty2+1 else duty2=duty2-1 endif if duty2=100 then df2=0 if duty2=0 then df2 = 1 freq2 = 1 endif adcin 2,freqs3 '------------------------led3 math if ticks>duty3 then led3 = 0 else led3 = 1 endif if freq3 > freqs3 then if df3 then duty3=duty3+1 else duty3=duty3-1 endif if duty3=100 then df3=0 if duty3=0 then df3 = 1 freq3 = 1 endif adcin 3,freqs4 '---------------------------led4 math if ticks>duty4 then led4 = 0 else led4 = 1 endif if freq4 > freqs4 then if df4 then duty4=duty4+1 else duty4=duty4-1 endif if duty4=100 then df4=0 if duty4=0 then df4 = 1 freq4 = 1 endif GOTO MAIN '-----------------------ISR TimeBase: BSR = 0 portb = display ' update led's freq1 = freq1 +1 freq2 = freq2 +1 freq3 = freq3 +1 freq4 = freq4 +1 if ticks >99 then ticks = 0 else ticks = ticks+1 endif pir1.1 = 0 ' clear interrupt @ RETFIE
Thanks for the contest RMTEO! Sorry I seem to be the only one entering.




Bookmarks