240 likes | 563 Views
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. RS232.
E N D
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.
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.
Example • This example receives data and resends it via RS232
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+.
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
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.
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 of interrupt. See the devices .h file for all valid interrupts for the part
Interrupts • Third: Write the Interrupt Subroutine: This function is executed when the interrupt occurs. The subroutine is written outside the Main loop. The function should be written directly after #Int_xxx The name of subroutine should refer to the action taken.
#Int_xxx • These directives specify the following function is an interrupt function. • #INT_EXT //External interrupt • #INT_RB //Port B any change on B4-B7
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.
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.
#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() • { inti; • 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);}
Cont. code • 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);}}
Other Interrupts • INT_EXT //External interrupt • INT_RDA //RS232 receive data available • INT_TIMERx //Timer x overflow
INT_EXT • 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)
INT_RDA • 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.
Example • The First PIC transmit data to be received by the second PIC. see the figure next slide. • Receive the data using Interrupt RDA.
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); }}
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);}}
INT_TIMERx • 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.
INT_TIMERx • // 20 mhz clock, no prescaler, set timer 0 • // to overflow in 35us • set_timer0(81); // 256-(.000035/(4/20000000))