Microcontrollers JULES CALELLA
Microcontrollers vs Microprocessors Microprocessors – do not contain RAM, ROM, I/O Microcontrollers – The whole package (mini computer)
Where are Microcontrollers Used? Mouse, toys, phones, game controllers, etc. Anything that requires complex instructions but doesn’t need a full OS
How Do You Control a Microcontroller? Programming in C or Assembly C – slower, good starting point for beginners, easy to debug Timing doesn’t work as expected
Microchip’s Models 8-bit Start here 16-bit (includes dsPIC) 32-bit
Using the Data Sheet Contains everything about the device Summary of modules Pinout Dimensions Registers
Errata Sheet Revisions and mistakes
Configuration Bits Oscillator Watchdog timer Brown-out reset Every model offers different options
Oscillator Controls the timing Internal or external 4MHz, 8MHz (PLL)
OSCCON = 0xD2 ( ) OSCCONbits.IRCF = 0x5
GPIO TRIS – output (0) or input (1) LAT – Turn ON (1) or OFF (0) PORT – Read ON (1) or OFF (0)
Example: TRISA = 0x00; //All are outputs LATAbits.LATA5 = 1; //Turn LED on char pinState = PORTAbits.RA5; //Determine if LED is on
ADC Analog-to-digital converter 10 – 14 bit resolution Reference voltage (increase sensitivity) Only some ports have functionality (ANSx) Temperature sensors, light sensors, potentiometers, pressure sensors
ANSx = 1 TRISx = 1//Disables digital output driver Left justifiedx x x x x x x x x x _ _ _ _ _ _ Right justified_ _ _ _ _ _ x x x x x x x x x x Time Acquisition
Interrupts Practically every module has one Interrupt Flags Priority
Timers Hardware based counters with interrupts 8, 16, 32 bit timers Divisible Used as timer or counter
void interrupt Timer0_ISR(void) { if (T0IE && T0IF) { T0IF = 0; //Perform some task (e.g. blink LED) //LATA = !LATA; TMR0 = 4; //Depends on how long task takes }
Communication UART – slow, 2-wire, asynchronous I2C – faster, 2-wire, synchronous SPI – fastest, 3-4 wires, synchronous Controllable Baud Rate USB – able to communicate with USB hosts
Wires for Communication UART – RX & TX I2C – Data & Clock SPI – SS, SCLK, SDO, SDI
USB 4 wires (PWR, D+, D-, GND) Host mode and Device mode Internal USB PLL offers higher clock rate Look up and download “Microchip MLA”
Peripheral Pin Select Modules multiplexed amongst pins Allows the user to move the modules to desired pin Requires an unlock sequence
#include //PRAGMAS //Defines and Global Initializations //Timer Function int main(){ OSCCON = 0x00;//Set up or functions for setup TRISA = 0xF0;//Half input, half output //Don’t forget de-bounce when using buttons while(1) { //Program here } return 0; //You should not reach this point }
Debugging For WHEN your program doesn’t run as expected Break points Stopwatch Watch (variables) In-circuit debugging
How To Get Started Programmer/debugger (Pickit 3) Starter board (8-bit recommended) Additional microcontrollers IDE (MPLAB X) Compiler (XC or mikroC) Breadboard, LEDs, etc. A book with code examples (optional, but not really…)
Final Tips Learn how to display data (LEDs/LCD/computer) Use binary operations Make sure IC’s are shipping from the U.S. Ensure Oscillator works correctly Make small programs to test functions Review Microchip’s tutorial documents Join forums to ask people for help
Thank You Questions?