ATTINY10 MYSTERY PROBLEM

Status
Not open for further replies.

caiyongda123

Newbie
Joined
Aug 2, 2020
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
56
Help!! Attiny 10 is killing me. I am using Atmel studio for attiny 10 programming. I want to flip the io. But there are inconsistent things happening. in the code below. when you run it to flip PORTB, it won't let you do that. It act the same way either on CHIP or in the simulator. However, if you commend off sei(); the port start to blink again!! but if you run it in simulator with the assmebly code ATMEL translate from my C code. No matter whether sei() is commend off, the port is blinking, why?

version 7.0.2397


The code I am using:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* LED_BLINKER.c
*
* Created: 2020/5/31 10:47:01
* Author : Administrator
*/
#define F_CPU 800000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
//#include <stdio.h>
volatile int ADC_Result=0;
//volatile int y_1=200;
volatile int y=125;
volatile int block_count=0;
volatile int thres=180;
//volatile int thres2=30;
volatile int ADC_Result_1=0;
volatile int a1=0;
volatile int a2=0;
volatile int count_2=0;
//volatile long int initial=0;
volatile int refresh_div=0;
volatile int refresh_div2=0;
volatile int gap=0;
volatile int trigger=0;
//volatile int max=0;
//cycle=10,2ms
void delay_2(long cycle) {
    long dummy=0;
    while(1) {
        if(dummy>cycle)
        {
            return;
        }
//        filter(10);
        dummy=dummy+1;
    }
}
ISR (ADC_vect) {
 
    ADC_Result=    ADCL;
//    y=ADC_Result/100+99*y_1/100;
 
}
 
ISR (TIM0_OVF_vect) {
    count_2=0;        //RESET count_2er
//    block_count=0;
//    thres-=2;
}
 
int filter(int inputt)
{
    inputt=(20*ADC_Result_1+35*inputt)/100+45*inputt/100;
    return inputt;
}
//void delay_2(long cycle);
int filter(int inputt);
int main(void)
{
 
//    CCP=0xD8;    //UNLOCK CLKPSR REGISTER
//    CLKPSR=0;    //NO DIVIDER, 8MHZ
//    ADMUX=1<<MUX0;        //choose pin0 as input
//    ADCSRA=0B11101000;  //enable ADC and enable interupt, clock division factor of 2
    ADCSRA|=1<<ADEN;    //enable ADC
    ADCSRA|=1<<ADATE;    //enable Auto triggering
 
    ADCSRA|=1<<ADIE;    //enable interrupt
    sei();
    ADCSRA|=1<<ADSC;    //start conversion
 
//    ADCSRB=0B00000000;    //free runing mode
    DDRB=0B00000100;
    DIDR0|=1<<ADC0D;
//    PORTB|=0B00000100;
//count_2er setting
    TIMSK0|=1<<TOIE0;   // overflow interrupt enable
    TCCR0B|=1<<CS00;    //CS2 to set the prescaler to internal clk devided by 256, 12kHz
    TCCR0B|=1<<CS01;        //prescaler of 8
//    TCCR0B|=1<<CS02;        //prescaler of 8
    /* Replace with your application code */
    while (1)
    {
 
 
    PORTB^=0B00000100;
    _delay_ms(100);
    if (ADC_Result_1>(thres+8+((thres-y)>>1)))
    {
 
            if (trigger>0)
            {
                trigger=0;
            }
            PORTB^=0B00000100;
            if (count_2==0)
            {
                count_2=1;
            } else if (count_2==1)
            {
                    if (block_count<2)
                    {
                        block_count++;
                    }
                    a1=TCNT0;
                    if (a1>200)    //pulse too short
                    {
 
                        count_2=2;
                     
                    } else {
                        count_2=0;        //RESET count_2er
                        a1=0;
                        a2=0;
                    }
            } else {
                    a2=TCNT0;
//                PORTB^=0B00000100;
                    if (a2>200)    //pulse too short
                    {
//                        PORTB^=0B00000100;
                     
                        gap=a1>>2;
                        if (a1>a2)
                        {
                            if ((a1-a2)<gap)
                            {
                                if (block_count==1)
                                {
//                                    PORTB^=0B00000100;
                                    block_count=0;
                                    trigger=1;
                                }
                             
                            }
                            } else {
                            if ((a2-a1)<gap)
                            {
                                if (block_count==1)
                                {
//                                    PORTB^=0B00000100;
                                    block_count=0;
                                    trigger=1;
                                }
                             
                            }
                        }
 
                    }
                    a1=0;
                    a2=0;
                    count_2=0;
                 
                 
                }
//                delay_2(600);
                TCNT0=0;
                ADC_Result_1=0;
    }
        if (refresh_div>30)
        {
            refresh_div=0;
            thres=thres-1;
            if (ADC_Result_1>thres)
            {
                thres=ADC_Result_1;
            }
        }
        refresh_div+=1;
        if (refresh_div2>60)
        {
            refresh_div2=0;
            thres=thres-1;
            y=filter(10);
        }
        refresh_div2+=1;
 
  }
}

 
Last edited by a moderator:

Seems like you missed to manage interrupt flags within interrupt vectors.
Not doing that, there could happen reentrant interrupt calls.
 

Don't forget variables used inside ISR that are not local to declare them
"volatile".




Regards, Dana.
 

Hi,

your "trigger" variable and handling is useless. It does nothing.
What do you expect it to do?

Why you create a function "filter" for just one line of code? It just causes processing time and stack memory...

What about ADCH?

the ISR "ADC_Vect" makes no sense to me. The same could be done in main loop without drawback.
... and ADC_result is never used...

There will be even more issues...

"Attiny 10 is killing me "
Btw: most probably the problems are not caused by the ATtiny.

Klaus
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…