Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE."— Presentation transcript:

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

2 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.

3 //------------------------------------------------------------------------------------------------------ // 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 1 - 16-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. //------------------------------------------------------------------------------------------------------

4 #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 }

5 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 * 65536 + TMR1H * 256 + TMR1L; }

6 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

7 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

8 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 }

9 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 = 64786 d for the first function and F63C h = 63036 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.

10 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 }

11 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

12 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. 0.15 ms delay using Timer 2: Note: any other combination of A, B and C resulting in app. 0.15 ms delay is OK, i.e. full marks.

13 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 }

14 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)

15 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)

16 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


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

Similar presentations


Ads by Google