Led curtain help

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
ctesla75
Posts: 22
Joined: 10 Jul 2010 06:05

Led curtain help

#1 Post by ctesla75 » 17 Oct 2021 00:01

Hi all,
I am trying to learn C using mikroe c for avr on the easy avr7. I have tried making a led curtain but only suceeded in making a binary led curtain, The led curtain example works, but i dont understand the code. is there a line by line explanation available? Are there any books anyone could suggest that would help in learning C for AVR, Or a complete list of command for mikroec.
For example is see the command DDR and TRIS but searching in help doesn't find any information. I only see their uses in examples
char counter;

void wait() {
Delay_ms(100);
}

void main() {
DDRA = 0xFF; // set direction to be output
DDRB = 0xFF; // set direction to be output
DDRC = 0xFF; // set direction to be output
DDRD = 0xFF; // set direction to be output
PORTA = 0x00; // turn OFF the PORTA leds
PORTB = 0x00; // turn OFF the PORTB leds
PORTC = 0x00; // turn OFF the PORTC leds
PORTD = 0x00; // turn OFF the PORTD leds

while (1) {
for (counter=0; counter<8; counter++){
PORTA |= 1 << counter;
PORTB |= 1 << counter;
PORTC |= 1 << counter;
PORTD |= 1 << counter;

wait();
}

counter = 0;
while (counter<8) {
PORTA &= ~(1 << counter);
PORTB &= ~(1 << counter);
PORTC &= ~(1 << counter);
PORTD &= ~(1 << counter);

wait();
counter++;
}
}
}

User avatar
Tanja_Kovacevic
mikroElektronika team
Posts: 98
Joined: 09 Aug 2021 11:39

Re: Led curtain help

#2 Post by Tanja_Kovacevic » 20 Oct 2021 09:44

Hi,

DDRA, DDRB, PORTA, PORTB, etc are not commands, that's why you couldn't find them in the Help section.
Those are the registers in the microcontroller.

Capture1.PNG
Capture1.PNG (70.79 KiB) Viewed 1270 times

The picture is from the datasheet of default MCU:
https://ww1.microchip.com/downloads/en/ ... oc2503.pdf

If you are asking about the clarification of the LED turning on and off:
  • the first for loop goes through all 4 ports (A, B, C, and D), and in every iteration set the current bit to 1 (which lit up the diode at that location)
  • in the while loop (below for) the opposite happens, bits are set to zero - one by one (which turn off the LED at the current location)
<< is an operator for shifting the value, the statement "1 << counter" means that you will shift 1 at the counter position. For example 1<<3 is 1000.
|= is the bit-wise OR operator in abbreviated form with the assignment. For example, PORTA |= 1; is the same as PORTA = PORTA | 1;
&= bit-wise AND operator with the assignment.
~ is a negation (~1 is 0 and ~0 is 1).

Kind regards,
Tanja

ctesla75
Posts: 22
Joined: 10 Jul 2010 06:05

Re: Led curtain help

#3 Post by ctesla75 » 20 Oct 2021 12:55

Thanks for the reply Tanja,
I have now downloaded the datasheet to try and better understand,. As for the led turning on and off. My attempt worked but only as a binary counter , and i couldnt understand how the example worked. I thought it was converting hex to decimal, and i didnt understand the operators. This clears up a lot, Thank you very much. I am still on the lookout for a C language book specifically for micros , avr specifically, but will try a pc c book for now,

Post Reply

Return to “mikroC PRO for AVR General”