Em xin tách ra cho dễ nhìn nha ^_^ :
ADC:
Code:
#define SAMPLINGRATE 1800
#define SAMPCOUNT (FCY/SAMPLINGRATE)+1 //dung cho TMR3 cho ADC
#define adc_read(pin) adc_buf[pin]
unsigned int adc_buf[16];
volatile unsigned int* ADC16Ptr = &ADCBUF0; //Pointer to ADC register buffer,
unsigned int adc_filter_buffer[4][6] = { {0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0} };
unsigned int current_buf = 1;
//==== ADC ====
void adc_init()
{
adc_stop(); // reset adc
//====ADCON1====
ADCON1bits.SIMSAM = 0; // SIMSAM = 0 lay mau tung kenh 1 cach tuan tu
// SIMSAM = 1 lay mau 4 kenh (0,1,2,3) 1 cach dong thoi
ADCON1bits.FORM = 0b00; // output la integer
ADCON1bits.SSRC = 0b010; // chon bo dem de chuyen doi (3bit) => dung Timer 3 de dem
ADCON1bits.ASAM = 1; // 1 : tu dong tiep tuc lay mau khi chuyen doi xong ,bit SAMP tu dong set
// 0 : chuyen doi xong ,doi bit SAMP set thi moi tiep tuc lay mau
//====ADCON2====
ADCON2bits.SMPI = 5; // SMPI gom 4bit ,tuong ung uC ngat khi 1->16 lan lay mau
ADCON2bits.CHPS = 0; // CHPS = 00 chon kenh 0 de chuyen doi
// CHPS = 01 chon kenh 0 va kenh 1 de chuyen doi
// CHPS = 1x chon tat ca cac kenh de chuyen doi
ADCON2bits.BUFM = 0; // BUFM = 0 lay 1 lan 16 bit
// BUFM = 1 lay 2 lan ,moi lan 8 bit
ADCON2bits.BUFS = 0;
ADCON2bits.ALTS = 0; // ALTS = 0 luon luon su dung MUX A lam bo don kenh
// ALTS = 1 ban dau dung Mux A ,sau do xen ke MuxB va Mux A lam bo don kenh
ADCON2bits.CSCNA = 1; // 1 quet ngo vao
// 0 ko quet
ADCON2bits.VCFG = 0b000; // 3bit : chon nguon tham khao => dung AVdd va AVss
//====ADCON3====
ADCON3bits.SAMC = 0b11111; // dung 31 Tad cho lay mau
ADCON3bits.ADRC = 0; // dung clock he thong
ADCON3bits.ADCS = 0b011111; // chon xung chuyen doi (6bit tuong ung 1->32xTcy)
//====ADCHS====
ADCHSbits.CH0SA = 2; // CH0SA = 0 chon nguon duong cua kenh A la AN0
// CH0SA = x chon nguon duong cua kenh A la ANx
ADCHSbits.CH0NA = 0; // CH0NA = 0 chon nguon am cua kenh A la Vref-
// CH0NA = 1 chon nguon am cua kenh A la AN1
// chon nguon am duong cho cac kenh 1,2,3
ADCHSbits.CH123SA = 0;
ADCHSbits.CH123NA = 0;
// MUX B Input Select
ADCHSbits.CH123SB = 0;
ADCHSbits.CH123NB = 0;
//====ADCSSL====
ADCSSL = 0b0000000000111100; // Scan AN2..AN5 quet chan nao thi cho chan do len 1
// adc_buf tinh tu bit2=>5 ,vay adc_buf[0] = quet chan AN2
//====ADPCFG====
ADPCFG = 0xFF00; // AN0..AN7 la analog
// 0 : ngo vao la analog
// 1 : ngo vao la digital
//==== khai bao PORTB ====
TRISB = 0b11111111; // Port B la input
// INITIALIZE TIMER3 (dung cho bo dem ADC)
TMR3 = 0x0000;
PR3 = SAMPCOUNT;
IFS0bits.ADIF = 0; // Clear the A/D interrupt flag bit
IEC0bits.ADIE = 1; // Set the A/D interrupt enable bit
}
void adc_start()
{
ADCON1bits.ADON = 1;
T3CONbits.TON = 1;
}
void adc_stop()
{
ADCON1bits.ADON = 0;
T3CONbits.TON = 0;
}
void __attribute__((__interrupt__)) _ADCInterrupt(void)
{
unsigned int i;
for (i=0; i < 6; i++)
adc_filter_buffer[current_buf][i] = ADC16Ptr[i];
for (i=0; i < 6; i++)
adc_buf[i] = (adc_filter_buffer[current_buf][i]/6 +
adc_filter_buffer[(current_buf+1)%4][i]/6 +
adc_filter_buffer[(current_buf+2)%4][i]/3 +
adc_filter_buffer[(current_buf+3)%4][i]/3);
current_buf = (current_buf+1) % 4;
IFS0bits.ADIF = 0;
}
típ UART :
Code:
#define BAUDRATE 9600
#define U1BRG FCY/16/BAUDRATE -1 //dung cho UART
//==== UART1 ====
void uart1_close()
{
CloseUART1(); // co san trong uart.h cua microchip
}
void uart1_open()
{
unsigned int baudvalue;
unsigned int U1MODEvalue;
unsigned int U1STAvalue;
uart1_close();
ConfigIntUART1(UART_RX_INT_DIS & UART_TX_INT_DIS);
baudvalue = U1BRG;
U1MODEvalue = 0x8000;
U1STAvalue = 0x0400;
OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);
}
void uart1_puts(char string[])
{
putsUART1 ((unsigned int *)string); // lay gia tri de gui di
while (BusyUART1());
}
hàm main :
Code:
//==== ham main ===
int main()
{
float tb;
adc_init();
adc_start();
uart1_open();
while(1)
{
float trungbinh_AN0()
{
return (((float)adc_read(AN0) / 65500.0*5.)-2.5)/0.002; // gia tri so voi trung binh /don vi
}
tb = trungbinh_AN0();
PrintSignedInteger(tb, &uart1_puts); // Send result to uart
uart1_puts("\n\r");
}
uart1_close();
return 0;
}
void PrintSignedInteger(int x, void (*printer)(char[]))
{
static char tmpChar[2] = "0";
unsigned int radix, i, converted;
char emptyChar = ' ';
if (x < 0)
{
printer("-");
x*=-1;
}
else if (x == 0)
{
printer("0");
return;
}
converted = (unsigned int) x;
// auto-align on 16bit size (655.. = 5 positions)
for (radix=10000, i=0; i < 5; radix/=10, i++)
{
if (converted > radix-1)
{
tmpChar[0] = '0' + (converted/radix);
printer(tmpChar);
converted = converted % radix;
emptyChar = '0';
}
else if (emptyChar == '0')
{
printer("0");
}
}
}
Mấy bác giúp em với nha ,em vẫn chưa tìm được lỗi ở đâu ,ban đầu nghĩ ở chỗ ngắt AD ,rùi chỗ lấy adc_buf ,rùi hàm truyền lên pc ,nhưng đều ko fải ,hihixhix