Automatically Read A Single Analog Input CloseADC10();// ensure the ADC is off before setting the configuration OpenADC10( (ADC_MODULE_ON | ADC_IDLE_STOP | ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_SAMP_ON), //AD1CON1 register (ADC_VREF_AVDD_AVSS | ADC_SCAN_OFF | ADC_ALT_INPUT_OFF | ADC_SAMPLES_PER_INT_10), //AD1CON2 register (ADC_SAMPLE_TIME_4 | ADC_CONV_CLK_PB | ADC_CONV_CLK_3Tcy), //AD1CON3 register (SET THE SAMPLE TIME ETC) (ENABLE_AN0_ANA), //AD1PCFG register (SET […]
All posts by
PWM Output
Simple PWM Ouptut Setup timer 2 or 3 to be the timebase. The PR value used for the timer will be the total PWM period: //—– SETUP TIMER 2 —– //Used for: PWM OpenTimer2((T2_ON | T2_IDLE_CON | T2_GATE_OFF | T2_PS_1_1 | T2_SOURCE_INT), 5000); Turn on the PWM output. The OCxRS value will be the duty […]
Interrupt Functions
Interrupt Priority Level 1 (lowest) to 7 (highest) Multi Vector / Single Vector Interrupts //—————————– //—– ENABLE INTERRUPTS —– //—————————– //INTEnableSystemSingleVectoredInt(); INTEnableSystemMultiVectoredInt(); Interrupt Functions New Style void __ISR(_TIMER_5_VECTOR, IPL7SRS) Timer5IntHandler(void) //(<<<<<<< IPL# must match the priority level assigned to the irq where its enabed, Use 'SRS' if config FSRSSEL is assigned to same level, or use […]
Configuration Bits
Getting Configuration Bit Settings From MPLABX In MPLABX you can open the Configuration settings window (Window > PIC Memory Views > Configuration Bits) and set the desired configuration from the IDE. When finished click on the 'Generate Source Code to Output' button to get the settings for you to copy and paste into your source file.
Inline assembly generates “error: (876) syntax error”
#asm GLOBAL _uc_asm_irq_temp GLOBAL _uc_asm_irq_temp1 //Reset timer for next rollover movff TMR0L,_uc_asm_irq_temp //read_current_timer_value (read low byte loads high byte) movff TMR0H,_uc_asm_irq_temp1 movlw 0xfb addwf _uc_asm_irq_temp,1,0 //(1 = file register, 0 = access ram) movlw 0xd8 addwfc _uc_asm_irq_temp1,1,0 //(1 = file register, 0 = access ram) movff _uc_asm_irq_temp1,TMR0H //Store new value (high byte first) movff […]
Non interrupt SPI
Simple Example WORD rx_data; SpiChnOpen(2, (SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CKE_ON), 10); //20MHz fpb / 10 = 2MHz. Clock idles low, SDO changes on falling edge of clock DAC_CS(0); SpiChnPutC(2, (int)(0xff)); rx_data = SpiChnGetC(2); DAC_CS(1);
Non IRQ timer
Using a Timer Without An IRQ //Setup the timer OpenTimer2((T2_ON | T2_IDLE_CON | T2_GATE_OFF | T2_PS_1_1 | T2_32BIT_MODE_OFF | T2_SOURCE_INT), 100); INTClearFlag(INT_T2); //Wait for timer to loop while (!INTGetFlag(INT_T2)) ; INTClearFlag(INT_T2); Faster Bit Test Of IRQ Flag The INTGetFlag() function is not particuarly fast. If you want very tight timing then do a bit […]
Rename project
Open the project in MPLAB X, right click it and select rename.
Using assembly code
Simple example of changes to typical heartbeat timer irq inline assembly Original C18 Code _asm //Reset timer for next rollover movff TMR0L,uc_asm_irq_temp //read_current_timer_value (read low byte loads high byte) movff TMR0H,uc_asm_irq_temp1 movlw 0xbb addwf uc_asm_irq_temp,1,0 //(1 = file register, 0 = access ram) movlw 0x3c addwfc uc_asm_irq_temp1,1,0 //(1 = file register, 0 = access ram) […]
.General C18 to XC18 differences
See the "C18 to XC8 C Compiler Migration Guide"