(a)
The first five lines of the given code initialise the PWM peripheral, set PORTC pin 5 as output for PWM signal and defines the duty cycle number which is used to alter the duty cycle of the PWM signal.
In the infinite while loop we continuously check if pin4 of PORTC is high. We know that when the button connected to PORTC pin 4 is pressed the pin goes low (logic 0). Hence the if condition is true when the button is not pressed. In this case the duty number is set to 16. This sets the duty cycle to 16/80 X 100 = 20%
Incase the button is pressed the if condition becomes false and else condition is executed where the duty number is set to 64 which gives a duty cycle of 64/80 X 100 = 80%
Lastly the PWM3DH and PWM3DL registers are updated to output PWM signal based on current status of the button.
(b)
We see that
PWM3DH = DutyCycleNumber>>2 and
PWM3DL = DutyCycleNumbber<<6
These shifts effectively break the duty cycle number into 2 parts. Most significant bits from b9-b2 and least significant bits b1-b0. PWM3DL gets the bit1-bit0 at the position 7 and 6 while PWM3DH gets the bit9-bit2 at the position 7-0. Now binary value of bit numbers and corresponding values of PWM3DH and PWM3DL can be found out. These are tabulated as given below
Button state | Duty Number | binary representation | PWM3DH | PWM3DL |
Not Pressed |
16 | 0b00010000 |
0b00000100 (4) |
0b00000000 (0) |
Pressed | 64 | 0b01000000 |
0b00010000 (16) |
0b00000000 (0) |
The numbers in brackets () are decimal representation of the binary number for the PWM3DH and PWM3DL values.
(c)
PIC16f1619 is also equipped with UART, SPI and a 10-bit ADC peripherals.
The following code implements some Pulse Width Modulation (PWM) code and interacts with a Curiosity board (usual connections of pushbutton on portC pin4 (logic 0 when pressed), LEDs on portA pins 1,...