Skip to content
EmbeddedPlayX

Mission 02 / 07

Data Hunter

The ship can't feel anything. Teach it to measure.

On this page
  1. The situation
  2. Why this matters
  3. The idea in one picture
  4. Your mission
  5. How you know it worked
  6. Debrief

01The situation

Temperature, pressure, fuel: the sensors still put out voltages, but the ship can't read them. A ship that can't measure can't survive.

02Why this matters

The real world is analog. Every sensor reading in every device passes through an ADC before software can use it.

  • TemperatureA thermostat reads its sensor's voltage through an ADC.
  • Battery levelYour phone's battery percentage starts as an ADC reading.
  • JoysticksEvery stick position is a voltage turned into a number.

03The idea in one picture

Try adding 128…Vin = 2.0 V (102)0255?128?64?32?16?8?4?2?1running total: 0
Successive approximation: try each weight from 128 down to 1, keep it if it fits.

Think of it as: weighing something on a balance with weights of 128, 64, 32… 1 grams. Try the heaviest first; if the scale tips too far, take it off. The weights left on the pan are the answer.

Go deeper

This ADC is 8 bits: 0 V reads as 0 and the 5 V reference reads as 255. Each ADCON register has three switches: ADON turns the converter on, CHS picks the input (AN0–AN7) and ADCS picks how fast it converts.

code = round( Vin / Vref × 255 )
Vin = 2 V, Vref = 5 V → round(2 / 5 × 255) = 102 (binary 0110 0110)
BitTrial≤ 102?Result
7128No, reject0
664Yes, keep1
596Yes, keep1
4112No, reject0
3104No, reject0
2100Yes, keep1
1102Yes, keep1
0103No, reject0

04Your mission

  • OSCCONStart the clock. Any speed works.
  • ADCON0Turn converter 0 on, connect it to AN2, use the Fosc/16 clock.
  • ADCON1Turn converter 1 on, connect it to AN4, same clock.
Hint 1
Each ADCON register has three switches: on/off (ADON), which input (CHS) and how fast (ADCS).
Hint 2
ADON is bit 0. CHS is bits 4:2, the channel number in binary (AN2 = 010, AN4 = 100). ADCS is bits 7:6, and Fosc/16 is 01.
Show the solution
  1. 1Step 1 — OSCCON (0x20)Set it to 0x05 (0000 0101). Switch the internal oscillator on at 4 MHz (any speed wins).
  2. 2Step 2 — ADCON0 (0x25)Set it to 0x49 (0100 1001). Enable ADC module 0, select AN2, conversion clock Fosc/16.
  3. 3Step 3 — ADCON1 (0x26)Set it to 0x51 (0101 0001). Enable ADC module 1, select AN4, conversion clock Fosc/16.
  4. 4Program & ExecutePress P to compile your configuration into machine code in ROM, then press E to run it. When every register matches you get Target Reached.

The same thing as a program (MOVLW loads a value into W, MOVWF copies W into a register):

MOVLW 0x05
MOVWF 0x20   ; OSCCONMOVLW 0x49
MOVWF 0x25   ; ADCON0MOVLW 0x51
MOVWF 0x26   ; ADCON1
Bit-by-bit breakdown

OSCCON0x20= 0x05

Switch the internal oscillator on at 4 MHz (any speed wins). 0x05 = 0000 0101 = SCS0 = 1, IRCF = 10

Bit76543210
Value00000101
Field—IRCFSCS0
  • IRCF (bits 2:1) = 10 → 4 MHz
  • SCS0 (bit 0) = 1 → Internal oscillator on
  • Every remaining bit is 0: those bits are not needed for this level, but they must stay clear for the value to match.

ADCON00x25= 0x49

Enable ADC module 0, select AN2, conversion clock Fosc/16. 0x49 = 0100 1001 = ADON = 1, CHS = 010, ADCS = 01

