View Single Post
Old 28-11-2009, 10:52 PM   #71
npbaoduy
Đệ tử 2 túi
 
Tham gia ngày: Sep 2008
Nơi Cư Ngụ: Tp. HCM
Bài gửi: 38
:
Tất nhiên là em có cấp nguồn .
code em làm tương tự như code mẫu trên trang của microchip
Code:
void Init_ADC12(void)
{
   //ADCON1 Register
        //Set up A/D for Automatic Sampling
        //Use Timer3 to provide sampling time
        //Set up A/D conversrion results to be read in fractional 
        //number format.
        //All other bits to their default state
        ADCON1bits.FORM = 3; // luu du lieu dinh dang signed fractional 1.15
        ADCON1bits.SSRC = 2; // dung timer3 de kich chuyen doi ADC
        ADCON1bits.ASAM = 1; // cai dat che do tu dong lay mau

    //ADCON2 Register
        //Set up A/D for interrupting after 16 samples get filled in the buffer
        //All other bits to their default state
        ADCON2bits.SMPI = 15; // cai da che do ngat ADC sau khi day 16 mau trong bo dem du lieu
      
   //ADCON3 Register
        //We would like to set up a sampling rate of 8KHz
        //Total Conversion Time= 1/Sampling Rate = 125 microseconds
        //At 29.4 MIPS, Tcy = 33.9 ns = Instruction Cycle Time
        //Tad > 667ns (for -40C to 125C temperature range)
        //We will set up Sampling Time using Timer3 & Tad using ADCS<5:0> bits
        //All other bits to their default state
        //Let's set up ADCS arbitrarily to the maximum possible amount = 63
        //So Tad = Tcy*(ADCS+1)/2 = 1.085 microseconds
        //So, the A/D converter will take 14*Tad periods to convert each sample
        ADCON3bits.ADCS = 63;

        //Next, we will to set up Timer 3 to time-out every 125 microseconds
        //As a result, the module will stop sampling and trigger a conversion
        //on every Timer3 time-out, i.e., 125 microseconds. At that time,
        //the conversion process starts and completes 14*Tad periods later.
        //When the conversion completes, the module starts sampling again
        //However, since Timer3 is already on and counting, about 110
        //microseconds later (=125 microseconds - 14*Tad), Timer3 will expire
        //again. Effectively, the module samples for 110 microseconds and
        //converts for 15 microseconds
        //NOTE: The actual sampling rate realized may be 7998.698 Hz
        //      due to a small round off error. Ensure you provide the
        //      true sampling rate to dsPICworks if you are trying to plot
        //      the sampled or filtered signal.
        TMR3 = 0x0000;
        PR3 = 0X0E65;
        IFS0bits.T3IF = 0; // cho co ngat timer3=0
        IEC0bits.T3IE = 0; // khong cho phep ngat timer3

        //ADCHS Register
        //Set up A/D Channel Select Register to convert AN7 on Mux A input
        ADCHS = 0x000F; // chon AN15 la kenh thu du lieu analog


        //ADCSSL Register
        //Channel Scanning is disabled. All bits left to their default state
        ADCSSL = 0x0000; // tat che do quet cac kenh. Cac n=bit con lai de che do mac dinh

        //ADPCFG Register
        //Set up channels AN15 as analog input and configure rest as digital
        //Recall that we configured all A/D pins as digital when code execution
        //entered main() out of reset
      	ADPCFG = 0xFFFF;
        ADPCFGbits.PCFG15 = 0; //ngo AN15 la ngo vao analog

        //Clear the A/D interrupt flag bit
        IFS0bits.ADIF = 0; // xoa co ngat chuyen doi ADC

        //Set the A/D interrupt enable bit
        IEC0bits.ADIE = 1; // cho phep ngat ADC

        //Turn on the A/D converter
        //This is typically done after configuring other registers
        ADCON1bits.ADON = 1; // bat dau cho do ADC hoat dong

        //Start Timer 3
        T3CONbits.TON = 1; // cho timer3 bat dau chay

}   

void __attribute__((interrupt, no_auto_psv)) _ADCInterrupt(void)
{
     IFS0bits.T3IF = 0; //Xoa co ngat timer3
     unsigned int i = 0;
    IFS0bits.ADIF = 0; //Xoa co ngat ADC
     adcPtr=&ADCBUF0; // lay dia chi thanh ghi ADCBUF0 ghi vao thanh ghi adcPtr

   for(i=0;i<=15;i++)
      {
      *iPtr++=*adcPtr++; //lay noi dung cua cac thanh ghi Buffer lan luot ghi vao cac bien
                     // inputsignal[]
      }
   if (iPtr>&inputsignal[159])     doFilterFlag=1;         // thu duoc 160 mau thi vao xu ly                                 
}
Trong chương trình chính:
Code:
....
	while(1)
	{
    	while(!doFilterFlag){};   //Vong lap cho day 160 mau trong bo dem ADC
        doFilterFlag=0;           //chuan bi vong lap sau
        (.....tính toán xử lý .........................)
Anh xem giúp em nhé!
npbaoduy vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn