Академический Документы
Профессиональный Документы
Культура Документы
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
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
Timeravbrott
Industrial Electrical Engineering and Automation
Avbrott
Industrial Electrical Engineering and Automation
Avbrottsregister
Avbrottsregister
Industrial Electrical Engineering and Automation
Timer0
Industrial Electrical Engineering and Automation
isr_cnt++;
T0IF = 0; // Nollställ TIMER0 interrupt flag
}
}
void main(void){
...
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"
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
while(1)
{
...
}
}
Några datatyper 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
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);
void setup() { __CONFIG(PWRTDIS & UNPROTECT & BORDIS & WDTDIS & INTIO);
Industrial Electrical Engineering and Automation
pinMode(ledPin, OUTPUT);
void main(void){
} OSCCON=0x70;
TRISC=0b00000001;
while(1){
...
CCPR1L = 123;
}
}
www.microchip.com
www.mikroe.com