16
EEEB371 APPENDIX SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may have one or two PWM (CCP) modules. A CCP module contains a PWM master/slave duty cycle register. The CCP module can be configured to generate a waveform with certain frequency and duty cycle. In PWM mode, the CCPx pin produces up to a 10-bit resolution PWM output. Since the CCP2 pin is multiplexed with PORTC data latch, the appropriate TRISC bit must be cleared to make the CCP1 pin an output. PWM module in PIC18F4550 is Timer2. Figure 7.0 shows a simplified block diagram of the CCP module in PWM mode Figure 7.0 Simplified PWM block diagram A PWM output has a time base (PWM period) and a time that the output stays high (PWM duty cycle).

SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

  • Upload
    vomien

  • View
    223

  • Download
    3

Embed Size (px)

Citation preview

Page 1: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

SELF LEARNING MANUAL

Pulse Width Modulation (PWM)

A PIC18 device may have one or two PWM (CCP) modules. A CCP module contains a

PWM master/slave duty cycle register. The CCP module can be configured to generate a

waveform with certain frequency and duty cycle. In PWM mode, the CCPx pin produces up

to a 10-bit resolution PWM output. Since the CCP2 pin is multiplexed with PORTC data

latch, the appropriate TRISC bit must be cleared to make the CCP1 pin an output. PWM

module in PIC18F4550 is Timer2.

Figure 7.0 shows a simplified block diagram of the CCP module in PWM mode

Figure 7.0 Simplified PWM block diagram

A PWM output has a time base (PWM period) and a time that the output stays high (PWM

duty cycle).

Page 2: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

PWM period is specified by writing to the PR2 register. The PWM period can be calculated

using the following formula:

PWM period = [(PR2) + 1] 4 TOSC (TMR2 prescale factor)

The PWM duty cycle is specified by writing to the CCPR1L register and to the CCP1CON

bit 4 and bit 5. The following equation is used to calculatethe PWM duty cycle in time:

PWM duty cycle = (CCPR1L:CCP1CON<5:4>) TOSC (TMR2 prescale factor)

Procedure for using the PWM module:

Step 1

Set the PWM period by writing to the PR2 register.

Step 2

Set the PWM duty cycle by writing to the CCPR1L register and CCPR1H (slave as latch).

Step 3

Configure the CCP1 pin at PORTC for output

Step 4

Set the TMR2 prescale value and enable Timer2 by writing to T2CON register

Step 5

Configure CCP1CON module for PWM operation

Step 6

Clear the bit 6 and bit 3 of the T3CON register.

Page 3: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

PWM mode using MikroC

Library Routines

PWM1_Init

PWM1_Set_Duty

PWM1_Start

PWM1_Stop

Page 4: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

Controlling DC Motor

Please refer to Experiment 6. An advantage of DC motors is speed control of motor can be

easily achieved by providing variable voltage to it. There are many methods to offer more

precise control and maximum efficiency in controlling the speed. PWM (pulse width

modulation) is among the popular alternative in DC motor speed control.

Hardware Configuration

For hardware configuration, put mini jumper on JP20 and JP21 to select DC motor and mini

jumper on JP10 to select PWM.

Page 5: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

Procedure (Examples)

Speed Control

1. Calculate the PR2 value if PWM frequency = 5kHz and CCPR1 value if PWM duty

cycle = 40%

2. Write a source code in MPLAB, build it and download the Hex file into the

microcontroller and run the program.

3. Modify the program to change the speed of motor to 85%. Write down your

observation and print out the source file.

PR2_val EQU ??

CCPR1_val EQU ??

org 0x00

goto start

org 0x08

retfie

org 0x18

retfie

start: MOVLW 0X0F

MOVWF ADCON1

MOVLW 0X00

MOVWF TRISB

BSF PORTB,4 ;to rotate motor

BCF PORTB,5

MOVLW PR2_val ;set the PWM period

MOVWF PR2

MOVLW CCPR1_val ;set PWM duty cycle

MOVWF CCPR1L ;

MOVWF CCPR1H ;SLAVE

BCF TRISC,CCP1 ; CCP1 as output

MOVLW 0X81 ;Timer2 as base time for PWM1

MOVWF T3CON

CLRF TMR2 ;clear TMR2

MOVLW 0X5 ;enable Timer 2 and prescale=4

MOVWF T2CON

