Top Banner
C Examples 5
15

C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

Jan 13, 2016

Download

Documents

Lionel Morris
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

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.

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.
Page 4: 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

Page 5: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

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.

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.
Page 8: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

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.

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.

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.

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.

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.

GP2D120

Page 14: C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

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.

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; } } }//~!