1 | /***********************************************************************************************/
|
2 | int16_t calculate_temperature(uint16_t adc){
|
3 | //t=0,64593625*adc-155,4
|
4 | //#warning "TODO: update equation" done ;)
|
5 | int32_t adc_temp = 13229 * (uint32_t) adc;
|
6 | adc_temp = adc_temp / 2048;
|
7 | adc_temp = adc_temp - 1554;
|
8 | adc_temp = adc_temp / 10;
|
9 | return (int16_t) adc_temp;
|
10 | }
|
11 | /***********************************************************************************************/
|
12 | void sprint_temperature(char* str,int16_t temperature){
|
13 | uint8_t one = 0, ten = 0, houndred = 0;
|
14 | bool _signed = false;
|
15 | if(temperature < 0){
|
16 | temperature *= -1;
|
17 | str[0] = '-';
|
18 | _signed = true;
|
19 | if(temperature > 99) temperature = 99;
|
20 | }
|
21 | one = temperature % 10;
|
22 | ten = (temperature % 100) / 10;
|
23 | houndred = (temperature % 1000) / 100;
|
24 | if(houndred == 0){
|
25 | if(ten == 0){
|
26 | if(_signed){
|
27 | str[1] = str[0];
|
28 | }else{
|
29 | str[1] = ' ';
|
30 | }
|
31 | str[0] = ' ';
|
32 | }else{
|
33 | if(_signed){
|
34 | str[0] = str[0];
|
35 | }else{
|
36 | str[0] = ' ';
|
37 | }
|
38 | str[1] = ten + '0';
|
39 | }
|
40 | }else{
|
41 | str[0] = houndred + '0';
|
42 | str[1] = ten + '0';
|
43 | }
|
44 | str[2] = one + '0';
|
45 | }
|
46 | /***********************************************************************************************/
|
47 | void adc_init(void){
|
48 | uint16_t result;
|
49 | ADMUX |= (1<<REFS0); // VCC with external capacitor at AREF
|
50 | ADCSRA |= (1<<ADEN) |/* (1<<ADPS2) | (1<<ADPS1) | */(1<<ADPS0); // ADC enabled, prescaler: 1
|
51 | ADCSRA |= (1<<ADSC);
|
52 | while(ADCSRA & (1<<ADSC)){;}
|
53 | result = ADCW; //dummy readout
|
54 | //ADCSRB = 0x00;
|
55 | }
|
56 | /***********************************************************************************************/
|
57 | uint16_t read_adc(uint8_t portbit){
|
58 | /*uint16_t result;*/
|
59 | //ADMUX |= portbit;
|
60 | //uint16_to_string(str5,ADMUX);
|
61 | ADCSRA |= (1<<ADSC);
|
62 | while(ADCSRA & (1<<ADSC)){;}
|
63 | /*result = ADCW; //dummy readout
|
64 | result = 0;
|
65 | for(i=0;i<4;i++){
|
66 | while(ADCSRA & (1<<ADSC)){;}
|
67 | result += ADCW;
|
68 | }
|
69 | //ADMUX &= ~portbit;*/
|
70 | return ADCW;
|
71 | }
|
72 | static void avr_init(void)
|
73 | {
|
74 |
|
75 | //Enable SPI
|
76 | spi_init();
|
77 | adc_init();
|
78 |
|
79 | //init communication
|
80 | //*
|
81 | USART_Init(0);
|
82 |
|
83 |
|
84 | RELAY_DDR |= (1<<RELAY_DISPLAY) | (1<<RELAY_CAM);
|
85 |
|
86 | DDRC |= (1<<PORTC5);
|
87 |
|
88 | CTRL_DDR |= (1<<REQUEST) | (1<<DISPLAY_READY);
|
89 | CTRL_PORT |= (1<<REQUEST) | (1<<DISPLAY_READY);
|
90 | //*/
|
91 | //Set power mode
|
92 | set_sleep_mode(SLEEP_MODE_IDLE);
|
93 |
|
94 | //init PAL SYNC port
|
95 | set_bit(DDRC,7);
|
96 | //B.4 is sync:1000 ohm + diode to 75 ohm resistor
|
97 | //MOSI is video:330 ohm + diode to 75 ohm resistor
|
98 |
|
99 | // Initialize Sync for PAL
|
100 | OCR1A = 1024; //One PAL line 64us
|
101 | TCCR1B = (1<<WGM12)|(1<<CS10);//full speed; clear-on-match
|
102 | TCCR1A = 0x00; //turn off pwm and oc lines
|
103 | TIMSK = 1<<OCIE1A; //enable interrupt from Timer1 CompareA
|
104 | return;
|
105 | }
|