Presentation is loading. Please wait.

Presentation is loading. Please wait.

Saxion University of Applied Sciences Advanced Microcontrollers A practical approach lesson 2 serial communication.

Similar presentations


Presentation on theme: "Saxion University of Applied Sciences Advanced Microcontrollers A practical approach lesson 2 serial communication."— Presentation transcript:

1 Saxion University of Applied Sciences Advanced Microcontrollers A practical approach lesson 2 serial communication

2 Microcontroller programming 2 Topics  Some about architecture  Serial communication Microcontrollers A practical approach

3 Microcontroller programming 3  Morse code telegraphy Morse codetelegraphy  RS-232 (low-speed, implemented by Serial Ports) RS-232Serial Ports  RS485 RS485  Universal Serial Bus (moderate-speed, for connecting computers to peripherals) Universal Serial Bus  FireWire FireWire  Fibre Channel (high-speed, for connecting computers to mass storage devices) Fibre Channel  InfiniBand (very high speed, broadly comparable in scope to PCI) InfiniBandPCI  Serial Attached SCSI Serial Attached SCSI  Serial ATA Serial ATA  PCI Express PCI Express  CAN Microcontrollers serial communication A practical approach

4 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 communication A practical approach

5 Microcontroller programming 5  (PC )) RS232 connector A practical approach 9 Pin Connector on a DTE device (PC connection ) Male RS232 DB9 Pin Number Direction of signal : 1Carrier 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

6 Microcontroller programming 6 RS232 voltages A practical approach LevelTransmitter (V) Receiver (V) Space status (0) Mark status (1) Not defined …+3. +3

7 Microcontroller programming 7 RS232 cablelenght A practical approach Baud rateMaximum cabellength(m)

8 Microcontroller programming 8 Topics  PIC family  Architecture PIC18F2580  Interrupts  Serial communication  LCD  Assembly  A very little microcontroller Serial communiaction A practical approach

9 Microcontroller programming 9 PIC18 UART practical approach Asynchronous - Auto-wake-up - Auto baud calibration - 13-bit Break character Synchronous – selectable clock Synchronous – clock polarity

10 Microcontroller programming 10 Baud rate practical approach

11 Microcontroller programming 11 PIC18F2580 pinout

12 Microcontroller programming 12 Programming  Header  Functions  Initialisation …  Main program 1.Initialisation (function call) 2.Program with function calls in infinite loop

13 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

14 Microcontroller programming 14 Main Program send serial #include #include // 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 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(); // run forever: while(1) { //do nothing} }

15 Microcontroller programming 15 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 }

16 Microcontroller programming 16 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

17 Microcontroller programming 17 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) { _asmgoto InterruptHandlerHigh_endasm }

18 Microcontroller programming 18 Interrupt Service Routine #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 }

19 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 }

20 Microcontroller programming 20 Assignments lesson 2 1.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


Download ppt "Saxion University of Applied Sciences Advanced Microcontrollers A practical approach lesson 2 serial communication."

Similar presentations


Ads by Google