Hello all.
I have a PIC16F88 with ADC on PortA.0. I am measuring the voltage on a supply rail from 0-5VCD. I have a 35 LED display (3mm LEDs each) that is wired in a 7x5 matrix. I only want one LED to come on at any one time. Hence a moving 'dot'. The code works for the most part, but I have a minor annoyance. If I slowly raise the voltage I am measuring, there will be certain places on the dot display that has 2 x LEDs coming ON and flickering. It is like the ADC is just between bits and oscillates between two thus displaying 2 LEDs instead of one.
The code most certainly can be re-written a whole lot better and more efficiently. In my code, I simply look for values of adval and turn on the appropriate LED. On each LED turn-on, I also run a subroutine that first sets all the LEDs OFF. The ADC is set up as 8 bit. I know that is way more than what I need for resolution. 6 bit would probably work just fine.
Any help including a re-write would be greatly appeciated!
The code:
Code:'Initialize variables PIC16F88 #CONFIG __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF & _INTRC_IO __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_OFF #ENDCONFIG OSCCON=$60 'Set Internal Oscillator to 4MHz DEFINE OSC 4 DEFINE ADC_BITS 8 ' 8 bit A/D Conversion DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3) DEFINE ADC_SAMPLEUS 50 ' 50 uS A/D sample time ANSEL = %00000001 ' Set pin (AN0) to analog input, the rest to digital ADCON0 = %11000000 ' Set up A/D converter - Right Just., VDD REF., CH 0, ON CMCON = 7 adval var word pause 1000 'settle high PortA.1 'Col1 high PortA.2 'Col2 high PortA.3 'Col3 high PortA.4 'Col4 high PortB.0 'Col5 low PortB.1 'Row1 low PortB.2 'Row2 low PortB.3 'Row3 low PortB.4 'Row4 low PortB.5 'Row5 low PortB.6 'Row6 low PortB.7 'Row7 Col1 var PortA.1 Col2 var PortA.2 Col3 var PortA.3 Col4 var PortA.4 Col5 var PortB.0 Row1 var PortB.1 Row2 var PortB.2 Row3 var PortB.3 Row4 var PortB.4 Row5 var PortB.5 Row6 var PortB.6 Row7 var PortB.7 mainloop: ADCIN 0, adval if adval <5 then gosub ResetLEDs goto mainloop 'Zero, all LEDs OFF endif if adval >4 and adval <13 then 'Led2 gosub ResetLEDs Row1 = 1 Col1 = 0 endif if adval >12 and adval <21 then 'Led3 gosub ResetLEDs Row2 = 1 Col1 = 0 endif if adval >20 and adval <29 then 'Led4 gosub ResetLEDs Row3 = 1 Col1 = 0 endif if adval >28 and adval <36 then 'Led5 gosub ResetLEDs Row4 = 1 Col1 = 0 endif if adval >35 and adval <44 then 'Led6 gosub ResetLEDs Row5 = 1 Col1 = 0 endif if adval >43 and adval <52 then 'Led7 gosub ResetLEDs Row6 = 1 Col1 = 0 endif if adval >51 and adval <59 then 'Led8 gosub ResetLEDs Row7 = 1 Col1 = 0 endif if adval >58 and adval <67 then 'Led9 gosub ResetLEDs Row1 = 1 Col2 = 0 endif if adval >66 and adval <75 then 'Led10 gosub ResetLEDs Row2 = 1 Col2 = 0 endif if adval >74 and adval <83 then 'Led11 gosub ResetLEDs Row3 = 1 Col2 = 0 endif if adval >82 and adval <90 then 'Led12 gosub ResetLEDs Row4 = 1 Col2 = 0 endif if adval >89 and adval <98 then 'Led13 gosub ResetLEDs Row5 = 1 Col2 = 0 endif if adval >97 and adval <106 then 'Led14 gosub ResetLEDs Row6 = 1 Col2 = 0 endif if adval >105 and adval <114 then 'Led15 gosub ResetLEDs Row7 = 1 Col2 = 0 endif if adval >113 and adval <121 then 'Led16 gosub ResetLEDs Row1 = 1 Col3 = 0 endif if adval >120 and adval <129 then 'Led17 gosub ResetLEDs Row2 = 1 Col3 = 0 endif if adval >128 and adval <137 then 'Led18 gosub ResetLEDs Row3 = 1 Col3 = 0 endif if adval >136 and adval <144 then 'Led19 gosub ResetLEDs Row4 = 1 Col3 = 0 endif if adval >143 and adval <152 then 'Led20 gosub ResetLEDs Row5 = 1 Col3 = 0 endif if adval >151 and adval <160 then 'Led21 gosub ResetLEDs Row6 = 1 Col3 = 0 endif if adval >159 and adval <168 then 'Led22 gosub ResetLEDs Row7 = 1 Col3 = 0 endif if adval >167 and adval <175 then 'Led23 gosub ResetLEDs Row1 = 1 Col4 = 0 endif if adval >174 and adval <183 then 'Led24 gosub ResetLEDs Row2 = 1 Col4 = 0 endif if adval >182 and adval <191 then 'Led25 gosub ResetLEDs Row3 = 1 Col4 = 0 endif if adval >190 and adval <199 then 'Led26 gosub ResetLEDs Row4 = 1 Col4 = 0 endif if adval >198 and adval <206 then 'Led27 gosub ResetLEDs Row5 = 1 Col4 = 0 endif if adval >205 and adval <214 then 'Led28 gosub ResetLEDs Row6 = 1 Col4 = 0 endif if adval >213 and adval <222 then 'Led29 gosub ResetLEDs Row7 = 1 Col4 = 0 endif if adval >221 and adval <229 then 'Led30 gosub ResetLEDs Row1 = 1 Col5 = 0 endif if adval >228 and adval <237 then 'Led31 gosub ResetLEDs Row2 = 1 Col5 = 0 endif if adval >236 and adval <245 then 'Led32 gosub ResetLEDs Row3 = 1 Col5 = 0 endif if adval >244 and adval <253 then 'Led33 gosub ResetLEDs Row4 = 1 Col5 = 0 endif if adval >252 then 'Led34 gosub ResetLEDs Row5 = 1 Col5 = 0 endif GoTo mainloop ResetLEDs: high PortA.1 'Col1 high PortA.2 'Col2 high PortA.3 'Col3 high PortA.4 'Col4 high PortB.0 'Col5 low PortB.1 'Row1 low PortB.2 'Row2 low PortB.3 'Row3 low PortB.4 'Row4 low PortB.5 'Row5 low PortB.6 'Row6 low PortB.7 'Row7 return End
Bookmarks