1 / 15

C Examples 5

C Examples 5. Download Links. MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference. Measuring Tin Will include an offset error. Decoupling Capacitor needed. Input Capture. IC1CON = 0x0082. Ping Distance Measurement.

silas-flynn
Download Presentation

C Examples 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Examples 5

  2. Download Links • MPLAB IDE • dsPIC30F4011/4012 Data Sheet • dsPIC30F Family Reference Manual • MikroC • MikroC Manual • MikroC Quick Reference

  3. Measuring Tin Will include an offset error Decoupling Capacitor needed

  4. Input Capture

  5. IC1CON = 0x0082

  6. Ping Distance Measurement • int dist, i; • char txt[6]; • void main () // 20 MHz and PLL 80 • { • Uart1_Init(19200); • TRISD=0b11111110; • PR2=20; • T2CON=0x8010; // Enable Timer 2 Prescaler=8 pulse will be 8 useconds • while(1) • { • Delay_ms(1000); • PORTD = 1; // Start Pulse • TMR2 = 0; // Reset Timer 2 • IEC0=0x0040; // Only Interrupt for Timer 2 is enabled. To stop Pulse • } • } • void interrupt_T2() org 0x000020 • { • PORTD.F0 =0; // End Pulse • IEC0=0x0002; // Only input capture interrupt 1 is enabled • IC1CON = 0x0082; // Interrupt capture Timer 2 Every falling edge • TRISD=TRISD|0b00000001; // Set D0 to input to wait for echo • PR2=0xFFFF; // Allow maximum count on TIMER2 • TMR2 = 0; // Reset Timer 2 • IFS0=0x0000; // Clear interrupt flags • } • void interrupt_I1() org 0x000016 • { • dist =TMR2/147-13; // Compensates offset error • WordToStr(dist, txt); // Convert distance value to text • i=0; • while (txt[i]) • { • Uart1_Write_Char(txt[i]); // Send text to UART one character at a time • i=i+1; • } • IC1CON = 0x0000; // Interrupt capture off • IEC0=0x0000; // Disable interrupts • TRISD=TRISD&0b11111110; // Return D0 to output • PR2=20; // Prepare to generate next pulse • IFS0=0x0000; // Clear interrupt Flags • }

  7. Main Function • int dist, i; • char txt[6]; • void main () // 20 MHz and PLL 80 • { • Uart1_Init(19200); • TRISD=0b11111110; • PR2=20; • T2CON=0x8010; // Enable Timer 2 Prescaler=8 pulse will be 8 useconds • while(1) • { • Delay_ms(1000); • PORTD = 1; // Start Pulse • TMR2 = 0; // Reset Timer 2 • IEC0=0x0040; // Only Interrupt for Timer 2 is enabled. To stop Pulse • } • }

  8. Start Pulse Generation • void interrupt_T2() org 0x000020 • { • PORTD.F0 =0; // End Pulse • IEC0=0x0002; // Only input capture interrupt 1 is enabled • IC1CON = 0x0082; // Interrupt capture Timer 2 Every falling edge • TRISD=TRISD|0b00000001; // Set D0 to input to wait for echo • PR2=0xFFFF; // Allow maximum count on TIMER2 • TMR2 = 0; // Reset Timer 2 • IFS0=0x0000; // Clear interrupt flags • }

  9. Time Measurement • void interrupt_I1() org 0x000016 • { • dist =TMR2/147-13; // Compensates offset error • WordToStr(dist, txt); // Convert distance value to text • i=0; • while (txt[i]) • { • Uart1_Write_Char(txt[i]); // Send text to UART one character at a time • i=i+1; • } • IC1CON = 0x0000; // Interrupt capture off • IEC0=0x0000; // Disable interrupts • TRISD=TRISD&0b11111110; // Return D0 to output • PR2=20; // Prepare to generate next pulse • IFS0=0x0000; // Clear interrupt Flags • }

  10. GP2D120

  11. Components: GP2D120

  12. GP2D120 Distance Measurement • // Measures distance with GP2D120 • //unsigned float adcRes; • unsigned long adcRes; • char txt[15]; • void main() { • int i; • PORTB = 0x0000; • TRISB.F1 = 1; // set pin as input - needed for ADC to work • Uart1_Init(19200); • while (1) { • Delay_ms(1000); • adcRes = Adc_Read(0); // Read ADC channel 1 • adcRes = adcRes*500/1023; // Transform ADC result into volts • adcRes = 128000/adcRes; // Calculates distance • // FloatToStr(adcRes, txt); // Convert ADC value to text • LongToStr(adcRes, txt); // Convert ADC value to text • i=0; • while (txt[i]) • { • Uart1_Write_Char(txt[i]); // Send text to UART one character at a time • i=i+1; • } • } • }//~!

More Related