I am migrating a design from 18F4520 to dsPIC33FJ32GS606. Clock is generated by 6MHz crystal.
I have all done EXCEPT reading ADC1. It hangs.
I have taken the demo for the dsPIC33FJ256GP710A. Code is :
Code: Select all
program ADC_Demo;
var adc_result : longint;
sx : string[6];
begin
// PLL settings
PLLPRE_4_bit := 0; // PLLPRE<4:0> = 0 -> N1 = 2 8MHz / 2 = 4MHz
PLLPRE_3_bit := 0; // (must be within 0.8 MHz to 8 MHz range)
PLLPRE_2_bit := 0;
PLLPRE_1_bit := 0;
PLLPRE_0_bit := 0;
PLLFBD := 38; // PLLDIV<8:0> = 38 -> M = 40 4MHz * 40 = 160MHz
// (must be within 100 MHz to 200 MHz range)
PLLPOST_0_bit := 0; // PLLPOST<1:0> = 0 -> N2 = 2 160MHz / 2 = 80MHz
PLLPOST_0_bit := 0; // (must be within 12.5 MHz to 80 MHz range)
ADPCFG := 0xFF00; // Set PCFG0 as analog input
UART2_Init_Advanced(9600, 1, 1, 1); Delay_ms(100); // Wait for UART module to stabilize
TRISD := 0; // Set PORTD as output
TRISB := 0xFFFF;
adc_result := 0; // Set initial ADC value to zero
ADC1_Init; // Initialize ADC module
Delay_ms(100);
while TRUE do
begin
Uart2_write('.');
adc_result := ADC1_Get_Sample(5); // Get ADC value from corresponding channel
// adc_result := ADC1_Read(5); // Get ADC value from corresponding channel
wordtostr(adc_result,sx);
Uart2_write_text(sx);
Delay_ms(50);
end;
end.