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++;
}
}
}
Led curtain help
- Tanja_Kovacevic
- mikroElektronika team
- Posts: 98
- Joined: 09 Aug 2021 11:39
Re: Led curtain help
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.
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:
|= 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
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.
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 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
Re: Led curtain help
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,
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,