Professional Documents
Culture Documents
PIC18f452
bit 1 TRMT: TSR (Transmit Shift Register) Status bit This bit can also be used to check transmission of data
1 = TSR empty
0 = TSR full
NOTE: If TSR is Empty then flag TXIF will be high. It becomes automatically zero when TSR is loaded with new data!
NOTE: Table of SPBRG values for different Baud Rates with Different Foscs given in book
and Datasheet
Load TXSTA Load SPBRG register Enable USART Load TXREG
reg for async Set TX (RC6) according to Baud using SPEN bit in with data to be
mode pin as output Rate RCSTA reg transmitted
NOTE: TXIF flag automatically get cleared when data is loaded in TXREG
PROBLEM: Write a MIKROC program to transmit character ‘A’ continuously with Baud rate 9600 with
XTAL=10Mhz.
SOLUTION:
void main()
{
TRSIC.TRISC6 = 0; // TX configured as o/p
TXSTA = 0x20; // 8.bit async mode, low baud rate
SPBRG=15; //9600 Baud Rate, Xtal =10Mhz
TXSTA.TXEN=1; //enable transmission .. Optional
RCSTA.SPEN = 1; // Enable USART
while(1)
{
TXREG = 'A'; // Load the character to be transmitted
while(PIR1.TXIF==0); // Wait here till transmission is complete
}
}
Load RCSTA reg Load SPBRG register
Load TXSTA reg to
for continuous Set RX (RC7) according to Baud
select low or high
reception for 8-bit pin as input Rate
speed Baud rate
data
NOTE: RCIF flag automatically get cleared when received data is copied from RCREG
PROBLEM: Write a MIKROC program to throw serially received data to PORTB continuously. Take
Baud rate 9600 with XTAL=10Mhz.
SOLUTION:
void main()
{
TRSIC.TRISC7 = 1; // RX configured as o/p
RCSTA = 0x90; // enable serial port and receiver
TXSTA.BRGH=0; //low baud rate
SPBRG=15; //9600 Baud Rate, Xtal =10Mhz
while(1)
{
while(PIR1.RCIF==0); //Wait here till transmission is complete
PORTB = RCREG; // throw received data to PORTB
}
}
ASSIGNMENT
Try to understand examples 10.12 to 10.15
SOME USEFUL MIKROC LIBRARY
FUNCTIONS OF USART
MIKROC LIBRARY FUNCTIONS FOR USART
void UARTx_Init(unsigned long baud_rate); Configures and
initializes the USART module. The internal USART module is setting
configured in this function can be seen in MIKROC manual
// CODE to Initialize hardware UART1 module and
establish communication at 2400 bps
UART1_Init(2400);
unsigned UARTx_Read();
The function receives a byte via USART. Use
the UARTx_Data_Ready function // read data if ready
to test if data is ready first. unsigned char receive;
UART1_Init(2400); //baud rate=2400
It Returns Received byte. if (UART1_Data_Ready())
receive = UART1_Read();
MIKROC LIBRARY FUNCTIONS FOR USART
void UARTx_Write(unsigned _data);
Sends text (maximum 255 characters) via USART. Text should be zero terminated.