rs232 #use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7 baud=x; set baud rate to x units bits/sec ...

24
RS232 #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7 BAUD=x; Set baud rate to x units bits/sec XMIT=pin ; Set transmit pin RCV=pin; Set receive pin printf("%u",value); the printf command is used to write data to serial port.

Upload: nelson-morgan

Post on 12-Jan-2016

362 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

RS232

#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7

BAUD=x; Set baud rate to x units bits/sec XMIT=pin ; Set transmit pin RCV=pin; Set receive pin printf("%u",value); the printf command is

used to write data to serial port.

Page 2: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

RS232

RS232 receive data available, at the receiver pin , for example for PIC16F877 at pin RC7.

value = getc(), value = fgetc(stream), value=getch(), value=getchar()

This function waits for a character to come in over the RS232 RCV pin and returns the character.

Page 3: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Example

This example receives data and resends it via RS232

Page 4: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin
Page 5: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Continue ADC

The A/D converter module has the following features:

The converter generates a 10-bit binary result (or 8-bit) using the method of successive approximation and stores the conversion results into the ADC registers (ADRESL and ADRESH);

The A/D converter converts an analog input signal into a 10-bit binary number (or 8-bit);

The minimum resolution or quality of conversion may be adjusted to various needs by selecting voltage references Vref- and Vref+.

Page 6: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Continue ADC

Page 7: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

To select the Vref- and Vref+: Choose Constants used in

SETUP_ADC_PORTS(): Example:(AN0_VREF_VREF) // A0 VRefh=A3 VRefl=A2(AN0_AN1_AN2_AN4_VSS_VREF)// A0 A1 A2 A5

VRefh=A3

Page 8: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Continue ADC

To select the number of bits which read_adc() returns use:

#Device ADC=10 ADC=x Where x is the number of bits

read_adc() should return If we use PIC16f877 x could be 8 or 10

Bits.

Page 9: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Interrupts

Three simple steps: First: Enable Interrupts Global:enable_interrupts(GLOBAL);//tells the PIC that we are going to use interrupt Second: Enable the Specific type of Interrupt

you want to use:enable_interrupts(INT_xxx);//specify the type ofinterrupt.See the devices .h file for all valid interrupts for

the part

Page 10: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Interrupts10

Third: Write the Interrupt Subroutine:This function is executed when the interruptoccurs.The subroutine is written outside the Main

loop.The function should be written directly after

#Int_xxxThe name of subroutine should refer to theaction taken.

Page 11: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

#Int_xxx11

These directives specify the following function is an interrupt function.

#INT_EXT //External interrupt #INT_RB //Port B any change on B4-B7

Page 12: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Example Using INT_RB

The following example shows how to use INT_RB.

The Microcontroller is reading analog signal from analog channel one, but if a change occurred at pin RB4 (Low to High or High to Low) an Interrupt occurs and the microcontroller is programmed to light a LED at Pin RB2 and a Motor is on at pins RB0, RB1.

Page 13: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

13

RA0/AN02

RA1/AN13

RA2/AN2/VREF-4

RA4/T0CKI6

RA5/AN4/SS7

RE0/AN5/RD8

RE1/AN6/WR9

RE2/AN7/CS10

OSC1/CLKIN13

OSC2/CLKOUT14

RC1/T1OSI/CCP216

RC2/CCP117

RC3/SCK/SCL18

RD0/PSP019

RD1/PSP120

RB7/PGD40

RB6/PGC39

RB538

RB437

RB3/PGM36

RB235

RB134

RB0/INT33

RD7/PSP730

RD6/PSP629

RD5/PSP528

RD4/PSP427

RD3/PSP322

RD2/PSP221

RC7/RX/DT26

RC6/TX/CK25

RC5/SDO24

RC4/SDI/SDA23

RA3/AN3/VREF+5

RC0/T1OSO/T1CKI15

MCLR/Vpp/THV1

U1

PIC16F877

RXD

RTS

TXD

CTS

R210k

U1(RA1/AN1)

D2LED-RED

R3330

Note: Practically the motor is not simply connected as shown. (it needs a driver circuit between MC and

the motor it self).

Practically Don't

forget to connect the

oscillator and power.

Page 14: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

14

#include <16f877.h> #fuses hs,nowdt #use delay (clock=20000000) #use rs232 (baud=9600,xmit=pin_c6,rcv=pin_c7) #int_Rb //Interrupt Subroutine (Motor on Clock Wise

and LED On/Off five times) Rb_isr() { int i; output_high(pin_b0); output_low(pin_b1); for ( i=1;i<=5;i++) {output_high(pin_b2); delay_ms(500); output_low(pin_b2); delay_ms(500);} output_low(pin_b0); output_low(pin_b1);}

Page 15: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Cont. code15

