Saxion University of Applied Sciences
Advanced MicrocontrollersA practical approach
Microcontroller programming
2
Goals Responsibilities
Topics
MicrocontrollersA practical approach
Microcontroller programming
3
Goals Some more advanced
knowledge of microcontrollers
Reading and using data-sheets
Building, programming and documenting a little microcontroller system.
Microcontroller programming
4
Responsibilities
The student is responsible for the hard-en software, there are more groups who must use it
Defects must be reported immediately
Microcontroller programming
5
Topics PIC family Architecture PIC18F2580 Interrupts Serial communication LCD Assembly A very little microcontroller
MicrocontrollersA practical approach
Microcontroller programming
6
Topics PIC family Architecture PIC18F2580 Interrupts Serial communication LCD Assembly A very little microcontroller
MicrocontrollersA practical approach
Microcontroller programming
7
PIC18F2580 Device
Microcontroller programming
8
PIC18F2580 pinout
Microcontroller programming
9
Block diagram
Microcontroller programming
10
Programming
Header Functions
Initialisation…
Main program1. Initialisation (function call)2. Program with function calls in
infinite loop
Microcontroller programming
11
// Project name// Author// Date of creation// Revision number
#include <p18f2580.h>
#define off 0#define on1
Program header
Microcontroller programming
12
Initialisation function Digital pin configuration
void init(void) {PORTB = 0; // All OFFTRISA = 0b11111111; // All inputsTRISC = 0b11111111; // All inputsTRISB = 0b00111111; // RB6, RB7 output
}
Microcontroller programming
13
Write Output function
void LED2(char in) {PORTBbits.RB6 = in; // LED2 = in
}
Microcontroller programming
14
Main Program
void main(void) {init(); // initialisation
while(1) { // infinite loopif (PORTAbits.RA4 == 1) LED2(ON);else
LED2(OFF);}
}
Microcontroller programming
15 21-04-23 15
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
Microcontroller programming
16 21-04-23 16
Interrupts(chapter 10 datasheet)sources
Microcontroller programming
17
Timer 0
• 8-bit/16-bit timer/counter
• Readable and writeable
• 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
Microcontroller programming
18
Timer 0 T0CON
Microcontroller programming
19
Timer 0 ( interrupt example)
/*********************** 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
20
Timer 0 ( interrupt example (2))
// Function-declarations:void InterruptHandlerHigh(void); void InitTimer0(void);void EnableHighInterrupts(void);// Global variables:unsigned int count=0;
Microcontroller programming
21
Timer 0 ( interrupt example (3))
#pragma codevoid main (void){ TRISC=0; //led's output PORTC=0; //led off InitTimer0(); EnableHighInterrupts(); // run forever: while(1) {
// do nothing wait on interrupt }}
Microcontroller programming
22
Timer 0 ( interrupt example (4))#pragma codevoid InitTimer0(void){
// init timer 0:INTCONbits.GIE = 0; // disable global interruptINTCON2bits.TMR0IP = 1; // Timer 0 high priority interruptRCONbits.IPEN = 1; // enable priority levelsINTCONbits.PEIE = 1; // enable high priority interruptT0CONbits.T0CS = 0; // internal instuction cycle clock (CLKO)T0CONbits.PSA = 0; // prescaler is assignedT0CONbits.T0PS2 = 1; // prescale value bit 2T0CONbits.T0PS1 = 1; // prescale value bit 1T0CONbits.T0PS0 = 1; // prescale value bit 0 ==> 1:256T0CONbits.T08BIT = 0; // 16-bits timerTMR0H = 72; // set timerTMR0L = 229; // set timer ==> ca 1 secINTCONbits.TMR0IE = 1; // Timer 0 interrupt enabledT0CONbits.TMR0ON = 1; // enable Timer 0INTCONbits.GIE = 1; // enable global interrupt
}
Microcontroller programming
23
Timer 0 ( interrupt example (5))#pragma codevoid EnableHighInterrupts(void){
RCONbits.IPEN = 1; // enable interrupt priority levelsINTCONbits.GIEH = 1; // enable all high priority iterrupts
} // High priority interrupt vextor:#pragma code high_vector = 0x08void high_interrupt(void){
_asm goto InterruptHandlerHigh _endasm}#pragma code#pragma interrupt InterruptHandlerHighvoid InterruptHandlerHigh(void){
if(INTCONbits.TMR0IF) // check for TMR0 overflow {
INTCONbits.TMR0IF = 0; // clear interrupt flagTMR0H = 72; // reload timerTMR0L = 229; // reload timerPORTC++; // increase value on led }
}
Microcontroller programming
24
Assignments lesson 1
1. Make the example working2. Change the programm, let the
counting do every 2 seconds3. Reset the counting on the LED with a RB0 interrupt
Top Related