240 likes | 439 Views
Advanced Microcontrollers A practical approach. Microcontrollers A practical approach. Goals Responsibilities Topics. Goals. Some more advanced knowledge of microcontrollers Reading and using data-sheets Building, programming and documenting a little microcontroller system.
E N D
MicrocontrollersA practical approach • Goals • Responsibilities • Topics
Goals • Some more advanced knowledge of microcontrollers • Reading and using data-sheets • Building, programming and documenting a little microcontroller system.
Responsibilities • The student is responsible for the hard-en software, there are more groups who must use it • Defects must be reported immediately
MicrocontrollersA practical approach Topics • PIC family • Architecture PIC18F2580 • Interrupts • Serial communication • LCD • Assembly • A very little microcontroller
MicrocontrollersA practical approach Topics • PIC family • Architecture PIC18F2580 • Interrupts • Serial communication • LCD • Assembly • A very little microcontroller
Programming • Header • Functions • Initialisation … • Main program • Initialisation (function call) • Program with function calls in infinite loop
Program header // Project name // Author // Date of creation // Revision number #include <p18f2580.h> #define off 0 #define on1
Initialisation function Digital pin configuration void init(void) { PORTB = 0; // All OFF TRISA = 0b11111111; // All inputs TRISC = 0b11111111; // All inputs TRISB = 0b00111111; // RB6, RB7 output }
Write Output function void LED2(char in) { PORTBbits.RB6 = in; // LED2 = in }
Main Program void main(void) { init(); // initialisation while(1) { // infinite loop if (PORTAbits.RA4 == 1) LED2(ON); else LED2(OFF); } }
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
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
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 Xtalfrequentie 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
Timer 0 ( interrupt example (2)) // Function-declarations: void InterruptHandlerHigh(void); void InitTimer0(void); void EnableHighInterrupts(void); // Global variables: unsigned int count=0;
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 } }
Timer 0 ( interrupt example (4)) #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 }
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 } // High priority interrupt vextor: #pragma code high_vector = 0x08 void high_interrupt(void) { _asmgotoInterruptHandlerHigh _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 } }
Assignments lesson 1 • Make the exampleworking • Change the programm, let the counting do every 2 seconds • Reset the counting on the LED with • a RB0 interrupt