Example of Asyncronous Serial Comms on a PIC16F877

Similar documents
Chapter 10 Sections 1,2,9,10 Dr. Iyad Jafar

Embedded Systems Programming and Architectures

Serial Communication with PIC16F877A

EET203 MICROCONTROLLER SYSTEMS DESIGN Serial Port Interfacing

Section 21. Addressable USART

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

PIC16C7X 11.0 SYNCHRONOUS SERIAL PORT (SSP) MODULE SSP Module Overview. Applicable Devices

Experiment 7:The USART

ELE492 Embedded System Design

ELCT 912: Advanced Embedded Systems

Serial Communication

ELCT706 MicroLab Session #4 UART Usage for Bluetooth connection PC - PIC

Section 16. Basic Sychronous Serial Port (BSSP)

Kit Contents. Getting Started Kits. Board. Board

ELCT706 MicroLab Session #4 UART Usage for Bluetooth connection PC - PIC

APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller

RS232.C An Interrupt driven Asyncronous Serial Port

Parallel IO. Serial IO. Parallel vs. Serial IO. simplex vs half-duplex vs full-duplex. Wires: Full Duplex. Wires: Simplex, Half-duplex.

Chapter 13. PIC Family Microcontroller

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

Super Awesome Multitasking Microcontroller Interface for Electromechanical Systems (S.A.M.M.I.E.S.) Pinball Table

Interactive Modules for the Intelligent Teddy Bear

IE1206 Embedded Electronics

PIC18FXX2 Registers. Hyperlinked Index

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

PIC16F /40-Pin 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram PDIP. Microcontroller Core Features:

PIC16F87X. 28/40-pin 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram PDIP. Microcontroller Core Features:

The MICROPROCESSOR PRINCIPLES AND APPLICATIONS Lab 7

TB033. Using the PIC16F877 To Develop Code For PIC16CXXX Devices INTRODUCTION. Stan D Souza, Rodger Richey Microchip Technology Inc.

Design, Development & Implementation of a Temperature Sensor using Zigbee Concepts

PIC16F87XA Data Sheet

The University of Texas at Arlington Lecture 21_Review

11.4 THE SERIAL PERIPHERAL INTERFACE (SPI)

PIC16F7X Data Sheet. 28/40-pin, 8-bit CMOS FLASH Microcontrollers Microchip Technology Inc. DS30325B

PIC16F870/ /40-Pin, 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram. Microcontroller Core Features:

Weekly Report: Interactive Wheel of Fortune Week 4 02/014/07-02/22/07 Written by: Yadverinder Singh

PIC18F6310/6410/8310/8410

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

Interrupts and Serial Communication on the PIC18F8520

ECE 354 Introduction to Lab 1. February 5 th, 2003

Contents. PIC Mini Data Sheets

Acronyms and Abbreviations

Embedded System Design

SRF08 Ultra sonic range finder

/*Algorithm: This code display a centrifuge with five variable speed RPM by increaseing */

Using the 8-Bit Parallel Slave Port

UNIVERSITY OF NAIROBI FACULTY OF ENGINEERING DEPARTMENT OF ELECTRICAL AND INFORMATION ENGINEERING

PIC18F2525/2620/4525/4620

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

FULL DUPLEX BIDIRECTIONAL UART COMMUNICATION BETWEEN PIC MICROCONTROLLERS

These 3 registers contain enable, priority,

Timer1 Capture Mode:

Interrupts on PIC18F252 Part 2

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

USER S MANUAL VERSION June, KB1 AT-PS/2 Keyboard Interface Chip

ACCESS SECURITY SYSTEM USING RFID TAG

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

SA818 Programming Manual

TKT-3500 Microcontroller systems

Asynchronous Transmission. Asynchronous Serial Communications & UARTS

DIPLOMA THESIS DETECTION AND PREVENTION OF FAULTY PINS

Hong Kong Institute of Vocational Education Digital Electronics & Microcontroller. 8. Microcontroller

Lego Mindstorms NXT Camera

PIC16C745/ Bit CMOS Microcontrollers with USB. Pin Diagrams. Devices included in this data sheet: Microcontroller Core Features:

PIC16F872 Data Sheet. 28-Pin, 8-Bit CMOS Flash Microcontroller with 10-Bit A/D Microchip Technology Inc. DS30221C


Micro-Controller: PIC16C74 < Part 5: Interrupt >

Speed Control of a DC Motor using Digital Control

Interrupts on PIC18F252 Part 2. Interrupts Programming in C Language

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

Sales: Technical: Fax:

EEE394 Microprocessor and Microcontroller Laboratory Lab #6

PIC16F87/88 Data Sheet

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

Source Codes for DRA818U/DRA818V

Introduction. Embedded system functionality aspects. Processing. Storage. Communication. Transformation of data Implemented using processors

Lego Mindstorms NXT & CMUcam3

PIC18F2585/2680/4585/4680

ME 6405 Introduction to Mechatronics

19.1. Unit 19. Serial Communications

Embedded systems. Exercise session 3. Microcontroller Programming Lab Preparation

Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

