1 / 30

C Examples 1

C Examples 1. Download Links. MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference. LED ON. main () { TRISB=0b11111101; while(1) { PORTB.F1=1; } }.

israel
Download Presentation

C Examples 1

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 1

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

  3. LED ON • main () • { • TRISB=0b11111101; • while(1) • { • PORTB.F1=1; • } • }

  4. Blinking LED - 1 • main () • { • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB.F1=1; • while(1) • { • Delay_ms(1000); • PORTB.F1=PORTB.F1^1; • } • }

  5. Blinking LED - 2 • main () • { • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB.F1=1; • while(1) • { • Delay_ms(1000); • PORTB=PORTB^0x02; • } • }

  6. Timer 1 • 16-bit Timer Mode: In the 16-bit Timer mode, the timer increments on every instruction cycle up to a match value, preloaded into the Period register, PR1, then resets to 0 and continues to count. • When the CPU goes into the Idle mode, the timer will stop incrementing unless the TSIDL (T1CON<13>) bit 0. If TSIDL 1, the timer module logic will resume the incrementing sequence upon termination of the CPU Idle mode. • 16-bit Synchronous Counter Mode: In the 16-bit Synchronous Counter mode, the timer increments on the rising edge of the applied external clock signal, which is synchronized with the internal phase clocks. The timer counts up to a match value preloaded in PR1, then resets to 0 and continues. • When the CPU goes into the Idle mode, the timer will stop incrementing unless the respective TSIDL bit o. If TSIDL 1, the timer module logic will resume the incrementing sequence upon termination of the CPU Idle mode.

  7. Blinking LED - 3 • main () • { • T2CON=0x8030; // Enable Timer 2 Prescaler=256 • IEC0=0x0040; // Enable Interrupt for Timer 2 • PR2=0xFFFF; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB=PORTB|0b00000010; • while(1) • { • } • } • void interrupt_T2() org 0x000020 • { • PORTB=PORTB^0x02; • IFS0=0x0000; • }

  8. Blinking LED - 3 • main () • { • T2CON=0b1000000000001000; // Enable Timer 2/3 • IEC0=0x0080; // Enable Interrupt for Timer 3 • PR2=0xFFFF; • PR3=0x0AFF; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB=0x00; • while(1) • { • } • } • void interrupt_T2() org 0x000022 • { • PORTB=PORTB^0x02; • IFS0=0x0000; • }

  9. Switch + LED • main () • { • ADPCFG = 0xFFFF; • TRISB=0b11111101; • TRISE=0b000001; • PORTB=0x00; • PORTE=0x1F; • while(1) • { • if((PORTE&0b000001)==0) • {PORTE.F1=PORTE.F1^1;} • } • }

  10. Key DEBOUNCING - 1 • main () • { • int i; • const int Twentyms = 4210; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • TRISE=0b000001; • PORTB=0x00; • PORTE=0x1F; • while(1) • { • i = 0; // Wait 20 ms for Button Up • while (i < Twentyms) • { • if (0 == PORTE.F0) // Button Down/Start over • { • i = 0; • } • else // Button Up/Increment Count • { • i = i + 1; • } // • } // • i = 0; // Wait 20 ms for Button Down • while (i < Twentyms) • if (1 == PORTE.F0) // Button Up/Start over • i = 0; • else // Button Down/Increment Count • i = i + 1; • PORTE.F1=PORTE.F1^1; // Toggle LED to Turn ON/OFF LED • } • }

  11. Key DEBOUNCING - 2 • int function_key(void) • { • int i,m; • const int Twentyms = 4210; • m=1; • if (1 == PORTE.F0) {return(m);} • while(1) • { • i = 0; // Wait 20 ms for Button Up • while (i < Twentyms) • { • if (0 == PORTE.F0) // Button Down/Start over • { • i = 0; • } • else // Button Up/Increment Count • { • i = i + 1; • } // • } // • i = 0; // Wait 20 ms for Button Down • while (i < Twentyms) • if (1 == PORTE.F0) // Button Up/Start over • i = 0; • else // Button Down/Increment Count • i = i + 1; • m=0; // Toggle LED to Turn ON/OFF LED • return(m); • } • }

  12. Key DEBOUNCING - 2 • main () • { • int s=1; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB=0x00; • TRISE=0b000001; • PORTE=0x1F; • while(1) • { • s=function_key(); • if (s==0) PORTE.F1=PORTE.F1^1; • S=1; • } • }

  13. Key DEBOUNCING - 3 • main () • { • int s=1; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB=0x00; • TRISE=0b000001; • PORTE=0x1F; • while(1) • { • Delay_ms(5000); • PORTB=~PORTB; • s=function_key(); • if (s==0) PORTE.F1=PORTE.F1^1; • S=1; • } • }

  14. Key DEBOUNCING - 4 • main () • { • int s=1; • T2CON=0x8030; // Enable Timer 2 Prescaler=256 • IEC0=0x0040; // Enable Interrupt for Timer 2 • PR2=0xFFFF; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB=0x00; • TRISE=0b000001; • PORTE=0x1F; • while(1) • { • s=function_key(); • if (s==0) PORTE.F1=PORTE.F1^1; • S=1; • } • } • void interrupt_T2() org 0x000020 • { • PORTB=PORTB^0x02; • IFS0=0x0000; • }

  15. UART1

  16. UART1 • unsigned rx1; • unsigned char uc1; • void main() { • Uart1_Init(19200); • U1MODE = 0x8400; • delay_ms(200); • TRISE=0b000001; • PORTE=0x1F; • Uart1_Write_Char('a'); • while(1) { • if (Uart1_Data_Ready()) { • rx1 = Uart1_Read_Char(); • Uart1_Write_Char(++rx1); • PORTE=~PORTE; • } • } • }//~!

  17. UART2 • unsigned rx1; • unsigned adcRes; • unsigned char uc1; • void main() { • PORTB = 0x0000; • TRISB = 0xFFFF; • Uart1_Init(19200); • U1MODE = 0x8400; • delay_ms(200); • TRISE=0b000001; • PORTE=0x1F; • Uart1_Write_Char('a'); • while(1) { • adcRes = Adc_Read(3); • Uart1_Write_Char(adcRes); • PORTE=~PORTE; • delay_ms(1000); • } • }//~!

More Related