MOVLW 0XC ;enable CCP1 PWM mode

MOVWF CCP1CON

Wait BRA Wait

END

Page 6: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

4. Write the source code in MikroC Compiler, build it and download the Hex file into

the microcontroller and run the program. Print out the source file and write down your

observation.

5. Complete the program below. The program is to control the DC motor speed using SW1

(RB0) and SW2 (RB1). When SW1 is pressed, DC motor speed should increase the duty

cycle and when SW2 is pressed, DC motor speed should decrease the duty cycle. PWM

frequency = 5 kHz. Print out the source file and write down your observation.

PR2_val EQU ??

DUTY_CYCLE SET 0X00

Init_Duty_Cycle EQU D’20’

org 0x00

goto start

org 0x08

retfie

org 0x18

retfie

start: MOVLW 0X0F

MOVWF ADCON1

MOVLW 0X0F

MOVWF TRISB

BSF PORTB,4 ;to rotate motor

BCF PORTB,5

MOVLW PR2_val ;set the PWM period

MOVWF PR2

MOVLW Init_Duty_Cycle ;INITIAL CURRENT DUTY

MOVWF DUTY_CYCLE

MOVF DUTY_CYCLE,W

MOVWF CCPR1L ;

MOVWF CCPR1H ;SLAVE/LATCH

BCF TRISC,CCP1

