39
PIC MICROCONTROLLER PROGRAMMING BASED ON MIKROC

PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Embed Size (px)

Citation preview

Page 1: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

PIC MICROCONTROLLER

PROGRAMMING

BASED ON MIKROC

Page 2: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Difference from A

microprocessor?

• To communicate microprocessor with peripheral environment, special circuits must be used.

Page 3: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Internal Structure

Page 4: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Description

• PIC – “Peripheral Interface Controller”

• Made by Microchip Technology

• Most popular by industry developers and hobbyists

– Low cost (cents to dollars)

– Availability

– Extensive application notes

– Serial programming

4

Page 5: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Internal Units description

REGISTER

• An electronic circuit which can memorize the state of one byte

Page 6: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

SFR(Special Function Register)

• Registers whose function is predetermined by the manufacturer of the microcontroller

• Examples are timers, A/D converter, oscillators ,etc

Page 7: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

INPUT / OUTPUT PORTS

• Microcontroller has one or more registers (called ports) connected to the microcontroller pins.

• Can change a pin function as you wish

Page 8: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

MEMORY UNIT

• Memory is part of the microcontroller used for data storage.

• Types

1.MROM,

2.OTP ROM, 3.UV EP

ROM, 4.FLASH MEM

5. EEPROM, 6. RAM

Page 9: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

INTERRUPT

• Most programs use interrupts in their regular execution

• The signal which informs the central processor unit about such an event is called an INTERRUPT.

Page 10: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

CENTRAL PROCESSOR UNIT (CPU)

• Instruction decoder decodes program instructions and runs other circuits based on this

• Accumulator is a kind of working desk used for storing all data upon which some operation should be performed (addition, shift/move etc.)

• ALU

Page 11: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

OSCILLATOR

• Enable harmonic and synchronous operation of all circuits within the microcontroller.

• Usually configured so as to use quartz crystal or ceramic resonator for frequency stability

• but it can also operate as a stand-alone circuit (like RC oscillator).

Page 12: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

TIMERS/COUNTERS• To measure time between two events, it is

sufficient to count up pulses generated by the oscillator. This is exactly what the timer does.

• ‘Stopwatches’ commonly 8- or 16-bit SFRs the contents of which is automatically incremented by each coming pulse.

Page 13: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

HOW DOES THE TIMER OPERATE?

• It is easy to measure short time intervals, up to 256 microsecond in this mode

•To measure time intervals greater than 256 microsize we use two modified methods

Page 14: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Using a pre Scaler

Using

Interrupt

Page 15: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

COUNTERS

• If the timer receives pulses form the microcontroller input pin, then it turns into a counter.

• The only difference is that in this case pulses to be counted come over the microcontroller input pin and their duration (width) is mostly undefined.

Page 16: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

A/D CONVERTER

• Converts continuous signals to discrete digital numbers or circuit converts an analogue value into a binary number and passes it to the CPU for further processing.

Page 17: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
Page 18: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Basic path of processing codes

Page 19: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Tools required for programming

• Micro C IDE

• USB PIC Programmer drivers

• USB PIC Programmer

Page 20: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
Page 21: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

IDE FOR MIKROC PRO FOR PIC

• HNKKKKHH

Page 22: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

COMPILER’S TOOLS AND LIBRARIES

• USART TERMINAL

• EEPROM EDITOR

• ASCII CHART

• SEVEN SEGMENT EDITOR

• LCD CUSTOM CHARACTER

• GRAPHIC LCD BITMAP GENERATOR

• LIBRARIES

Page 23: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

SEVEN SEGMENT EDITOR

Page 24: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

PIC 16F877A LAYOUT

Analog I/O

UART

I2C

PORTA

http://www.mikroe.com/eng/chapters/view/74/pic-basic-book-chapter-1-world-of-microcontrollers/

24

Page 25: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
Page 26: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

FEATURES OF PIC 16F887A

• RISC architecture

– Only 35 instructions to learn

– All single-cycle instructions except branches

• Operating frequency 0-20 MHz

• Precision internal oscillator

– Factory calibrated

– Software selectable frequency range of 8MHz to 31KHz

• Power supply voltage 2.0-5.5V

– Consumption: 220uA (2.0V, 4MHz), 11uA (2.0 V, 32 KHz) 50nA (stand-by mode)

