Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***** Project Header *****/
- // Project Name:
- // Author:
- // Date: 21/10/2016 14:51
- // Code auto-generated by systemdesignerjs
- // from www.techideas.co.nz
- /***** Hardware defines *****/
- //make sure this matches your oscillator setting
- #define F_CPU 16000000//crystal
- /***** Includes *****/
- #include <avr/io.h>
- //#include <stdint.h>
- #include <util/delay.h>
- //#include <avr/interrupt.h>
- //#include <avr/eeprom.h>
- //#include <stdio.h>
- //#include <string.h>
- //#include <avr/pgmspace.h>
- /***** Hardware macros *****/
- //Hardware macros for outputs
- #define SET_FAN PORTB |= (1<<PB2)
- #define CLR_FAN PORTB &= ~(1<<PB2)
- #define SET_ PORTC |= (1<<PC5)
- #define CLR_ PORTC &= ~(1<<PC5)
- //Hardware macros for inputs
- //Hardware macros for ADC inputs
- #define TEMPR_SENSOR 5 //macro to refer to ADC channel
- /***** User macros *****/
- #define READING_INTERVAL 5000 //5 seconds
- #define ON_TEMP 71
- #define OFF_TEMP 55
- /***** Declare & initialise global variables *****/
- /***** Interrupt Service Routines *****/
- /***** Prototypes for Functions *****/
- uint8_t get_tempr(uint16_t adc_reading);
- void control_fan(uint8_t tempr);
- /***** Configure ADC *****/
- void init_ADC() {
- //At 16MHZ set prescaler to 128 to get 125Khz clock
- ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0));
- //At 8MHz set prescaler to 64 to get 125Khz clock
- //ADCSRA |= ((1<<ADPS2)|(1<<ADPS1));
- //At 1MHz set prescaler to 8 to get 125Khz clock
- //ADCSRA |= ((1<<ADPS1)|(1<<ADPS0));
- //AVcc as voltage reference with external capacitor on ARef
- ADMUX |= (1<<REFS1); //uncomment this line for internal voltage
- ADMUX |= (1 << REFS0);
- ADCSRA |= (1 << ADEN); //Power on the ADC
- ADCSRA |= (1 << ADSC); //Start initial conversion
- }
- // get a single adc reading from one channel
- uint16_t read_adc(uint8_t channel) {
- ADMUX &= 0xF0; //Clear previously read channel
- ADMUX |= channel; //Set to new channel to read
- ADCSRA |= (1 << ADSC); //Starts a new conversion
- while (ADCSRA & (1 << ADSC)); //Wait until the conversion is done
- return ADCW; //Returns the value from the channel
- }
- /***** Main function *****/
- int main(void) {
- /***** Initial hardware setups go here *****/
- init_ADC(); //setup the ADC to work
- /***** IO Hardware Config *****/
- // Initially make all micro pins outputs
- DDRB = 0xff; //set as outputs
- DDRC = 0xff; //set as outputs
- DDRD = 0xff; //set as outputs
- // make these pins inputs
- DDRC &= ~ (1 << PC5);
- DDRC &= ~ (1 << PC5);
- /***** Main variables go here *****/
- uint8_t tempr = 0;
- uint16_t tempr_sensor_val = 0;
- /***** Run once code goes here *****/
- /***** Loop code *****/
- while (1) {
- //DO NOT CHANGE THESE LINES OF CODE
- tempr_sensor_val = read_adc(5); // TEMPR_SENSOR_
- tempr = get_tempr(tempr_sensor_val);
- control_fan(tempr);
- _delay_ms(5000); // READING_INTERVAL_
- } //while end
- } //main end
- /***** Functions *****/
- uint8_t get_tempr(uint16_t adc_reading) {
- uint32_t temporary = 11*26*100*adc_reading;
- uint8_t tempr = temporary>>18;
- return tempr;
- }
- void control_fan(uint8_t tempr) {
- if (tempr > 71) { // ON_TEMP_
- PORTB |= (1 << PB2); // SET_FAN_
- }
- if (tempr < 55) { // OFF_TEMP_
- PORTB &= ~ (1 << PB2); // CLR_FAN_
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement