Professional Documents
Culture Documents
• After the timer reaches its limits and rolls over, in order to
repeat the process, the registers TMR0H and TMR0L must be
reloaded with the original value.
2. load register TMR0H followed by register TMR0L with initial count values.
3. keep monitoring the timer flag to see if it raised. Get out of the loop when
TMR0IF become high.
• INTCONbits.TMR0IF=0;
OR
• TMR0IF=0;
CSRC=0 BRGH=0
TX9=0
TXSTA=20H
TXEN=1
TRMT=0
TV9D=0
SYNC=0
SPEN=1
5. The character byte to be transmitted serially is
written into the TXREG register.
6. Monitor the TXIF bit of the PIR1 register to make
sure UART is ready for next byte
7. To transfer the next character, go to step 5.
11/16/2023 19EECN2601-Microprocesor and Microcontrolle 118
r
Importance of the TXIF flag
1. The byte character to be transmitted is written into the
TXREG register
3. The TSR reads the byte from TXREG and begins to transfer
the byte starting with the start bit.
Pin Buffer
TXREG(8-BIT) and
TSR
control
Interrupt
Baud rate clock
SPEN
TXIF SPBRG
Tx pin
TXREG(8-BIT) TSR
Data
TXIF
Tx pin
TXREG(8-BIT) TSR
Data (Start bit -8bit data-stop bit)
TXIF =1
Tx pin
TXREG(8-BIT) TSR
data (Start bit -8bit data-stop bit)
TXIF =1
Tx pin
TXREG(8-BIT) TSR
Data empty (Start bit -8bit data-stop bit)
TXIF =0
•SPEN=1
•RX9=0
•SREN=0
•CREN=1
•ADDE=0
•FERR=0
•OERR=0
•TXD9=0
RCSTA=90h
CSRC=0 BRGH=0
TX9=0 TRMT=0
TXEN=0 TV9D=0
SYNC=0
• TXSTA=0;
1. Select a channel.
2. Activate the start conversion (SC) signal to start
the conversion of analog input.
3. Keep monitoring the end-of-conversion (EOC)
signal.
4. After the EOC has been activated, Read data out
of the ADC chip.
• It is a 10-bit ADC.
• The PORTA pins of RA0-RA3 and RA5 and RE0-RE2 of PORTE are
used for the analog input channels.
• With PCFG:0110, we can use all the pins of the PORTA as the digital
I/O.
• The default is PCFG : 0000, which allows us to use all 8 pins for
analog inputs.
11/16/2023 19EECN2601-Microprocesor and Microcontrolle 178
r
ADC-pins
• In that case Vref : Vdd, the same voltage source used by the
PIC18 chip itself.
1. Turn on the ADC module of the PIC18 because it is disabled upon power-on
reset to save power.
2. Make the pin for the selected ADC channel an input pin.
3. Select voltage reference and NC input channels using ADCON0
and ADCON1.
4. Select the conversion speed using ADCON0 and ADCON1.
(Fosc/64, channel0,ADC ON
right justified)
5. Wait for the required acquisition time.
6. Activate the start conversion bit of GO/DONE.
7. Wait for the conversion to be completed by polling the end-of-conversion
(GO/DONE) bit.
8. After the GO/DONE bit has gone LOW read the ADRESL and ADRESH registers to
get the digital data output.
9. Go back to step 5.
11/16/2023 19EECN2601-Microprocesor and Microcontrolle 182
r
11/16/2023 19EECN2601-Microprocesor and Microcontrolle 183
r
CCP MODULE
void InitPWM()
{
TRISC2 = 0; // Make CCP1 pin as output
CCP1CON = 0x0C; // Configure CCP1 module in PWM mode
void SetPWMDutyCycle(unsigned int DutyCycle) // Give a value in between 0 and 1024 for DutyCycle
{
CCPR1L = DutyCycle>>2; // Put MSB 8 bits in CCPR1L
CCP1CON &= 0xCF; // Make bit4 and 5 zero
CCP1CON |= (0x30&(DutyCycle<<4)); // Assign Last 2 LSBs to CCP1CON
}