EEE447 Introduction to Microprocessors
description
Transcript of EEE447 Introduction to Microprocessors
![Page 1: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/1.jpg)
EEE447 Introduction to Microprocessors
Week x
![Page 2: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/3.jpg)
General I/O Registers
• TRIS• PORT• ADC Registers
– ADRESH– ADRESL– ADCON0– ADCON1
![Page 4: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/4.jpg)
ADCON0:
![Page 5: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/5.jpg)
ADCON1:
![Page 6: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/6.jpg)
Keypad/7-segment display
• Keypad– Matrix Keypad– GND output keypad– Serial output keypad– Dipswitches
• 7-Segment Display (Example…)
![Page 7: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/16.jpg)
Alphanumeric LCD
![Page 17: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/17.jpg)
Cont.: Frequently Used Commands
![Page 18: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/18.jpg)
Cont.:Use LCD in 4-bit mode
![Page 19: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/19.jpg)
Cont.:
![Page 20: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/20.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/21.jpg)
• 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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/25.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/33.jpg)
• 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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/34.jpg)
• TXREG• RXREG• TXIF• RXIF
![Page 35: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/35.jpg)
Baudrate generator register& formulas
![Page 36: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/36.jpg)
• 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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/37.jpg)
Serial communication HardWare
![Page 38: EEE447 Introduction to Microprocessors](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/39.jpg)
– 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](https://reader036.fdocuments.us/reader036/viewer/2022062304/56813ff6550346895dab1ae1/html5/thumbnails/40.jpg)
• void putrsUSART(const char *data)• { do
{ while(!(TXSTA & 0x02));TXREG = *data;} while( *data++ );
• }
• void putByteUSART(unsigned char data)• {
while(!(TXSTA & 0x02));TXREG = data;
• }