Department of Electronics & Telecommunication Engineering _______________________________________________________________________________________________________________________ Unit-05 PIC Microcontroller Architecture Lecture 1 Assist.Prof. C.K.Kalawade (M.E.(Electronics &Telecommunication ))
MCU Support Devices T o C O N 2
3
4 {} {} {}
5 IO port programming in PIC PIC18 has many ports Depending on the family member Depending on the number of pins on the chip Each port can be configured as input or output. Bidirectional port Each port has some other functions Such as timer , ADC, interrupts and serial communication Some ports have 8 bits, while others have not.
6 I/O SFR Each port can be configured as input or output. Bidirectional port Each port has three registers for its operation: TRIS register (Data Direction register). If the bit is 0 – Output, 1 -- Input PORT register (reads the levels on the pins of the device), LAT register (output latch) The Data Latch (LAT) register is useful for read- modify-write operations.
LED interfacing with PIC18fxxx
Programming steps: 1. Configure the TRISB register to make PortB as output port. 2. Set all the bits of PORTB register High (1) to glow all LEDs. 3. Provide some delay. 4. Set all the bits of PORTB register Low (0) to turn off the LEDs. 5. Provide some delay. 6. Repeat the process from step 2.
#include<p18f4520.h> void delay (unsigned int itime ); # pragma config OSC=HS # pragma config PWRT=OFF # pragma config WDT=OFF # pragma config DEBUG=OFF, LVP=OFF void main() { TRISB = 0; // PORTB AS OUTPUT PORT while(1) { PORTB = 0X00; // PORTB AS HIGH delay(100); PORTB = 0Xff; // PORTB AS LOW delay (100); } }
void delay (unsigned int itime ) { int i,j ; for( i =0;i<= itime;i ++) for(j=0;j<=1275;j++); }
The ‘# pragma ’ directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself.
Department of Electronics & Telecommunication Engineering _______________________________________________________________________________________________________________________ Unit-05 PIC Microcontroller Architecture Lecture 2 Assist.Prof. C.K.Kalawade (M.E.(Electronics &Telecommunication ))
LCD Display
LCD interfacing with PIC 18fxxx in 8bit mode
Steps in programming 1) initialize port pins connected to lcd as o/p 2)initialize LCd display 3)send data to lcd for diplaying on it
Lcd initializations lcdcmd (0x38); /*Double Line Display Command lcdcmd (0x0C); /*display on cursor off*/ lcdcmd (0x01); /*Clear Display Command lcdcmd (0x06); /*Auto Increment Location Address Command lcdcmd (0x81); /*first line first position
Department of Electronics & Telecommunication Engineering _______________________________________________________________________________________________________________________ Unit-05 PIC Microcontroller Architecture Lecture 3 Assist.Prof. C.K.Kalawade (M.E.(Electronics &Telecommunication ))
Pic 187f458 interfacing with LCD in 4-bit Mode In 4-bit mode, data/command is sent in a 4-bit (nibble) format. To do this 1st send Higher 4-bit and then send lower 4-bit of data/command.
Why 4bit mode Only 4 data (D4 - D7) pins of 16x2 of LCD are connected to the microcontroller and other control pins i.e. RS (Register select), RW (Read/write), E (Enable) are connected to other GPIO Pins of the controller. Therefore, due to such connections, we can save four GPIO pins which can be used for another application.
Department of Electronics & Telecommunication Engineering _______________________________________________________________________________________________________________________ Unit-05 PIC Microcontroller Architecture Lecture 4 Assist.Prof. C.K.Kalawade (M.E.(Electronics &Telecommunication ))
USING TIMERS IN PIC18F452 Timers and counters are important as timers can tell the time and count . Counting and timing allows for controlling the brightness of LEDs , controlling the angle of servo shafts and PWM signal generation etc. All microcontrollers have clocks in them or they use the one that resides outside of a microcontroller. Microcontroller needs clock so our programs can be executed in regularity with the clock. This is the basic function of microcontrollers. The PIC 18F452 is a high performance flash based microcontroller with 32 Kbytes of program memory and 1.5Kbytes of RAM . PIC18F452 has four different timers namely, Timer0, Timer1, Timer2 and Timer3.
Timer0: Timer0 can work as both 8-bit and 16-bit modes timer/counter Software programmable Prescaler Select able clock source (internal or external) Interrupt on overflow Timer1: Timer1 can work as 16-bit timer or counter Readable and writable 8-bit registers (TMR1H and TMR1L) Selectable clock source (internal or external) Alternate clock source can be provided at Timer1 oscillator pins (T1OSO & T1OSI) Interrupt on overflow Timer1 can be multiplexed with other peripherals like ADC and generates special event triggering for CCP (Capture, Compare and PWM) events.
Timer2 : 8-bit Timer and Period registers (TMR2 and PR2, respectively) Software programmable prescaler (1:1, 1:4 and 1:16) Software programmable postscaler (1:1 – 1:16) Interrupt on TMR2 to PR2 match Timer3 : Timer3 can work as 16-bit timer or counter Readable and writable 8-bit registers (TMR3H and TMR3L) Selectable clock source (internal or external) Alternate clock source can be provided at Timer1 oscillator pins (T1OSO & T1OSI) Interrupt on overflow Timer3 can be multiplexed with other peripherals like ADC and generates special event triggering for CCP (Capture, Compare and PWM) events .
Timer0 Control Register: TMR0ON: Timer0 on/off bit This bit is set to high to enable the Timer0. 1 = Enable the Timer0 0 = to stop Timer0 TMR0IE: Timer0 Interrupt Enable This bit is used to enable/disable the Timer0 overflow interrupt. 1 = TMR0 Interrupt enabled 0 = TMR0 Interrupt disabled TMR0 (Timer0 Register): This register is divided into registers TMR0H TMR0L