Microcontrollers serial communication A practical

advertisement
Advanced Microcontrollers
A practical approach
lesson 2 serial communication
Saxion University of Applied Sciences
Microcontroller programming
Microcontrollers
A practical approach
Topics
 Some about architecture
 Serial communication
2
Microcontroller programming
Microcontrollers serial communication











3
Morse code
telegraphy
A practical
approach
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
Microcontroller programming
Microcontrollers serial communication








4
A practical approach
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))
Microcontroller programming
RS232 connectorA practical approach
 (PC
))
Male RS232 DB9
Pin Number
5
9 Pin Connector on a DTE device (PC connection)
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
RS232 voltagesA practical approach
Level
Receiver
(V)
Space status (0)
+5 ... +15
+3 ... +25
Mark status (1)
-5 ... -15
-3 ... -25
-
-3 .. -3 …+3 . +3
Not defined
6
Transmitter
(V)
Microcontroller programming
RS232 cablelenghtA practical approach
Baud rate
7
Maximum cabellength(m)
19200
15
9600
150
4800
300
2400
900
Microcontroller programming
Serial communiaction
A practical approach
Topics







8
PIC family
Architecture PIC18F2580
Interrupts
Serial communication
LCD
Assembly
A very little microcontroller
Microcontroller programming
PIC18 UARTpractical approach
• Asynchronous
- Auto-wake-up
- Auto baud calibration
- 13-bit Break character
• Synchronous –
selectable clock
• Synchronous –
clock polarity
9
Microcontroller programming
Baud ratepractical approach
10
Microcontroller programming
11
PIC18F2580
pinout
Microcontroller programming
Programming
 Header
 Functions
 Initialisation
…
 Main program
1. Initialisation (function call)
2. Program with function calls in
infinite loop
12
Microcontroller programming
Program header
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
|
\**************************************************************************/
Microcontroller programming
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 code
void main (void)
{ TRISC=0; //led's output tx output
InitINT0();
InitUART();
InitTimer0();
EnableHighInterrupts();
14
// run forever:
while(1)
{
//do nothing}
}
Microcontroller programming
Iniatalisation RB0
#pragma code
void 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 edge
INTCONbits.PEIE = 1; // enable high priority interrupt
INTCONbits.INT0IE = 1; // enable INT0
}
15
Microcontroller programming
Initialisation UART
void InitUART(void)
{
// setup UART:
SPBRGH=0; SPBRG=31;
// interne clock Fosc=20MHz ->9600
// Baud p error 0,16%
TXSTAbits.SYNC = 0; // Enable asynchronous serial port
RCSTAbits.SPEN = 1; // Enable asynchronous serial port
TXSTAbits.TXEN = 1; // Enable transmission; als
puts("\rUART initialised\r"); // To Pc OR BLUETOOTH
}
For initialisation timer see lesson 1
16
Microcontroller programming
Initialisation interrupt
void EnableHighInterrupts(void)
{
RCONbits.IPEN = 1; // enable interrupt priority levels
INTCONbits.GIEH = 1; // enable all high priority iterrupts
}
// High priority interrupt vextor:
#pragma code high_vector = 0x08
void high_interrupt(void)
{
_asm goto InterruptHandlerHigh _endasm
}
17
Microcontroller programming
Interrupt Service Routine
18
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh(void)
{
if(INTCONbits.TMR0IF)
// check for TMR0 overflow
{
INTCONbits.TMR0IF = 0;
// clear interrupt flag
TMR0H = 72;
// reload timer
TMR0L = 229;
// reload timer
PORTCbits.RC2 = !PORTCbits.RC2; // toggle LED on RC2
PORTBbits.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
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
}
19
Microcontroller programming
Assignments lesson 2
1.
20
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
Download