I have installed the first version of the dimmer and I must say – it works like a charm! I still need to find a suitable place for the pot-meter and ideally I would really like to have a rotary encoder installed instead. Partly because I haven’t interfaced a rotary encoder before, but also because I think they a pretty cool small devices. Well, That will call for an update later on. Here is the quick and dirty version of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <htc.h> __CONFIG(INTIO & WDTDIS); //internal oscillator, no WDT int i=0, j=0; int treshold=0; void init(void) { //OSC OSCCON=0x70; //run at 8MHz //PORTS TRISC=0xFE; //input on all but 0 PORTC=0x00; ANSEL=0x80; //enable ADC on AN7 //ADC ADFM=0; VCFG=0; ADCS0=1; ADCS1=0; ADCS2=1; ADON=1; CHS0=1; CHS1=1; CHS2=1; } getval(void) { GODONE=1; while ((ADCON0&0x02) ==2) {} //wait for the conversion to finish } void main(void) { init(); //initialize while(1) { if (i<treshold) //if i lower than treshold, turn on RC0=1; else RC0=0; //turn off i++; if (i>255) //rollover i=0; j++; if (j==200) //sample ADC every 200th cycle { getval(); treshold=ADRESH; //update value j=0; //reset } } } |