Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #include <p18f4321.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <usart.h>
  5. #pragma config OSC = INTIO2
  6. #pragma config WDT=OFF
  7. #pragma config LVP=OFF
  8. #pragma config BOR =OFF
  9. #define DP PORTCbits.RC4 //sets rb2 to blue always
  10.  
  11.  
  12. //float volt;
  13. char sevenseg[10] ={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x18}; //seven segment A-G code
  14.  
  15. void init_UART()
  16. {
  17. OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
  18. USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX &
  19. USART_BRGH_HIGH, 25);
  20. OSCCON = 0x60;
  21. }
  22. void putch(char c) //makes it so the tera tree functions
  23. {
  24. while(!TRMT);
  25. TXREG = c;}
  26.  
  27. void DO_INIT() //step one
  28. {
  29. ADCON0=0x01; // select channel AN0, and turn on the ADC subsystem
  30. ADCON1=0x1B; // select pins AN0 through AN3 as analog signal, VDD-VSS as
  31. ADCON2=0xA9; // right justify the result. Set the bit conversion time (TAD) and
  32. TRISA = 0xFF; //imports
  33. TRISB = 0x00; //outport
  34. TRISC = 0x00; //outputs
  35. TRISD = 0x00; //outputs
  36.  
  37. }
  38. unsigned int get_full_ADC(void) //step 2
  39. {
  40. int result;
  41. ADCON0bits.GO=1; // Start Conversion
  42. while(ADCON0bits.DONE==1); // wait for conversion to be completed
  43. result = (ADRESH * 0x100) + ADRESL; // combine result of upper byte and
  44. //lower byte into result
  45. return result; // return the result.
  46. }
  47.  
  48.  
  49. void display_7seg (char H, char L) //program seven segment
  50. {
  51. PORTD = sevenseg[H]; //set portd to h
  52. int I = sevenseg[L]; //insert interger i
  53. PORTC= I&0x0F; //a-d go to portC
  54. PORTB= ((I&0x70)>>4); //e-g go to port B and shift right to be in correct position
  55.  
  56. }
  57. void main()
  58. {
  59. float volt;
  60. int V;
  61. DO_INIT();
  62. DP= 0;
  63. init_UART();
  64. int H, L; //set H and L for the two digits
  65. while(1){
  66. V = get_full_ADC()*4;
  67. volt = V/1000.0;
  68. //get_volt(); //runs temperature code
  69. H = (int)volt; //give 10s place only
  70. L = (int)((volt-H)*10); //give 1s place only
  71. display_7seg(L, H); //runs 7 seg display
  72. printf ("Volt= %2.2f V\r\n", volt );
  73. } //print out temperature
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement