eee447 introduction to microprocessors

40
EEE447 Introduction to Microprocessors Week x

Upload: sherry

Post on 08-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

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 . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: EEE447 Introduction to Microprocessors

EEE447 Introduction to Microprocessors

Week x

Page 2: EEE447 Introduction to Microprocessors

Interfacing Concepts

• How the microcontroller communicates with the world? The procedures of data exchange?– User interfaces: Keypad, LCD, 7-segment display,

switches, etc.– Machine to machine interfaces: rotary encoders,

ADCs, memory modules, peripheral microcontrollers,modems etc.

• Basic Input/Output interfacing– The process of reading input signals and sending

output signals is called input/output (I/O) – The processor transfers the data to and from the

peripherals using special hardwares and protocols.

Page 3: EEE447 Introduction to Microprocessors

General I/O Registers

• TRIS• PORT• ADC Registers

– ADRESH– ADRESL– ADCON0– ADCON1

Page 4: EEE447 Introduction to Microprocessors

ADCON0:

Page 5: EEE447 Introduction to Microprocessors

ADCON1:

Page 6: EEE447 Introduction to Microprocessors

Keypad/7-segment display

• Keypad– Matrix Keypad– GND output keypad– Serial output keypad– Dipswitches

• 7-Segment Display (Example…)

Page 7: EEE447 Introduction to Microprocessors

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);// --astatic bit seg_b @((unsigned)&PORTA*8+0);// f| |bstatic bit seg_c @((unsigned)&PORTC*8+6);// -- ---->gstatic bit seg_d @((unsigned)&PORTC*8+4);// e| |cstatic bit seg_e @((unsigned)&PORTC*8+5);// --dstatic bit seg_f @((unsigned)&PORTB*8+5);static bit seg_g @((unsigned)&PORTC*8+7);

Page 8: EEE447 Introduction to Microprocessors

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;

}

Page 9: EEE447 Introduction to Microprocessors

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;

Page 10: EEE447 Introduction to Microprocessors

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;

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;

Page 11: EEE447 Introduction to Microprocessors

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 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;

Page 12: EEE447 Introduction to Microprocessors

case 9:seg_a = 1; seg_b = 1;seg_c = 1;seg_d = 1;seg_e = 0;seg_f = 1;seg_g = 1;break;

default:break;

}switch(segment){ case 0:

seg_0 = 1;seg_1 = 0;

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;

Page 13: EEE447 Introduction to Microprocessors

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;

Page 14: EEE447 Introduction to Microprocessors

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;

Page 15: EEE447 Introduction to Microprocessors

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

Page 16: EEE447 Introduction to Microprocessors

Alphanumeric LCD

Page 17: EEE447 Introduction to Microprocessors

Cont.: Frequently Used Commands

Page 18: EEE447 Introduction to Microprocessors

Cont.:Use LCD in 4-bit mode

Page 19: EEE447 Introduction to Microprocessors

Cont.:

Page 20: EEE447 Introduction to Microprocessors

PWM• What is PWM signal?

• A square wave form with two parameters:

• 1. PWM period (TPWM) and 2. Duty cycle (d)

Page 21: EEE447 Introduction to Microprocessors

• 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.

Page 22: EEE447 Introduction to Microprocessors

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.

Page 23: EEE447 Introduction to Microprocessors

Duty cycle/ Duty Time

• Duty time is the ‘ON’ time in one period. (td)

• td<TPWM • td can be found using following eq.:

t

Page 24: EEE447 Introduction to Microprocessors

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.

Page 25: EEE447 Introduction to Microprocessors

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.

Page 26: EEE447 Introduction to Microprocessors

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).

Page 27: EEE447 Introduction to Microprocessors

Example: Generate 17 KHz PWM signal with duty cycle 25% #include <pic.h>__CONFIG (LVPDIS & HS & WDTDIS & BORDIS);#define _XTAL_FREQ 20000000float freq; char duty;

void init(){

TRISC = 0x00;//PWM settings

T2CKPS1 = 0;T2CKPS0 = 0;CCP1M0 = 1;CCP1M1 = 1;CCP1M2 = 1;

Page 28: EEE447 Introduction to Microprocessors

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;

Page 29: EEE447 Introduction to Microprocessors

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);

Page 30: EEE447 Introduction to Microprocessors

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 KHzduty = 25; // set duty cycle = 25%updatePWM();

}}

Page 31: EEE447 Introduction to Microprocessors

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.

Page 32: EEE447 Introduction to Microprocessors

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)

Page 33: EEE447 Introduction to Microprocessors

• 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.

Page 34: EEE447 Introduction to Microprocessors

• TXREG• RXREG• TXIF• RXIF

Page 35: EEE447 Introduction to Microprocessors

Baudrate generator register& formulas

Page 36: EEE447 Introduction to Microprocessors

• 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?

Page 37: EEE447 Introduction to Microprocessors

Serial communication HardWare

Page 38: EEE447 Introduction to Microprocessors

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; //

Page 39: EEE447 Introduction to Microprocessors

– putrsUSART("\r\n\ 1234567890ABCDEFGHIJKLMNOPRSTVZXYQW");– putrsUSART("\r\n\ ASCI from 33 to 253"); – 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}

}}

Page 40: EEE447 Introduction to Microprocessors

• void putrsUSART(const char *data)• { do

{ while(!(TXSTA & 0x02));TXREG = *data;} while( *data++ );

• }

• void putByteUSART(unsigned char data)• {

while(!(TXSTA & 0x02));TXREG = data;

• }