1 / 14

EEE305 Microcontroller Systems

Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at www.eej.ulst.ac.uk My office 5B18, telephone 028 90 366364 My email IJ.McCrum@ulster.ac.uk. EEE305 Microcontroller Systems.

clea
Télécharger la présentation

EEE305 Microcontroller Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 7: Embedded C using PIC microcontrollers Serial i/o with the XC8 Teaching resources are at www.eej.ulst.ac.uk My office 5B18, telephone 028 90 366364 My email IJ.McCrum@ulster.ac.uk EEE305 Microcontroller Systems http://www.eej.ulst.ac.uk/~ian/modules/EEE305

  2. Normal serial transmission of the ASCII character set uses the RS232 PORT found on PCs RS232 is an old interface; it uses +/-12 Volts and therefore needs line drivers and line receivers to convert from the normal 0 and +5 volt logic signals of the microcontroller In the old days “bit” timing was imprecise, RS232 sends each character with its own timing synchronisation, the intercharacter delay is an asynchronous one. All timing is done from the leading (falling) edge of the start bit and is valid for one character. This means that the line has to idle in a ‘1’ state and the start bit has to be a ‘0’ Two characters could be transmitted back to back, we have to finish each with a ‘1’ stop bit So to send 8 data bits requires sending at least 10 bits. The logic is upside down, a ‘0’ is +12 Volts and a ‘1’ is -12 volts, the line driver/receivers invert There are standard bit rates; sending one change of state for each data bit this is the BAUD rate e.G 1200 Baud, 2400 Baud, 4800 baud, 9600 baud etc., There are variations; you can send 7 or 8 data bits, one or two stop bits, and an extra bit just before the stop bits known as the parity bit. The format 8N1 is typical

  3. RS232 has standard connectors • The transmit and receive wires are uni-directional, hence we have DTE and DCE when data TERMINAL equipment is wired to a data COMMUNICATION equipment such as a modem. • The PC is given a DTE port – male pins, transmitting on PIN 3, labelled TxD • A receiving device (using a 1:1 cable) should have a female 9 pin D-type connector, it receives on PIN 3, still (confusingly) labelled TxD – take care! Use Hyperterminal on the PC if XP Use REALTERM if windows 7 Use TERMINAL.EXE if windows 8 (by Br@y++) Use minicom in Linux

  4. The serial port on the PIC • Is a Superset of an RS232 port known as the Addressable Universal Synchronous Asynchronous Receiver Transmitter (USART) • Some texts just call it a Serial Communications Interface or SCI ( there is also a SPI and IIC interface – these are different) • The USART has three important registers TXSTA, RCSTA, SPBRG as well as data registers and interrupt registers

  5. So a good guess to configure this might be; CSRC = x, TX9=0 TXEN = 1,SYNC=0 BRGH= ? Probably ‘1’ We read TRMT, put a ‘1’ in it to make it empty TX9D = X or 0 i.e 0b00100110

  6. Guess; SPEN = 1, RX9 = 0 CREN = 1, ADDEN=0 FERR and OERR are read to see if errors have occurred RX9D is read if using 9 bit mode (stick to 8 bits!)

  7. Baud rate Generator • Depends on PIC crystal, and TXSTA<2> (BRGH) • But there are lookup tables on page 98 of the datasheet for common baud rates and clock frequencies, for both BRGH= 0 or 1

  8. Use the lookup tables in Page 98 of datasheet

  9. Understanding the USART

  10. void serial_setup(void) { /* * Comms setup: */ #define BAUD 19200 #define DIVIDER 25 // lookup from table – 4MHz and we want 9600 #define HIGH_SPEED 1 SPBRG=DIVIDER; BRGH=HIGH_SPEED; //data rate for sending SYNC=0; //asynchronous SPEN=1; //enable serial port pins CREN=1; //enable reception SREN=0; //no effect TXIE=0; //disable tx interrupts RCIE=0; //disable rx interrupts TX9=0; //8-bit transmission RX9=0; //8-bit reception TXEN=0; //reset transmitter TXEN=1; //enable the transmitter }

  11. //writes a character to the serial port void putch(unsigned char c) { while(!TXIF) //set when register is empty { clear_usart_errors_inline; CLRWDT(); } TXREG=c; // send data to USART DelayUs(60); } unsigned char dummy; #define clear_usart_errors_inline \ if (OERR) \ { \ TXEN=0; \ TXEN=1; \ CREN=0; \ CREN=1; \ } \ if (FERR) \ { \ dummy=RCREG; \ TXEN=0; \ TXEN=1; \ } //gets a character from the serial port without timeout unsigned char getch(void) { while(!RCIF) { CLRWDT(); clear_usart_errors_inline; } return RCREG; } Most examples omit the reference to errors, it is only needed if running comms for more than 24 hours and an interruption occurs, such as a source of RS232 powering off half way through a transfer.

  12. The SPI and IIC peripherals

More Related