Вы находитесь на странице: 1из 35

Industrial Electrical Engineering and Automation

Microprocessor / Microcontroller
Varför?
Industrial Electrical Engineering and Automation

• Billiga
• Innehåller bara det nödvändigaste
• Kräver få kringkomponenter
• Enkla att programmera
Industrial Electrical Engineering and Automation

PIC16F887
Industrial Electrical Engineering and Automation

PIC16F887
Industrial Electrical Engineering and Automation
Industrial Electrical Engineering and Automation

In- och utgångar

Saknas något?
Industrial Electrical Engineering and Automation

Pinnar på PIC16F887
Exempel: pinne RA1
In / Ut
väljs med TRIS
Industrial Electrical Engineering and Automation

Digital / Analog
väljs med ANSEL
Komparatoringång
Industrial Electrical Engineering and Automation

Timer
Industrial Electrical Engineering and Automation

Timer med prescaler


Industrial Electrical Engineering and Automation

Timeravbrott
Industrial Electrical Engineering and Automation

Avbrott
Industrial Electrical Engineering and Automation

Avbrottsregister
Avbrottsregister
Industrial Electrical Engineering and Automation

GIE - Global Interrupt Enable bit


PEIE - Peripheral Interrupt Enable bit
T0IE - TMR0 Overflow Interrupt Enable bit
INTE - RB0/INT External Interrupt Enable bit
RBIE - RB Port Change Interrupt Enable bit.
T0IF - TMR0 Overflow Interrupt Flag bit
INTF - RB0/INT External Interrupt Flag bit
RBIF - RB Port Change Interrupt Flag bit
Industrial Electrical Engineering and Automation

Timer0
Industrial Electrical Engineering and Automation

Port B Pull up Enable bit


Interrupt Edge Select bit
TMR0 Clock Source Select bit
TMR0 Source Edge Select bit
Prescaler Assignment bit
Prescaler Rate Select bits
Industrial Electrical Engineering and Automation
Generera avbrott varje ms med Timer0
//Fosc = 4 MHz

static void interrupt isr(void){


if(T0IF){ // TIMER0 interrupt flag
TMR0 = 6; // Ladda nytt värde till TMR0
Industrial Electrical Engineering and Automation

isr_cnt++;
T0IF = 0; // Nollställ TIMER0 interrupt flag
}
}

void main(void){
...

PSA = 0; // Prescaler till timer0


PS2 = 0; // 1:4 prescaler
PS1 = 0;
PS0 = 1;
T0CS = 0; // välj Fosc/4 som insignal
TMR0 = 6; // Timer värde för avbrott efter 1 ms
T0IE = 1; // Enable timer0 avbrott
GIE = 1; // Enable Global interrupt
while(1){
if(isr_cnt > 0){ T0IF=1
... 256

isr_cnt = 0; TMR0
}
}
} 6
Industrial Electrical Engineering and Automation

Timer1
Timer som pulsräknare
static void interrupt isr(void){
if(TMR0IF){
TMR0 = 6;
timer1H = TMR1H;
timer1L = TMR1L;
Industrial Electrical Engineering and Automation

TMR1H = 0; T0IF=1
TMR1L = 0; 256
puls_cnt = (timer1H<<8)+timer1L;
TMR0
isr_cnt++;
TMR0IF = 0;
} 6
}

void main(void)
{ puls_cnt
...
TMR1H&L

while(1){
if(isr_cnt>0){
lcd_putint(puls_cnt,5);
isr_cnt = 0;
}

}
}
Timer för tidtagning
static void interrupt isr(void){
if(CCP1IF){
capture_time = ((CCPR1H<<8)+CCPR1L) - capture_old;
capture_old = (CCPR1H<<8)+CCPR1L;
Industrial Electrical Engineering and Automation

isr_cnt++;
65536
CCP1IF = 0;
} capture_time
}
capture_old
void main(void){
...

CCP1IF=1

CCP1IF=1
if(isr_cnt>0){
lcd_putlong(capture_time,6);
isr_cnt = 0;
}
}
Industrial Electrical Engineering and Automation

PWM
PR2
CCPR

TMR2
Industrial Electrical Engineering and Automation

PWM
Industrial Electrical Engineering and Automation

A/D omvandlare
Industrial Electrical Engineering and Automation

Oscillator
#include <htc.h>
#include "lcd.h"

#pragma config IESO=ON, FCMEN=ON,OSC=INTIO7


