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.

what's wrong with this code? (AVR)

Status
Not open for further replies.
here is the code:

#include <util/delay.h>
#include <avr/io.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
void makeit (int my)
{
switch (my)
{
case 0 :
PORTB = ~(0b01111110);
break;

case 1:
PORTB = ~0b00010010;
SETBIT(PORTC,2);
break;

case 2:
PORTB = ~0b10111101;
break;

case 3:
PORTB = ~0b10110111;
break;

case 4:
PORTB = ~0b11010011;
break;

case 5:
PORTB = ~(0b11100111);
break;

case 6:
PORTB = ~0b11101111;
break;

case 7:
PORTB = ~0b00110010;
break;

case 8:
PORTB = ~0b11111111;
SETBIT(PORTC,2);
break;

case 9:
PORTB = ~0b11110111;
break;
}
}
int main(void)
{
DDRA=0x00;
DDRB=0xFF;
DDRC=0xFF;
DDRD=0xFF;
int num=00;
int ten;
int one;
PORTB=0xFF;
PORTA=0xFF;
do
{

SETBIT(PORTC,2);
_delay_ms(1000);
CLEARBIT(PORTC,2);
_delay_ms(1000);
if(!CHECKBIT(PINA,0))num=01;
else if(!CHECKBIT(PINA,1))num=12;
else if(!CHECKBIT(PINA,2))num=23;
else if(!CHECKBIT(PINA,3))num=34;
else if(!CHECKBIT(PINA,4))num=45;
else if(!CHECKBIT(PINA,5))num=56;
else if(!CHECKBIT(PINA,6))num=67;
else if(!CHECKBIT(PINA,7))num=78;
} while (PINA == 0b11111111);



do
{
SETBIT(PORTC,2);
asm("NOP");
} while (PINA!=0xFF);
while(1)
{
CLEARBIT(PORTC,2);
if(!CHECKBIT(PINA,6)) num--;
else if(!CHECKBIT(PINA,7))num++;
if(num<0) num=0;
else if(num>99) num=99;
ten=num/10;
one=num%10;
SETBIT(PORTD,4);
makeit(ten);
_delay_ms(1000);
CLEARBIT(PORTD,4);
SETBIT(PORTD,1);
makeit(one);
_delay_ms(1000);
CLEARBIT(PORTD,1);
}
}
the circuit i had attached it...
temporally i didn't put resistors to the 7segment because i'm using small voltage 2.7 to 3 volt.......
foxbrain
 

Attachments

  • Photo0341.jpg
    Photo0341.jpg
    257.7 KB · Views: 86

it's on the left of the circuit connected to the seven segment with a resistor 1k connected to the base, the emitter is connected to the 7segment and the collector is connected to Vcc....and so the other seven segment
 

I have just tried your code in the debugger of AVRstudio4 , there is nothing wrong with it.
It counts fine up/down and I can also assign any initial value

Alex

---------- Post added at 02:25 ---------- Previous post was at 02:24 ----------

Is your MCU model setup correctly in avrstudio?

---------- Post added at 02:27 ---------- Previous post was at 02:25 ----------

I just hope it isn't a bug of your compiler.
Can you give me the settings you use so that I can compile it and send it to you

clock=?

Alex
 

You should set the clock in the projest properties 7372800 Hz
and the mcu is already ATMEGA16 I suppose

upload this hex to the mcu
 

Attachments

  • test4.zip
    754 bytes · Views: 93

what is the function of this code? it appears to me like my code but with some changes like instead of appearing 23 it appears 33 when i press pina,2 when i increase the number the two digits increase together...
foxbrain
 

I didn't change anything, it is yout code from post #21


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
#include <util/delay.h>
#include <avr/io.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
 
void makeit (int my)
{
    switch (my)
    {
    case 0 :
        PORTB = ~(0b01111110);
        break;
 
    case 1:
        PORTB = ~0b00010010;
        SETBIT(PORTC,2);
        break;
 
    case 2:
        PORTB = ~0b10111101;
        break;
 
    case 3:
        PORTB = ~0b10110111;
        break;
 
    case 4:
        PORTB = ~0b11010011;
        break;
 
    case 5:
        PORTB = ~(0b11100111);
        break;
 
    case 6:
        PORTB = ~0b11101111;
        break;
 
    case 7:
        PORTB = ~0b00110010;
        break;
 
    case 8:
        PORTB = ~0b11111111;
        SETBIT(PORTC,2);
        break;
 
    case 9:
        PORTB = ~0b11110111;
        break;
    }
}
int main(void)
{
    DDRA=0x00;
    DDRB=0xFF;
    DDRC=0xFF;
    DDRD=0xFF;
    int num=0;
    int ten;
    int one;
    PORTB=0xFF;
    PORTA=0xFF;
    do
    {
 
        SETBIT(PORTC,2);
        _delay_ms(1000);
        CLEARBIT(PORTC,2);
        _delay_ms(1000);
        if(!CHECKBIT(PINA,0))num=01;
        else if(!CHECKBIT(PINA,1))num=12;
        else if(!CHECKBIT(PINA,2))num=23;
        else if(!CHECKBIT(PINA,3))num=34;
        else if(!CHECKBIT(PINA,4))num=45;
        else if(!CHECKBIT(PINA,5))num=56;
        else if(!CHECKBIT(PINA,6))num=67;
        else if(!CHECKBIT(PINA,7))num=78;
    } while (PINA == 0b11111111);
 
 
 
    do
    {
        SETBIT(PORTC,2);
        asm("NOP");
    } while (PINA!=0xFF);
    while(1)
    {
        CLEARBIT(PORTC,2);
        if(!CHECKBIT(PINA,6)) num--;
        else if(!CHECKBIT(PINA,7))num++;
        if(num<0) num=0;
        else if(num>99) num=99;
        ten=num/10;
        one=num%10;
        SETBIT(PORTD,4);
        makeit(ten);
        _delay_ms(1000);
        CLEARBIT(PORTD,4);
        SETBIT(PORTD,1);
        makeit(one);
        _delay_ms(1000);
        CLEARBIT(PORTD,1);
    }
}



Alex

---------- Post added at 03:07 ---------- Previous post was at 03:05 ----------

And this code works fine in the debugger, it assigns the correct values and increase/decrease by one with the buttons
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top