210 likes | 284 Views
C Examples 1. Download Links. dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference. Parallel I/O (PIO) Ports.
E N D
Download Links • dsPIC30F4011/4012 Data Sheet • dsPIC30F4013/3014 • dsPIC30F Family Reference Manual • MikroC • MikroC Manual • MikroC Quick Reference
Parallel I/O (PIO) Ports • All port pins have three registers directly associated with the operation of the port pin. The Data Direction register (TRISx) determines whether the pin is an input or an output. If the Data Direction register bit is a ‘1’, then the pin is an input. All port pins are defined as inputs after a Reset. • Reads from the latch (LATx), read the latch. • Writes to the latch, write the latch (LATx). • Reads from the port (PORTx), read the port pins and • writes to the port pins, write the latch (LATx). • When a peripheral is enabled and the peripheral is actively driving an associated pin, the use of the pin as a general purpose output pin is disabled. The I/O pin may be read, but the output driver for the Parallel Port bit will be disabled. If a peripheral is enabled, but the peripheral is not actively driving a pin, that pin may be driven by a port.
LED ON void main () { ADPCFG = 0xFFFF; // In order to use PORTB as digital input TRISB=0b11111101; // Sets bit 1 as output, the other bits as inputs. See comments Bellow while(1) // Infinite Loop { PORTB.F1=1; // Sets Bit 1 of PORT B to logic “1”. See comments Bellow } } // The mikroC for dsPIC30/33 and PIC24 allows you to access individual (works with micro variables) // bits of 16-bit variables. Simply use the direct member selector (.) // with a variable, followed by one of identifiers F0, F1, … , F15 with // F15 being the most significant bit. // All dsPIC30/33 and PIC24 SFR registers are also available as structures // with bitfields named identically to the Microchip datasheets in order // to facilitate bit access e.g // // TRISBbits.TRISB3 = 1.
FLASH 1 void main () { ADPCFG = 0xFFFF; // In order to use PORTB as digital input TRISB=0b11111101; // Sets bit 1 as output, the other bits as inputs. See comments Bellow PORTB.F1=1; // Sets Bit 1 of PORT B to logic “1”. See comments Bellow while(1) // Infinite Loop { Delay_ms(1000); // Library function delay ms PORTB.F1=PORTB.F1^1; // Takes the complement } }
Blinking LED - 2 void main () { ADPCFG = 0xFFFF; TRISB=0b11111101; PORTB=PORTB|0b00000010; // Bitwise setting of bit 1 while(1) { Delay_ms(1000); PORTB=PORTB^0x02; // Bitwise toggle of bit 1 } }
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. • 16-bit Asynchronous Counter Mode: In the 16-bit Asynchronous Counter mode, the timer increments on every rising edge of the applied external clock signal. The timer counts up to a match value preloaded in PR1, then resets to 0 and continues. • The 16-bit timer can be placed in the Gated Time Accumulation mode. This mode allows the internal TcY to increment the respective timer when the gate input signal (Ti CK pin) is asserted high. Control bit, TGATE (T1CON<6>), must be set to enable this mode. The timer must be enabled (TON 1) and the timer clock source set to internal (TCS 0). • 16-bit Timer Mode • 16-bit Synchronous Counter • 16-bit Asynchronous Counter Mode • The 16-bit timer in Gated Time Accumulation mode
Blinking LED - 3 • void main () // 10 MHz and PLL 4 @ 20MHZ not possible to generate 1 sec delay • { • T2CON=0x8030; // Enable Timer 2 Prescaler=256 • IEC0=0x0040; // Enable Interrupt for Timer 2 • PR2=0x9897; • ADPCFG = 0xFFFF; • TRISB=0b11111101; • PORTB=PORTB|0b00000010; • while(1) • { • } • } • void interrupt_T2() org 0x000020 • { • PORTB=PORTB^0x02; • IFS0=0x0000; • // IFS0 = IFS0&0b1111111110111111; // If you want to turn off just this flag • }
Blinking LED - 4 • void main () • { • ADPCFG = 0xFFFF; • T2CON=0b1000000000001000; // Enable Timer 2/3, Prescaler =1 • IEC0=0x0080; // Enable Interrupt for Timer 3 • PR2=0x2D00; // (PR3PR2)BASE16 = (20,000,000)BASE10 • PR3=0x0131; • // There will be an interrupt every 20,000,000 instruction cycles • TRISB=0b11111101; • PORTB=0x00; • while(1) • { • } • } • void interrupt_T2() org 0x000022 • { • PORTB=PORTB^0x02; • IFS0=0x0000; • }
Counter • void main () • { • ADPCFG = 0xFFFF; • T2CON=0b1000000000001000; // Enable Timer 2/3, Prescaler =1 • IEC0=0x0080; // Enable Interrupt for Timer 3 • PR2=0x2D00; // (PR3PR2)BASE16 = (20,000,000)BASE10 • PR3=0x0131; • // There will be an interrupt every 20,000,000 instruction cycles • TRISB=0b11111101; • TRISF=0x0000; • PORTB=0x00; • while(1) • { • } • } • void interrupt_T2() org 0x000022 • { • PORTB=PORTB^0x02; • PORTF=PORTF+PORTB.F1; • IFS0=0x0000; • }