Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PRINCIPLES AND
APPLICATIONS
Lab 7
Timer, USART
Cheng-Chien Su
Outline
HANEL
Timer0
Introduction
prescaler
HANEL
Timer0
Introduction
Block Diagram
HANEL
Timer0
Introduction
T0CON Register
HANEL
Timer0
Introduction
Prescaler
HANEL
Timer0
Operation
HANEL
Timer0
Operation
Interrupt
HANEL
Timer1
Introduction
HANEL
Timer1
Introduction
Block Diagram
HANEL
10
Timer1
Introduction
T1CON Register
HANEL
11
C18 C Libraries
of Timerx
Overall
HANEL
CloseTimerx
OpenTimerx
ReadTimerx
WriteTimerx
12
C18 C Libraries
of Timer
OpenTimer0
Timer Width:
HANEL
Prescale Value:
Clock Source:
13
C18 C Libraries
of Timer
OpenTimer1
Timer Width:
prescale
prescale
prescale
prescale
HANEL
1:1
1:2
1:4
1:8
T1_PS_1_1
T1_PS_1_2
T1_PS_1_4
T1_PS_1_8
Oscillator Use:
Prescaler:
Clock Source:
14
C18 C Libraries
of Timer
WriteTimer0
HANEL
15
C18 C Libraries
of Timer
ReadTimer0
HANEL
16
C18 C Libraries
of Timer
CloseTimer0
HANEL
17
C18 C Libraries
of Timer
Example
#include <p18C4520.h>
#include <timers.h>
#include <stdlib.h>
void main( void ){
int result;
char str[7];
// configure timer0
OpenTimer0( TIMER_INT_OFF &
T0_SOURCE_INT &
T0_PS_1_32 );
TRISD = 0;
while( 1 ) {
while( ! PORTBbits.RB3 );
result = ReadTimer0();
if( result > 0xc000 )
break;
WriteTimer0( 0 );
// close modules
HANEL
18
USART
Introduction
HANEL
19
USART
Introduction
(cont)
HANEL
20
USART
Introduction
(cont)
HANEL
21
Registers of
USART
TXSTA Register
HANEL
22
Registers of
USART
RCSTA Register
HANEL
23
Registers of
USART
BAUDCON Register
HANEL
24
USART
Operation
Baud Rate
Generator
(BRG)
HANEL
25
USART
Operation
Example
Lookup table
Baud Rate Formula
Baud Rate
Generator
(BRG)
HANEL
26
USART
Operation
Asynchronous
Mode
HANEL
27
USART
Operation
Asynchronous
Transmitter
HANEL
28
USART
Operation
Asynchronous
Transmitter
HANEL
29
USART
Operation
Asynchronous
Transmitter
HANEL
30
USART
Operation
Asynchronous
Receiver
HANEL
31
USART
Operation
Asynchronous
Receiver
HANEL
32
C18 Libraries of
USART
Overall
HANEL
33
C18 Libraries of
USART
Interrupt on Transmission:
OpenUSART
Interrupt on Receipt:
Baud rate:
HANEL
Reception mode:
Slave/Master Select*:
Transmission Width:
USART Mode:
34
Arguments: spbrg
C18 Libraries of
USART
OpenUSART
(cont)
HANEL
35
C18 Libraries of
USART
baudUSART
HANEL
RX Pin Monitoring:
36
C18 Libraries of
USART
BusyUSART
Return Value:
HANEL
37
C18 Libraries of
USART
WritUSART
putcUSART
Arguments: data
Remarks:
Code Example:
unsigned int outval;
USART_Status.TX_NINE = (outval & 0x0100) >> 8;
Write1USART( (char) outval );
HANEL
38
C18 Libraries of
USART
putsUSART
putrsUSART
Arguments: data
HANEL
Remarks:
39
C18 Libraries of
USART
DataRdyUSART
Code Example:
HANEL
1 if data is available
0 if data is not available
while (!DataRdyUSART());
40
C18 Libraries of
USART
Remarks:
ReadUSART
getcUSART
HANEL
The 9th bit is read-only if 9-bit mode is enabled. The Status bits are
always read.
Return Value: This function returns the next character in the USART
receive buffer.
Code Example:
int result;
result = ReadUSART();
result |= (unsigned int) USART_Status.RX_NINE << 8;
41
C18 Libraries of
USART
getsUSART
Arguments: len
Remarks:
Code Example:
char inputstr[10];
getsUSART( inputstr, 5 );
HANEL
42
C18 Libraries of
USART
ClosUSART
HANEL
43
C18 Libraries of
USART
Example
HANEL
#include <p18C4520.h>
#include <usart.h>
void main(void){
// configure USART
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX
&
USART_BRGH_HIGH,
25 );
while(1) {
while( ! PORTAbits.RA0 ); //wait for RA0 high
WritUSART( PORTD );
//write value of PORTD
if(PORTD == 0x80)
//check for termination
break;
//value
}
ClosUSART();
}
44
Reference
HANEL
45
Ex
HANEL
Ex 7-1
0.5
PORTD LED
46
Ex
HANEL
Ex 7-2
0.5
PORTD LED
47
Ex 7-3
,
RATE : 38400
Ex
Baud Rate
Asynchronous mode, high speed:
FOSC / (16 * (spbrg + 1))
16M/16*(25+1) 38400
HANEL
48
HANEL
49
HANEL
50
HANEL
51
HANEL
52