Advertisement
abdullahkahraman

Untitled

Apr 30th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: abdullah
  4.  *
  5.  * Created on 30 Mart 2013 Cumartesi, 13:29
  6.  */
  7.  
  8. #include <xc.h> // Include the compiler's header file.
  9. //This definition is needed for using __delay(int x):
  10. #define _XTAL_FREQ 4000000 // We are using the 4MHz Internal Oscillator.
  11.  
  12. /******* Configuration Word ********/
  13. __CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_ON & CPD_ON);
  14. //INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN
  15. // WDT disabled
  16. // PWRT enabled
  17. // GP3/MCLR pin function is digital input, MCLR internally tied to VDD
  18. // Brown-out Detect Enabled
  19. // Program Memory code protection is enabled
  20. // Data memory code protection is enabled
  21. /**** End of Configuration Word ****/
  22.  
  23. /****** Input Output Definitions *******/
  24. #define Fan_Transistor          GPIObits.GP0   // Controls a transistor that drives a fan.
  25. #define TRIS_Fan_Transistor     TRISIO0        // Tri-state control bit
  26. /*** End Of Input Output Definitions ***/
  27.  
  28. /****** ADC Channel Definitions *******/
  29. // Shows analog channel connections. For example, "3" shows that it's connected to AN3
  30. #define NTC_AnalogChannel   2 // There is an NTC thermistor connected to this channel.
  31. #define TRISNTC             TRISIO2 // Tri-state control bit
  32. /*** End Of ADC Channel Definitions ***/
  33.  
  34. unsigned int temperatureReading;
  35.  
  36. void ADC_Sampler(void)
  37. {
  38.     // temp in degC = 830 - 1.025*ADC_Result
  39.     ADON = 1; // Turn the ADC ON
  40.     ADCON0bits.CHS = NTC_AnalogChannel; // Select the appropriate analog channel (Max)
  41.     __delay_us(50); // Wait for 50us ADC acquisition time.
  42.     GO_nDONE = 1; // Start the conversion.
  43.     while (GO_nDONE); // Wait until the conversation is done.
  44.  
  45.     temperatureReading = ADRESL + (unsigned int) (ADRESH << 8); // Combine 8 bit "ADRESH" and  8 bit "ADRESL" to one 16 bit register.
  46.     ADON = 0; // Turn the ADC OFF.
  47. }
  48.  
  49. void main()
  50. {
  51.     ADCON0 = 0x80; // ADC result is right justified.
  52.     ANSEL = 0x14; // Disable all the analog inputs, except for NTC. ADC clock is FOSC/8
  53.     CMCONbits.CM = 0x07; // CM2:CM0: Comparator Mode bits: 111 = Comparator OFF
  54.     TRIS_Fan_Transistor = 0; // Set the port as output
  55.     Fan_Transistor = 0; // and clear it.
  56.     TRISNTC = 1; // Set the port as input.
  57.  
  58.     while (1)
  59.     {
  60.         ADC_Sampler(); // Call the ADC sampler.
  61.         if (temperatureReading >= 575)
  62.         {
  63.             Fan_Transistor = 1;
  64.         }
  65.         else if (temperatureReading <= 530)
  66.         {
  67.             Fan_Transistor = 0;
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement