Title: EEE447 Introduction to Microprocessors
1EEE447 Introduction to Microprocessors
2Interfacing 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.
3General I/O Registers
- TRIS
- PORT
- ADC Registers
- ADRESH
- ADRESL
- ADCON0
- ADCON1
4ADCON0
5ADCON1
6Keypad/7-segment display
- Keypad
- Matrix Keypad
- GND output keypad
- Serial output keypad
- Dipswitches
- 7-Segment Display (Example)
7Example Drive 4 seven segment. (a four digit
number can be written)
include ltpic.hgt include "delay.h" __CONFIG
(LVPDIS HS WDTDIS BORDIS) define
_XTAL_FREQ 20000000
//seven segment LEDS static bit seg_a
_at_((unsigned)PORTC83)// --a static bit seg_b
_at_((unsigned)PORTA80)// f b static bit
seg_c _at_((unsigned)PORTC86)// --
----gtg static bit seg_d _at_((unsigned)PORTC84)//
e c static bit seg_e _at_((unsigned)PORTC85)/
/ --d static bit seg_f _at_((unsigned)PORTB85)
static bit seg_g _at_((unsigned)PORTC87)
8static bit seg_0 _at_((unsigned)PORTA83) static
bit seg_1 _at_((unsigned)PORTA85) static bit
seg_2 _at_((unsigned)PORTC80) static bit seg_3
_at_((unsigned)PORTC81)
void init() ADCON1 0x07 TRISB
0b11001110 TRISC 0x00 TRISA 0x00
9void 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
10 seg_c 1 seg_d 1 seg_e
0 seg_f 0 seg_g 1 break cas
e 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
11 case 7 seg_a 1 seg_b
1 seg_c 1 seg_d 0 seg_e
0 seg_f 0 seg_g 0 break cas
e 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
12 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
13case 3 seg_0 0 seg_1 0 seg_2
0 seg_3 1 break default break
void main() while(1) int
temp_int2953
14sayi temp_int10 seg 0 write2sevensegment(se
g,sayi) DelayMs(1) temp_int - sayi temp_int
temp_int/10 sayi temp_int10 seg
1 write2sevensegment(seg,sayi) DelayMs(1) temp_
int - sayi temp_int temp_int/10
15sayi temp_int10 seg 2 write2sevensegment(se
g,sayi) DelayMs(1) temp_int - sayi temp_int
temp_int/10
sayi temp_int10 seg 3 write2sevensegment(se
g,sayi) DelayMs(1) temp_int - sayi temp_int
temp_int/10
16Alphanumeric LCD
17Cont. Frequently Used Commands
18Cont.Use LCD in 4-bit mode
19Cont.
20PWM
- What is PWM signal?
- A square wave form with two parameters
- 1. PWM period (TPWM) and 2. Duty cycle (d)
21- 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.
22Uses 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.
23Duty cycle/ Duty Time
- Duty time is the ON time in one period. (td)
- tdltTPWM
- td can be found using following eq.
24Pulse 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.
25Setting 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 (CCP1Mlt04gt1). - T2CON Least significant 4bits of T2CON set
Timer2 Prescaler value(1/4/16). (T2CKPSlt01gt) - PR2 The PWM period is set using PR2 reg.
- CCPR1L(8bit)ltCCP1Y(1bit)gtltCCP1X(1bit)gt This
register group sets duty time. CCP1X and CCP1Y
are 5th and 4th bits of CCP1CON register.
26PWM period/duty formulas
- The required PWM period and duty time are
adjusted by setting the PR2 and
ltCCPR1LCCP1XCCP1Ygt 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).
27Example Generate 17 KHz PWM signal with duty
cycle 25
include ltpic.hgt __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
28CCP1M3 1 updatePWM() TMR2ON 1
void updatePWM() unsigned int temp char
i PR2 (char)(((_XTAL_FREQ/4)/(freq1000))) -
1 temp(int)((duty(1/(freq1000))/100)_XTAL_FR
EQ) CCP1X 0 if(temp2) CCP1X
1 temp(int)(temp/2) CCP1Y
0 if(temp2) CCP1Y 1
29CCPR1L 0 temp(int)(temp/2) if(temp2) CCP
R1L CCPR1L 0b00000001 temp(int)(temp/2) i
f(temp2) CCPR1L CCPR1L 0b00000010 temp(i
nt)(temp/2) if(temp2) CCPR1L CCPR1L
0b00000100 temp(int)(temp/2) if(temp2) CCP
R1L CCPR1L 0b00001000 temp(int)(temp/2)
if(temp2) CCPR1L CCPR1L
0b00010000 temp(int)(temp/2)
30if(temp2) CCPR1L CCPR1L 0b00100000 temp(
int)(temp/2) if(temp2) CCPR1L CCPR1L
0b01000000 temp(int)(temp/2) if(temp2) CCP
R1L CCPR1L 0b10000000
void main() init() while(1) freq
27.0// set PWM frequency as 27 KHz duty 25
// set duty cycle 25 updatePWM()
31UART
- 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.
32UART Registers
- The USART module will be configured as asynch.
full dublex mode. - RCSTAlt7gt (SPEN, serial port enable) and TRISClt7gt
have to be set.TRISlt6gt0 - RCSTAlt6gt 19bit, 08bit reception
- RCSTAlt5gt 0 in async. mode
- RCSTAlt4gt (CREN, continious receive enable). Have
to be set - RCSTAlt30gt Error detection bits. All zero
(disabled)
33- TXSTAlt7gt zero in async. Mode
- TXSTAlt6gt(TX9) 19bit 08bit mode.
- TXSTAlt5gt(TXEN Transmit enable1)
- TXSTAlt4gt 0 for async. Mode
- TXSTAlt2gt(BRGH High baudrate select bit) 1 high
speed, 0 low speed - TXSTAlt1gt (TRMT Transmit buffer full/empty). 1
the byte is sent. 0 The byte is not sent yet. - TXSTAlt0gt error detection bit0.
34 35Baudrate generator register formulas
36- 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?
37Serial communication HardWare
38Example Write a code that transmits the received
character
- include ltpic.hgt
- __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 a33,b0,udata
- main()
-
- SPBRG 12 // 19200 baud _at_ 4MHz
- TXSTA 0x24 // setup USART transmit
- RCSTA 0x90 // setup USART receive
- PORTC 0 // Clear PORTC
- TRISC 0x80 //
39- putrsUSART("\r\n\ 1234567890ABCDEFGHIJKLMNOPRSTVZX
YQW") - putrsUSART("\r\n\ ASCI from 33 to 253")
- putByteUSART(10)
- putByteUSART(13)
- for(a33alt254a) //send ASCI table
- putByteUSART(a)
- b
- if(bgt25)
- putByteUSART(10)
- putByteUSART(13)
- b0
-
-
- while(1)
- if(RCIF)
- udata RCREG
- putByteUSART(udata) //send char back
-
40- void putrsUSART(const char data)
-
- do
-
- while(!(TXSTA 0x02))
- TXREG data
- while( data )
-
- void putByteUSART(unsigned char data)
-
- while(!(TXSTA 0x02))
- TXREG data
-