Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Examples 3. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

Similar presentations


Presentation on theme: "C Examples 3. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference."— Presentation transcript:

1 C Examples 3

2 Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference

3

4

5 Key DEBOUNCING

6 Key DEBOUNCING – 3 (SWITCH4B) void main() { int start = 0; ADPCFG = 0xFFFF; TRISB=0b111111101; PORTB=0x0000; TRISF=0b000001; PORTF=0x0000; while(1) { Delay_ms(5000); // LONG DELAY IMPAIRS SWITCH OPERATION PORTB=~PORTB; // LED FLASHING if (Button(&PORTF, 0, 1, 1)) // Switch at PORTF Bit 0 start = 1; if (start&&Button(&PORTF, 0, 1, 0)) // Switch at PORTF Bit 0 { start=0; PORTF.F1=PORTF.F1^1; // Toggles PORTF Bit 1 }

7 Key DEBOUNCING – 4 (SWITCH5B) void main () { int start=0; T2CON=0b1000000000001000; // Enable Timer 2/3, Prescaler =1 IEC0=0x0080; // Enable Interrupt for Timer 3 PR2=0xE100; // (PR3PR2)BASE16 = (100,000,000)BASE10 PR3=0x05F5; ADPCFG = 0xFFFF; TRISB=0b111111101; PORTB=0x0000; TRISF=0b000001; PORTF=0x0000; while(1) { if (Button(&PORTF, 0, 1, 1)) // Switch at PORTF Bit 0 start = 1; if (start&&Button(&PORTF, 0, 1, 0)) // Switch at PORTF Bit 0 { start=0; PORTF.F1=PORTF.F1^1; // Toggles PORTF Bit 1 } void interrupt_T2() org 0x000022 { PORTB=PORTB^0x02; IFS0=0x0000; }

8 UART1 U1MODE = 0x8400;

9 UART1 unsigned char uc1, rx1; void main() { ADPCFG=0xFFFF; // Starts UART Uart1_Init(19200); // U1MODE = 0x8400; // This instruction required if using alternate pinout delay_ms(200); TRISB=0x0000; PORTB=0xFFFF; Uart1_Write_Char('a'); while(1) { if (Uart1_Data_Ready()) { rx1 = Uart1_Read_Char(); Uart1_Write_Char(++rx1); PORTB=~PORTB; } }//~!

10

11

12

13

14

15 UART2B-EEL unsigned adcRes; char txt[6]; void main() { int i; PORTB = 0x0000; TRISB.F1 = 1; // set pin as input - needed for ADC to work Uart1_Init(19200); while (1) { adcRes = Adc_Read(1); // Read ADC channel 1 WordToStr(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; Delay_ms(1000); } }//~!

16 UART2B-CPE unsigned adcRes; char txt[6]; void Uart1_Write_Text(char *txt_to_wr) { while (*txt_to_wr) Uart1_Write_Char(*(txt_to_wr++)); } void main() { PORTB = 0x0000; TRISB.F1 = 1; // set pin as input - needed for ADC to work Uart1_Init(19200); while (1) { adcRes = Adc_Read(1); WordToStr(adcRes, txt); Uart1_Write_Text(txt); Delay_ms(1000); } }//~!

17

18 The Family reference manual shows the connection in red, it does not show the direct connection to VREF-

19

20 Sampling Time To minimize the effects of pin leakage currents on the accuracy of the A/D converter, the maximum recommended source impedance, RS, is 5 kΩ for the conversion rates of up to 500 ksps and a maximum of 500Ω for conversion rates of up to 1 Msps

21 UART manually unsigned char data; int main(){ U1MODE = 0X0000; // 8 data bits no parity 1 stop bit IEC0= 0x0600; // Interrupts enabled both transmit and receive IPC2= 0x0760; // Highest priority for both interrupts U1STA = 0x8000; // Interrupt when transmit buffer empty, Interrupt when a character is received U1MODE = U1MODE | 0x8000; // Enable UART Module U1STA = U1STA | 0x0400; // Enable Transmission IFS0 = IFS0 & 0xF9FF; // Clear UART Interrupt Flags TRISB = 0; PORTB = 0XFFFF; ADPCFG = 0XFFFF; while(1){ } void interrupt_UART1_receive() org 0x000026{ data = U1RXREG; // Read Buffer IFS0 = IFS0 & 0xFDFF; // Clear UART Interrupt Flags U1RXREG = ++data; // Transmit Data U1STA = U1STA | 0x0400; // Enable Transmission } void interrupt_UART1_transmit() org 0x000028{ PORTB = ~PORTB; IFS0 = IFS0 & 0xFBFF; // Clear UART Interrupt Flags }

22 Initialize the UxBRG register for the appropriate baud rate. U1RBG = 64; Set the number of data bits, number of STOP bits, and parity selection by writing to the PDSEL (UxMODE ) and STSEL (UxMODE ) bits. U1MODE = 0X0000; 8 data bits no parity 1 stop bit

23 If transmit interrupts are desired, set the UxTXIE control bit in the corresponding interrupt enable control register (IEC). IEC0= 0x0600; Interrupts enabled both transmit and receive

24 Specify the interrupt priority using the UxTXIP control bits in the corresponding interrupt priority control register (IPC). IPC2= 0x0770; Highest priority for both interrupts

25 Select the transmit interrupt mode by writing the UTXISEL (UxMODE ) bit. U1STA = 0x8000; Interrupt when transmit buffer empty Interrupt when a character is received

26 Enable the UART module by setting the UARTEN (UxMODE ) bit. U1MODE = U1MODE | 0x8000;

27 Enable the transmission by setting the UTXEN (UxSTA ) bit. This will also set the transmit interrupt flag UxTXIF bit. During the initialization, the interrupt request of the UART module transmitter UxTXIF bit should be cleared. Also in the interrupt service routine the interrupt request UxTXIF should be cleared. U1STA = USTA | 0x0400;

28 The interrupt request of the UART module transmitter UxTXIF bit should be cleared. Also in the interrupt service routine the interrupt request UxTXIF should be cleared. IFS0 = IFS0 & 0xF9FF;

29 Finally, load data to the transmit FIFO buffer by writing to the UxTXREG register. If 8-bit transmission is used, load a byte (8-bits). If 9- bit transmission has been selected, load a word (9-bits, higher bits are ignored).


Download ppt "C Examples 3. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference."

Similar presentations


Ads by Google