+ All Categories
Home > Documents > C Examples 5

C Examples 5

Date post: 01-Jan-2016
Category:
Upload: silas-flynn
View: 25 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
Popular Tags:
15
C Examples 5
Transcript

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

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

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

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 }

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

GP2D120

Components: GP2D120

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


Recommended