410 likes | 502 Views
EEE447 Introduction to Microprocessors. Week x. Interfacing Concepts. How the microcontroller communicates with the world ? The procedures of data exchange ? User interfaces : Keypad , LCD, 7-segment display , switches , etc .
E N D
InterfacingConcepts • How themicrocontrollercommunicateswiththeworld? Theprocedures of data exchange? • User interfaces: Keypad, LCD, 7-segment display, switches, etc. • Machine tomachineinterfaces: rotaryencoders, ADCs, memorymodules, peripheralmicrocontrollers,modemsetc. • Basic Input/Outputinterfacing • Theprocess of readinginputsignalsandsendingoutputsignals is calledinput/output (I/O) • Theprocessortransfersthe data toandfromtheperipheralsusingspecialhardwaresandprotocols.
General I/O Registers • TRIS • PORT • ADC Registers • ADRESH • ADRESL • ADCON0 • ADCON1
Keypad/7-segment display • Keypad • Matrix Keypad • GND output keypad • Serial output keypad • Dipswitches • 7-Segment Display (Example…)
Example: Drive 4 seven segment. (a four digit number can be written) #include <pic.h> #include "delay.h" __CONFIG (LVPDIS & HS & WDTDIS & BORDIS); #define _XTAL_FREQ 20000000 //seven segment LEDS static bit seg_a @((unsigned)&PORTC*8+3);// --a static bit seg_b @((unsigned)&PORTA*8+0);// f| |b static bit seg_c @((unsigned)&PORTC*8+6);// -- ---->g static bit seg_d @((unsigned)&PORTC*8+4);// e| |c static bit seg_e @((unsigned)&PORTC*8+5);// --d static bit seg_f @((unsigned)&PORTB*8+5); static bit seg_g @((unsigned)&PORTC*8+7);
static bit seg_0 @((unsigned)&PORTA*8+3); static bit seg_1 @((unsigned)&PORTA*8+5); static bit seg_2 @((unsigned)&PORTC*8+0); static bit seg_3 @((unsigned)&PORTC*8+1); void init() { ADCON1 = 0x07; TRISB = 0b11001110; TRISC = 0x00; TRISA = 0x00; }
void write2sevensegment(char segment, char number) { switch(number) { case 0: seg_a = 1; seg_b = 1; seg_c = 1; seg_d = 1; seg_e = 1; seg_f = 1; seg_g = 0; break; case 1: seg_a = 0; seg_b = 1; seg_c = 1;
seg_c = 1; seg_d = 1; seg_e = 0; seg_f = 0; seg_g = 1; break; case 4: seg_a = 0; seg_b = 1; seg_c = 1; seg_d = 0; seg_e = 0; seg_f = 1; seg_g = 1; break; case 5: seg_a = 1; seg_d = 0; seg_e = 0; seg_f = 0; seg_g = 0; break; case 2: seg_a = 1; seg_b = 1; seg_c = 0; seg_d = 1; seg_e = 1; seg_f= 0; seg_g = 1; break; case 3: seg_a = 1; seg_b = 1;
case 7: seg_a = 1; seg_b = 1; seg_c = 1; seg_d = 0; seg_e = 0; seg_f = 0; seg_g = 0; break; case 8: seg_a = 1; seg_b = 1; seg_c = 1; seg_d = 1; seg_e = 1; seg_f = 1; seg_g = 1; break; seg_b = 0; seg_c = 1; seg_d = 1; seg_e = 0; seg_f = 1; seg_g = 1; break; case 6: seg_a = 1; seg_b = 0; seg_c = 1; seg_d = 1; seg_e = 1; seg_f = 1; seg_g = 1; break;
case 9: seg_a = 1; seg_b = 1; seg_c = 1; seg_d = 1; seg_e = 0; seg_f = 1; seg_g = 1; break; seg_2 = 0; seg_3 = 0; break; case 1: seg_0 = 0; seg_1 = 1; seg_2 = 0; seg_3 = 0; break; case 2: seg_0 = 0; seg_1 = 0; seg_2 = 1; seg_3 = 0; break; default: break; } switch(segment) { case 0: seg_0 = 1; seg_1 = 0;
case 3: seg_0 = 0; seg_1 = 0; seg_2 = 0; seg_3 = 1; break; default: break; } } void main() { while(1){ int temp_int=2953;
sayi = temp_int%10; seg = 0; write2sevensegment(seg,sayi); DelayMs(1); temp_int -= sayi; temp_int = temp_int/10; sayi = temp_int%10; seg = 1; write2sevensegment(seg,sayi); DelayMs(1); temp_int -= sayi; temp_int = temp_int/10;
sayi = temp_int%10; seg = 2; write2sevensegment(seg,sayi); DelayMs(1); temp_int -= sayi; temp_int = temp_int/10; sayi = temp_int%10; seg = 3; write2sevensegment(seg,sayi); DelayMs(1); temp_int -= sayi; temp_int = temp_int/10; } }
PWM • What is PWM signal? • A square wave form with two parameters: • 1. PWM period (TPWM) and 2. Duty cycle (d)
The duty cycle is defined as the percentage of digital ‘high’ to digital ‘low + high ’ signals present during a PWM period. It is shown in the figure below, (10%, 50%, 90%). • The PWM resolution is defined as the maximum number of pulses that you can pack into a PWM period. • The PWM period is an arbitrarily time period in which PWM takes place. It is chosen to give best results for your particular use.
Uses of PWM • 1) To digitally create an analog output voltage level for control functions and power supplies. • Thermal system • DC Motor speed controllers • Lighting control • Any application where you need a variable DC voltage • 2) To digitally create analog signals for arbitrary waveforms, sounds, music and speech.
Duty cycle/ Duty Time • Duty time is the ‘ON’ time in one period. (td) • td<TPWM • td can be found using following eq.:
Pulse Width Modulation Mode • Many of the Microchip microcontroller have a PWM Mode of operation • You can set both the period of the wave form and the Duty Cycle to realize a PWM waveform • The register and bit identification are given in the data sheet.
Setting PWM parameters • The PWM registers: • CCP1CON, CCP2CON : PWM mode selected using CCPxCON registers. If PWM module1 (on PORTC,2) is used, the Least significant 4 bits of CCP1CON must be set to 1 (CCP1M<0:4>=1). • T2CON: Least significant 4bits of T2CON set Timer2 Prescaler value(1/4/16). (T2CKPS<0:1>) • PR2: The PWM period is set using PR2 reg. • CCPR1L(8bit)<CCP1Y(1bit)><CCP1X(1bit)>: This register group sets duty time. CCP1X and CCP1Y are 5th and 4th bits of CCP1CON register.
PWM period/duty formulas • The required PWM period and duty time are adjusted by setting the PR2 and <CCPR1L:CCP1X:CCP1Y> registers. • Following equations can be used to determine these values. Tosc is (1/XTAL frequency) and TMR2 prescale value will be adjusted by the designer(1,4 or 16).
Example: Generate 17 KHz PWM signal with duty cycle 25% #include <pic.h> __CONFIG (LVPDIS & HS & WDTDIS & BORDIS); #define _XTAL_FREQ 20000000 float freq; char duty; void init() { TRISC = 0x00; //PWM settings T2CKPS1 = 0; T2CKPS0 = 0; CCP1M0 = 1; CCP1M1 = 1; CCP1M2 = 1;
CCP1M3 = 1; updatePWM(); TMR2ON = 1; } void updatePWM() { unsigned int temp; char i; PR2 = (char)(((_XTAL_FREQ/4)/(freq*1000))) - 1; temp=(int)((duty*(1/(freq*1000))/100)*_XTAL_FREQ); CCP1X = 0; if(temp%2) CCP1X = 1; temp=(int)(temp/2); CCP1Y = 0; if(temp%2) CCP1Y = 1;
CCPR1L = 0; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b00000001; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b00000010; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b00000100; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b00001000; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b00010000; temp=(int)(temp/2);
if(temp%2) CCPR1L = CCPR1L + 0b00100000; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b01000000; temp=(int)(temp/2); if(temp%2) CCPR1L = CCPR1L + 0b10000000; } void main() { init(); while(1) { freq = 27.0;// set PWM frequency as 27 KHz duty = 25; // set duty cycle = 25% updatePWM(); } }
UART • What is RS232? It's just a name for a standard that has propagated from generation to generation of computers. The first computers had serial ports that used RS232, and even current computers have serial ports (or at least USB ports that act like RS232 ports). • Back in the day, serial information needed to be passed from devices like printers, joysticks, scanners, etc to the computer. The simplest way to do this was to pass a series of 1s and 0s to the computer. • Both the computer and the device agreed on a speed of information - 'bits per second'. A computer would pass image data to a printer at 9600 bits per second and the printer would listen for this stream of 1s and 0s expecting a new bit every 1/9600 = 104us (104 micro-seconds, 0.000104 seconds). As long as the computer output bits at the pre-determined speed, the printer could listen.
UART Registers • The USART module will be configured as asynch. & full dublex mode. • RCSTA<7> (SPEN, serial port enable) and TRISC<7> have to be set.TRIS<6>=0 • RCSTA<6>: 1=9bit, 0=8bit reception • RCSTA<5>: 0 in async. mode • RCSTA<4> (CREN, continious receive enable). Have to be set • RCSTA<3:0> Error detection bits. All zero (disabled)
TXSTA<7>: zero in async. Mode • TXSTA<6>(TX9): 1:9bit 0:8bit mode. • TXSTA<5>(TXEN: Transmit enable:1) • TXSTA<4>: 0 for async. Mode • TXSTA<2>(BRGH: High baudrate select bit) 1: high speed, 0: low speed • TXSTA<1>: (TRMT: Transmit buffer full/empty). 1: the byte is sent. 0: The byte is not sent yet. • TXSTA<0>: error detection bit=0.
TXREG • RXREG • TXIF • RXIF
The standard that is 'RS232' dictates that a bit ranges from -12V to +12V. Modern electronics do not operate at such high positive and negative voltages. In fact, our PIC runs 0V to 5V. So how do we get our 5V micro to talk the RS232 +/-12V voltages?
Example: Write a code that transmits the received character • #include <pic.h> • __CONFIG (LVPDIS & HS & WDTDIS & BORDIS); • #define _XTAL_FREQ 4000000 //function prototypes: • void putrs1USART(const char *data); • void putrsUSART(const char *data); • void putByteUSART(unsigned char data); • unsigned char a=33,b=0,udata; • main() • { • SPBRG = 12; // 19200 baud @ 4MHz • TXSTA = 0x24; // setup USART transmit • RCSTA = 0x90; // setup USART receive • PORTC = 0; // Clear PORTC • TRISC = 0x80; //
putrsUSART("\r\n\ 1234567890ABCDEFGHIJKLMNOPRSTVZXYQW"); • putrsUSART("\r\n\ ASCI from 33 to253"); • putByteUSART(10); • putByteUSART(13); • for(a=33;a<254;a++){ //send ASCI table • putByteUSART(a); • b++; • if(b>25){ putByteUSART(10); putByteUSART(13); b=0; } • } • while(1){ if(RCIF) {udata= RCREG; putByteUSART(udata); //send char back } } }
void putrsUSART(const char *data) • { do { while(!(TXSTA & 0x02)); TXREG = *data; } while( *data++ ); • } • void putByteUSART(unsigned char data) • { while(!(TXSTA & 0x02)); TXREG = data; • }