I
I
icenoon2020-06-10 03:44:09
C++ / C#
icenoon, 2020-06-10 03:44:09

What is the problem in the code?

#include <mega8.h>
#include <delay.h>

int z=0;

void main(void)
{
    DDRB = 0b0000000;
    PORTB = 0b0000001;
    
    DDRD = 0b0000111;
    PORTD = 0b0000000;
    
    
    while (1)
    {   
        if (PINB==0b0000000)
        {   
            
            TCCR0 = 0b00000101; 
            TCNT0 = 0
            
            while (z < 21) 
            {   
                if (TCNT0 == 255) 
                {
                    z++;    
                };
    
            };
            
            TCNT0 = 0;
            TCCR0 = 0b00000000;
            PORTD=0b00000111;
            _delay_ms(1000)
        };
        
        if (PINB==0b0000001)
        {
            PORTD=0b00000000;  
        };
    };
};


5ee02a9128b1b889855504.png

In short, port b0 is for input with a pull-up, ports d are 3 pieces for output, I announce it, then in an endless loop, I announced the frequency of the zero timer of the handkerchief, reset its memory cell, and until z becomes 1024/1000000*255*20 seconds, we wait, then we reset the timer just in case, all of a sudden we will use the code below, turn on 3 LED idiots and, in essence, in

Russian speaking, pressed the button, waited for about 5.2 s, the lights came on for 1000 ms, checked that you had already released the button and went out)

The code does not work in the proteus and throws errors, searched for an error on the Internet and did not find anything shameful in the code
5ee02cccbabc2857932180.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-06-10
@icenoon

until z becomes 1024/1000000*255*20 seconds wait
while (z < 21) 
{   
    if (TCNT0 == 255) 
    {
        z++;    
    };
};

Speaking in Russian, I pressed the button and waited for about 5.2 s

No, you waited some other time here. Because if clk io / 1024 enters the timer, then TCNT will be equal to 255 for far more than one cycle.
The code does not work in proteus and throws errors

Well, he writes you the address, PC=0x005e, look what happens at this address.

C
CityCat4, 2020-06-10
@CityCat4

To eat, in English on white it is written what is the problem :) Writing to the memory area at the address ... outside the selected area. The area is either small or the pointer is set incorrectly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question