(C30):
Код
#include "p24Fxxxx.h"
// using 24FJ256GB110 device
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
_CONFIG2( FCKSM_CSDCMD
& OSCIOFNC_OFF
& POSCMOD_HS // Кварц 8MHz
& FNOSC_PRIPLL // primary+pll в дальнейшем потребуется для USB
& DISUVREG_OFF // не надо питать юсб-девайсы
& PLL_96MHZ_ON
& IOL1WAY_ON)
#define maxtime 20000
void init_migat(void)
{
TRISA = 0b1100000000000000; // 1 - input
T1CON = 0b1000000000110000;
TRISD = 0b0000001100000000; // 0 - output
}
unsigned int migat_diodom(unsigned int DELAY)
{
if (DELAY == 0 || DELAY > maxtime) DELAY=3000;
char i; unsigned int N = (unsigned int)(maxtime/DELAY);
for (i = 1; i < N; i++)
{
TMR1 = 0;
PORTDbits.RD0 = !PORTDbits.RD0;
while (TMR1 < DELAY){}
}
return(DELAY);
}
void initAdc1(void)
{
AD1PCFG = 0x0000; // Configure A/D port
// Чтоб не промахнуться, ног (пока) много - 100 штук
AD1CON1 = 0x2002; // Configure sample clock source 0010 0000 0000 0010
// and conversion trigger mode.
// Unsigned Integer (FORM<1:0>=00),
// Manual conversion trigger (SSRC<2:0>=000),
// Manual start of sampling (ASAM=0),
// No operation in Idle mode (ADSIDL=1),
// S/H in Sample (SAMP = 1)
AD1CON2 = 0; // Configure A/D voltage reference
// and buffer fill modes.
// Vr+ and Vr- from AVdd and AVss (VCFG<2:0>=000),
// Inputs are not scanned,
// Interrupt after every sample
AD1CON3 = 0x0100; // Configure sample time = 1Tad,
// A/D conversion clock as Tcy
// Выбираем, какая нога будет входом АЦП:
// Connect AN? as positive input 0000xxxx0000xxxx
AD1CHS = 0b0000000000000000; // где xxxx - номер входа
AD1CSSL = 0; // No inputs are scanned.
IFS0bits.AD1IF = 0; // Clear A/D conversion interrupt.
IEC0bits.AD1IE = 1; // Enable A/D conversion interrupt
AD1CON1bits.ADON = 1; // Turn on A/D
AD1CON1bits.SAMP = 1; // Start sampling the input
migat_diodom(50);
AD1CON1bits.SAMP = 0; // End A/D sampling and start conversion
}
static unsigned int result;
void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void)
{
result = (unsigned int)ADC1BUF0;
IFS0bits.AD1IF = 0;
AD1CON1bits.SAMP = 1; // start sampling...
migat_diodom(result); // в качестве паузы мигаем диодом с периодом = result
AD1CON1bits.SAMP = 0; // start converting
}
int main (void)
{
init_migat();
RCONbits.SWDTEN = 0;// Disable Watch Dog Timer
initAdc1(); // Init the A/D converter
while (1);
return 0;
}
// using 24FJ256GB110 device
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2 )
_CONFIG2( FCKSM_CSDCMD
& OSCIOFNC_OFF
& POSCMOD_HS // Кварц 8MHz
& FNOSC_PRIPLL // primary+pll в дальнейшем потребуется для USB
& DISUVREG_OFF // не надо питать юсб-девайсы
& PLL_96MHZ_ON
& IOL1WAY_ON)
#define maxtime 20000
void init_migat(void)
{
TRISA = 0b1100000000000000; // 1 - input
T1CON = 0b1000000000110000;
TRISD = 0b0000001100000000; // 0 - output
}
unsigned int migat_diodom(unsigned int DELAY)
{
if (DELAY == 0 || DELAY > maxtime) DELAY=3000;
char i; unsigned int N = (unsigned int)(maxtime/DELAY);
for (i = 1; i < N; i++)
{
TMR1 = 0;
PORTDbits.RD0 = !PORTDbits.RD0;
while (TMR1 < DELAY){}
}
return(DELAY);
}
void initAdc1(void)
{
AD1PCFG = 0x0000; // Configure A/D port
// Чтоб не промахнуться, ног (пока) много - 100 штук
AD1CON1 = 0x2002; // Configure sample clock source 0010 0000 0000 0010
// and conversion trigger mode.
// Unsigned Integer (FORM<1:0>=00),
// Manual conversion trigger (SSRC<2:0>=000),
// Manual start of sampling (ASAM=0),
// No operation in Idle mode (ADSIDL=1),
// S/H in Sample (SAMP = 1)
AD1CON2 = 0; // Configure A/D voltage reference
// and buffer fill modes.
// Vr+ and Vr- from AVdd and AVss (VCFG<2:0>=000),
// Inputs are not scanned,
// Interrupt after every sample
AD1CON3 = 0x0100; // Configure sample time = 1Tad,
// A/D conversion clock as Tcy
// Выбираем, какая нога будет входом АЦП:
// Connect AN? as positive input 0000xxxx0000xxxx
AD1CHS = 0b0000000000000000; // где xxxx - номер входа
AD1CSSL = 0; // No inputs are scanned.
IFS0bits.AD1IF = 0; // Clear A/D conversion interrupt.
IEC0bits.AD1IE = 1; // Enable A/D conversion interrupt
AD1CON1bits.ADON = 1; // Turn on A/D
AD1CON1bits.SAMP = 1; // Start sampling the input
migat_diodom(50);
AD1CON1bits.SAMP = 0; // End A/D sampling and start conversion
}
static unsigned int result;
void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void)
{
result = (unsigned int)ADC1BUF0;
IFS0bits.AD1IF = 0;
AD1CON1bits.SAMP = 1; // start sampling...
migat_diodom(result); // в качестве паузы мигаем диодом с периодом = result
AD1CON1bits.SAMP = 0; // start converting
}
int main (void)
{
init_migat();
RCONbits.SWDTEN = 0;// Disable Watch Dog Timer
initAdc1(); // Init the A/D converter
while (1);
return 0;
}
Вход AN0 подключен к реостату ([http://www.techtoys.com.hk/PIC_boards/PIC24-Eval-C/PIC24-Eval-RevC.htm]хардвер[/url])
Почемуто если я подставляю в AD1CHS = 0b0000xxxx0000xxxx; любой xxxx > 0, то соответствующий вход работает (подключаю реотстат туда параллельно с AN0 и от кручения движка реостата получается дивная музыка), а если я ставлю AD1CHS = 0b0000000000000000; то диод мигает с постоянной частотой, т.е. ADC1BUF0 не меняется, хотя АЦП запускается, делает преобразование и вызывает прерывание. Поковырявшись выяснил что он всё же меняется случайным образом, но не более чем на одну единицу младшего разряда, а от напряжения на AN0 (= нога № 25) не зависит никак

Peripheral Pin Select, как я понял, по умолчанию на этот вывод ничего не назначает. Тогда почему не оцифровывается напряжение на нём?