160 likes | 272 Views
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.
E N D
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
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 • }
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 • } • }
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 • }
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 • }
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; • } • } • }//~!