communication to other devices - naresuan university 04.pdf · communication to other devices 3...

21
COMMUNICATION TO OTHER DEVICES 1

Upload: others

Post on 31-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

COMMUNICATION TO

OTHER DEVICES

1

Page 2: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Communication to Other Devices

Synchronous

Asynchronous

Handshaking

2

Output

Device

DATA

READY ?

Yes/ No

Handshaking (Output)

Micro

controller

System

I / O

P

O

R

T

Page 3: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Communication to Other Devices

3

Simple input output

Single Handshake I/O

Simple Strobe I/O

Double Handshake I/O

Page 4: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Simple Strobe I/O Example

4

ตวอยาง ใช Port 0 ส ำหรบสงขอมลแบบขนำน และใช P2.0 เปนสญญำณ และก ำหนดให ท ำกำรสงขอมลจ ำนวน 11 Bytes ซงมอยใน Internal data memory

Sender (Microcontroller A)

#include<reg51.h>

char send_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};

unsigned int bytecounter; //Data counter

sbit strobe=P2^0;

/********** BASIC FUNCTION **********/ void delayms(unsigned int count2) { TMOD = (TMOD & 0xF0) | 0x01; /* Set T/C0 Mode */ TR0=1; while (count2) {if (TF0==1) { TR0=0; /* Stop Timer 0*/ TF0=0; count2--; TH0 = 0xD8; /* Load Timer with FFFFH- 10000 */ TL0 = 0xEA; TR0 = 1; /* Start Timer 0*/ }}} /*******************************************/ void main() { strobe = 1; //set STB to high for(bytecounter=1;bytecounter<=11;bytecounter++) { P0 = send_data[bytecounter]; //Send the data strobe = 0; //set STB to low delayms(500); //Wait subroutine strobe = 1; //set STB to high } }// end of main()

Page 5: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Simple Strobe I/O Example

5

Receiver (Microcontroller B)

ตอขอมลทรบเขำมำทำง Port 1 และสญญำณ ทเปนตวบอกวำขอมลเขำมำแลวเขำทขำ P3.2 (INT0) กำรเขยนโปรแกรมเพอรบขอมลเขำมำแลวน ำไปเกบใน Internal data memory สำมำรถท ำได 2 วธ คอกำร Polling หรอ Interrupt

ตวอยำงโปรแกรมทใชวธกำร Polling จะไดเปน

#include<reg51.h>

#include <intrins.h>

char rec_data[11];

unsigned int bytecounter,i; //Data counter

sbit strobe=P3^2;

/************************************/

void dusec (unsigned int count) // usec Delay

{ unsigned int i;

while (count)

{ i = 10; while (i>0) i--;

count--; }

}

void main()

{

bytecounter=1;

P1=0xFF;

while(1)

{if(strobe==0) // Wait until P3.2 = 0

{ if (bytecounter<=11)

{rec_data[bytecounter] = P1;

//receive the data

L1: if (strobe==0){goto L1;}

// Wait for sender to finish sending old

value

bytecounter++;

// Next data address to be stored

}

}//end if

}// end while

}// end of main()

ตวอยำงโปรแกรมทใชวธกำร Interrupt จะไดเปน #include<reg51.h>

#include <intrins.h>

char rec_data[50];

unsigned int bytecounter,i; //Data counter

/*************************************/

void int0_ISR (void) interrupt 0

{

rec_data[bytecounter] = P1; //receive the data

bytecounter++; // Next data address to be

stored

}

/*************************************/

void dusec (unsigned int count) { // mSec Delay

unsigned int i; // Keil CA51 (x2)

while (count) {

i = 10; while (i>0) i--;

count--;

}

}

/************************************/

void main()

{

bytecounter=1;

IT0=1;

// use edged triggering

interrupt

IE=0x81;

// Enable interrupt for

INT 0

P1=0xFF;

while(1)

{

………………………../

/ Other Operations

}

}// end of main()

Page 6: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

6

Mode 1 กบ Mode 2 Baud rate ก ำหนดดวยกำรก ำหนดคำ SMOD ใน PCON ซงไมสำมำรถเขำถง ขอมลระดบบตได ตองอำนคำขนมำแกไขแลวเขยนลงไปใหม (Read-Modify-Write) เชน

PCON |= 0x80

Page 7: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

7

Page 8: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

8

Mode 2 สงขอมลชดละ 11 bits คอ 1 Start bit, 8 Data bits, 1

programmable

data bit จำก TB8 หรอ RB8 และ 1 Stop bit อตรำกำรสงขอมลขนอยกบ SMOD

bitใน PCON

SMOD = 1, Baud Rate = Osc Freq./32

SMOD = 0, Baud Rate = Osc Freq./64

Mode 3 สงขอมลชดละ 11 bits เหมอน Mode 2 แตอตรำกำรสงขอมลสำมำรถก ำหนดไดดวย Timer 1 (ใน Mode 2) หรอ Timer 2 (8052)

Page 9: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

9

Baud

Rate

Crystal

(MHz)

SMOD TH1 Baud

Rate *

9,600 12 1 -7(F9H) 8,923

2,400 12 0 -13(F3H) 2,404

1,200 12 0 -16(E6H) 1,202

19,200 11.059 1 -3(FDH) 19,200

9,600 11.059 0 -3(FDH) 9,600

2,400 11.059 0 -12(F4H) 2,400

1,200 11.059 0 -24(E8H) 1,200

Page 10: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

10

Page 11: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

Example 1.1 (ตอ) เขยนโปรแกรมไดดงน

void start232 (void) {

SCON = 0x52; // ก ำหนดพอรทอนกรมโหมด 1

TMOD = 0x20; // ก ำหนด Timer 1 โหมด 2

TH1 = 0xF3; // ใสคำก ำหนด Baud rate

TL1 =TH1;

TR1 = 1; // เรม Timer 1

}

11

Page 12: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

12

Example จำก Ex 1.1 เขยนฟงกชน สงคำตวอกษรออกทำงพอรทอนกรม

void out_char (send_data) {

L1:if (TI==0) //เชควำ Buffer วำงรยง {goto(L1)}

TI=0;

L2:if (TF==0) // รอจน Timer overflow

{goto(L2)};

SBUF=send_data;// สงขอมลออก TF=0;}

Example จำก Ex 1.1 เขยนฟงกชน รบขอมลตวอกษรจำกทำงพอรทอนกรม

unsigned char in_char(void)

unsigned char aa;

{ L1: if(RI==0) {goto(L1)}; // รอรบขอมล RI=0;

aa=SBUF; // อำนขอมลจำกพอรท return(aa);

}

Page 13: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

13

Page 14: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication 14

EX2 (ตอ)

void start232 (void) {

SCON = 0xD2; // ก ำหนดพอรทอนกรมโหมด - TMOD = 0x20; // ก ำหนด Timer 1 โหมด 2 PCON |= 0x80; // Double baud rate

TH1 = 0xfd; // ใสคำก ำหนด Baud rate

TR1 = 1; // เรม Timer 1

}

void out_char (send_data)

{TB8=P; // ใสคำ parity bit ลงใน TB8

L1:if (TI==0) {goto(L1);} // เชควำ Buffer วำงรยง TI=0;

L2:if(TF=0) {goto(L2);} // รอจน Timer overflow

SBUF= send_data; // สงขอมลออก TF=0; }

unsigned char in_char(void)

unsigned char aa;

L1: if (RI==0) {goto(L1);} // รอรบขอมล RI=0;

aa=SBUF;

return(aa);

}

Page 15: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

Example 3.1 เขยนฟงกชน initializing ก ำหนดคำเรมตนใหพอรทอนกรม เพอใช Serial Interrupt โดยใช

Mode1 สงขอมล 8 bits ดวยอตรำ Baud 19,200 fclock 11.0592MHz

เลอกใช Mode 1, SMOD=1

𝑩𝒂𝒖𝒅 𝒓𝒂𝒕𝒆 =𝟐𝑺𝑴𝑶𝑫 × 𝐎𝐬𝐜𝐢𝐥𝐥𝐚𝐭𝐨𝐫 𝐅𝐫𝐞𝐪.

𝟑𝟐 × 𝟏𝟐 × 𝟐𝟓𝟔 − 𝐓𝐇𝟏

𝟏𝟗𝟐𝟎𝟎 =𝟐𝟏 × 𝟏𝟏. 𝟎𝟓𝟗𝟐𝐌𝐇𝐳

𝟑𝟐 × 𝟏𝟐 × 𝟐𝟓𝟔 − 𝐓𝐇𝟏

𝐓𝐇𝟏 = 𝟐𝟓𝟑 = 𝐅𝐃𝐇

ขนตอนกำรเขยนโปรแกรม

1. เลอก Mode 1 เพอกำรรบสงและ Set bit REN เปน “1” เพอใหรบขอมลได

2. Set bit TI เปน “1” เพอบอกวำ buffer สงขอมลวำง

3. Set bit M1/M0 ใน TMOD เปน “1/0” ให Timer 1 ท ำงำนในโหมด 2, 8 bits auto-reload

4. ก ำหนดให TH1 เปน 0FDH หรอ -3 เพอก ำหนด Baud rate 19,200

5. Set bit TR1 ใน TCON เปน “1” เพอเรมใช Timer 1

15

Page 16: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

Example 3.1(ตอ)

เขยนโปรแกรมไดดงน

void start232 (void) {

SCON = 0x52; // ก ำหนดพอรทอนกรมโหมด 1

TMOD = 0x20; // ก ำหนด Timer 1 โหมด 2

TH1 = -3; // ใสคำก ำหนด Baud rate

TL1 =TH1; PCON |= 0x80; // 19200, speed x 2

TR1 = 1; // เรม Timer 1

}

void startinterupt(void)

{

EA = 1;

ES = 1;

}

16

Page 17: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

Example 3.2 จำกตวอยำง 3.1 เขยนฟงกชน รบสงขอมลตวอกษร

จำกทำงพอรทอนกรมโดยใช Serial Interrupt

กำรรบขอมลกำรรบจะเกดขนโดยอตโนมตเมอเขยน ISR ของ Serial

Interrupt ดงน

void serial_ISR(void) interrupt 4

{

if(RI){

ES = 0;

receive_data=SBUF;

RI = 0;

ES = 1;

}

17

else{

ES = 0;

SBUF=send_data;

TI=0;

ES = 1;

}

}

เมอตองกำรสงขอมลตองเขยนไวในสวน main

program ดงน

send_data=‘&’; // ตวอกษรทตองกำรสง คอ&

TI=1; //Force serial interrupt

Page 18: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

Example 4 กำรรบตวอกษร ASCII code 50 ตว สง 3 ตว

โดยใช Serial port interrupt

#include <reg51.h>

#include <stdio.h>

#include <absacc.h>

void start232 (void);

void serial_ISR(void);

void recieve_ISR(void);

int i=1;

int j=1;

char receive_data=[], senddata

[]={‘a’,’b’,’c’};

/*******************************/

void main(){

start232();

IE=0x90; // Enable serial port

interrupt

while(1) {

………….;

………….;

TI=1; TI=1; TI=1;

………...;

}// end while(1)

}// end void main

18

Page 19: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Serial Communication

Example 4 (ตอ)

void start232 (void) {

SCON = 0x52; /* Mode1, set TI to force 1st

interrupt and send 1st char*/

TMOD |= 0x20; // Timer1, mode2

TH1 = -3; PCON |= 0x80;

/* speed x 2, Crystal = 11.0592MHz,19200*/

TR1 = 1;

RI = 0;

}

void serial_ISR(void) interrupt 4 {

if(RI){

ES = 0;

recieve_ISR ();

RI=0;

ES = 1;

}

else {

ES = 0;

send_ISR();

TI=0;

ES = 1;

}

}

void recieve_ISR (void){

if(i<=50)

{

receive_data[i]=SBUF;

i++;

}

}

void send_ISR (void){

if(j<=3){

SBUF=send_data[j];

j++;

}

}

19

Page 20: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Simple Strobe I/O Example

20

ตวอยาง ใช Port 0.0 ส ำหรบสงขอมลแบบอนกรม และใช P2.0 เปนสญญำณ และก ำหนดให ท ำกำรสงขอมลจ ำนวน 11 Bytes ซงมอยใน Internal data memory

Sender (Microcontroller A)

#include<reg51.h>

char

send_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x

7f,0x6f,0x40};

unsigned int bytecounter; //Data counter

unsigned int bitcounter; //Bit counter

sbit strobe=P2^0;

/********** BASIC FUNCTION

**********/

void delayms(unsigned int count2)

{

TMOD = (TMOD & 0xF0) | 0x01;

/* Set T/C0 Mode */

TR0=1;

while (count2)

{

if (TF0==1)

{

TR0=0; /* Stop Timer 0*/

TF0=0;

count2--;

TH0 = 0xD8;

/* Load Timer with FFFFH- 10000 */

TL0 = 0xEA;

TR0 = 1; /* Start Timer 0*/

}}}

/*******************************************/

void main()

{

strobe=1;

for(bytecounter=1;bytecounter<=11;

bytecounter++)

{

P0 = send_data[bytecounter]; //Prepare to

send

for(bitcounter=1; bitcounter <= 8;

bitcounter++)

{

strobe = 0; //set STB to low

delayms(500); //Wait subroutine

strobe = 1; //set STB to high

//Send 1 bit to P0.0

P0=_crol_(P0,1);

}}}// end of main()

Page 21: COMMUNICATION TO OTHER DEVICES - Naresuan University 04.pdf · Communication to Other Devices 3 Simple input output Single Handshake I/O Simple Strobe I/O Double Handshake I/O

Simple Strobe I/O Example

21

Receiver (Microcontroller B)

โดยกำรตอขอมลทรบเขำมำทำง P1.0 และสญญำณ CLK ทจะเปนตวบอกวำ Bit ขอมลเขำมำแลวเขำทขำ P3.2 (INT0) กำรเขยนโปรแกรมเพอรบขอมลเขำมำแลวน ำไป เกบใน Internal data memory สำมำรถท ำได 2 วธ คอกำร Polling หรอ Interrupt

ตวอยางโปรแกรมทใชวธการ Polling จะไดเปน

#include<reg51.h>

#include <intrins.h>

#include <absacc.h>

char rec_data[50];

unsigned int bytecounter,i; //Data counter

unsigned int bitcounter; //Bit counter

/**********************************/

void dusec (unsigned int count) {

unsigned int i; /

while (count) {

i = 10; while (i>0) i--;

count--;

}}

/***********************************/

void int0_ISR (void) interrupt 0{

if (bytecounter<=11) // Wait until P3.2 = 0

{ while(bitcounter<8)

{P1=_crol_(P1,1);

bitcounter++; }

if (bitcounter==8)

{ rec_data[bytecounter] = P1; //receive

the data

// Wait for sender to finish sending old

value

bytecounter++; // Next data address to be

stored

}}}

/***************************************

*/

void main(){

bytecounter=1;

bitcounter=1;

IT0=1; // use edged triggering interrupt

IE=0x81; // Enable interrupt for INT 0

while(1)

{………………………… // Other

Operations}

}// end of main()

ตวอยางโปรแกรมทใชวธการ Interrupt จะไดเปน #include<reg51.h>

#include <intrins.h>

#include <absacc.h>

char rec_data[50];

unsigned int bytecounter,i;

//Data counter

unsigned int bitcounter; //Bit

counter

/**************************

**********/

void dusec (unsigned int count)

{ //mSec Delay

unsigned int i; //

Keil CA51 (x2)

while (count) {

i = 10; while (i>0) i--;

count--;

}}

/**************************

**********/

void int0_ISR (void) interrupt

0{

if (bytecounter<=11) //

Wait until P3.2 = 0

{ while(bitcounter<8)

{P1=_crol_(P1,1);

bitcounter++; }

if (bitcounter==8)

{ rec_data[bytecounter] = P1;

//receive the data

// Wait for sender to finish sending

old value

bytecounter++; // Next data address

to be stored

}}}

/**********************************

***/

void main(){

bytecounter=1;

bitcounter=1;

IT0=1; // use edged triggering

interrupt

IE=0x81; // Enable interrupt for INT 0

while(1)

{………………………… // Other

Operations}

}// end of main