Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits...
Transcript of Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits...
![Page 1: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/1.jpg)
Programming in C
Chapter 10
![Page 2: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/2.jpg)
Lesson 10
C Programming Examples for
Serial Port
![Page 3: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/3.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education3
SCON and SBUF at 0x98 and 0x99 respectively
for serial port communication
• Write into the SBUF transmits the bits
serially
• A read from SBUF receives the bits serially
• SCON controls the transmission and
receiving actions at the serial port
• SCON also holds the status its for TI and RI
• REN control bit means receiver enable serial
bits
![Page 4: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/4.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education4
TI status bit
• = 1 means transmitter interrupt has occurred
and new byte can be written into SBUF for
transmitting next character
• TI has to be set in the interrupt function
• Not auto reset on start of its interrupt
function.
![Page 5: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/5.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education5
RI status bit
• = 1 means receiver interrupt has occurred
and new byte has been received in SBUF
• RI has to be set in the interrupt function
• Not auto reset on start of its interrupt
function.
![Page 6: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/6.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education6
SCON mode bits
• SM0, SM1 and SM2 to specify the mode
![Page 7: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/7.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education7
SCON bit TB8
• TB8 specifies the 8th bit for transmission
after bits 0 to 7 for serial transmission from
SBUF in case of mode 2 or 3
![Page 8: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/8.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education8
TB8 use in multiple ways
• Specify the parity (odd or even) of the
preceding 8 bits
• Used to specify the purpose of the preceding 8
bits, whether they specify the destination
address or data for the destination, whether
specifying destination address or data for the
destination or command or control word for
the destination or data
![Page 9: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/9.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education9
SCON bit RB8
• Specifies the 8th bit received after bits 0 to 7
for serial receive at SBUF in case of mode 2
or 3
• Interrupt function saves the RB8 and the
appropriate use of the received 8 bits is done
at the receiving device or MCU
![Page 10: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/10.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education10
Programming a serial port
• Programming the SCON bits and SBUF
unsigned character
![Page 11: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/11.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education11
C statement to write 0x75 into the SBUF 8-
bits for transmission
• SBUF = 0x75; /* Assign the SBUF transmitter
= 0x75*/
![Page 12: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/12.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education12
C Program to set the baud rate = 9600 in
mode 3
• Use serial transmission using T1 mode 2
• Set the baud rate = 9600 in mode 3 for serial
transmission using T1 mode 2 in 8051 for
specifying baud rate
• Assume 11.0592 MHz Xtal
• Reset SMOD bit = 0
![Page 13: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/13.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education13
C Program
• #include <reg51.h> /* Include header file for the registers and SFRs of 8051. */
• void main (void)
• {
• SMOD =0; /*SMOD in PCON = 0 */
• /* Calculate TH1 = 256 – [(2) SMOD × Xtal frequency/(32 × 12 × Baud rate)] = 256 –[1 ×11.0592 × 1000000 /(32 × 12 × 9600)] = 253 = 0xFD = – 3. */
![Page 14: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/14.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education14
C Program continued
• TH1= – 3; /* Baud rate =9600 */
• TMOD = 0x20; /*Assign TMOD upper four
bits = 0010, means internal timer T1 auto-
reload mode 2 as 8bit timer, no external start-
stop control or internal count inputs.* /
• TR1 = 1; /* Start timer T1*/
• /* remaining codes of main function*/
• }
![Page 15: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/15.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education15
C program for 10 characters
A, B, …. to J at baud 9600 mode 3
• Set the baud rate = 9600 in mode 3 for serial
transmission using T1 mode 2 in 8051 for
specifying baud rate
• Assume 11.0592 MHz Xtal. Reset SMOD bit =
0 and set TB8 =1
• Send 10 characters
![Page 16: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/16.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education16
#include <reg51.h> /* Include header file for the
registers and SFRs of 8051. */
C Statements for Preprocessor Directives
![Page 17: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/17.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education17
void main (void)
{unsigned char setChar [255]; /* Declare array of 255
characters */
int numChar; /* Variable numChar. It is for number of
characters to be transmitted or received */
unsigned character i; /* Temporary variable i. It can be
between 0 and 255*/
IE = 0x00; /* Disable all interrupts*/
SMOD =0; = 0x9F; /*SMOD = 0 */
C Statements for Main
![Page 18: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/18.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education18
• /* Calculate TH1 = 256 – [(2) SMOD × Xtal frequency/(32 × 12 × Baud rate)] = 256 –[1 ×11.0592 × 1000000 /(32 × 12 × 9600)] = 253 = 0xFD = – 3. */
• TH1= – 3; /* Baud rate =9600 */
• TMOD = 0x20; /*Assign TMOD upper four bits = 0010, means internal timer T1 auto-reload mode 2 as 8bit timer, no external start-stop control or internal count inputs.*
C Statements for T1 auto-reload mode 2 as 8-
bit timer
![Page 19: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/19.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education19
• SM0 =1; /* SCON bit 7 = 0 for mode 3 serial
variable baud rate 11T */
• SM1= 1; /* SCON bit 6 = 1 for mode 2*/
• SM2 = 0; /* SCON bit 5 = 0 for mode 2*/
C Statements for Serial Mode 3 Set
![Page 20: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/20.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education20
• REN = 1; /* SCON bit 4 for Enable receiver */
• TB8 =1 ; /* SCON bit 3 for 8th bit after 0th to
7th serial transmitted bits for the character. */
C Statements for Receiver Enable and TB8
![Page 21: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/21.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education21
• TI = 0; /* SCON bit 1 for reset transmitter
interrupt flag. */
• RI =0; /* SCON bit 0 for reset receiver
interrupt flag. */ EA = 1; /*Enable interrupt
service functions.*/
C Statements for Reset TI and RI
![Page 22: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/22.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education22
C Statements for 10 characters
• numChar = 10; /* assume 10 characters to be
transmitted. */
• for (i = 0; i<10; i++) {setChar [i] = 97 + i]; /*
Assign array of 10 characters as A, B, C, D, E,
F, G, H, I and J. ASCII code of A is 97. */
• i = 0; /* Reassign the temporary variable */
• SBUF = setChar [i];; /* Write the character
into SBUF for transmission */
![Page 23: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/23.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education23
• ES = 1; /*Enable interrupt function for serial
transmission and receiver. */
• TR1 = 1; /* Start timer T1*/
• . /* remaining codes of main
function*/
• }
![Page 24: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/24.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education24
Interrupt Function
if ( i < numChar) {i++;
TI = 0; SBUF = setChar [i];} /* reset SCON bit 1
for resetting the transmitter interrupt flag. This
enables next serial interrupt after sending the
second character. */
else {i = 0; TR1 =0; ES =0; } /* Reset array
variable, stop Timer T1, Disable serial port
interrupts*/
} /* End of interrupt function */
![Page 25: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/25.jpg)
Summary
![Page 26: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/26.jpg)
2011 Microcontrollers-... 2nd Ed. Raj Kamal
Pearson Education26
We learnt
• Serial bit baud rate setting program
• SMOD Program
• SCON Program
• Main and Interrupt functions for mode 3 serial
UART communication
![Page 27: Programming in C - Devi Ahilya Vishwavidyalaya , Indore statement to write 0x75 into the SBUF 8-bits for transmission • SBUF = 0x75 ; /* Assign the SBUF transmitter = 0x75*/ 2011](https://reader031.fdocuments.us/reader031/viewer/2022030414/5a9fb37c7f8b9a6c178d166c/html5/thumbnails/27.jpg)
End of Lesson 10 on
C Programming Examples for
Serial Port