200 likes | 315 Vues
This guide provides code examples and explanations for programming the dsPIC30F family of microcontrollers. It covers implementing debouncing for switches, initializing UART communication, and reading ADC values. The sample code demonstrates how to create a responsive user interface by avoiding switch bounce and how to send data over UART. This resource is essential for developing embedded systems with dsPIC30F4011/4012/4013/4014 microcontrollers, including practical tips for setup and operation.
E N D
Download Links • dsPIC30F4011/4012 Data Sheet • dsPIC30F4013/3014 • dsPIC30F Family Reference Manual • MikroC • MikroC Manual • MikroC Quick Reference
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 } } }
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; }
UART1 • U1MODE = 0x8400;
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; • } • } • }//~!
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); • } • } • }//~!
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); • } • }//~!
The Family reference manual shows the connection in red, it does not show the direct connection to VREF-
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