Serial Communications

PIC18F87J50 Family Silicon Errata and Data Sheet Clarification. Part Number Device ID (1) Revision ID for Silicon Revision (2) A2 A3 A4

Week1. EEE305 Microcontroller Key Points

GPS Reader. Figure 1: RMC NEMA Tag Definition

Appendix A Acronyms and Abbreviations

CENG-336 Introduction to Embedded Systems Development. Timers

Lab 6 RS-232 Communication The following routines are provided for devices with a single USART peripheral:

Chapter 11: Interrupt On Change

Embedded Systems and Software. Serial Communication

Embedded Systems and Software

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

TKT-3500 Microcontroller systems

PIC16F818/819 Data Sheet

PIC16C63A/65B/73B/74B

Tutorial by Example Issue 1C

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

Transcription:

/***************************************************************************************/ /* Example of Asyncronous Serial Comms on a PIC16F877 */ /* Target: PIC16F877 */ /* Baud: 9600 */ /* Bits: 8 */ /* Parity: none */ /* Stop bits : 1 */ /* Warning: Clock frequency dependant */ /* This example sends a message then */ /* echos lines typed on a terminal. */ /***************************************************************************************/ #pragma CLOCK_FREQ 3579545 // Processor clock frequency in Hz. // Default value for PIC is 4000000 (4MHz) // and for SX 50000000 (50MHz). // This will not affect the comms speed! // if you use a different frequency crystal then change SPBRG // My PORT Configuration #define PortAConfig 0x00 #define PortBConfig 0xf0 #define PortCConfig 0x98 /* SCL & SDA as Inputs */ #define PortDConfig 0x00 #define PortEConfig 0x00 // USART Register bits #define CSCR 7 #define TX9 6 #define TXEN 5 #define SYNC 4 #define BRGH 2 #define TRMT 1 #define TX9D 0 #define SPEN 7 #define RX9 6 #define SREN 5 #define CREN 4 #define ADDEN 3 #define FERR 2 #define OERR 1 #define RX9D 0 #define TRMT_MASK 2 // Masks for PIR1 #define PSPIF_MASK 0x80 #define ADIF_MASK 0x40 #define RCIF_MASK 0x20 #define TXIF_MASK 0x10 // Registers for I2C char SSPSTAT@0x94; // Bank 1 char SSPCON@0x14; // Bank 0 char SSPCON2@0x91; // Bank 1 char SSPBUF@0x13; // I2C Buffer char SSPADD@0x93; // I2C Slave Address register 1

// Bits of SSPSTAT #define SMP 7 #define CKE 6 #define D_A 5 #define P 4 #define S 3 #define R_W 2 #define R_W_MASK 0x04 #define UA 1 #define BF 0 // Bits of SSPCON2 #define GCEN 7 #define ACKSTAT 6 #define ACKDT 5 #define ACKEN 4 #define RCEN 3 #define PEN 2 #define RSEN 1 #define SEN 0 // Bits of PIR1 #define PSPIF 7 #define ADIF 6 #define RCIF 5 #define TXIF 4 #define SSPIF 3 #define SSPIF_MASK 0x08 #define CCP1IF 2 #define TMR2IF 1 #define TMR1IF 0 // Bits of SSPCON #define WCOL 7 #define SSPOV 6 #define SSPEN 5 #define CKP 4 #define SSPM3 3 #define SSPM2 2 #define SSPM1 1 #define SSPM0 0 // Port addresses char PORTC@0x07; char PORTD@0x08; char PORTE@0x09; // USART Registers char TXREG@0x19; char RCREG@0x1a; char TXSTA@0x98; char RCSTA@0x18; char SPBRG@0x99; // Extra Ports on PIC16F877 char TRISC@0x87; char TRISD@0x88; char TRISE@0x89; 2

// Other regs char PIE1@0x8c; char PIE2@0x8d; char PIR1@0x0c; char PIR2@0x0d; char PCON@0x8e; // ADC bits char ADCON0@0x1f; char ADCON1@0x9f; // Function Declarations void Setup(void); void ConfigureComms(void); void SendChar(char); char RxChars(void); // Setup the PIC // Configure the comms // Send a character // Receive a characters when RX Interrupt occurs void SendString( const char *ptr );// Send a const string char MyFlags; char BufferIndex; char RxFifo[20]; // Status flags // Yep! an index to the buffer // Receive data buffer. #define RX_BUFFER_SIZE 20 #define BufferReady 0 #define BufferReadyMask 0x01 // Bit 0 of MyFlags const char *Msg1 ="PIC16F877 RS232 Test"; 3

