Download presentation
Presentation is loading. Please wait.
Published byMeghan Robbins Modified over 9 years ago
1
Microcontrollers session
2
What is Microcontroller? Microcontroller is composed of microprocessor,ram, flash memory,EEPROM and some modules like timers, Analog to Digital converter, CCP, USART, etc.… Ahmed Hussam 2
3
The Microprocessor: In the PIC there is a processor that can execute only some instructions according to its type (8, 16, 32, 64 bits). The instructions are excited by the processor in four main steps : (fetch, decode, execute, store ) The speed of the microprocessor is how many instructions can be executed in one second The processor speed depends on the crystal oscillator frequency so by increasing the crystal frequency the speed of the processor increase Ahmed Hussam 3
4
Memory organization (Flash Memory) - The PIC18F has a 21-bit program counter (PC)it can address 2 Mbyte (221 locations). -PC determines the address of the instruction to fetch for execution. -The size of any instruction is 1 words excepts 4 instruction that takes 2 words. (word=2 byte) Ahmed Hussam4
5
Memory organization (RAM) Ahmed Hussam 5
6
Assembly instructions There are 75 assembly instruction. The most important instructions are : (MOVLW, MOVWF, MOVF, MOVFF, ADDWF, CLRF, SETF, BCF, BSF, BRA, GOTO,BTFSS, BTFSC, CPFSGT, CPFSLT, CPFSEQ, RETURN, RETFIE FAST) Ahmed Hussam6
7
7
8
8
9
9
10
List of symbols used in instruction set Ahmed Hussam10
11
Digital input/output ports Some pins of the I/O ports are multiplexed with an alternate function from the peripherals of the MCU. If the PIN is enabled as I/O other function is disabled. Each port has three registers for its operation: TRIS register: Data direction register PORT register: read the levels (1 or 0) of the pins LAT register: output latch (we clear it always once at the beginning) Any bit in TRIS determine the direction of its corresponding bit/pin in the PORT. If TRIS bit =1 corresponding PORT pin is Input If TRIS bit =0 corresponding PORT pin is Output Ahmed Hussam 11
12
Example: write an initializing code for PORTA to set RA4 as an O/P while the rest of pins are set as I/P. Solution: clrf PORTA, ACCESS clrf LATA, ACCESS movlw 0XEF ;1110 1111 (1 Input, 0 Output) movwf TRISA, ACCESS ;set all PORTA pins are I/P except RA4 is o/p. Ahmed Hussam12
13
Analog to digital convertor (ADC) Ahmed Hussam13
14
How to use the ADC ?? There are 5 special function registers (SFR) used in the ADC MODULE IN PIC 18F4620 A/D Result High Register (ADRESH) A/D Result Low Register (ADRESL) A/D Control Register 0 (ADCON0) A/D Control Register 1 (ADCON1) A/D Control Register 2 (ADCON2) Ahmed Hussam14
15
bit 7-6 Unimplemented: Read as ‘0’ bit 5-2 CHS3:CHS0: Analog Channel Select bits 0000 = Channel 0 (AN0) 0001 = Channel 1 (AN1) 0010 = Channel 2 (AN2) 0011 = Channel 3 (AN3) 0100 = Channel 4 (AN4) 0101 = Channel 5 (AN5)(1,2) 0110 = Channel 6 (AN6)(1,2) 0111 = Channel 7 (AN7)(1,2) 1000 = Channel 8 (AN8) 1001 = Channel 9 (AN9) 1010 = Channel 10 (AN10) 1011 = Channel 11 (AN11) 1100 = Channel 12 (AN12 1101 = Unimplemented(2) 1110 = Unimplemented(2) 1111 = Unimplemented(2) Ahmed Hussam 15
16
bit 1 GO/DONE: A/D Conversion Status bit When ADON = 1: 1 = A/D conversion in progress 0 = A/D Idle bit 0 ADON: A/D On bit 1 = A/D converter module is enabled 0 = A/D converter module is disabled Ahmed Hussam16
17
bit 7-6 Unimplemented: Read as ‘0’ bit 5 VCFG1: Voltage Reference Configuration bit (VREF- source) 1 = VREF- (AN2) 0 = VSS bit 4 VCFG0: Voltage Reference Configuration bit (VREF+ source) 1 = VREF+ (AN3) 0 = VDD Ahmed Hussam17
18
Ahmed Hussam18
19
bit 7 ADFM: A/D Result Format Select bit 1 = Right justified 0 = Left justified bit 6 Unimplemented: Read as ‘0’ bit 5-3 ACQT2:ACQT0: A/D Acquisition Time Select bits 111 = 20 TAD 110 = 16 TAD 101 = 12 TAD 100 = 8 TAD 011 = 6 TAD 010 = 4 TAD 001 = 2 TAD 000 = 0 TAD(1) bit 2-0 ADCS2:ADCS0: A/D Conversion Clock Select bits 111 = FRC (clock derived from A/D RC oscillator)(1) 110 = FOSC/64 101 = FOSC/16 100 = FOSC/4 011 = FRC (clock derived from A/D RC oscillator)(1) 010 = FOSC/32 001 = FOSC/8 000 = FOSC/2 Ahmed Hussam 19
20
The acquisition time bits table Ahmed Hussam20
21
ADC block diagram Ahmed Hussam21
22
Pulse width modulation (PWM) CCP module is a module can used in one of three modes 1- Capture 2- Compare 3- PWM PWM is used to control on the width of the pulse and its frequency in pwm mode you must run timer 2 to determine the frequency and the pulse width There are three SFR used to control the ccp module 1- CCP1CON 2-CCP1RL 3-CCP1RH Ahmed Hussam22
23
Ahmed Hussam23 bit 7-6 Unimplemented: Read as ‘0’ bit 5-4 DCxB1:DCxB0: PWM Duty Cycle bit 1 and bit 0 for CCP Module x Capture mode: Unused. Compare mode: Unused. PWM mode: These bits are the two LSbs (bit 1 and bit 0) of the 10-bit PWM duty cycle. The eight MSbs (DCx9:DCx2) of the duty cycle are found in CCPRxL. you also need two SFR in timer 2 1- T2CON 2- PR2 the pwm module in pic 18F4620 is 10 bits pwm.
24
bit 3-0 CCPxM3:CCPxM0: CCP Module x Mode Select bits 0000 = Capture/Compare/PWM disabled (resets CCP module) 0001 = Reserved 0010 = Compare mode, toggle output on match (CCPIF bit is set) 0011 = Reserved 0100 = Capture mode, every falling edge 0101 = Capture mode, every rising edge 0110 = Capture mode, every 4th rising edge 0111 = Capture mode, every 16th rising edge 1000 = Compare mode: initialize CCP pin low; on compare match, force CCP pin high (CCPxIF bit is set) 1001 = Compare mode: initialize CCP pin high; on compare match, force CCP pin low (CCPxIF bit is set) 1010 = Compare mode: generate software interrupt on compare match (CCPxIF bit is set, CCP pin reflects I/O state) 1011 = Compare mode: trigger special event, reset timer, start A/D conversion on CCP2 match (CCPxIF bit is set) 11xx = PWM mode Ahmed Hussam24
25
Timer 2 Ahmed Hussam25 bit 7 Unimplemented: Read as ‘0’ bit 6-3 T2OUTPS3:T2OUTPS0: Timer2 Output Postscale Select bits 0000 = 1:1 Postscale 0001 = 1:2 Postscale 1111 = 1:16 Postscale bit 2 TMR2ON: Timer2 On bit 1 = Timer2 is on 0 = Timer2 is off bit 1-0 T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select bits 00 = Prescaler is 1 01 = Prescaler is 4 1x = Prescaler is 16
26
Period and duty determination RC2 must be initialized as output for PWM. PWM Period = (4/Fosc) * (PR2+1) * TMR2 prescale PWM duty cycle = (1/Fosc) * (CCPR1L:CCP1CON ) * TMR2 prescale Ahmed Hussam26
27
Learn more about: Capture Compare EEPROM Lock up table Interrupt Low voltage detector USART CAN (found in pic 18F458) Ahmed Hussam27
28
Interrupt: PIC18F has high and low priority interrupts. High priority interrupt vector at 0X0008 Low priority interrupt vector at 0X0018 The high priority interrupt override low priority interrupt Each interrupt source has 3 bits to control its operation: 1. Flag bit: to indicate the occurrence of the interrupt event 2. Enable bit: to enable the interrupt (allow the PC to branch to the interrupt vector address when the flag bit is set) vector address when the flag bit is set) 3. Priority bit: to select high or low priority Interrupt priority feature is enabled by setting IPEN bit in RCON register Interrupt priority feature is enabled by setting IPEN bit in RCON register. Ahmed Hussam 28
29
Ahmed Hussam29 bit 7 GIE/GIEH: Global Interrupt Enable bit When IPEN = 0: 1 = Enables all unmasked interrupts 0 = Disables all interrupts When IPEN = 1: 1 = Enables all high priority interrupts 0 = Disables all interrupts bit 6 PEIE/GIEL: Peripheral Interrupt Enable bit When IPEN = 0: 1 = Enables all unmasked peripheral interrupts 0 = Disables all peripheral interrupts When IPEN = 1: 1 = Enables all low priority peripheral interrupts 0 = Disables all low priority peripheral interrupts
30
bit 5 TMR0IE: TMR0 Overflow Interrupt Enable bit 1 = Enables the TMR0 overflow interrupt 0 = Disables the TMR0 overflow interrupt bit 4 INT0IE: INT0 External Interrupt Enable bit 1 = Enables the INT0 external interrupt 0 = Disables the INT0 external interrupt bit 3 RBIE: RB Port Change Interrupt Enable bit 1 = Enables the RB port change interrupt 0 = Disables the RB port change interrupt bit 2 TMR0IF: TMR0 Overflow Interrupt Flag bit 1 = TMR0 register has overflowed (must be cleared in software) 0 = TMR0 register did not overflow bit 1 INT0IF: INT0 External Interrupt Flag bit 1 = The INT0 external interrupt occurred (must be cleared in software) 0 = The INT0 external interrupt did not occur bit 0 RBIF: RB Port Change Interrupt Flag bit 1 = At least one of the RB7:RB4 pins changed state (must be cleared in software) 0 = None of the RB7:RB4 pins have changed state Ahmed Hussam30
31
Ahmed Hussam31 bit 7 RBPU: PORTB Pull-up Enable bit 1 = All PORTB pull-ups are disabled 0 = PORTB pull-ups are enabled by individual port latch values bit 6 INTEDG0: External Interrupt 0 Edge Select bit 1 = Interrupt on rising edge 0 = Interrupt on falling edge bit 5 INTEDG1: External Interrupt 1 Edge Select bit 1 = Interrupt on rising edge 0 = Interrupt on falling edge bit 4 INTEDG2: External Interrupt 2 Edge Select bit 1 = Interrupt on rising edge 0 = Interrupt on falling edge bit 3 Unimplemented: Read as ‘0’ bit 2 TMR0IP: TMR0 Overflow Interrupt Priority bit 1 = High priority 0 = Low priority bit 1 Unimplemented: Read as ‘0’ bit 0 RBIP: RB Port Change Interrupt Priority bit 1 = High priority 0 = Low priority
32
Ahmed Hussam32 bit 7 INT2IP: INT2 External Interrupt Priority bit 1 = High priority 0 = Low priority bit 6 INT1IP: INT1 External Interrupt Priority bit 1 = High priority 0 = Low priority bit 5 Unimplemented: Read as ‘0’ bit 4 INT2IE: INT2 External Interrupt Enable bit 1 = Enables the INT2 external interrupt 0 = Disables the INT2 external interrupt bit 3 INT1IE: INT1 External Interrupt Enable bit 1 = Enables the INT1 external interrupt 0 = Disables the INT1 external interrupt bit 2 Unimplemented: Read as ‘0’ bit 1 INT2IF: INT2 External Interrupt Flag bit 1 = The INT2 external interrupt occurred (must be cleared in software) 0 = The INT2 external interrupt did not occur bit 0 INT1IF: INT1 External Interrupt Flag bit 1 = The INT1 external interrupt occurred (must be cleared in software) 0 = The INT1 external interrupt did not occur
33
Timer 0 with interrupt Timer 0 interrupts when an over flow happens in tmr0l (when TMR0 is in 8 bits mode) To use timer 0 there are three SFR are used 1- T0CON 2- TMR0L 3- TMR0H Ahmed Hussam33
34
Ahmed Hussam 34 bit 7 TMR0ON: Timer0 On/Off Control bit 1 = Enables Timer0 0 = Stops Timer0 bit 6 T08BIT: Timer0 8-bit/16-bit Control bit 1 = Timer0 is configured as an 8-bit timer/counter 0 = Timer0 is configured as a 16-bit timer/counter bit 5 T0CS: Timer0 Clock Source Select bit 1 = Transition on T0CKI pin 0 = Internal instruction cycle clock (CLKO) bit 4 T0SE: Timer0 Source Edge Select bit 1 = Increment on high-to-low transition on T0CKI pin 0 = Increment on low-to-high transition on T0CKI pin bit 3 PSA: Timer0 Prescaler Assignment bit 1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler. 0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output. bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits 111 = 1:256 Prescale value 110 = 1:128 Prescale value 101 = 1:64 Prescale value 100 = 1:32 Prescale value 011 = 1:16 Prescale value 010 = 1:8 Prescale value 001 = 1:4 Prescale value 000 = 1:2 Prescale value
35
Digital input output example Make a program that takes a digital signal from a push buttom so that when you push the button a led illuminates ORG 0X000000 CLRF PORTD,ACCESS CLRF LATD,ACCESS MOVLW 0X01 MOVWF TRISD,ACCESS AGAIN BTFSS PORTD,0,ACCESS BRA L1 BSF PORTD,1,ACCESS BRA L2 L1 BCF PORTD,1,ACCESS L2 BRA AGAIN Ahmed Hussam 35
36
Analog read example Make a program that read analog signal from a potentiometer so that when the volt is greater 2.5 volt make a led illuminates Ahmed Hussam36
37
ORG 0X000000 MOVLW 0X01 MOVWF ADCON0,ACCESS CLRF ADCON1,ACCESS MOVLW 0X2A MOVWF ADCON2,ACCESS CLRF PORTD,ACCESS CLRF LATD,ACCESS CLRF TRISD,ACCESS AGAIN BSF ADCON0,1,ACCESS L1 BTFSC ADCON0,1,ACCESS BRA L1 MOVLW 0X80 CPFSLT ADRESH,ACCESS BRA L2 BCF PORTD,0,ACCESS BRA L3 L2 BSF PORTD,0,ACCESS L3 BRA AGAIN Ahmed Hussam 37
38
Pwm example Make a program that read an analog signal from a potentiometer and take the result as 8 bit from the adc and use it in the pwm module where the frequency of the signal is 1 KHz Ahmed Hussam38
39
ORG 0X000000 MOVLW 0X01 MOVWF ADCON0,ACCESS CLRF ADCON1,ACCESS MOVLW 0X2A MOVWF ADCON2,ACCESS CLRF PORTC,ACCESS CLRF LATC,ACCESS CLRF TRISC,ACCESS MOVLW 0X0F MOVWF CCP1CON,ACCESS MOVLW 0X07 MOVWF T2CON,ACCESS AGAIN BSF ADCON0,1,ACCESS L1 BTFSC ADCON0,1,ACCESS BRA L1 MOVFF ADRESH,CCPR1L BRA AGAIN Ahmed Hussam39
40
Timer 0 with interrupt example Make a program of led flasher that make the led illuminats for one second then the led is turned of for one second by using timer 0 in 8 bit mode using interrupt Ahmed Hussam40
41
X EQU 0X00 ORG 0X00000 GOTO MAIN ORG 0X0008 INCF X,F,ACCESS BCF INTCON,2,ACCESS RETFIE FAST MAIN MOVLW 0XA0 ;MOVLW 0X00 MOVWF INTCON,ACCESS CLRF X,ACCESS MOVLW 0X47 MOVWF T0CON,ACCESS CLRF PORTD,ACCESS CLRF LATD,ACCESS CLRF TRISD,ACCESS CLRF TMR0L,ACCESS CLRF TMR0H,ACCESS AGAIN BTG PORTD,0,ACCESS ;BCF PORTD,1,ACCESS Ahmed Hussam41
42
CALL DELAY ;BSF PORTD,0,ACCESS ;CALL DELAY BRA AGAIN DELAY BSF T0CON,7,ACCESS MOVLW 0X3D L1 CPFSGT X,ACCESS BRA L1 CLRF X,ACCESS BCF T0CON,7,ACCESS CLRF TMR0L,ACCESS CLRF TMR0H,ACCESS RETURN Ahmed Hussam42
43
Configuration bits for pic 18F4620 title "PIC18F4620 counting program" list P=PIC18F4620 ;p=18fxxx #include ; This ?header file? contains all ;****************************************************************************** ;my configrations __CONFIG _CONFIG1H, _IESO_OFF_1H &_FCMEN_OFF_1H & _OSC_HS_1H ;_OSC_HSPLL_1H __CONFIG _CONFIG2L, _BOREN_OFF_2L & _PWRT_OFF_2L __CONFIG _CONFIG2H, _WDT_OFF_2H __CONFIG _CONFIG3H, _PBADEN_OFF_3H __CONFIG _CONFIG4L, _LVP_OFF_4L & _STVREN_OFF_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L __CONFIG _CONFIG7H, _EBTRB_OFF_7H Ahmed Hussam 43
44
;****************************************************************************** ;X EQU 0X00 ORG 0X00000 GOTO MAIN ORG 0X0008 RETFIE FAST MAIN ;put your code here END Ahmed Hussam44
45
Configuration bits for pic 18F452 title "PIC18F452 counting program" list P=PIC18F452 ;p=18fxxx,f=inhx32 #include ; This ?header file? contains all ;****************************************************************************** ;Configuration bits ; The __CONFIG directive defines configuration data within the.ASM file. ; The labels following the directive are defined in the P18F458.INC file. ; The PIC18FXX8 Data Sheet explains the functions of the configuration bits. ; Change the following lines to suit your application. __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H ; _HSPLL_OSC_1H __CONFIG _CONFIG2L, _BOR_OFF_2L & _PWRT_OFF_2L __CONFIG _CONFIG2H, _WDT_OFF_2H __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L __CONFIG _CONFIG6H, _WRTB_OFF_6H & _WRTC_OFF_6H & _WRTD_OFF_6H __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L __CONFIG _CONFIG7H, _EBTRB_OFF_7H ;****************************************************************************** Ahmed Hussam 45
46
;Variable definitions ORG 0X000000 GOTO MAIN ORG 0X00008 RETFIE FAST MAIN ;put your code here END Ahmed Hussam46
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.