For me too ... But I can prove it (by video) !
Maybe because when no button is pressed, ADC reading is NOT 1023 ?!
For me too ... But I can prove it (by video) !
Maybe because when no button is pressed, ADC reading is NOT 1023 ?!
I do not know what version of PICBasic you are using or what version of MPASM either.
I compiled and ran the following code in the MPASM Simulator and it works fine for all (3) IF/THEN statements in main.
If adval <= 419 then myLED does not increment.
If adval > 419 and <= 450 then myLED only increments 1 time for each loop (IF/THEN #1 : Not #2 and #3)
if adval > 450 and < 536, then myLED increments 3 times for each loop (IF/THEN #1, #2 and #3)
if adval => 536 and < 600 then myLED increments 2 times for each loop (IF/THEN #2 and #3 : Not #1)
if adval => 600 then myLED does not increment.
Code:'MCU = 12F675 'PBP v3.0.7.4 'MPASM v8.90 DEFINE OSC 4 DEFINE ADC_BITS 10 DEFINE ADC_CLOCK 3 DEFINE ADC_SAMPLEUS 50 CMCON = 7 OPTION_REG = %10000110 TRISIO = %00001100 GPIO = %00000001 ANSEL = %00010100 ADCON0 = %10001001 adval var word myLED var word adval = 0 myLed = 0 goto main LED: myLED = myLEd + 1 return main: for adval = 0 to 700 If adval > 419 and adval < 536 then gosub LED 'Works If adval > 450 and adval < 600 then gosub LED 'Works If adval < 600 and adval > 450 then gosub LED 'Works next myLed = 0 goto main
Regards,
TABSoft
Thanks !
In Proteus my code work fine too ... But not work in "real life".
Tomorrow I will come with another issue ...
...so I'm back !
After solving first problem, I meet another one ... I can not reading two channel ADC.
I try two version of code :
-first variant
-second variantCode:@ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON & _CP_ON DEFINE OSC 4 CMCON = 7 OPTION_REG.7= 1 TRISIO = %00001110 GPIO = %00000001 ANSEL = %00110110 ADCON0.7 = 1 adval var word advalb var word PAUSE 200 main: ADCON0.2 = 1 ADCON0.3 = 0 ADCON0.1 = 1 WHILE ADCON0.1 = 1 : WEND adval.HighByte = ADRESH adval.LowByte = ADRESL PAUSE 50 if adval < 950 then if adval < 940 AND adval > 650 THEN gosub Led1 IF adval < 600 AND adval > 450 THEN gosub Led2 IF adval < 400 AND adval > 150 THEN gosub Led3 IF adval < 100 THEN gosub Led4 endif ADCON0.2 = 0 ADCON0.3 = 1 ADCON0.1 = 1 WHILE ADCON0.1 = 1 : WEND adval.HighByte = ADRESH adval.LowByte = ADRESL PAUSE 50 if advalb < 950 then if advalb < 940 AND advalb > 650 THEN gosub Led1 IF advalb < 600 AND advalb > 450 THEN gosub Led2 IF advalb < 400 AND advalb > 150 THEN gosub Led3 IF advalb < 100 THEN gosub Led4 endif goto main
ONLY "advalA < 100" is sensing and LED4 command is executed ...Code:@ __config _XT_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _BODEN_ON & _CP_ON DEFINE OSC 4 DEFINE ADC_BITS 10 DEFINE ADC_CLOCK 3 DEFINE ADC_SAMPLEUS 200 CMCON = 7 OPTION_REG = %10000110 TRISIO = %00000110 GPIO = %00000110 ANSEL = %00000110 ADCON0.7 = 1 ADVALA VAR WORD ADVALB VAR WORD pAUSE 100 main: adcin 1, advalA pause 5 if advalA < 950 then if advaLA < 940 AND advalA > 650 THEN gosub LED1 IF advalA < 600 AND advalA > 450 THEN gosub LED2 IF advalA < 400 AND advalA > 150 THEN gosub LED3 IF advalA < 100 THEN gosub LED4 endif adcin 2, advalB pause 5 if advalB < 950 then if advalB < 940 AND advalB > 655 THEN gosub LED1 IF advalB < 600 AND advalB > 450 THEN gosub LED2 IF advalB < 400 AND advalB > 150 THEN gosub LED3 IF advalB < 100 THEN gosub LED4 endif goto main
Can somebody pointing to the right direction ? Thanks in advance !
Schematic added.
![]()
A couple of suggestions second variant.
Remove "GPIO = %00000110" you are trying to set a high on the input pins.
Change "ANSEL = %00000110" to "%01110110", this will set the PIC ADC clock to RC mode and will match the
"DEFINE ADC_CLOCK 3" directive.
Then give Variant 2 a try.
Regards,
TABSoft
Hi Fratello. A while back I did the same basic thing you're doing. On an ADC pin I attached a 47K pullup resistor. Then a 22K resistor with pushbutton to ground, 10K with button to ground, 2.2K with button to ground and a 1K with button to ground. Here is the code. It works perfect for me.
ANSEL = 0 'all inputs digital
CMCON = 7 'comparators off
DEFINE OSCCAL_1K 1 ' Set OSCCAL for 1K device to accurize osc
TRISIO = %11111111 'SET I/O'S to all inputs for now
@ device pic12F675, INTRC_OSC_NOCLKOUT, wdt_on, BOD_ON,pwrt_on, mclr_off, protect_on
BUT VAR BYTE 'BUTTON VARIABLE
BUT2 VAR BYTE 'VARIABLE TO COMPARE READINGS TO PREVENT WRONG ADC READINGS
Pause 500 'SETTLE DOWN
STAYHERE:
ADCIN 3,BUT 'ADCIN COMMAND AUTOMATICALLY CONVERTS I/O TO ADC PORT
IF BUT < 100 Then STAYHERE 'IF BUTTON STUCK on power up WAIT HERE
BEGIN:
NAP 0 'REDUCE BATTERY DRAIN
ADCIN 3,BUT
IF BUT > 100 Then begin 'BUTTON not PUSHED
table:
ADCIN 3,BUT
Pause 100
ADCIN 3,BUT2
IF BUT <> BUT2 Then TABLE 'RE-READ ADC should both be same
IF BUT < 91 AND BUT > 71 Then (DO FUNCTION 1)
IF BUT < 50 AND BUT > 40 Then (DO FUNCTION 2)
IF BUT < 15 AND BUT > 8 Then (DO FUNCTION 3)
IF BUT < 8 AND BUT > 4 Then (DO FUNCTION 4)
IF BUT < 5 AND BUT > 2 Then (DO FUNCTION 5) '1K & 2.2K BUTTON PUSHED @ SAME TIME
GOTO BEGIN
Bookmarks