C Programming
PIC Microcontroller
Hardware
- PIC16F877A development board
- PICkit 3 programmer
PICkit 3 support ends with MPLAB X IDE v6.20; check the Microchip download page before installing a newer version.
Quick calculators to help you practice electronics concepts.
Choose two values you know. The calculator finds voltage, current, resistance, and power.
V is voltage in volts, I is current in amperes, R is resistance in ohms, and P is power in watts.
12 V = 2 A × 6 Ω
Select the colors printed on a resistor to find its resistance and tolerance. Six-band resistors also show a temperature coefficient.
Read the bands from left to right, with the tolerance band near the right end.
Find a current-limiting resistor for one LED or several LEDs connected in series.
Use the LED forward voltage and current from its datasheet. The suggested wattage includes a 2× margin over the calculated resistor power.
Calculate the output voltage between two series resistors.
This is an unloaded divider. Connecting a load to the output can lower the actual voltage.
Estimate the energy used by a device over a number of days and its electricity cost.
Enter the device's power in watts, its daily running time, and your electricity rate per kWh. Results are estimates.
Decode the marking printed on a surface-mount resistor.
Three- and four-digit codes use the final digit as a power-of-ten multiplier. The letter R marks a decimal point. EIA-96 uses a two-digit value code and a multiplier letter.
The marking identifies nominal resistance; check the component datasheet for tolerance and power rating.
10 × 10³ Ω = 10 kΩ
Enter ARR and PSC register values, then fill in either timer frequency or delay. Leave the value you want to calculate blank.
STM32 calls the prescaler register PSC. Enter the raw register values; the calculator adds 1 to ARR and PSC.
Use the timer input clock, which may differ from the CPU or APB bus clock. This calculates the update period for an up-counting timer with no repetition counter.
(9999 + 1) × (7199 + 1) / 72 MHz = 1 s
For the STM32F103C8 IC only. Enter the USART peripheral clock and target baud rate to calculate its USART BRR register value.
For STM32F103 with 16× oversampling. Use the USART peripheral clock: USART1 uses APB2; USART2 and USART3 use APB1.
64,000,000 / (16 × 9,600) = 416.6667; fraction = round(16 × 0.6667) = 11; BRR = (416 << 4) | 11 = 0x1A0B.
USART1->BRR = 0x1A0B;
Calculate I²C_CCR, CR2.FREQ, and TRISE from the APB1 clock, target SCL frequency, and timing mode.
This is for the STM32F103C8 legacy I²C peripheral. SCL timings are ideal estimates; verify the bus rise time and the register settings against your MCU reference manual.
Standard mode: CCR = 32,000,000 / (2 × 100,000) = 160; I2C_CCR = 0x00A0.
CR2.FREQ = PCLK1 in MHz = 32 (0x20).
Standard mode: TRISE = (1000 ns / 31.25 ns) + 1 = 33.
I2C1->CR2 = (I2C1->CR2 & ~0x3FU) | 32U;
I2C1->CCR = 0x00A0;
I2C1->TRISE = 33U;
One complete SCL clock has a high time and a low time.
For a 32 MHz APB1 peripheral clock:
In standard mode, HIGH and LOW each last one CCR count:
I2C1->CCR = 160; // 0x00A0, standard mode
| Mode | HIGH time | LOW time | CCR formula |
|---|---|---|---|
| Standard | CCR × TPCLK1 | CCR × TPCLK1 | fPCLK1 / (2 × fSCL) |
| Fast, DUTY = 0 | CCR × TPCLK1 | 2 × CCR × TPCLK1 | fPCLK1 / (3 × fSCL) |
| Fast, DUTY = 1 | 9 × CCR × TPCLK1 | 16 × CCR × TPCLK1 | fPCLK1 / (25 × fSCL) |
Easy way to remember: standard mode has 1 high + 1 low = 2; fast mode with DUTY = 0 has 1 high + 2 low = 3; fast mode with DUTY = 1 has 9 high + 16 low = 25.
CR2.FREQ is the PCLK1 frequency expressed as a whole number of MHz. For 32 MHz, set FREQ to 32 (0x20).
Both fast-mode duty settings use the same 300 ns rise-time limit. Newer STM32 I²C peripherals may use I2C_TIMINGR instead of CR2, CCR, and TRISE.
Convert an ADC reading into a voltage using the ADC resolution and reference voltage.
Uses the full-scale endpoint convention. Confirm the transfer function in your MCU datasheet when precision matters.
Convert an unsigned 32-bit value and inspect its bits in groups of four.
Prefixes such as 0b and 0x are accepted for binary and hexadecimal input.
Apply 32-bit AND, OR, XOR, NOT, left shift, or logical right shift.
Inputs and results are unsigned 32-bit integers. NOT applies to A; shifts use B as the shift count (0–31).
Convert voltage, current, resistance, capacitance, frequency, length, temperature, power, and pressure.
Convert between a repeating signal's frequency and its period.
Find the software and download links required for your classes.
PICkit 3 support ends with MPLAB X IDE v6.20; check the Microchip download page before installing a newer version.
These sizes and ranges are typical for systems with 8-bit bytes and 32-bit int. C allows implementations to use different sizes. Check your compiler with sizeof, <limits.h>, and <float.h>.
| Type | Typical bytes | Typical range / precision |
|---|---|---|
signed char | 1 | −128 to 127 |
unsigned char | 1 | 0 to 255 |
short | 2 | −32,768 to 32,767 |
int | 4 | −2,147,483,648 to 2,147,483,647 |
unsigned int | 4 | 0 to 4,294,967,295 |
long | 4 or 8 | Depends on compiler and platform |
long long | 8 | About −9.22 × 1018 to 9.22 × 1018 |
float | 4 | About ±3.4 × 1038; 6–7 significant digits |
double | 8 | About ±1.7 × 10308; 15–16 significant digits |
Plain char may be signed or unsigned. Use <stdint.h> types such as uint8_t and int32_t when an exact integer width is needed and supported.
#include <stdio.h>
#include <limits.h>
printf("int: %zu bytes, range %d to %d\n",
sizeof(int), INT_MIN, INT_MAX);
The format must match the argument type. printf prints values; scanf usually needs the variable's address.
| Type / purpose | printf | scanf |
|---|---|---|
int | %d | %d |
unsigned int | %u | %u |
long | %ld | %ld |
long long | %lld | %lld |
float | %f (promoted to double) | %f |
double | %f | %lf |
char | %c | %c |
String (char[]) | %s | %s with a width limit |
size_t | %zu | %zu |
| Hexadecimal integer | %x / %X | %x |
int count = 10;
double voltage = 3.3;
printf("Count: %d, voltage: %.2f V\n", count, voltage);
scanf("%d", &count);
For a character array, limit scanf input width. For example, char name[20]; scanf("%19s", name); leaves room for the terminating null character.
| Category | Operators | Example |
|---|---|---|
| Arithmetic | + - * / % | 7 / 2 gives 3 for integers |
| Comparison | == != < > <= >= | a > b |
| Logical | && || ! | ready && enabled |
| Assignment | = += -= *= /= %= | count += 1 |
| Bitwise | & | ^ ~ << >> | flags & 0x04 |
| Increment / decrement | ++ -- | index++ |
| Conditional | ?: | max = a > b ? a : b |
&& and || are logical operators; & and | operate on bits. Use parentheses when combining operations so the intended order is clear.
Download and run the official MSYS2 installer. The default installation folder is C:\msys64.
From the Windows Start menu, launch MSYS2 UCRT64. Use this environment for the commands below. UCRT64 combines the /ucrt64 toolchain with MSYS tools in /usr/bin.
pacman -Suy
If MSYS2 asks you to close the terminal, reopen MSYS2 UCRT64 and run the command again until the update completes.
make --version
If Make is not found, install it and verify again:
pacman -S make
make --version
The MSYS Make package provides /usr/bin/make.exe, which works with Makefiles that invoke /bin/sh.
The STM32 Makefile needs arm-none-eabi-gcc, not only a desktop GCC compiler. Install the UCRT64 package and confirm the prompt with Y:
pacman -S mingw-w64-ucrt-x86_64-arm-none-eabi-gcc
Verify the compiler inside MSYS2 UCRT64:
arm-none-eabi-gcc --version
which arm-none-eabi-gcc
The path should start with /ucrt64/bin/. With the default installation folder, the Windows executable is C:\msys64\ucrt64\bin\arm-none-eabi-gcc.exe.
If you build from a Windows PowerShell terminal instead of MSYS2 UCRT64, add these folders to your Windows Path, in this order:
C:\msys64\ucrt64\bin
C:\msys64\usr\bin
In Windows, search for Environment Variables, open Edit the system environment variables, choose Environment Variables…, then edit Path. Add each folder, save, and restart all VS Code windows.
where.exe make
where.exe arm-none-eabi-gcc
make --version
arm-none-eabi-gcc --version
The first paths reported should be C:\msys64\usr\bin\make.exe and C:\msys64\ucrt64\bin\arm-none-eabi-gcc.exe. If Cygwin or another ARM toolchain appears first, adjust your Path order or build in MSYS2 UCRT64.
Package references: GNU Make and ARM GCC for UCRT64.