Department of Electronic & Electrical Engineering Introduction to microcontrollers A microcontroller is a small computer on a single integrated circuit containing: ● Processor core (CPU) ● Memory ● Programmable input/output peripherals. ● Program memory ● More . . . Microcontrollers are designed for embedded applications, in contrast to the microprocessors used in personal computers or other general purpose applications. Department of Electronic & Electrical Engineering Applications ● ● ● ● ● ● ● ● Car industry Telephones Appliances Cameras Displays Gadgets Remote controls Washing machine Department of Electronic & Electrical Engineering Remote schematic Department of Electronic & Electrical Engineering Typical Input and output ● ● ● ● ● ● ● Switches LEDs LCD Sensors(light,sound, humidity) ADC (often in the chip) DAC Control Power Electronics Department of Electronic & Electrical Engineering PIC microcontroller Made by Microchip Technology Peripheral Interface Controller We will be using the PIC16F84A. ● ● ● Simple ( 35 instructions RISC ) Cheap ( ~ £3 ) Robust (I haven't broken one yet!!!) Department of Electronic & Electrical Engineering Background reading • The Quintessential PIC (quite advanced) • Many simpler online articles on PIC programing. Department of Electronic & Electrical Engineering VOltmeter demo Department of Electronic & Electrical Engineering PIC 16F84A pin layout Department of Electronic & Electrical Engineering Summary of pins OSC1/CLKIN : Oscillator crystal input. OSC2/CLKOUT : Oscillator crystal output. MCLR(inv) : Master clear(reset)input. Programming voltage input. RA0 - RA3 RA4/T0CKI RB0/INT RB1 - RB7 : Bi-directional I/O port. : Bi-directional I/O port. Clock input to the TMR0 timer. : Bi-directional I/O port. External interrupt pin. : Bi-directional I/O port. VSS VDD : Ground : Positive supply(+2.0V to +5.5V) Department of Electronic & Electrical Engineering PIC development. Department of Electronic & Electrical Engineering Department of Electronic & Electrical Engineering Department of Electronic & Electrical Engineering Department of Electronic & Electrical Engineering LAB INTRO DEMO Department of Electronic & Electrical Engineering END OF LECT 1 Department of Electronic & Electrical Engineering Understanding the architecture • To program a PIC you need to know about the device. • Not like high level programming (Matlab JAVA C?). • The internal structure of the device is called the architecture. • The following slide shows the internal structure of the PIC16F84A • Registers • ALU • Buses • Memory (Program / User) Department of Electronic & Electrical Engineering FOCUS program data IO Department of Electronic & Electrical Engineering Registers Registers store binary numbers. In the PIC16F84A registers are 8 bit. Programming a PIC is mostly about moving information from one register to another and simple operation on the data. The WORKING register is a special register that is used a lot! The ARITHMETIC LOGIC UNIT (ALU) is used to operate on data. The STATUS register --- bits tell us about result of an operation. Department of Electronic & Electrical Engineering ALU and working register IN STATUS REGISTER ALU WORKING REGISTER OUT Department of Electronic & Electrical Engineering File Registers. The PIC has 2 banks of 8 bit registers. ● Some of these are special purpose ● ● ● ● IO Status interrupts 68 general purpose storage ● Store variables ● counters Department of Electronic & Electrical Engineering Registers and addresses • • Each register has a unique address. Instructions use this address to read/write a particular register 7 bits from opcode Selects the bank Department of Electronic & Electrical Engineering Important registers STATUS : W: PC: result of an operation (e.g. 0) also used to select register bank special register used to store result of operation (not got an address) program counter. Address in program memory of next instruction. Usually incremented by 1 after instruction has been executed. Department of Electronic & Electrical Engineering PIC16F84A program memory Program memory is separate from user data (Harvard architecture) ● ● ● ● 1024 words of program memory (instructions) Each instruction has 14 bits You can not read or write to program memory Execution starts at address 0 Department of Electronic & Electrical Engineering Opcode and Operands Instructions have ● opcode ➔ ● what it does (e.g. move something) operand(s) ➔ What it does it too (e.g. what we move) Department of Electronic & Electrical Engineering Machine Instructions (PIC16F84A) ● ● ● Each instruction is 14 bits For example 000010 0 0100000 000010 ● ● --- subtract the contents of a register from W 0 --- were we put result (W or file register) 010000 --- which register Definition: opcode (instruction) 000010 operand (data or address of data) 0 010000 Department of Electronic & Electrical Engineering Assembly code We would go mad trying to program in machine code so we use ASSEMBLY CODE For example 000010 0 0100000 is written as subwf 20h, w Subtract W from register number 20h and put the result in W. A program called an assembler converters assembly code into machine code (MPLABX uses mpasm). Department of Electronic & Electrical Engineering Department of Electronic & Electrical Engineering Department of Electronic & Electrical Engineering Instruction Descriptions (page 37 ...) Department of Electronic & Electrical Engineering mpasm reference (Assembly language). If you want to know more about the assembler please look at the E_Book mpasm_reference.pdf You can find this in the Resources folder. http://people.bath.ac.uk/eespjl/courses/Embedded/Resources/E-books/ Note that it might be easier to learn from the examples (depends on how your brain works). There are many features of mpasm that are not in the examples. Department of Electronic & Electrical Engineering Department of Electronic & Electrical Engineering Simple program (REDO) We want to do: Assembly code: movf addlw movwf 15,w D'101' NUM_2 NUM_2 = NUM_1 + 101; ; Copy the variable NUM_1 to W ; Add the literal constant 101 decimal to it ; Copy NUM_1+101 into NUM_2 Address Column one is blank (unless it's a label) 0 1 2 Machine code: 001000 0 0001111 111110 0 1100101 000000 1 0010000 Department of Electronic & Electrical Engineering Simulator doing prev slide Department of Electronic & Electrical Engineering TALK ABOUT LAB1 PROGRAM. Department of Electronic & Electrical Engineering Template program NUM EQU 0 ORG MOV ..