advanced microcontrollers a practical approach lesson 2 serial communication
DESCRIPTION
Advanced Microcontrollers A practical approach lesson 2 serial communication. Microcontrollers A practical approach. Topics Some about architecture Serial communication. Microcontrollers serial communication A practical approach. Morse code telegraphy - PowerPoint PPT PresentationTRANSCRIPT
Saxion University of Applied Sciences
Advanced MicrocontrollersA practical approach
lesson 2 serial communication
Microcontroller programming
2
Topics Some about architecture Serial communication
MicrocontrollersA practical approach
Microcontroller programming
3
Morse code telegraphy RS-232 (low-speed, implemented by Serial Ports) RS485 Universal Serial Bus (moderate-speed, for
connecting computers to peripherals) FireWire Fibre Channel (high-speed, for connecting
computers to mass storage devices) InfiniBand (very high speed, broadly comparable in
scope to PCI) Serial Attached SCSI Serial ATA PCI Express CAN
Microcontrollers serial communicationA practical approach
Microcontroller programming
4
Half duplex Full duplex 5,6,7,8 databits ASCII (0-127) Stop bit(s) Parity DCE (Data communication equipment) DTE(Data terminal Equipment (PC))
Microcontrollers serial communicationA practical approach
Microcontroller programming
5
(PC))
RS232 connectorA practical approach 9 Pin Connector on a DTE device (PC connection)
Male RS232 DB9
Pin Number Direction of signal:
1 Carrier Detect (CD) (from DCE) Incoming signal from a modem
2 Received Data (RD) Incoming Data from a DCE
3 Transmitted Data (TD) Outgoing Data to a DCE
4 Data Terminal Ready (DTR) Outgoing handshaking signal
5 Signal Ground Common reference voltage
6 Data Set Ready (DSR) Incoming handshaking signal
7 Request To Send (RTS) Outgoing flow control signal
8 Clear To Send (CTS) Incoming flow control signal
9 Ring Indicator (RI) (from DCE) Incoming signal from a modem
Microcontroller programming
6
RS232 voltagesA practical approach
Level Transmitter (V)
Receiver (V)
Space status (0) +5 ... +15 +3 ... +25
Mark status (1) -5 ... -15 -3 ... -25
Not defined - -3 .. -3 …+3 . +3
Microcontroller programming
7
RS232 cablelenghtA practical approach
Baud rate Maximum cabellength(m)
19200 15
9600 150
4800 300
2400 900
Microcontroller programming
8
Topics PIC family Architecture PIC18F2580 Interrupts Serial communication LCD Assembly A very little microcontroller
Serial communiactionA practical approach
Microcontroller programming
9
PIC18 UARTpractical approach • Asynchronous - Auto-wake-up- Auto baud calibration- 13-bit Break character• Synchronous –selectable clock• Synchronous –clock polarity
Microcontroller programming
10
Baud ratepractical approach
Microcontroller programming
11
PIC18F2580 pinout
Microcontroller programming
12
Programming Header Functions
Initialisation…
Main program1. Initialisation (function call)2. Program with function calls in
infinite loop
Microcontroller programming
13
/*********************** pic18f2580 **********************************\| Testprogram_1 MPlab C18-compiler || 11 sept 2012/21 juli 2013 J.S.D. Stokkink |+-----------------------------------------------------------------------------------+| Gebruit van INT0 , TIMER0 , UART , High-iterrupt priority |+-----------------------------------------------------------------------------------+| WERKING: || RB0 via schakelaar naar GND geeft INT0 interrupt text to ser|| ial port TX pin || TIMER0 is ingesteld op ca 1sec || De Xtal frequentie is 20MHz || After InitUART() ,putc(c,stdout) and puts("string") || De op RB2 aangesloten LED zal knipperen met ca 1Hz |\**************************************************************************/
Program header
Microcontroller programming
14
Main Program send serial#include <p18F2580.h>#include <stdio.h> // t.b.v. puts() en putc() functies#pragma config OSC = HS // HS oscillator 20 Mhz#pragma config WDT = OFF // Watchdog Timer disabled#pragma config LVP = OFF // Low Voltage ICSP disabled#pragma config PBADEN = OFF // PortB<4:0> configured as digital I/O// Function-declarations:void InterruptHandlerHigh(void);void InitINT0(void);void InitUART(void);void InitTimer0(void);void EnableHighInterrupts(void);
// Globale variabelen:char ch = '0'; //start with character '0'#pragma codevoid main (void){ TRISC=0; //led's output tx output
InitINT0();InitUART();InitTimer0();EnableHighInterrupts();
// run forever: while(1) { //do nothing}}
Microcontroller programming
15
Iniatalisation RB0#pragma codevoid InitINT0(void){
PORTB = 0; // clear PORT B TRISB = 0x01; // RB0=input; RB1-7: output INTCON2bits.RBPU=0; // pull-ups portB enabled
INTCON2bits.INTEDG0 =1; // interrupt on rising edgeINTCONbits.PEIE = 1; // enable high priority interruptINTCONbits.INT0IE = 1; // enable INT0
}
Microcontroller programming
16
Initialisation UARTvoid InitUART(void){
// setup UART:SPBRGH=0; SPBRG=31; // interne clock Fosc=20MHz ->9600
// Baud p error 0,16%TXSTAbits.SYNC = 0; // Enable asynchronous serial portRCSTAbits.SPEN = 1; // Enable asynchronous serial portTXSTAbits.TXEN = 1; // Enable transmission; alsputs("\rUART initialised\r"); // To Pc OR BLUETOOTH
}
For initialisation timer see lesson 1
Microcontroller programming
17
Initialisation interruptvoid EnableHighInterrupts(void){
RCONbits.IPEN = 1; // enable interrupt priority levelsINTCONbits.GIEH = 1; // enable all high priority iterrupts
}
// High priority interrupt vextor:#pragma code high_vector = 0x08void high_interrupt(void){
_asm goto InterruptHandlerHigh _endasm}
Microcontroller programming
18
Interrupt Service Routine#pragma interrupt InterruptHandlerHighvoid InterruptHandlerHigh(void){
if(INTCONbits.TMR0IF) // check for TMR0 overflow{
INTCONbits.TMR0IF = 0; // clear interrupt flagTMR0H = 72; // reload timerTMR0L = 229; // reload timerPORTCbits.RC2 = !PORTCbits.RC2; // toggle LED on RC2PORTBbits.RB3 = !PORTBbits.RB3; // toggle LED on RC2
ch++; //next character (see ASCII table)putc(ch,stdout); // ASCII-karakter naar PC}if(INTCONbits.INT0IF) // check for INT0 flag {INTCONbits.INT0IF =0; // clear interrupt INT0 (RB0) flag puts("\n"); puts("\rRB0 pressed\r"); // to PC}
}
Microcontroller programming
19
Extra part ISR for recieve if (PIR1bits.RCIF == 1) //check for recieve interrupt { TXSTAbits.TXEN=0; // transmitter off if (RCREG=='a') {PORTCbits.RC0=1; puts(" end \r ");} //LED on if (RCREG=='b') PORTCbits.RC0=0; //read receiverif character = ‘b' LED of TXSTAbits.TXEN=1; // transmitter on PIR1bits.RCIF=0; // interrupt flag off }
Microcontroller programming
20
Assignments lesson 21. Make the send serial example working : a. check the output on a scoop. (explain) RS232 output and 5v output b.Checkthe output on a terminal(via USB
or Bluetooth) (install terminal program on your
phone(BT) or PC 2. Make the recieving program working:3. Connect two boards let them
communicate