Mission 02 / 07
Data Hunter
The ship can't feel anything. Teach it to measure.
On this page
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
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.
Vin = 2 V, Vref = 5 V → round(2 / 5 × 255) = 102 (binary 0110 0110)
| Bit | Trial | ≤ 102? | Result |
|---|---|---|---|
| 7 | 128 | No, reject | 0 |
| 6 | 64 | Yes, keep | 1 |
| 5 | 96 | Yes, keep | 1 |
| 4 | 112 | No, reject | 0 |
| 3 | 104 | No, reject | 0 |
| 2 | 100 | Yes, keep | 1 |
| 1 | 102 | Yes, keep | 1 |
| 0 | 103 | No, reject | 0 |
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
Hint 2
010, AN4 = 100). ADCS is bits 7:6, and Fosc/16 is 01.Show the solution
- 1Step 1 — OSCCON (0x20)Set it to
0x05(0000 0101). Switch the internal oscillator on at 4 MHz (any speed wins). - 2Step 2 — ADCON0 (0x25)Set it to
0x49(0100 1001). Enable ADC module 0, select AN2, conversion clock Fosc/16. - 3Step 3 — ADCON1 (0x26)Set it to
0x51(0101 0001). Enable ADC module 1, select AN4, conversion clock Fosc/16. - 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 ; ADCON1Bit-by-bit breakdown
OSCCON0x20= 0x05
Switch the internal oscillator on at 4 MHz (any speed wins). 0x05 = 0000 0101 = SCS0 = 1, IRCF = 10
| Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
| Field | — | IRCF | SCS0 | |||||
- 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
| Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
| Field | ADCS | — | CHS | GO/DONE | ADON | |||
- 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
| Bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 |
| Field | ADCS | — | CHS | GO/DONE | ADON | |||
- 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 differHex 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 differHex 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 differHex 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
- 1Your channel connectsThe multiplexer lights the pipe for AN2 (and AN4 in the second room).Screenshot coming soon
- 2Bits are weighed one by oneEach trial pipe flashes blue, then turns green (kept) or red (rejected).Screenshot coming soon
- 3The reading latchesAfter 8 bits the result register holds the value, and the reading repeats.TARGET REACHEDScreenshot 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/16On a real chip the same switches can sit at different bit positions; its datasheet has the map. The idea is identical.