Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <p18f4321.h>
- #include <stdio.h>
- #include <math.h>
- #include <usart.h>
- #pragma config OSC = INTIO2
- #pragma config WDT=OFF
- #pragma config LVP=OFF
- #pragma config BOR =OFF
- #define DP PORTCbits.RC4 //sets rb2 to blue always
- //float volt;
- char sevenseg[10] ={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x18}; //seven segment A-G code
- void init_UART()
- {
- OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
- USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX &
- USART_BRGH_HIGH, 25);
- OSCCON = 0x60;
- }
- void putch(char c) //makes it so the tera tree functions
- {
- while(!TRMT);
- TXREG = c;}
- void DO_INIT() //step one
- {
- ADCON0=0x01; // select channel AN0, and turn on the ADC subsystem
- ADCON1=0x1B; // select pins AN0 through AN3 as analog signal, VDD-VSS as
- ADCON2=0xA9; // right justify the result. Set the bit conversion time (TAD) and
- TRISA = 0xFF; //imports
- TRISB = 0x00; //outport
- TRISC = 0x00; //outputs
- TRISD = 0x00; //outputs
- }
- unsigned int get_full_ADC(void) //step 2
- {
- int result;
- ADCON0bits.GO=1; // Start Conversion
- while(ADCON0bits.DONE==1); // wait for conversion to be completed
- result = (ADRESH * 0x100) + ADRESL; // combine result of upper byte and
- //lower byte into result
- return result; // return the result.
- }
- void display_7seg (char H, char L) //program seven segment
- {
- PORTD = sevenseg[H]; //set portd to h
- int I = sevenseg[L]; //insert interger i
- PORTC= I&0x0F; //a-d go to portC
- PORTB= ((I&0x70)>>4); //e-g go to port B and shift right to be in correct position
- }
- void main()
- {
- float volt;
- int V;
- DO_INIT();
- DP= 0;
- init_UART();
- int H, L; //set H and L for the two digits
- while(1){
- V = get_full_ADC()*4;
- volt = V/1000.0;
- //get_volt(); //runs temperature code
- H = (int)volt; //give 10s place only
- L = (int)((volt-H)*10); //give 1s place only
- display_7seg(L, H); //runs 7 seg display
- printf ("Volt= %2.2f V\r\n", volt );
- } //print out temperature
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement