← Projects/P-14
Embedded C on the dsPIC33
Lab work for the Electronic Systems course at ICAI, done in a pair with a classmate: short projects in C on a dsPIC33FJ32MC202 board, each adding one peripheral (timers, a software PWM, interrupts, the ADC). The PWM was checked on an oscilloscope, the last lab used a calibrated light sensor to dim a lamp, and the work led into the team's light-tracking turret.
- 0.50–2.52 ms
- servo pulse measured at 0° and 180°
- 49.9 Hz
- PWM frame, 20.04 ms on the scope
- 9°
- servo step per 0.1 ms tick
- 0.5–3 V
- light-sensor stage output, designed for 0 to 1,000 lx
01One peripheral at a time
The board carries a dsPIC33FJ32MC202, four active-low LEDs on RB12–RB15, a pushbutton on RB2 and a potentiometer on AN5 (RB3). Each exercise was its own MPLAB X / XC16 project in C on the course's configuration template, which runs the chip at a 39.6 MHz instruction clock from the internal oscillator and PLL.
- Timers. A Timer 1 setup that takes its period in tenths of a millisecond and picks the smallest prescaler (1, 8, 64 or 256) that keeps the period register within 16 bits, and a blocking delay built the same way that polls the timer's flag (Timer 1 in the lab 3 version, Timer 3 in the reusable function files).
- Software PWM. Timer 2 interrupts every 0.1 ms and a tick counter switches the output, so period and duty are both set in 0.1 ms steps. An earlier version kept a separate period and duty for each PORTB pin, so the one interrupt drove several outputs at once. Before that, a brightness ramp on a 100 Hz frame stepped an LED from 20 % to 100 % duty once per second.
- Servo command. With a 20 ms frame, each pushbutton press moves the commanded angle by 9°, one 0.1 ms tick, between 0.5 ms (0°) and 2.5 ms (180°). On the scope the pulse measured 0.50 ms at 0°, 0.90 ms at 36°, 2.20 ms at 153° and 2.52 ms at 180°, in a 20.04 ms frame.
- ADC. The potentiometer is read through the ADC interrupt, and a first three-point look-up table with linear interpolation converts counts to lux (155 → 0, 711 → 600, 1023 → 1200). Lab 5 replaced both with a real light sensor and a measured table (below).
The labs fed into the three-person light-tracking turret, built on the same microcontroller. I wrote its timer driver, which keeps the automatic prescaler selection from these labs. Its ADC and hardware-PWM drivers came from the course.
02Lab 5: a light sensor that dims a lamp
The last lab, in November 2024, replaced the potentiometer with a light-dependent resistor (LDR) and added a 12 V lamp. We designed an op-amp stage that turns the LDR's resistance into a voltage for the ADC: 0.5 V at 0 lx and 3 V at 1,000 lx. With R1 = 10 kΩ and R2 = 1.5 kΩ, the calculation gave R3 = 4.2 kΩ, but 10 kΩ measured closer to the target line, so we kept 10 kΩ.
We calibrated the stage at 15 points, from 4,440 Ω at 0.51 V to 300 Ω at 2.86 V, and compared the measured curve with the manufacturer's. A look-up table with linear interpolation then converts ADC counts to lux, from 158 counts (40 lx) to 887 counts (1,200 lx).
- Sampling: a Timer 1 interrupt every 100 ms starts a conversion on AN1, and the ADC interrupt stores the result.
- Lamp control: PWM on RB14 drives the lamp through a transistor, with the duty set from 1000 − lux. The lamp gets brighter as the room gets darker and switches off at 1,000 lx and above.
- Base resistor: the lamp draws up to 417 mA (about 5 W at 12 V). With β = 750 the base needs at least 0.556 mA, and from a 3.3 V pin with a 2.5 V base-emitter drop that gives Rb = 1.44 kΩ.