#pragma config BOREN=SBORDIS, BORV=0, PWRT=OFF, WDT=OFF
#pragma config CCP2MX=PORTC, PBADEN=OFF, LPT1OSC=ON, MCLRE=ON
#pragma config DEBUG=OFF, STVREN=ON, XINST=OFF, LVP=OFF
#pragma config CP0=OFF, CP1=OFF, CP2=OFF, CP3=OFF, CPD=OFF, CPB=OFF
#pragma config WRT0=OFF, WRT1=OFF, WRT2=OFF, WRT3=OFF, WRTB=OFF, WRTC=OFF,WRTD=OFF
#pragma config EBTR0=OFF, EBTR1=OFF, EBTR2=OFF, EBTR3=OFF, EBTRB=OFF
Industrial Electrical Engineering and Automation

char avbrott = 0; //Globala variabler


unsigned int counter = 0;

static void interrupt isr(void)


{
...
}

void main(void)
{
OSCCON = 0x70; // Välj 8 Mhz intern klocka
TRISA = 0x3D; // PORTA: RA1 utgång, resten ingångar
TRISB = 0xFF; // PORTB: ingångar
TRISC = 0xF7; // PORTC: RC3 utgång, resten in
TRISD = 0xF0; // PORTD: RD0-3 utgångar, resten in
TRISE = 0x04; // PORTE: RE0-1 utgångar
ADCON1 = 0x0E; // AN0 (RA0) analog ingång

isr_init(); // Initiera avbrott

while(1)
{
...
}
}
Några datatyper i C
Industrial Electrical Engineering and Automation

char tecken, 8 bitar


int heltal, 16 bitar
(float reella tal )
(double reella tal med dubbel precision)

signed med tecken


unsigned utan tecken

void uttrycket saknar typ


Några operatorer i C
Industrial Electrical Engineering and Automation

a++ a = a+1
a-- a = a-1
!a inte a
~ bitvis invers
== lika med
!= inte lika med
&& OCH
|| ELLER
& bitvis OCH
| bitvis ELLER
Ändra enstaka bitar i ett register
Industrial Electrical Engineering and Automation

a = 0b00010011;
b = a | 0b00000100; // bitvis ELLER
// b = 00010111

a = 0b00010011;
b = a & 0b11111110; // bitvis OCH
// b = 00010010
Skift
Industrial Electrical Engineering and Automation

a = 0x00000011;
b = a<<2 // multiplikation med 4
//b = 00001100

b = a>>1 // division med 2


//b = 00000001

Använd så mycket som möjligt!


Olika sätt att skriva samma sak
Industrial Electrical Engineering and Automation

a = 23 // decimalt
a = 0b00010111 // binärt
b = 0x17 // hexadecimalt
Industrial Electrical Engineering and Automation

Jaså…
”men Arduino är ju mycket roligare”
Arduino eller PIC?
int led = 13; #include <htc.h>
Industrial Electrical Engineering and Automation

void setup() { __CONFIG(PWRTDIS & UNPROTECT & BORDIS & WDTDIS & INTIO);

pinMode(led, OUTPUT); void main(void){


} OSCCON=0x70;
TRISC=0b00000001;
void loop() {
digitalWrite(led, HIGH); while(1){
RC0=1;
delay(100); __delay_ms(100);
digitalWrite(led, LOW); RC0=0;
__delay_ms(100);
delay(100);
}
}
}

Inte så mycket svårare!


Arduino eller PIC?
int ledPin = 9; #include <htc.h>

void setup() { __CONFIG(PWRTDIS & UNPROTECT & BORDIS & WDTDIS & INTIO);
Industrial Electrical Engineering and Automation

pinMode(ledPin, OUTPUT);
void main(void){
} OSCCON=0x70;
TRISC=0b00000001;

void loop() { //PWM setup


... CCP1CON=0b00001100;
analogWrite(ledPin, 123); T2CON=0b00000100;
} PR2=0xFF; //Välj PWM frekvens

while(1){
...
CCPR1L = 123;
}
}

några rader extra men


FULL KONTROLL OCH FULL FRIHET!
och vem har gjort bilderna då?
Industrial Electrical Engineering and Automation

www.microchip.com
www.mikroe.com

om man vill meta mer?

Bates - Programming 8-bit PIC Microcontrollers in C


LUBsearch

Verle - PIC Microcontrollers - Programming in C


http://www.mikroe.com/products/view/285/book-pic-microcontrollers-programming-in-c/

Вам также может понравиться