Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help to find bug in atmega 16 code

Status
Not open for further replies.

jdh_1984

Member level 2
Joined
Aug 11, 2010
Messages
52
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Norway
Activity points
1,854
Hi
I hope someone can help me to find the bug(s) in my code (Atmel Atmega16). I have tried to debug the code, but could not get the debugger in AVR studio4 to work properly.
I have tested and know that the interrupt and the reading from the ADC works as expected, but after this I have at least one bug.
 

Attachments

  • code akselerometer.txt
    1.9 KB · Views: 55

there is an error in

Code:
 PORTB = angleInt;  /*Writing the lowest eight bit of angleInt to  PORTB???*/
[COLOR="red"] PORTC = angleInt<<8;[/COLOR]  /*Writing the highest eight bit of angleInt to PORTC?? */

//this is wrong because you shift left zeroes, the correct would be 
PORTC = angleInt>>8;  // to shift right the high 8bit and pass them to the port

Alex

---------- Post added at 21:34 ---------- Previous post was at 21:05 ----------

I also see this
Code:
DDRB=0xff;          /*All B-pins set to output*/
DDRD=0xf3;          /*PD0 and PD1 set to output*/

but PORTC is not changed to output anywhere in your code,
and you use it to send the result
you should add
Code:
DDRC=0xff;

Alex
 
Last edited:
Tnx for replay Alex

I did the change that you suggested, and downloaded the code to the target. I am still getting strange outputs at the atmega16.
The out value is much to high. I did a test and removed some of the code and just outputted the read ADC value at the outputs. I am still getting a to high digital value at the out ports, the strange is when I attach a load (LED and a resistance) to some of the c-ports, the voltage drops to 1,5V from the measured 3.2V (Vcc=3.2V). This is not the case for the ports PC0,PC1, and all of the B-ports, which is staying at 3.2V.
I did another test and outputted the high ADC value at the PORTB, and this was ok, but I am still measuring 3,2V at some of the c-ports without writing to the PORTC register.
I have tested the code with three different atmega targets and all of this gives the same results.

Code test1:
ISR(ADC_vect)
:
:
adc_data = ADCW ;
PORTB = adc_data;
PORTC = adc_data>>8;

Code test2:
ISR(ADC_vect)
:
:
adc_data = ADCW ;
PORTB = adc_data>>8;

Do you have any idea of where my fault could be?
 

Attachments

  • code akselerometer.txt
    1.6 KB · Views: 59

The acos function is described like this

The acos() function computes the principal value of the arc cosine of __x. The returned value is in the range [0, pi] radians. A domain error occurs for arguments notin the range [-1, +1].

I think you are using values that are out of this range.
for example with ADC result 256,
0.002429*256=0.621824
1.586/0.621824 =2.550

Also check that you have added the libm.a to the project.

Alex
 

The acos function is described like this



I think you are using values that are out of this range.
for example with ADC result 256,
0.002429*256=0.621824
1.586/0.621824 =2.550

Also check that you have added the libm.a to the project.

Alex

Hi
To avoid the size problem you mention, I am using the If-statement to check the size of the in value.
Could the problem be that I am reading a 10bit value from the ADCW(ADCL+ADCH) into a 16 bit integer value. The ADCH register uses only the two lowest bits (if right adjusted). I am reading all eight from this register and expect the six highest bits to be zero all the time, but are these bit really zero? maybe they are undefined and this is the reason for my troubles.
How do I check that the libm.a is added to the project?
 

in AVRSstudio, go to Project -> Configuration Options ->setup dialog. Then go to the Libraries button down the side, and you can select libm.a to be used.

Alex

---------- Post added at 16:29 ---------- Previous post was at 16:25 ----------

The 16bit integer isn't the problem but if you want you can use (ADCW & 0x3ff)

Alex
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top