Microcontroller By: Muhammad Usman Rafique B.Sc. Electronic Engineering U.E.T. Lahore. 2 Thursday, October 13, 2005 Prerequisites Programming in C language. Number systems. Digital Logic. Note: No microcontrollers architecture is required to know for this course. 3 Thursday, October 13, 2005 A standalone system with program memory, data memory, timers, counters and I/O. Programmed according to the task. Machine language is the generic language of all the microcontrollers. They can be programmed in assembly language and some high level languages . Cis the mostly used high level language for microcontrollers. 4 Thursday, October 13, 2005 How does a microcontroller look like? 5 Thursday, October 13, 2005 Architecture of 8051 Our target MCU is Intels 8051. Two 16 bit timers. 4 parallel I/O Ports. 5 interrupts. Memory: 4 kilobytes Program memory 128 bytes RAM 8 GPRs Accumulator One associated accumulator 6 Thursday, October 13, 2005 HLL and LLL HLL is easy to learn More flexible More users friendly LLL is difficult to learn Complex algorithms Mnemonics are not English like 7 Thursday, October 13, 2005 CPU Microcontroller Microprocessor PLDs 8 Thursday, October 13, 2005 Why C language? Close to assembly language Powerful enough Industry standard Portable Rich literature Users friendly 9 Thursday, October 13, 2005 Specifications of 8051 C. Special port variables. Special functions. Special I/O access. Special compilers. 10 Thursday, October 13, 2005 Tools for the course: Keil C51 compiler. (Industry standard) IBMclone with: Windows98, ME, 2000, Xp 133MHz Pentium or above 128MB RAM or higher 10GB Hard Drive or higher Color monitor 11 Thursday, October 13, 2005 #i ncl ude <r eg51. h> voi d mai n( ) { i nt a; a=0x55; P1=a; whi l e( 1) { } / / end whi l e } / / end mai n 12 Thursday, October 13, 2005 Programs surgery #i ncl ude <r eg51. h> is the header file for 8051 library. mai n( ) is the function where execution starts. a is the i nt type variable. 0x55 is the format for hex number. a is 55H. P1 is the reserved word for port 1 of 8051. Port 1 is 01010101. whi l e( 1) { } is infinite loop. 13 Thursday, October 13, 2005 Bit accessing: Ppor t number ^bi t number i . e. P1^0 accesses bit0 at port 1. P2^3 accesses bit3 at port 2. P3^7 accesses bit7 at port 3. 14 Thursday, October 13, 2005 Demo. of bit access. #i ncl ude <r eg51. h> sbi t LED=P1^0 / / decl ar i ng bi t name i s necessar y voi d mai n( ) { LED=0; / / set LED LOW / / st ar t i nf i ni t e l oop f or ( ; ; ) { LED=~LED; / / i nver t i ng LED P1^0=LED; } } 15 Thursday, October 13, 2005 Reading bits. #i ncl ude <r eg51. h> sbi t i n=P1^2; / / assi gn any name i nst ead of i n sbi t out =P1^3; / / assi gn any name i nst ead of out voi d mai n( ) { out =0; whi l e( 1) { i f ( i n==1) out =1; } } 16 Thursday, October 13, 2005 Sample Programs: Program for input and output through a bit. #i ncl ude <r eg51. h> sbi t i nput =P0^1; sbi t out put =P0^2; voi d mai n( ) { whi l e( 1) { i f ( i nput ==0) out put =1; out put =0; } } 17 Thursday, October 13, 2005 Port Operations: Pnumber is the reserved word for accessing port. i.e. P1 = 0X45; P0 = 253; P2 = 01000101 b i nt a; a=148; P1=a; / / P1 wi l l be 148. 18 Thursday, October 13, 2005 Sample programs: / / Thi s pr ogr amwr i t es some dat a t o Por t 1. #i ncl ude <r eg51. h> / / no need of def i ni ng a por t name. voi d mai n( ) { i nt dat a; dat a = 0XAA; f or ( ; ; ) { P1 = dat a; / / P1=AAH or 10101010 } } 19 Thursday, October 13, 2005 Contd: / *Thi s pr ogr amr eads dat a f r omPor t 1. and st or es i t i n var i abl e i nput . */ #i ncl ude <r eg51. h> voi d mai n( ) { i nt i nput ; whi l e( 1) { i nput = P1; } } 20 Thursday, October 13, 2005 Contd: / * Thi s pr og. r eads dat a f r ompor t 1 and t hen wr i t es i t t o Por t 2. */ #i ncl ude <r eg51. h> voi d mai n( ) { i nt hol d; f or ( ; ; ) { hol d = P1; / / r eadi ng f r ompor t 1. P2 = hol d; / / wr i t i ng t o por t 2. } } 21 Thursday, October 13, 2005 Exercises: Prob. 1: Set and clear P1^0 and P1^1 bits alternatively. Prob. 2: Read data from P2 and after inverting write to P2 as well. Prob. 3: Think some other activity to polish your knowledge. 22 Thursday, October 13, 2005 Operations on data: Arithmetic operations: Addition Subtraction Multiplication Division Logical operations: Shifting Comparison Boolean operations: AND OR NOT XOR 23 Thursday, October 13, 2005 Addition: Similar style as in regular C. Performs on 8-bit operands. Carry flag may change. (note: 1 for result>255, 0 for result<255) Singed and unsigned values does matter. 24 Thursday, October 13, 2005 Examples: #i ncl ude <r eg51. h> voi d mai n( ) { unsi gned i nt m, n, out ; m=223; n=32; out = m+n; P1 = out ; / / Por t 1 wi l l be 255. f or ( ; ; ) { } } 25 Thursday, October 13, 2005 Checking overflow: / *Thi s pr og. cl ear s B0 of P1 i f over f l ow occur s. */ #i ncl ude <r eg51. h> sbi t out = P1^0; voi d mai n( ) { unsi gned i nt x, y; x=246; y=10; x = x+y; i f ( x>255) out =0; whi l e( 1) { } } 26 Thursday, October 13, 2005 Pr og. f or mul t i pl i cat i on / / Onl y f unt i on mai n i s wr i t t en her e. voi d mai n( ) { unsi gned i nt x, y; x=10; y=25; x = x*y; whi l e( 1) { P1 = x; / / P1 i s 250. } } 27 Thursday, October 13, 2005 Di vi si on: / / Onl y mai n f unct i on i s wr i t t en. voi d mai n( ) { unsi gned i nt a, b; a=10; b=2; a=a/ 2; f or ( ; ; ) { P1=a; } } 28 Thursday, October 13, 2005 Shifting: Two types of shifting are: Right Shift Left Shift >> for right shift. << for left shift. 29 Thursday, October 13, 2005 Programs on shifting: #i ncl ude <r eg51. h> voi d mai n( ) { P1 = 255; / / Checks whet her P1 has become 0 or not . whi l e( P1 ! = 0) { / / One bi t l ef t shi f t . P1 = P1 << 1; } } 30 Thursday, October 13, 2005 Discussion: Number followed by <<symbol is for number of shifts in one operation. i.e. y<<2 means y is shifted two bits left. Same is the case with right shift. i.e. y>>4 means y is shifted four bits right. 31 Thursday, October 13, 2005 Contd: / *Onl y mai n f unct i on i s wr i t t en her e. */ voi d mai n( ) { P1 = P1<<1; P2 = P2>>1; } 32 Thursday, October 13, 2005 Comparison: Comparing two operands. < used for less than. > used for greater than. i.e. i nt x, y; i f ( x<y) {} 33 Thursday, October 13, 2005 Programs on comparison: #i ncl ude <r eg51. h> voi d mai n( ) { char x, y; x= A ; y= D ; whi l e( 1) { i f ( x<y) P1=0xF0; el se P1=0x0F; } } 34 Thursday, October 13, 2005 Contd: #i ncl ude <r eg51. h> voi d mai n( ) { unsi gned i nt a; si gned i nt b; a=0xA5; b=0xA5; f or ( ; ; ) { i f ( a>b) P2= A ; el se P2= a ; } } 35 Thursday, October 13, 2005 Logical operations: Performed as byte operations. AND OR XOR NOT are the only reserved operators. & for ANDing | for ORing ~ for NOTor inverting 36 Thursday, October 13, 2005 Programs on logical operations #i ncl ude <r eg51. h> voi d mai n( ) { i nt x, y, out 1, out 2; x=0x55; y=0x2A; out 1 = x&y; out 2 = x| y; whi l e( 1) { P0 = out 1; P1 = out 2; } } 37 Thursday, October 13, 2005 / *Thi s pr ogr ami nver t s t he P0 dat a and out put s on P2 af t er checki ng t he l ow st at us on P1^0 */ #i ncl ude <r eg51. h> sbi t out = P1^0 voi d mai n( ) { i nt x; whi l e( 1) { x=P0; x=~x; i f ( out ==0) P2=x; } } 38 Thursday, October 13, 2005 More Programs on Logical Operations: / *Thi s pr ogr amcl ear s P1^0 when bot h P1^1 AND P1^2 ar e l ow. */ #i ncl ude <r eg51. h> sbi t i n1 = P1^1; Sbi t i n2 = P1^2; Sbi t out = P1^0; voi d mai n( ) { whi l e( 1) { i f ( i n1 == 0 && i n2== 0) out = 0; } } 39 Thursday, October 13, 2005 Contd: / *Thi s pr og. Checks t he same case but now wi t h OR. */ i ncl ude <r eg51. h> sbi t i n1 = P1^1; sbi t i n2 = P1^2; Sbi t out = P1^0; voi d mai n( ) whi l e( 1) { i f ( i n1==0 | i n2==0) out =0; } } 40 Thursday, October 13, 2005 Contd: / *Thi s pr og. Checks t he same case but now wi t h XOR. */ i ncl ude <r eg51. h> sbi t i n1 = P1^1; sbi t i n2 = P1^2; Sbi t out = P1^0; voi d mai n( ) whi l e( 1) { i f ( i n1==0 ^ i n2==0) / / ^ symbol s XOR out =0; } } 41 Thursday, October 13, 2005 Functions in C Language: Functions are like nested programs. Functions may or may not return the value. voi d declared function does not return value while other does. Some functions are reserved functions for 8051 C language. Functions may take arguments or parameters. 42 Thursday, October 13, 2005 Example of function: Following is a function. out ( ) { i nt x; P1 = ~P1; P2 = 0x55; x = P1+P2; } Discussion: Inverts P1, loads P2 with 55H and sum of P1 and P2 is assigned to x. 43 Thursday, October 13, 2005 Program with function. #i ncl ude <r eg51. h> i nt x; / / Var i abl e decl ar ed her e wi l l be gl obal . voi d t hr ow( ) { x = 138; P1 = x; } / / mai n( ) i s t he gener i c f unct i on of any C pr ogr am. voi d mai n( ) { t hr ow( ) ; whi l e( 1) { } } 44 Thursday, October 13, 2005 Examples: #i ncl ude <r eg51. h> voi d myf unct i on( ) { i nt a; a=100; P2=a; } voi d mai n( ) { f or ( ; ; ) { myf unct i on( ) ; } } 45 Thursday, October 13, 2005 Contd: / / Exampl e of f unct i on cal l i nsi de a f unct i on. #i ncl ude <r eg51. h> sbi t LED = P1^0; voi d pause( ) { i nt x; f or ( x=0; x<30000; x++) ; } voi d bl i nk( ) { LED = ~LED; pause( ) ; } voi d mai n( ) { f or ( ; ; ) { bl i nk( ) ; } } 46 Thursday, October 13, 2005 Discussion: Function pause( ) creates the delay of roughly 1 second if 8051 is clocked at 12MHz crystal. bl i nk( ) function is the second function. bl i nk( ) is called in the mai n( ) . Hence functions can be nested in other functions. 47 Thursday, October 13, 2005 Timers in 8051: 8051 has two 16-bit timers. Timer0 and Timer1. Each timer can be operated in either of the four modes. Timers can be run internally (as timer) and externally (as counter). Timers are to be initialized before use. 48 Thursday, October 13, 2005 Modes of timers: Mode0 Mode1 Mode2 Mode3 Operates as 13-bit timer/counter. Operates as 16-bit timer/counter. Operates as autoreload 8-bit timer/counter. Two independent 8- bit timer/counter (Obsolete mode) 49 Thursday, October 13, 2005 Registers for timers: Each timer is composed of two cascaded registers, each of 8-bit. TH and TL are the higher and lower bytes of timer/counter registers. TH0 and TL0 are for timer0 . TH1 and TL1 are for timer1. TMODcontrols the timer and timer modes simultaneously. TCONcontrols the interrupts and whether timer mode or counter mode is used. 50 Thursday, October 13, 2005 Initializing timers: Initializing means setting hardware inside the MCU via software. Some registers are to be programmed to control the timer operation. These registers are the reserved words in C. 51 Thursday, October 13, 2005 More on timer registers: All the register are treated as variables. TCONregister is bit accessible. Highingand Lowingspecific bits further control the operation. TR0 and TR1 control the start and stop of timers. ET0 and ET1 control the interrupt of timers (Discussed later). 52 Thursday, October 13, 2005 Programs on timers: #i ncl ude <r eg51. h> voi d t i mer _st ar t ( ) { TMOD = 0x01; EA = 0; ET0 = 0; TR0 = 1; } voi d mai n( ) { t i mer _st ar t ( ) ; whi l e( 1) { } } 53 Thursday, October 13, 2005 Discussion: TMODis the variable for selecting timer mode. Loading predefined values to TMODwill select different timer and its mode simultaneously. EAis the variable for interrupt (Discussed later). ET0 is also interrupt variable. TR0 variable starts timer by assigning logic high and stops timer by assigning logic low. 54 Thursday, October 13, 2005 More on timers: TMOD = 0x00; TMOD = 0x01; TMOD = 0x02; TMOD = 0x00; TMOD = 0x10; TMOD = 0x20; Selects timer0 in mode0. Selects timer0 in mode1. Selects timer0 in mode2. Selects timer1 in mode0. Selects timer1 in mode1. Selects timer1 in mode2. 55 Thursday, October 13, 2005 More programs on timers: / *Onl y t i mer i ni t i al i zi ng f unct i on and mai n f unct i on i s wr i t t en. */ voi d st ar t ( ) { TMOD = 0x02; TH0 = 156; EA = 0; TR0 = 1; } voi d mai n( ) { st ar t ( ) ; whi l e( 1) { } } 56 Thursday, October 13, 2005 Auto-reload mode: Mode2 is auto-reload mode. In this mode timer counts from value in TL. After completing the count, i.e. counting upto 0xFF, value in the TH is loaded into the TL automatically and timer starts again from that value. 57 Thursday, October 13, 2005 Creating delays: Delays are efficiently created through timers. Clock frequency is of great importance. Assumption: We run 8051 with 12MHz crystal. Internal cycle is 1MHz as crystal frequency is divided by 12 internally. 58 Thursday, October 13, 2005 More on timers: Timer increments its count after each clock cycle. If 8051 operated on 12 MHz, clock cycle will be 1MHz and hence after 1 microsecond timer will count next value. Timers are used efficiently for producing delays in programs. Timers produce interrupts that will be discussed later. 59 Thursday, October 13, 2005 Interrupts: Interrupts are temporary suspension of main program. Interrupts are not treated in 8051 in a very excellent way. After servicing the interrupt main program is again executed from where it was left. 8051 has five interrupts. Interrupts can be disabled or enabled through software. 60 Thursday, October 13, 2005 Types of interrupts: 8051 has five interrupts and they are handled in following order. External interrupt 0 Timer interrupt 0 External interrupt 1 Timer interrupt 1 Serial port interrupt 61 Thursday, October 13, 2005 Handling the interrupts: Interrupts are handled in the same order as discussed before. If more than one interrupt occurred simultaneously then default priority is same as before. Interrupt priority can be changed by programming the I P (interrupt Priority) register. Special built-in functions are used for interrupt programming. 62 Thursday, October 13, 2005 Timer interrupts: / *As we ar e usi ng t i mer i nt er r upt s we add some ot her f eat ur es i n t i mer i ni t i al i zi ng f unct i on. */ voi d t i mer _i nt er r upt ( ) { TMOD = 0x02; / / Ti mer 0 i n mode2 TH0 = 56; EA = 1; / / Enabl e I nt er r upt Mode ET0 = 1; / / Enabl e Ti mer 0 i nt er r upt TR0 = 1; / / St ar t Ti mr e0 } 63 Thursday, October 13, 2005 Discussion: Referring to previous program, EA is the global interrupt variable and must be set before using any interrupt. ET0 is the interrupt variable for timer0. TH0 contains the re-loadable value after one roll off. Same is for timer1. ET1 is the interrupt variable for timer1. TH1 and TL1 are the high and low bytes of timer1. 64 Thursday, October 13, 2005 Interrupt functions for timer: Following are the reserved functions for interrupts of timers. Whenever timer interrupt occurs programs starts execution from respective function and after coming out of the function main program is again executed from where it was left over. voi d t i mer 0( ) i nt er r upt 1{} / / Ti mer 0 i nt er r upt . voi d t i mer 1( ) i nt er r upt 3{} / / Ti mer 1 i nt er r upt . 65 Thursday, October 13, 2005 Programs for interrupts: / *Onl y i nt er r upt f unct i on, t i mer st ar t i ng f unct i on and mai n f unct i on i s wr i t t en*/ / / Ti mer st ar t i ng f unct i on. voi d st ar t ( ) { TMOD = 0x02; TH0 = 156; EA = 1; ET0 = 1; TR0 = 1; } 66 Thursday, October 13, 2005 Contd: / / I nt er r upt f unct i on f or t i mer 0. voi d t i mer 0 ( ) i nt er r upt 1{ P1 ++; } / / Mai n f unct i on. voi d mai n( ) { P1=0; st ar t ( ) ; whi l e( 1) { / *st y i n endl ess l oop and wai t f or i nt er r upt . */ } } 67 Thursday, October 13, 2005 Prob. 1 Write a program for 8051 in C language to show the count of timer1 running in mode2 on Port2. ***Hint: P2 = TL1; Prob. 2 Write a program so that P0^0 changes its state after 1 sec. using either timer. Prob. 3 Write a program to output the sequence 1,2, 4,9,16 upto 225 each after 500ms on P1. Delay should be created through either timer. Prob. 4 Think of some other activity to enhance your skill in using timers with and without interrupt. 68 Thursday, October 13, 2005 Two kinds of external interrupts: Interrupt0 Interrupt1 Note: Special pins on Port3 are dedicated for other purposes. Hence Port3 is multifunctional port. 69 Thursday, October 13, 2005 Pin 12 (P3^2) is for interrupt0 Pin 13 (P3^3) is for interrupt1 Interrupts will be acknowledged , by default, when a logic low is detected on these pins. These can be configured on falling edge or rising edge of the interrupt signal. 70 Thursday, October 13, 2005 cnt ( ) i nt er r upt 0{} Functions for external interrupt0 cnt ( ) i nt er r upt 2{} Functions for external interrupt1 Control automatically transfers to these two functions whenever respective interrupt is acknowledged. 71 Thursday, October 13, 2005 Initializing external interrupts: voi d ext er nal 0( ) { EA = 1; / / Gl obal i nt er r upt set . EX0 = 1; / / Ex. I nt pt 0. enabl ed. I T0 = 1; / *I nt er r upt wi l l be acknowl edged on f al l i ng edge. */ } 72 Thursday, October 13, 2005 Programs on Ex. Interrupts: / *Thi s pr ogr amchanges st at us of P2^0 when i nt er r upt 0 acknowl edged. */ #i ncl ude <r eg51. h> sbi t OUT = P2^0; voi d i ni t i at e( ) { EA = 1; / / Gl obal enabl ed. EX0 = 1; / / Ext er nal I nt r pt 0. Enabl ed. I T0 = 1; / / Conf i gur ed on f al l i ng edge. } cnt ( ) i nt er r upt 0{ OUT = 0; } 73 Thursday, October 13, 2005 Contd: voi d mai n( ) { i ni t i at e( ) ; whi l e( 1) { } / / Wai t f or i nt er r upt . } 74 Thursday, October 13, 2005 More on external interrupts: Changes for ex. Intrpt1. EX1 = 1; Enables Ex.1 interrupt. I T1 = 1; Enables falling edge response. I T1 = 0; Enables rising edge response. 75 Thursday, October 13, 2005 Serial Port Operation: 8051 has one full-duplex serial port. Port supports RS232 standard. Serial port has associated registers. Theses registers are variables in C. 76 Thursday, October 13, 2005 Serial Port Register: SCONis the only register to work with using serial communication. SCONcontrols the serial operation of the serial port. Serial port has 4 operating modes. 77 Thursday, October 13, 2005 Modes of operation: Mode0 Fixed baud rate , equal to one clock cycle. Mode1 8-bit mode ,variable baud rate, set by Timer1. Mode2 9-bit mode, fixed baud rate, Osc/12 or Osc/64. Mode3 9-bit mode, variable baud rate, set by Timer1. 78 Thursday, October 13, 2005 Baud rate: Tells the number of transmitted or received bits per second. Baud rate depends upon many factors, primarily on the crystal frequency. 1200, 2400, 4800, 9600 are most commonly used baud rates. TH1 is loaded with predefined values to obtain these baud rates. 79 Thursday, October 13, 2005 Creating baud rates: 1200 2400 4800 9600 0XE6 *(SMOD = 0) 0XF3 *(SMOD = 0) 0XF9 *(SMOD = 0) 0XF9 *(SMOD = 1) 80 Thursday, October 13, 2005 Discussion: These values are loaded in the TH1 register to achieve the corresponding baud rates. Assumption: It is assumed that 8051 is running on 12 MHz external crystal frequency. 81 Thursday, October 13, 2005 Initializing serial port: / *Thi s f unct i on i ni t i al i zes ser i al por t f or 4800 baud. */ voi d ser i al _por t ( ) { SCON = 0x50; / / Ser i al por t i n mode1. TMOD = 0x20; / / Ti mer 1 i n aut o r el oad mode. TH1 = 0xF9; / / Rel oadabl e val ue. TI = 1; / / Set 1 f or t r ansmi t r eady. TR1 = 1; / / St ar t Ti mer 1. } 82 Thursday, October 13, 2005 Another Program #i ncl ude <r eg51. h> voi d ser _st ar t ( ) { SCON=0x50; / / 8- bi t dat a mode TMOD=0x20; / / Ti mer 1 i n mode2 TH1=0xF3; / / 2400 baud TR1=1; TI =1; / / Tr ansmi t r eady i ndi cat i on } voi d mai n( ) { ser _st ar t ( ) ; whi l e( 1) { P2= A ; P2= B ; P2= C ; } } 83 Thursday, October 13, 2005 Another program / *Thi s pr ogr ampr i nt s t he message t o t he comput er vi a PC ser i al por t connect ed wi t h 8051 ser i al por t . Onl y ser i al por t i ni t i al i zi ng and mai n f unct i ons ar e wr i t t en */ #i ncl ude <st di o. h> / / St andar d C l i br ar y #i ncl ude <r eg51. h> voi d ser i al ( ) { SCON=0x50; TMOD=0x20; TH1=0xF9; TR1=1; TI =1; / / Cont d. 84 Thursday, October 13, 2005 } voi d mai n( ) { ser i al ( ) ; pr i nt f ( Hel l o, I ama Ravi an! \ n) ; pr i nt f ( Hel l o, I ama UETi an! \ n) ; whi l e( 1) { } } __________________________________________________ Discussion: You will have to search for the circuit diagram over the internet and then start hyper terminal in Windows, set baud rate of your computer same as your MCU (4800)and check the results. 85 Thursday, October 13, 2005 Input and output via serial port: / * Thi s pr ogr amr eads a char act er and t hen out put s i t on ser i al por t . */ #i ncl ude <st di o. h> #i ncl ude <r eg51. h> voi d ser _st ar t ( ) { SCON = 0x50; TMOD = 0x20; TH1 = 0xF3; TR1 = 1; TI = 1; } voi d mai n( ) { / / cont d. 86 Thursday, October 13, 2005 unsi gned char i n; ser _st ar t ( ) ; whi l e( 1) { pr i nt f ( Ent er a char act er \ n) ; i n = get char ( ) ; pr i nt f ( You ent er ed %c, i n) ; } } Discussion: get char ( ) is the function in C to input a character from the keyboard. Thursday, October 13, 2005 87 These slides are prepared for These slides are prepared for lectures purposes only. These lectures purposes only. These might not be suitable for self might not be suitable for self study. study. For any technical guidance For any technical guidance contact the author at: contact the author at: engineerusmanuet@yahoo.co.uk engineerusmanuet@yahoo.co.uk usmanrafique83@yahoo.co.uk usmanrafique83@yahoo.co.uk