void main() {int value; enable_interrupts(INT_Rb); enable_interrupts(GLOBAL); setup_adc( ADC_CLOCK_INTERNAL ); setup_adc_ports( ALL_ANALOG );

while (1) { set_adc_channel(1); value= read_adc(); printf("A/D value = %u\n\r",value);}}

Page 16: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Other Interrupts16

INT_EXT //External interrupt INT_RDA //RS232 receive data

available INT_TIMERx //Timer x overflow

Page 17: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

INT_EXT17

Change at pin RB0 Also requires specifying the edge change using:

EXT_INT_EDGE( ) Determines when the external interrupt is acted

upon. The edge may be L_TO_H or H_TO_L to specify the rising or falling edge

Example: ext_int_edge( H_TO_L ); // Sets up EXT This is written after enabling the interrupts (In

the main loop)

Page 18: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

INT_RDA18

Rather than waiting serial data to be received from the Rx pin, give this job to a specialist which is INT_RDA.

If data is available at the receiver pin an Interrupt occurs.

Page 19: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Example

The First PIC transmit data to be received by the second PIC. see the figure next slide.

Receive the data using Interrupt RDA.

Page 20: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

RA0/AN02

RA1/AN13

RA2/AN2/VREF-/CVREF4

RA4/T0CKI/C1OUT6

RA5/AN4/SS/C2OUT7

RE0/AN5/RD8

RE1/AN6/WR9

RE2/AN7/CS10

OSC1/CLKIN13

OSC2/CLKOUT14

RC1/T1OSI/CCP216

RC2/CCP117

RC3/SCK/SCL18

RD0/PSP019

RD1/PSP120

RB7/PGD40

RB6/PGC39

RB538

RB437

RB3/PGM36

RB235

RB134

RB0/INT33

RD7/PSP730

RD6/PSP629

RD5/PSP528

RD4/PSP427

RD3/PSP322

RD2/PSP221

RC7/RX/DT26

RC6/TX/CK25

RC5/SDO24

RC4/SDI/SDA23

RA3/AN3/VREF+5

RC0/T1OSO/T1CKI15

MCLR/Vpp/THV1

U1

PIC16F877A

X1

CRYSTAL

C122p

C222p

RA0/AN02

RA1/AN13

RA2/AN2/VREF-/CVREF4

RA4/T0CKI/C1OUT6

RA5/AN4/SS/C2OUT7

RE0/AN5/RD8

RE1/AN6/WR9

RE2/AN7/CS10

OSC1/CLKIN13

OSC2/CLKOUT14

RC1/T1OSI/CCP216

RC2/CCP117

RC3/SCK/SCL18

RD0/PSP019

RD1/PSP120

RB7/PGD40

RB6/PGC39

RB538

RB437

RB3/PGM36

RB235

RB134

RB0/INT33

RD7/PSP730

RD6/PSP629

RD5/PSP528

RD4/PSP427

RD3/PSP322

RD2/PSP221

RC7/RX/DT26

RC6/TX/CK25

RC5/SDO24

RC4/SDI/SDA23

RA3/AN3/VREF+5

RC0/T1OSO/T1CKI15

MCLR/Vpp/THV1

U2

PIC16F877A

R110k

R210k

RXD

RTS

TXD

CTS

D7

14D6

13D5

12D4

11D3

10D2

9D1

8D0

7

E6

RW

5RS

4

VSS

1

VDD

2

VEE

3

LCD1LM016L

50%

RV2

1k

X2

CRYSTAL

C422p

C322p

Page 21: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Transmitter Code

#include <16f877a.h>#fuses xt,nowdt#use delay(clock=4000000)#users232(baud=9600,xmit=pin_c6,rcv=pin_c7)void main(){while(1){printf("HIJJAWI ");delay_ms(500);}}

Page 22: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

Receiver Code#include<16f877a.h>

#fuses xt,nowdt

#use delay(clock=4000000)

#include<lcd.c>

#use rs232(baud=9600,xmit=PIN_c6,rcv=PIN_c7)

char Rx[50];

int i=0;

#int_RDA

RDA_isr()

{Rx[i++]=getch();}

void main()

{enable_interrupts(INT_RDA);

enable_interrupts(GLOBAL);

lcd_init();

while(1)

{lcd_putc('\f');

lcd_gotoxy(1,1);

printf(lcd_putc,"%s",Rx);}}

Page 23: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

INT_TIMERx

23

To deal with timers you must know the following simple commands:

value=get_timer0() //Returns the count value of a real time clock/counter. RTCC and Timer0 are the same. All timers count up. When a timer reaches the maximum value it will flip over to 0 and continue counting (254, 255, 0, 1, 2...).

set_timer0(value)// Sets the count value of a real time clock/counter.

Page 24: RS232  #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7  BAUD=x; Set baud rate to x units bits/sec  XMIT=pin ; Set transmit pin  RCV=pin; Set receive pin

INT_TIMERx

// 20 mhz clock, no prescaler, set timer 0 // to overflow in 35us set_timer0(81);     //

256-(.000035/(4/20000000))