void main() { ADCON1 = 0x0F; // Configure A/D for digital inputs CMCON = 0x07; // Configure comparators for digital input TRISB = 0x00; // Configure PORTB as output TRISC = 0x00; // Configure PORTC as output PWM1_Init(5000); // Initialize PWM1 module at 5KHz PORTB.F4=1; PORTB.F5=0; PWM1_Start(); // start PWM1 PWM1_Set_Duty(102); // Set 40% duty cycle for PWM1 }

Page 7: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

\

MOVLW 0X81

MOVWF T3CON

CLRF TMR2

MOVLW 0X5

MOVWF T2CON

MOVLW 0XC

MOVWF CCP1CON

CHECK_RB0 BTFSC PORTB,0

BRA CHECK_RB1 ;NO

BRA INC_CD ;YES

CHECK_RB1 BTFSC PORTB,1

BRA CHECK_RB0 ;NO

BRA DEC_CD ;YES

GEN_PWM CALL DELAY5ms

; complete the program—load the value for PWM duty cycle

BRA CHECK_RB0

INC_CD ; complete the program increment the duty cycle

BRA GEN_PWM

DEC_CD ; complete the programdecrement the duty cycle

BRA GEN_PWM

DELAY5ms ; Please complete the program

RETURN

END

Page 8: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

6. Write the source code in MikroC Compiler, build it and download the Hex file into

the microcontroller and run the program.

unsigned short current_duty; void main() { ADCON1 = 0x0F; // Configure A/D for digital inputs CMCON = 0x07; // Configure comparators for digital input TRISB = 0x00; // Configure PORTB as output TRISC = 0x00; // Configure PORTC as output PWM1_Init(5000); // Initialize PWM1 module at 5KHz PORTB.F4=1; PORTB.F5=0; current_duty = 10; // initial value for current_duty PWM1_Start(); // start PWM1 PWM1_Set_Duty(current_duty); // Set current duty for PWM1 while (1) { // endless loop if (PORTB.F0==0) { // button on RB0 pressed Delay_ms(40); current_duty++; // increment current_duty if (current_duty>=250){current_duty = 250; } PWM1_Set_Duty(current_duty); } if (PORTB.F1==0) { // button on RB1 pressed Delay_ms(40); current_duty--; // decrement current_duty if (current_duty<=10){current_duty = 10;} PWM1_Set_Duty(current_duty); } Delay_ms(5); // slow down change pace a little } }

Page 9: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

PIC Analog to Digital Converter

Introduction

The world as we know is analog world, whereas the microprocessor world is a digital world.

Therefore, in order for microprocessor to communicate with the real analog world, a digital to

analog (D/A) or analog-to-digital (A/D) conversion must take place. As an example,

temperature, humidity and brightness are changing continuously. This experiment will focus

on A/D conversion by using the PIC18F4550 Analog-To-Digital Converter.

The ADC module on the PIC18F4550 is capable of converting up to 13-channels of analog

DC input voltages connected to the pin of PORTA, PORTB and PORTE or label as AN0-

AN12. The A/D allows conversion of an analog input signal to a corresponding 10-bit digital

number. The analog reference voltage is software selectable to either the device’s positive

and negative supply voltage (VDD and VSS) or the voltage level on the RA3/AN3/VREF+

pin and A2/AN2/VREF- pin.

Optimal Voltage Range for A/D Conversion

- A/D converter requires a low reference voltage (VREF-) and a high reference voltage

(VREF+) to perform conversion.

- Most A/D converters are ratiomertic:

1. An analog input of VREF- is converted to digital code 0.

2. An analog input of VREF+ is converted to digital code 2n – 1.

3. An analog input of k V is converted to digital code

(2n – 1) (k - VREF-) (VREF+ - VREF-)

- The A/D conversion result k corresponds to the following analog input:

VK = VREF- + (VREF+ - VREF-) k (2n – 1)

- Most systems use VDD and 0V as VREF+ and VREF-, respectively.

- The output of a transducer should be scaled and shifted to the range of 0V ~ VDD in

order to achieve the best accuracy

Note: k refers to the 8 binary digits displayed on the LED arrays whereas kDEC is the

equivalent decimal value for the binary digit. i.e., k = 00001010, kDEC = 10.

The A/D module has four registers. These registers are:

• 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)

Page 10: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

ADCON0: A/D CONTROL REGISTER 0

Page 11: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

ADCON1: A/D CONTROL REGISTER 1

Page 12: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

ADCON2: A/D CONTROL REGISTER 2

Page 13: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

Hardware Configuration

Figure below shows the connection between analog input and PIC.

External Digital Input

Offers option for external digital input. User may connect additional digital input such as

photo-electric sensor and select the voltage for digital input at JP15.

Analog Input

Analog input is designed to read analog voltage from potential meter, temperature sensor or

external analog input. Only 1 analog input can be used at the same time. Use mini jumper at

JP14 to select analog input used. JP12 is provided to connect others analog input besides

temperature sensor and potentiometer. For hardware configuration, put mini jumper on JP12

to select potential meter (POT).

Page 14: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

Procedures (Examples)

PART I

1. Based on the flowchart Figure 8.1; write a program in assembly language to read analog

input from AN0 and write the binary value to PORTA [2-1] (MSB) and PORTD [7-0].

Build and download your program to the PIC18 board. The microcontroller is to be used

to read an analog signal from the potentiometer and write the values to PORTD and

PORTA. Turn the potentiometer and observe the changes.

2. Verify the capability of your program and hardware with 5 sets of different input analog

signal and tabulate them into the table in the worksheet. Each set should contain:

a. Binary values read from LEDs (10bits)

b. Converted to equivalent voltage value of output in decimal.

c. Measured analog input voltage value using voltmeter between RA0 and gnd(

please use jumper).

d. Percentage error between analog and digital values

Q1.What is the highest voltage value you are able to record?

10

Page 15: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

3. Write the following source using MikroC compile, build download and run the

application. Print the C file.

4. Test the program.

PART II

1. Modify the program of PART 1 to change the output array of LEDs into a running light

display at PORTD. This can be achieved by sequencing the outputs with ‘1’. You are also

required to be able to control speeds of running light into 3 different speeds through the

potentiometer. You can write the program using assembly language or in C language.

Hint: control the speed by control the delay.

2. Build, assemble, download and run the program.

unsigned int temp_res; void main() { ADCON2= 0X80; // Configure ADFM = 1 ADCON1 = 0XE; ADCON0 = 0X3; CMCON = 0X07; // Disable comparators TRISA = 0x1; // PORTA(AN0) is input //RA2 AND RA1 as output TRISD = 0; // PORTD is output do { temp_res = ADC_Read(0); // Get 10-bit results of AD conversion PORTD = temp_res; // Send lower 8 bits to PORTD PORTA = temp_res >> 7; // Send 2 most significant bits to RA2, RA1 } while(1); }

Page 16: SELF LEARNING MANUAL - metalab.uniten.edu.mymetalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1617S1 Self... · SELF LEARNING MANUAL Pulse Width Modulation (PWM) A PIC18 device may

EEEB371 APPENDIX

Figure 8.1 flowchart

Power-Up ADC

Initialize AN0 ADFM = 1

ADRESH Write to RA2 and RA1 ADRESL Write to PORTD

Delay10ms

Conversion

complete?

Start

Y

N