← 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.

Course
Electronic Systems lab, Comillas ICAI
Period
Sep – Dec 2024
Team
Pair, with a classmate
Stack
dsPIC33FJ32MC202 · C · MPLAB X · XC16 · Tektronix TBS1072B-EDU
Result
Servo PWM checked on the scope · light sensor calibrated at 15 points · timer driver carried into the turret
Fig. 01Servo command at 180°: a 2.519 ms pulse in a 20.04 ms frame (49.90 Hz), from the Timer 2 software PWM.
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.

Fig. 02Two outputs from one Timer 2 interrupt, set to a 200 ms period with 150 ms high (cyan) and a 500 ms period with 250 ms high (yellow).

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Ω.
Fig. 03Conditioning stage for the light-dependent resistor R(L). An op-amp stage with an R1/R2 reference produces the voltage the ADC reads on AN1, aiming for 0.5 V at 0 lx and 3 V at 1000 lx. Built with R1 = 10 kΩ, R2 = 1.5 kΩ and R3 = 10 kΩ.
Fig. 04Lamp driver: the PWM output on RB14 switches a 12 V lamp through transistor Q1. The base resistor RB was sized at 1.44 kΩ for a 417 mA collector current and a current gain of 750.