• Power-Saving Sleep Mode

• Brown-out Reset (BOR) with software control option

• 35 input/output pins

– High current source/sink for direct LED drive

Page 27: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

• 256 bytes EEPROM memory

– Data can be written more than 1.000.000 times

• 368 bytes RAM memory

• A/D converter:

– 14-channels

– 10-bit resolution

• 3 independent timers/counters

• Watch-dog timer

• Analogue comparator module with

– Two analogue comparators

– Fixed voltage reference (0.6V)

– Programmable on-chip voltage reference

• PWM output steering control

• Enhanced USART module

– Supports RS-485, RS-232 and LIN2.0

– Auto-Baud Detect

• Master Synchronous Serial Port (MSSP)

– supports SPI and I2C mode

Page 28: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

BASIC CONNECTION OF A PIC

Page 29: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Basic codes of MikroC

• TRISB=0; means that all the pins of PORTB is set as output. If it is set as 1 then all the pins will be configured as input.

• PORTB=0; means that all the pins of PORTB is Logic low. If it is set as 1 then all the pins will be set as Logic high state.

• ANSELA = 0; Configure PORTA pins as digital.

ANSELA = 0b00000101; Means that RA0/C12IN0-and RA2/C2IN+ pins are analog inputs.

• ANSELH=0; Configure PORTB pins as digital.

Page 30: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

• TRISC.F0 = 1; Makes 0th bit of PORTC Input

• TRISC.F5 = 0; Makes 5th bit of PORTC Output

• PORTB.F3 = 1; Makes 3ed bit of PORTB at Logic High

• PORTB.F7 = 0; Makes 7th bit of PORTB at Logic Low

Page 31: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Simple MikroC program for blinking an

LED

Page 32: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

Interfacing DC Motor with PIC

Microcontroller using L293D

• Control Signals and Motor Status

RB0/IN1 RB2/IN2 Motor

Status LOW LOW Stops

LOW HIGH ANT-CLK

HIGH LOW CLK

HIGH HIGH CLK

Page 33: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
Page 34: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

CCP(Capture/Compare/PWM) Module

• Three modes-Capture, compare , PWM

• PWM

• Signals with varying duty cycle.

• Importance in power control circuits.

Page 35: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

• /* In this example, PWM module is initialized and set to give a pulse train of 50% dutycycle.For this purpose, functions PWM1_Init(), PWM1_Start() and PWM1_Set_Duty() are used.All of them are already contained in the mikroC PRO for PIC PWM library and just need tobe copied to the program. */

unsigned short duty_c; // Define variable duty_c

void initMain() {ANSEL = ANSELH = 0; // All I/O pins are configured as digitalPORTC = TRISC = 0; // Initial state of port C output pinsPWM1_Init(5000); // PWM module initialization (5KHz)}

void main() {initMain();duty_c = 127; // Initial value of duty-cyclePWM1_Start(); // Start PWM1 modulePWM1_Set_Duty(duty_c); // Set PWM duty-cycle to 50%...

Page 36: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

SERIAL COMMUNICATION

MODULES• EUSART

Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART)module is a serial I/O communication peripheral unit.

• also known as Serial

Communications Interface (SCI)

• contains all clock generators,

shift registers and data buffers

necessary to perform an input/output serial data transfer

Page 37: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

• In order to enable data transmission via EUSART module it is necessary to define the state of the following pins-RX , TX

• RX-Receiver

• TX-Transmitter

Page 38: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

/* In this example, internal EUSART module is initialized and set to send back themessage immediately after receiving it. Baude rate is set to 9600 bps. The programuses UART library routines UART1_init(), UART1_Write_Text(), UART1_Data_Ready(),UART1_Write() and UART1_Read().*/

char uart_rd;

void main() {ANSEL = ANSELH = 0; // Configure AN pins as digitalC1ON_bit = C2ON_bit = 0; // Disable comparatorsUART1_Init(9600); // Initialize UART module at 9600 bpsDelay_ms(100); // Wait for UART module to become stableUART1_Write_Text("Start");

while (1) { // Endless loopif (UART1_Data_Ready()) { // If data is received,uart_rd = UART1_Read(); // read the received data,UART1_Write(uart_rd); // and send data back via UART}`}

Page 39: PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS

END

BY