Bit76543210
Value01001001
FieldADCS—CHSGO/DONEADON
  • ADCS (bits 7:6) = 01 → Conversion clock = Fosc/16
  • CHS (bits 4:2) = 010 → Channel AN2
  • ADON (bit 0) = 1 → Converter on
  • Every remaining bit is 0: those bits are not needed for this level, but they must stay clear for the value to match.

ADCON10x26= 0x51

Enable ADC module 1, select AN4, conversion clock Fosc/16. 0x51 = 0101 0001 = ADON = 1, CHS = 100, ADCS = 01

Bit76543210
Value01010001
FieldADCS—CHSGO/DONEADON
  • ADCS (bits 7:6) = 01 → Conversion clock = Fosc/16
  • CHS (bits 4:2) = 100 → Channel AN4
  • ADON (bit 0) = 1 → Converter on
  • Every remaining bit is 0: those bits are not needed for this level, but they must stay clear for the value to match.

Try it: build the values

Flip the bits until each register matches the mission. It's practice only; nothing is saved.

0 of 3 registers match.

OSCCON 0x20

✗ Not yet – 2 bits differ
7–
6–
5–
4–
3–
2IRCF1
1IRCF0
0SCS0

Hex 0x00 Binary 0000 0000 Target 0x05 (0000 0101)

  • IRCF (bits 2:1) = 00 → 1 MHz
  • SCS0 (bit 0) = 0 → Internal oscillator not enabled

ADCON0 0x25

✗ Not yet – 3 bits differ
7ADCS1
6ADCS0
5–
4CHS2
3CHS1
2CHS0
1GO/DONE
0ADON

Hex 0x00 Binary 0000 0000 Target 0x49 (0100 1001)

  • ADCS (bits 7:6) = 00 → Conversion clock = Fosc/4
  • CHS (bits 4:2) = 000 → Channel AN0
  • GO/DONE (bit 1) = 0 → Conversion GO/DONE bit: not part of the win value (leave 0)
  • ADON (bit 0) = 0 → Converter off

ADCON1 0x26

✗ Not yet – 3 bits differ
7ADCS1
6ADCS0
5–
4CHS2
3CHS1
2CHS0
1GO/DONE
0ADON

Hex 0x00 Binary 0000 0000 Target 0x51 (0101 0001)

  • ADCS (bits 7:6) = 00 → Conversion clock = Fosc/4
  • CHS (bits 4:2) = 000 → Channel AN0
  • GO/DONE (bit 1) = 0 → Conversion GO/DONE bit: not part of the win value (leave 0)
  • ADON (bit 0) = 0 → Converter off

05How you know it worked

  1. 1Your channel connectsThe multiplexer lights the pipe for AN2 (and AN4 in the second room).
    Screenshot coming soon
  2. 2Bits are weighed one by oneEach trial pipe flashes blue, then turns green (kept) or red (rejected).
    Screenshot coming soon
  3. 3The reading latchesAfter 8 bits the result register holds the value, and the reading repeats.TARGET REACHED
    Screenshot coming soon

Simulation note

Your program never reads the result; the game latches and shows it for you. The 2 V example is an illustration.

Stuck?

  • A room never convertsThe converter is off. Set ADON (bit 0).
  • The wrong input pipe lights upCHS picks the channel: AN2 = 010, AN4 = 100.
  • Channels look swappedADCON0 reads AN2 and ADCON1 reads AN4, not the other way round.
  • It converts, but no Target ReachedCheck the clock: ADCS must be 01 (Fosc/16).

06Debrief

You just gave the ship its senses.

Reading sensors is the most common job in embedded systems: set up the ADC once, then it hands you numbers forever.

// the same idea in CADCON0 = 0x49;  // on, AN2, Fosc/16ADCON1 = 0x51;  // on, AN4, Fosc/16

On a real chip the same switches can sit at different bit positions; its datasheet has the map. The idea is identical.

Next mission · 03Clock Warrior (Timers)