//----------------------------------------------------------------------------------- // Start of MAIN //----------------------------------------------------------------------------------- void main(void) Setup(); /* Setup the PIC */ BufferIndex = 0; clear_bit( MyFlags, BufferReady ); /* Clear the buffer ready flag */ SendString( Msg1 ); /* Send a message to the terminal */ while( 1 ) if ( MyFlags & BufferReadyMask ) /* ECHO the buffer back to the terminal */ /* After the enter key was pressed */ while( RxFifo[ BufferIndex ]!= 0 ) SendChar( RxFifo[ BufferIndex++ ] ); BufferIndex = 0; clear_bit( MyFlags, BufferReady ); /* Clear the buffer ready flag */ // end while 1 // end of Main() void interrupt(void) if ( ( PIR1 & RCIF_MASK )!= 0 ) // If USART RX Interrupt RxChars(); // Process the received character clear_bit( PIR1, RCIF ); // Clear flag // Return from Interrupt 4

/* Send a const string */ /* ( Null terminated ) */ void SendString(const char *ptr) char i; i = 0; // Check for end of string while( ptr[i]!= 0 ) SendChar( ptr[i++] ); /* setup PIC16F877 options,ports,interrupts */ void Setup(void) INTCON = 0x00; set_bit( INTCON, GIE ); set_bit( INTCON, PEIE ); // Enable Global Interrupts // Enable all Peripheral Interrupts set_bit( STATUS, RP0 ); // Register page 1 // Compiler doesn't detect ram bank switch here! // with OPTION_REG OPTION_REG = 0x0C; // Set Option register // Prescaler = WDT // WDT rate := 1:16 TRISD = PortDConfig; ADCON1 = 0x7f; // Disable ADC TRISA = PortAConfig; TRISB = PortBConfig; TRISC = PortCConfig; TRISE = PortEConfig; clear_bit( STATUS, RP0 ); // Register page 0 PIR1 = 0; ConfigureComms(); /* Configure USART for Asyncronous Comms */ 5

/*******************************************************/ /* Configure USART for communications */ /* Asynchronous mode */ /* 19,200 Baud ( With 3.579545 Mhz Clock ) */ /* 8 data bits ( For other rates see PIC16F8XX Data ) */ /* 2 stop bits */ /* No Parity */ /*******************************************************/ void ConfigureComms(void) set_bit( RCSTA, SPEN ); // Enable Serial port clear_bit( RCSTA, RX9 ); // 8 bit receive mode set_bit( STATUS, RP0 ); // *** Register page 1 *** clear_bit( TXSTA, TX9 ); // 8 bit transmit mode // SPBRG = 0; // SPBRG = 1 ( Set Baud rate 115,200 ) // SPBRG = 5; // SPBRG = 5 ( Set Baud rate 38,400 ) // SPBRG = 22; // SPBRG = 22 ( Set Baud rate 9,600 ) // SPBRG = 11; // SPBRG = 11 ( Set Baud rate 19,200 ) SPBRG = 22; // SPBRG = 22 ( Set Baud rate 9,600 ) //------------------------------------------ // For this value at a given clock rate // see the microchip document //------------------------------------------ set_bit( TXSTA, BRGH ); // RRGH = 1 ( High speed mode ) clear_bit( TXSTA, SYNC ); // Asyncronous mode; set_bit( TXSTA, TXEN ); set_bit( PIE1, RCIE ); // Enable Transmitter // Enable Receive Interrupt clear_bit( STATUS, RP0 ); // *** Register page 0 *** set_bit( RCSTA, CREN ); clear_bit( PIR1, RCIF ); set_bit( INTCON, PEIE ); set_bit( INTCON, GIE ); // Enable continuous receive // Clear Receive Interrupt flag // Enable all Peripheral Interrupts // Enable Global Interrupts 6

/* Send a character over the RS232 Port */ void SendChar(char ch) char TxEmpty; asm bsf STATUS, RP0; // *** Register page 1 *** asm movf TXSTA, W; // Save TXSTA value asm bcf STATUS, RP0; // *** Register page 0 *** asm movwf _TxEmpty_SendChar; // Restore TXSTA value while ( ( TxEmpty & TRMT_MASK ) == 0 ) // Wait for TX Empty asm bsf STATUS, RP0; // *** Register page 1 *** asm movf TXSTA, W; // Save TXSTA value asm bcf STATUS, RP0; // *** Register page 0 *** asm movwf _TxEmpty_SendChar; // Restore TXSTA value asm bcf STATUS, RP0; // *** Register page 0 *** TXREG = ch; // Load the TXREG /* Receive a character over the RS232 Port */ /* Called from Interrupt service routine */ /* Returns the char received */ /* and saves it in the buffer */ char RxChars(void) if ( ( RCSTA & 6 ) == 0 ) // Then if no errors // Process received character // If terminated by a carrage return // or Buffer end reached received */ if ( ( RCREG == 13 ) ( BufferIndex == RX_BUFFER_SIZE ) ) set_bit( MyFlags, BufferReady ); /* Set a flag to indicate line else RxFifo[ BufferIndex ] = 0; /* NULL Terminate the buffer */ RxFifo[ BufferIndex++] = RCREG; // Save the data set_bit( RCSTA, CREN ); // Enable receiver. else // process any errors here // Beware, we are in the Interrupt routine. //... clear_bit( RCSTA, CREN ); // Clear any errors set_bit( RCSTA, CREN ); // Enable receiver. return RCREG; 7