Sunteți pe pagina 1din 16

Introduction to PIC Microcontroller Programming & Interfacing

August 19-21, 2010


Franz Duran

21-Aug-2010

Paranz

Serial Communication
August 21, 2010 DAY 3 Afternoon Session
21-Aug-2010 Paranz 2

OVERVIEW
Basics of serial communication PIC UART architecture Programming exercises
TX and RX characters Interrupts FIFO buffer SMS application
E-gizmo GSM module
21-Aug-2010 Paranz 3

BASICS OF SERIAL COM.


Bits are transmitted one at time, in series Advantages:
2 wires (TX and RX) More immune to noise, inter-conductor crosstalk
Longer cables Higher clock speed
21-Aug-2010 Paranz 4

BASICS OF SERIAL COM.


RS232 Serial Comm.
1960s technology DTE DCE connection

21-Aug-2010

Paranz

PIC16F SERIAL COM.


Interface to PC

21-Aug-2010

Paranz

MAX232
- RS232 Line Driver Chip - voltage double & voltage inverter

21-Aug-2010

Paranz

MAX232
to PC RX +10v from MCU TX +5v 0v -10v

to MCU RX +5v 0v

from PC TX +10v

-10v
21-Aug-2010 Paranz 8

RS232 Signal

21-Aug-2010

Paranz

DB9 Connector

21-Aug-2010

Paranz

10

PIC USART Module


Universal Syncronous/Asynchronous Receiver & Transmitter (USART) Modes
Asynchronous mode
used in RS232 serial communication

Synchronous mode
specialized applications
21-Aug-2010 Paranz 11

PIC USART SFRs


TXSTA USART TX Status & Control Reg. RXSTA USART RX Status & Control Reg. TXREG USART TX Register RCREG USART RX Register SPBRG Serial Port Baud Rate Register INTCON Interrupt Control Register PIE1 Peripheral Interrupt Enable Reg. 1 PIR1 Peripheral Interrupt Flag Reg. 1
Paranz 12

21-Aug-2010

PIC UART: TX Block Diagram

21-Aug-2010

Paranz

13

TXSTA Register
UART Transmit Status and Control 0 Asynchronous mode Not used if TX9=0 register USART UART

Dont care in Asyn. mode


21-Aug-2010

0 8-bit data

1 TSR is empty 0 TSR is active 1 - Enable TX 1 high speed Baud Rate 0 low speed Baud Rate
Paranz 14

PIC UART: Baud Rate


SPBRG register
Baud Rate = FOSC 64 ( SPBRG + 1)
FOSC 16 (SPBRG + 1)
BRGH = 0 (Low Speed)

Baud Rate =

BRGH = 1 (High Speed)

21-Aug-2010

Paranz

15

PIC UART: Baud Rate


Example SPBRG calculation
Baud Rate = 9600 bits per second FOSC = 20MHz FOSC SPBRG = -1 BRGH=1 (16 Baud Rate ) SPBRG = 129.21 SPBRG = 129 Actual Baud Rate = 9615 bps (0.15% error)
21-Aug-2010 Paranz 16

PIC UART: Baud Rate

21-Aug-2010

Paranz

17

PIC UART: Baud Rate

21-Aug-2010

Paranz

18

PIC UART: Initialization


void UARTInit(void) { BRGH = 1; SPBRG = 129; SYNC = 0; SPEN = 1; TXEN = 1; CREN = 0; TX9 = 0; RX9 = 0; RCIE = 0; TXIE = 0; PEIE = 0; GIE = 0; }
21-Aug-2010 Paranz 19

//High-speed //Baud Rate = 9600 bps at 20 Mhz //Set UART in Asynchronous mode. //Enable serial port. //Enable Transmit. //Disable Receive. //8-bit data

//Disable //Disable //Disable //Disable

Rx interrupt. Tx interrupt. peripheral interrupts. all interrupts.

PIC UART: TX a Single Char.


void UARTPutch(unsigned { while(!TXIF) continue; TXREG = byte; } void main() { ... UARTPutch(A); } char byte) //Wait while UART transmits // the previous character. //Transmit the character.

21-Aug-2010

Paranz

20

PIC UART: TX Block Diagram


1 0 0
01000001 01000001

TXREG=0x41;

1 0 129 1 0 1

BRGH=1 SYNC=0 PEIE=0 GIE=0

21-Aug-2010

Paranz

21

HYPERTERMINAL

21-Aug-2010

Paranz

22

HYPERTERMINAL

21-Aug-2010

Paranz

23

PIC UART: TX a String of Chars.


void UARTPutch(unsigned char byte) { while(!TXIF) //Wait while UART transmits continue; // the previous character. TXREG = byte; //Transmit the character. } void UARTPuts(const byte *s) { while(*s) //While end of string is not reached, { UARTPutch(*s++); //send next character. } }

21-Aug-2010

Paranz

24

PIC UART: TX a String of Chars.


void main() { ... UARTPutch(A); UARTPuts(I love E-lab!); ... }

21-Aug-2010

Paranz

25

PIC UART: TX a String of Char.


void UARTPuts(const byte *s) { while(*s!=0x00) { UARTPutch(*s); s++; } }

21-Aug-2010

Paranz

26

PIC UART: RX Block Diagram

21-Aug-2010

Paranz

27

RCSTA Register
UART Receive Status & Control Reg.

21-Aug-2010

Paranz

28

PIC UART: Initialization


void UARTInit(void) { BRGH = 1; //High-speed SPBRG = 129; //Baud Rate = 9600 bps at 20 Mhz SYNC = 0; //Set UART in Asynchronous mode. SPEN = 1; //Enable serial port. TXEN = 1; //Enable Transmit. CREN = 0; //Disable Receive. TX9 = 0; //8-bit data RX9 = 0; RCIE = 0; //Disable Rx interrupt. TXIE = 0; //Disable Tx interrupt. PEIE = 0; //Disable peripheral interrupts. GIE = 0; //Disable all interrupts. }
21-Aug-2010 Paranz 29

PIC UART: RX a char


char UARTGetch(void) { char c; if(OERR) { CREN = 0; CREN = 1; } while(!RCIF) continue; c = RCREG; return c; }
21-Aug-2010 Paranz 30

//Check if there is a RX error //Clear //Re-enable

//Wait while no character is received. //Get character //and return it.

10

PIC UART: RX a char


improved getch()
char UARTTimedGetch(void) { unsigned char timeout = 0; char timeout_error = FALSE; //Wait while no character is received and delay timer (timeout) hasn't elapsed while( !UARTKbhit() && (++timeout<10) ) // This is 10 millisecond delay, DelayMs(1); //edit here to suit application if( UARTKbhit() ) //If character is present, { return RCREG; //retrieve from RX register } else { timeout_error = TRUE; //Else, return(0); //return NULL character } } 21-Aug-2010 Paranz 31

PIC UART: RX a char


Exercises:
Send the ff. strings from Hyperterminal to PC to toggle LEDs
LED1 ON LED1 OFF LED2 ON LED2 OFF

21-Aug-2010

Paranz

32

PIC UART: Interrupt


Interrupt-driven FIFO buffer

21-Aug-2010

Paranz

33

11

PIC UART: Interrupt

21-Aug-2010

Paranz

34

21-Aug-2010

Paranz

35

E-gizmo GSM MODULE

21-Aug-2010

Paranz

36

12

E-gizmo GSM MODULE

21-Aug-2010

Paranz

37

E-gizmo GSM MODULE


ATE0 OK AT+CMGF=1\r\n OK <Disable Echo> <Text Mode>

+CMTI: "SM",13 <SMS is received> AT+CMGR=13 <Read SMS> +CMGR: "REC UNREAD","+639277949241",,"10/07/17,11:28:40+32" Led2 on <SMS> +CMTI: "SM",14 <SMS is received> AT+CMGR=14 <Read SMS> +CMGR: "REC UNREAD","+639277949241",,"10/07/17,11:32:48+32" Led2 off <SMS>

21-Aug-2010

Paranz

38

E-gizmo GSM MODULE


uart.h & uart.c
UARTInit(), UARTPuts(), UARTPutch(), UARTGetch(), UARTKBhit()

delay.h & delay.c


DelayMs(), DelayUs()

main.c
UARTGetChars() DoGSMTask() ReadSMS() is_equal()
21-Aug-2010 Paranz 39

13

E-gizmo GSM MODULE


void main() { .. .. UARTInit(); UARTPuts("ATE0\r\n"); DelayMs(100); UARTPuts("ATE0\r\n"); DelayMs(100); UARTPuts("AT+CMGF=1\r\n"); DelayMs(100); while(1) { UARTGetChar(); DoGSMTask(); } }
21-Aug-2010 Paranz 40

//Disable echo //Disable echo //TEXT Mode

//Get characters from UART //and do something about it.

E-gizmo GSM MODULE


void UARTGetChar(void) { unsigned char uart_rx_byte; uart_rx_byte = UARTGetch(); //Get character if(uart_rx_byte) { if (uart_rx_byte=='\r' || uart_rx_byte=='\n') { if (uart_rx_count) //If characters are in RX buffer { uart_data_rcv_flag = TRUE; //set flag. return; } } else if (uart_rx_count<UART_RX_BUF_SIZE) { uart_rx_buffer[uart_rx_count] = uart_rx_byte; ++uart_rx_count; } } return; 21-Aug-2010 Paranz 41 }

E-gizmo GSM MODULE


void DoGSMTask(void) { if(uart_data_rcv_flag==TRUE) { if(sms_flag == TRUE) { if(is_equal("LED1 ON",7)) { LED1 = ON; } else if(is_equal("LED1 OFF",8)) { LED1 = OFF; } ...// more codes here ... ... sms_flag = FALSE; uart_data_rcv_flag = FALSE; uart_rx_count = 0; return; ...// more codes ...// here 21-Aug-2010 Paranz

42

14

EGBT9830 Module
E-gizmo Bluetooth module National Semiconductor LMX9830 Simply Blue BT module chip 3.3V supply 9600 bps (preset)
21-Aug-2010 Paranz 43

EGBT9830 Module

21-Aug-2010

Paranz

44

EGBT9830 Module

21-Aug-2010

Paranz

45

15

LMX9830 Command
Communication interface
Command/Event structure Packets

21-Aug-2010

Paranz

46

LMX9830 Command

21-Aug-2010

Paranz

47

16

S-ar putea să vă placă și