2361 Lab Report 7

download 2361 Lab Report 7

of 4

Transcript of 2361 Lab Report 7

  • 8/2/2019 2361 Lab Report 7

    1/4

    Anthony Lambert

    Lab Report 7

    3717384

    Abstract

    In this lab, we built an application that simulated a digital voltmeter on a PIC18F4550 MCU. The

    application sampled analog voltages via a reference voltage of 5 volts and a potentiometer and then

    reported them through the RS-232 interface on the computer.

    Introduction

    There were three specific tasks we had to accomplish in this lab; Acquire, calculate and convert,

    and print. The application obtained 128 samples and then output a final value that was the average of

    them. The output is updated very frequently and was written over the previous one. Our reference

    voltages were 0 and 5 volts so we only got output values within that range. Then the program converted

    that number into decimal and printed it as an ASCII value on the hyperterminal display.

    Components

    1 1k ohm resistor 1 10k potentiometer 1 8MHz crystal 2 .1 microfarad capacitors 5 33 picofarad capacitors 1 PIC18F4550 MCU

    Schematic

  • 8/2/2019 2361 Lab Report 7

    2/4

  • 8/2/2019 2361 Lab Report 7

    3/4

    Subroutines

    Our code was very minimal in this program including the two subroutines. The first subroutine

    was to output a character. It waited for the TXIF flag which told us that the register had been emptied

    and then we filled it up with a new byte of data. The other subroutine was a simple wait function. The

    function needed to wait for 13.8 microseconds prior to the data acquisition.

    Source Code

    #include#pragma config PLLDIV=2, CPUDIV=OSC1_PLL2, USBDIV=2, IESO=ON, WDT=OFF#pragma config BOR=OFF, PWRT=ON, LVP=OFF, FOSC=HSPLL_HS, FCMEN=OFF,VREGEN=OFF#pragma config MCLRE=ON, STVREN=ON, LPT1OSC=ON, PBADEN=OFF

    void putch(char byte){while (!PIR1bits.TXIF);TXREG = byte;}

    void wait(void){int x = 3;while(x--);}

    void main(void)

    { unsignedlongint i, average, d0, d1, d2;unsignedlongint values;unsignedlongint scaleFactor = 130944; //130944 = 1023*128//initialize serial registersTXSTAbits.TXEN = 1;RCSTAbits.SPEN = 1;RCSTAbits.CREN = 1;BAUDCONbits.BRG16 = 1;SPBRGH = 77 >> 8;SPBRG = 77 & 0x00FF;//initialize the ADC registersADCON0 = 0b00000001;ADCON1 = 0b00000000;

    ADCON2 = 0b10001110;putch(0x0D);putch(0x0A);while(1){

    values = 0;average = 0;for (i = 0; i < 128; i++){

  • 8/2/2019 2361 Lab Report 7

    4/4

    wait(); //wait 13.8 us for acquisitionADCON0bits.GO = 1; //start AD conversionwhile (ADCON0bits.GO)

    ; //wait for AD to finishvalues += ADRES;

    }average = (values * 500) / (scaleFactor);d0 = average \% 10;d1 = (average / 10) \% 10;d2 = (average / 100) \% 10;putch('0' + d2);putch('.');putch('0' + d1);putch('0' + d0);putch(' ');putch('V');putch('o');putch('l');putch('t');putch('s');

    putch(0x0D);}

    }

    Conclusion

    Through this lab we accomplished building a digital voltmeter. By using USART and ADC we were

    able to sample a bunch of analog voltages and then average them out to get a fairly accurate value in

    decimal. Our problems with this lab were mostly with hyperterminal. Even though our code was written

    correctly prior to lab we spent the entire period trying to get our application to respond in the

    hyperterminal. Fortunately when we came back the following lab period we were able to get the

    application to respond.

    Questions:

    1. .036 volts is the resolution.2. Our accuracy is to .72%.3. 37 samples per millisecond.4. Portion of time for each iteration

    a. 43.1% of the time handling ADCb. 52.3% of the time handling USARTc. .8% of the time calculating the averaged. 3.6% of the time converting to ASCII