Advertisement
Guest User

Untitled

a guest
May 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. /***** Project Header *****/
  2. // Project Name:
  3. // Author:
  4. // Date: 21/10/2016 14:51
  5. // Code auto-generated by systemdesignerjs
  6. // from www.techideas.co.nz
  7.  
  8. /***** Hardware defines *****/
  9. //make sure this matches your oscillator setting
  10. #define F_CPU 16000000//crystal
  11.  
  12. /***** Includes *****/
  13. #include <avr/io.h>
  14. //#include <stdint.h>
  15. #include <util/delay.h>
  16. //#include <avr/interrupt.h>
  17. //#include <avr/eeprom.h>
  18. //#include <stdio.h>
  19. //#include <string.h>
  20. //#include <avr/pgmspace.h>
  21.  
  22. /***** Hardware macros *****/
  23. //Hardware macros for outputs
  24. #define SET_FAN PORTB |= (1<<PB2)
  25. #define CLR_FAN PORTB &= ~(1<<PB2)
  26. #define SET_ PORTC |= (1<<PC5)
  27. #define CLR_ PORTC &= ~(1<<PC5)
  28. //Hardware macros for inputs
  29. //Hardware macros for ADC inputs
  30. #define TEMPR_SENSOR 5 //macro to refer to ADC channel
  31.  
  32. /***** User macros *****/
  33.  
  34. #define READING_INTERVAL 5000 //5 seconds
  35. #define ON_TEMP 71
  36. #define OFF_TEMP 55
  37. /***** Declare & initialise global variables *****/
  38.  
  39.  
  40. /***** Interrupt Service Routines *****/
  41.  
  42. /***** Prototypes for Functions *****/
  43.  
  44. uint8_t get_tempr(uint16_t adc_reading);
  45. void control_fan(uint8_t tempr);
  46.  
  47. /***** Configure ADC *****/
  48. void init_ADC() {
  49. //At 16MHZ set prescaler to 128 to get 125Khz clock
  50. ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));
  51. //At 8MHz set prescaler to 64 to get 125Khz clock
  52. //ADCSRA |= ((1<<ADPS2)|(1<<ADPS1));
  53. //At 1MHz set prescaler to 8 to get 125Khz clock
  54. //ADCSRA |= ((1<<ADPS1)|(1<<ADPS0));
  55. //AVcc as voltage reference with external capacitor on ARef
  56. ADMUX |= (1<<REFS1); //uncomment this line for internal voltage
  57. ADMUX |= (1 << REFS0);
  58. ADCSRA |= (1 << ADEN); //Power on the ADC
  59. ADCSRA |= (1 << ADSC); //Start initial conversion
  60. }
  61. // get a single adc reading from one channel
  62. uint16_t read_adc(uint8_t channel) {
  63. ADMUX &= 0xF0; //Clear previously read channel
  64. ADMUX |= channel; //Set to new channel to read
  65. ADCSRA |= (1 << ADSC); //Starts a new conversion
  66. while (ADCSRA & (1 << ADSC)); //Wait until the conversion is done
  67. return ADCW; //Returns the value from the channel
  68. }
  69.  
  70. /***** Main function *****/
  71. int main(void) {
  72. /***** Initial hardware setups go here *****/
  73. init_ADC(); //setup the ADC to work
  74.  
  75. /***** IO Hardware Config *****/
  76. // Initially make all micro pins outputs
  77. DDRB = 0xff; //set as outputs
  78. DDRC = 0xff; //set as outputs
  79. DDRD = 0xff; //set as outputs
  80. // make these pins inputs
  81. DDRC &= ~ (1 << PC5);
  82. DDRC &= ~ (1 << PC5);
  83.  
  84. /***** Main variables go here *****/
  85. uint8_t tempr = 0;
  86. uint16_t tempr_sensor_val = 0;
  87.  
  88. /***** Run once code goes here *****/
  89. /***** Loop code *****/
  90. while (1) {
  91.  
  92. //DO NOT CHANGE THESE LINES OF CODE
  93. tempr_sensor_val = read_adc(5); // TEMPR_SENSOR_
  94. tempr = get_tempr(tempr_sensor_val);
  95. control_fan(tempr);
  96. _delay_ms(5000); // READING_INTERVAL_
  97. } //while end
  98. } //main end
  99. /***** Functions *****/
  100. uint8_t get_tempr(uint16_t adc_reading) {
  101. uint32_t temporary = 11*26*100*adc_reading;
  102. uint8_t tempr = temporary>>18;
  103. return tempr;
  104. }
  105.  
  106. void control_fan(uint8_t tempr) {
  107. if (tempr > 71) { // ON_TEMP_
  108. PORTB |= (1 << PB2); // SET_FAN_
  109. }
  110. if (tempr < 55) { // OFF_TEMP_
  111. PORTB &= ~ (1 << PB2); // CLR_FAN_
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement