One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
C Examples 1.
CSC Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
1.  8051 Timers “count up,” incrementing the Timer’s respective “count register” each time there is a triggering clock pulse. 2  When the “count register”
The 8051 Microcontroller and Embedded Systems
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
Today’s Lecture List the Timers of PIC18 and their associated registers Describe the various modes of the PIC18 timers Program the PIC18 timers in Assembly.
8051 timer/counter.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Introduction Purpose:  This training module provides a technical description of Renesas.
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
ARM Timers.
System Clocks.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Interrupts, Counter and Timers. Interrupts (1) Interrupt-driven I/O uses the processor’s interrupt system to “interrupt” normal program flow to allow.
Chapter 4 TIMER OPERATION
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This course provides an overview of the CPU architecture.
Timers /Counters Programming  The 8051 has 2 timers/counters: ○ timer/counter 0 ○ timer/counter 1 They can be used as 1. The timer is used as a time.
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Saxion University of Applied Sciences Advanced Microcontrollers A practical approach.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Real Time Interrupts Section Real-Time Interrupt (RTI) Most operating systems (OS) require an interrupt every T seconds by the RTI RTI interrupts.
Chapter 5 - Interrupts.
The Silicon Laboratories C8051F020
C Examples 1. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
8051 Aula 02 Prof Afonso Ferreira Miguel. Microcontrolador 8051 (MCS-51)  Timers do MCS51 Two 16-bit Counter/Timers: Two 16-bit Counter/Timers: Up counters,
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Timer Programming in Assembly and C Prepared By:
Microprocessors A practical approach..
TIMERS.
Timer modules in PIC 16F877.  The PIC 16F877 basically has three independent timer modules,  denoted by the symbols, TIMER-0, TIMER1,andTIMER2. . These.
16F877A.
Chapter 9 PIC18 Timer Programming in Assembly
Timer and Interrupts.
Microprocessor Systems Design I
Interrupts, Counter and Timers
Interrupt Source: under
Microprocessors Timers
UNIT 5 TIMRERS/COUNTERS
Pugazhendhi.M 11/12/2018 TIMERS 11/12/2018 CCET CCET.
8051 Timers / Counters It has two timers Timer 0 and Timer 1.
전자의료시스템 및 실습 System Configuration/Interrupt
Timer Source: under
Interrupt Source: under
PIC18 Timer Programming “Explain the assembly language programming for the timer and counter module”
EECE.3170 Microprocessor Systems Design I
Interrupt Source: under
EECE.3170 Microprocessor Systems Design I
Timer Source: under
PIC18 Interrupt Programming
Timers Chapter 2.
Presentation transcript:

One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE

Example: Write a C program to measure the unknown frequency of the signal supplied on the RC0/T1OSO/T1CKI pin of PIC18F252. Use Timer0 in a timer mode in order to create a one second delay. Timer 1 as a counter to count the rising (or falling) edges of an unknown signal arrived in one second which would measure the frequency of that signal. Write a program to implement this idea assuming that the PIC18 is running with a 32 MHz crystal oscillator.

// // Purpose: Frequency counter program // // Explanation: // This program uses Timer 0 as a timer to create a 1-second delay // and Timerl to count the number of rising (or falling) edges of // an unknown signal (at the TOCKI pin) arriving in 1 second. // This would measure the frequency of the unknown signal. // A 32-MHz crystal oscillator is used for the Timer 0 delay calculations. // // Timer settings are: // Timer 0 - works in a 16 bit mode //- uses instruction cycle clock as the clock source //- prescaler set to 256 // Timer bit mode //- prescaler value set to 1 //- osicllator disabled //- no synchronisation with external clock input //- RC0/T1OSO/T1CKI pin signal used as a clock source // // Priority interrupt scheme enabled. // Timer 1 interrupt on overflow enabled with high priority. //

#include unsigned int t1ov_cnt; unsigned int freq; void high_ISR(void); #pragma code high_vector = 0x08 // force the following statement to void high_interrupt (void) // start at 0x08 { _asm goto high_ISR _endasm } #pragma code// return to default code section #pragma interrupt high_ISR void high_ISR (void) { PIR1bits.TMR1IF = 0;// clear interrupt flag t1ov_cnt ++;// update timer 1 overflow count } void delay (void) { T0CON = 0x87;// configure Timer 0 - select 16-bit mode, // internal clock source, enable 1:256 prescaler TMR0H = 0x85; // load Timer 0 registers for 1 second delay TMR0L = 0xEE; INTCONbits.TMR0IF = 0;// make sure interrupt flag is reset while(!(INTCONbits.TMR0IF)); // wait until TMR0 rolls over }

void main (void) { t1ov_cnt = 0; freq = 0; TMR1H =0; // force Timer1 to count from 0 TMR1L =0; PIR1bits.TMR1IF = 0; // clear Timer1 interrupt flag RCONbits.IPEN = 1; // enable priority interrupt scheme IPR1bits.TMR1IP = 1; // set Timer1 interrupt to high priority PIE1bits.TMR1IE = 1; // enable Timer1 roll-over interrupt T1CON = 0x87; // enable Timer1 with external clock, prescaler 1 INTCON = 0xC0; // enable global and peripheral interrupts delay (); // start one-second delay and wait for interrupts INTCONbits.GIE = 0; // disable global interrupt // get the total Timer 1 count, i.e. calculate the frequency freq = t1ov_cnt * TMR1H * TMR1L; }

Last years exam question - 1 Two C functions shown on the next slide use Timer 0 to generate delays of different time duration. a)Assuming f osc = 20 MHz, calculate delays generated by each of those functions. [15 marks] b)Now, use those two functions to write another C function, called “pattern_1”. This function should generate a specific pattern on pin 0 of Port C, every time it is called. The timing and signal level on pin 0 should look like the following: (high = 5V, low = 0V) [10 marks] High Low 0.5 ms 0.15 ms 0.5 ms

Last years exam question - 2 Rewrite the function Timer0_delay1() given in question one in order to generate delay of same duration, but now using Timer 2 instead of Timer 0 used in the original function. Assume f osc = 20 MHz. You can call this new function Timer2_delay1(). [25 marks] High Low 0.5 ms 0.15 ms 0.5 ms

Last years exam question - 1 void Timer0_delay1(void) { T0CON = 0x08; TMR0H = 0xFD; TMR0L = 0x12; T0CONbits.TMR0ON = 1;// Turn ON Timer 0 while(INTCONbits.TMR0IF == 0); T0CONbits.TMR0ON = 0;// Turn OFF Timer 0 INTCONbits.TMR0IF = 0;// clear Timer 0 interrupt flag } void Timer0_delay2(void) { T0CON = 0x08; TMR0H = 0xF6; TMR0L = 0x3C; T0CONbits.TMR0ON = 1;// Turn ON Timer 0 while(INTCONbits.TMR0IF == 0); T0CONbits.TMR0ON = 0;// Turn OFF Timer 0 INTCONbits.TMR0IF = 0;// clear Timer 0 interrupt flag }

Last years exam question 1 - Solution Delay generated by Timer 0 can be calculated using a simple equation: where T ic represents the amount of time taken by timer to increment its count by one and is calculated as (f osc is the oscillator frequency) T full is the full timer count, which is for 16-bit timer mode calculated as and T l is the actual loading of the timer (FD12 h = d for the first function and F63C h = d for the second function) After inserting two values for two different timer loadings into this equation, delays of andare obtained. This means that the function Timer0_delay1() generates 0.15 ms delay and the other function, Timer0_delay2() generates delay of 0.5 ms.

Last years exam question 1 - Solution Function below does the job of generating required pattern on Port C: void pattern_1(void) { TRISC0 = 0; RC0 = 1; Timer0_delay2(); // high for 0.5 ms RC0 = 0; Timer0_delay1(); // low for 0.15 ms RC0 = 1; Timer0_delay1(); // high for 0.15 ms RC0 = 0; Timer0_delay1(); // low for 0.15 ms RC0 = 1; Timer0_delay1(); // high for 0.15 ms RC0 = 0; Timer0_delay2(); // low for 0.5 ms }

Last years exam question 2 - Solution Formula to calculate time delay generated by Timer 2, i.e. Timer 2 overflow rate is: where: T ic represents the amount of time taken by timer to increment its count by one and is calculated as (f osc is the oscillator frequency) A is determined by the state of bits 3-6 from T2CON register (add 1 to a 4- bit value to get A) B is determined by the state of bits 0 and 1 from T2CON register (B can take values of 1,4 and 16 for various combinations of bits T2CKPS1 and T2CKPS0 from T2CON) C is the content of register PR2+1

Last years exam question 2 - Solution We can get close to the required delay of 0.15 ms (generated by the original function Timer0_delay1()) by using the following settings for A and B: A = 16 and B = 4. Parameter C can now be calculated by slightly rearranging the formula for T2 ofl, i.e.: Obtained result (for parameter C) implies the value of 12-1 = 11 loaded into register PR2. Value set for parameter A implies the value of 16-1 = 15 loaded into bits 3- 6 of register T2CON and value set for B implies T2CKPS1 = 0 and T2CKPS0 = 1 for bits 1 and 0 from register T2CON. This leads to the following registers T2CON and PR2 loadings required for the generation of 0.15 ms delay: T2CON = 79 h, PR2 = 000B h and will result in the following function that generates app ms delay using Timer 2: Note: any other combination of A, B and C resulting in app ms delay is OK, i.e. full marks.

Last years exam question 2 - Solution void Timer2_delay1(void) { // load T2CON and PR2 registers with pre-calculated values T2CON = 0x79; PR2 = 0x0B; T2CONbits.TMR2ON = 1;// start the Timer 2 while(!PIR1bits.TMR2IF);// wait for timer flag T2CONbits.TMR2ON = 0;// stop the Timer 2 PIR1bits.TMR2IF = 0;// clear timer flag }

Revision Topics Basic PIC programming –Using and configuring ports on PIC18F252 TRISx and PORTx registers on PIC18F252 Accessing individual bits in the above registers (i.e. using “PORTxbits.xx” and “TRISxbits.xx” type structures) Creating simple delays in the program, defining and declaring functions in the C program Timer 2 –Configuration and loading (i.e. loading Timer 2 for specific time delay, calculating delays achieved with specific Timer 2 loading)

Revision Topics Interrupts –Use of interrupts (compare interrupt and pooling techniques) –General principles – how does the CPU handle interrupt processing Interrupts on PIC18F252 –Understanding the template program for writing the ISRs (mixing assembler and C, #pragma directive etc.) –Interrupt priorities and multiple interrupts (interrupt vector table on PIC18F252)

Revision Topics External Hardware Interrupts –Use and (interrupt) configuration PortB - Change Interrupts –Use and (interrupt) configuration Timer 0 –Configuration and loading for specific delay Timer 1 – Configuration and loading for specific delay