c examples 5. download links mplab ide dspic30f4011/4012 data sheet dspic30f family reference manual...
TRANSCRIPT
![Page 1: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/1.jpg)
C Examples 5
![Page 2: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/2.jpg)
Download Links
MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference
![Page 3: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/3.jpg)
![Page 4: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/4.jpg)
Measuring Tin Will include an offset error
Decoupling Capacitor needed
![Page 5: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/5.jpg)
Input Capture
![Page 6: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/6.jpg)
IC1CON = 0x0082
![Page 7: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/7.jpg)
![Page 8: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/8.jpg)
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 }
Ping Distance Measurement
![Page 9: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/9.jpg)
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 } }
sMhz
DurationTMR
PulsesMHz
TCY
820
1602
1608*2020
1
![Page 10: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/10.jpg)
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 }
![Page 11: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/11.jpg)
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 }
![Page 12: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/12.jpg)
cmTMR
dist
mTMR
TMRm
dist
mms
timedist
msmmsdist
TMRMHz
time
147
2
705,14
22
105.18*1020
145.3*8
145.3*5.18
145.3/340*5.182
1max
8*220
1
36
![Page 13: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/13.jpg)
GP2D120
![Page 14: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/14.jpg)
Components: GP2D120
![Page 15: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference](https://reader033.vdocument.in/reader033/viewer/2022061602/56649ef65503460f94c0a964/html5/thumbnails/15.jpg)
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; } } }//~!