PowerPoint-presentatie

Download Report

Transcript PowerPoint-presentatie

Advanced Microcontrollers
A practical approach
Saxion University of Applied Sciences
Microcontroller programming
Microcontrollers
A practical approach
 Goals
 Responsibilities
 Topics
2
Microcontroller programming
Goals
 Some more advanced
knowledge of microcontrollers
 Reading and using datasheets
 Building, programming and
documenting a little
microcontroller system.
3
Microcontroller programming
Responsibilities
 The student is responsible for the
hard-en software, there are more
groups who must use it
 Defects must be reported
immediately
4
Microcontroller programming
Microcontrollers
A practical approach
Topics







5
PIC family
Architecture PIC18F2580
Interrupts
Serial communication
LCD
Assembly
A very little microcontroller
Microcontroller programming
Microcontrollers
A practical approach
Topics







6
PIC family
Architecture PIC18F2580
Interrupts
Serial communication
LCD
Assembly
A very little microcontroller
Microcontroller programming
PIC18F2580 Device
7
Microcontroller programming
8
PIC18F2580
pinout
Microcontroller programming
Block diagram
9
Microcontroller programming
Programming
 Header
 Functions
 Initialisation
…
 Main program
1. Initialisation (function call)
2. Program with function calls in
infinite loop
10
Microcontroller programming
Program header
// Project name
// Author
// Date of creation
// Revision number
#include <p18f2580.h>
#define off 0
#define on1
11
Microcontroller programming
Initialisation function
Digital pin configuration
void init(void) {
PORTB = 0;
TRISA = 0b11111111;
TRISC = 0b11111111;
TRISB = 0b00111111;
}
12
// All OFF
// All inputs
// All inputs
// RB6, RB7 output
Microcontroller programming
Write Output function
void LED2(char in) {
PORTBbits.RB6 = in;
}
13
// LED2 = in
Microcontroller programming
Main Program
void main(void) {
init();
// initialisation
while(1) {
// infinite loop
if (PORTAbits.RA4 == 1)
LED2(ON);
else
LED2(OFF);
}
}
14
Microcontroller programming
Interruptssources










•
•
•
•
•
•
•
•
•
•
External Interrupt RA2/INT
TMR0 Overflow Interrupt
PORTA Change Interrupts
2 Comparator Interrupts
A/D Interrupt
Timer1 Overflow Interrupt
Timer2 Match Interrupt
EEPROM Data Write Interrupt
Fail-Safe Clock Monitor Interrupt
Enhanced CCP Interrupt
18-7-2015
15
15
Interrupts(chapter 10 datasheet)sources
Microcontroller programming
18-7-2015
16
16
Microcontroller programming
Timer 0 • Readable and writeable
• 8-bit/16-bit timer/counter
• 3-bit software programmable prescaler (2-4-8-…-256)
• Internal or external clock select
• Edge select for external clock
• Interrupt on overflow from 0xFF to 0x00
17
Timer 0
T0CON
Microcontroller programming
18
Microcontroller programming
Timer 0 ( interrupt example)
19
/*********************** pic18f2580 **************************\
| Testprogram_1
MPlab
C18-compiler
|
|21 juli 2013 J.S.D. Stokkink
|
+----------------------------------------------------------------------+
| USES: TIMER0 ,
High-interrupt priority |
+---------------------------------------------------------------------+|
|| TIMER0 gives interrupt each 1sec (internal inerrupt)|
| De Xtal frequentie is 20MHz
|
| after each interrupt 1 second a binairy increase on led
|\*************************************************************/
#include <p18F2580.h>
#pragma config OSC = HS
// HS oscillator 20 Mhz
#pragma config WDT = OFF
// Watchdog Timer disabled
#pragma config LVP = OFF
// Low Voltage ICSP disabled
#pragma config PBADEN = OFF
// PortB<4:0> configured as digital I/O
Microcontroller programming
Timer 0 ( interrupt example (2))
// Function-declarations:
void InterruptHandlerHigh(void);
void InitTimer0(void);
void EnableHighInterrupts(void);
// Global variables:
unsigned int count=0;
20
Microcontroller programming
Timer 0 ( interrupt example (3))
#pragma code
void main (void)
{ TRISC=0;
//led's output
PORTC=0;
//led off
InitTimer0();
EnableHighInterrupts();
// run forever:
while(1)
{
// do nothing wait on interrupt
}
}
21
Microcontroller programming
Timer 0 ( interrupt example (4))
22
#pragma code
void InitTimer0(void)
{
// init timer 0:
INTCONbits.GIE = 0; // disable global interrupt
INTCON2bits.TMR0IP = 1; // Timer 0 high priority interrupt
RCONbits.IPEN = 1; // enable priority levels
INTCONbits.PEIE = 1; // enable high priority interrupt
T0CONbits.T0CS = 0; // internal instuction cycle clock (CLKO)
T0CONbits.PSA = 0; // prescaler is assigned
T0CONbits.T0PS2 = 1; // prescale value bit 2
T0CONbits.T0PS1 = 1; // prescale value bit 1
T0CONbits.T0PS0 = 1; // prescale value bit 0 ==> 1:256
T0CONbits.T08BIT = 0; // 16-bits timer
TMR0H = 72;
// set timer
TMR0L = 229;
// set timer ==> ca 1 sec
INTCONbits.TMR0IE = 1; // Timer 0 interrupt enabled
T0CONbits.TMR0ON = 1; // enable Timer 0
INTCONbits.GIE = 1; // enable global interrupt
}
Microcontroller programming
Timer 0 ( interrupt example (5))
#pragma code
void EnableHighInterrupts(void)
{
RCONbits.IPEN = 1; // enable interrupt priority levels
INTCONbits.GIEH = 1; // enable all high priority iterrupts
}
23
// High priority interrupt vextor:
#pragma code high_vector = 0x08
void high_interrupt(void)
{
_asm
goto InterruptHandlerHigh_endasm
}
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh(void)
{
if(INTCONbits.TMR0IF)
// check for TMR0 overflow
{
INTCONbits.TMR0IF = 0;
// clear interrupt flag
TMR0H = 72;
// reload timer
TMR0L = 229;
// reload timer
PORTC++;
// increase value on led
}
Microcontroller programming
Assignments lesson 1
1. Make the example working
2. Change the programm, let the
counting do every 2 seconds
3. Reset the counting on the LED with
a RB0 interrupt
24