3 - C Programming
3 - C Programming
LECTURE 3
AVR Programing in C
Programming in C
◼ Compilers produce Hex files that is loaded in the
microcontroller
}
C data types for AVR C
◼ One goal for AVR programming is to create a smaller Hex
file
◼ Table on the next slide shows the C data types that are
common and widely used in AVR C compilers. This shows
the data types and their sizes for AVR GCC but may vary
for different compiler
C Data Types in AVR GCC
PORTC = PINB
Example
◼ Write an AVR program to get a byte of data from Port C. If
it is less then 100 send it to port B otherwise, send it to Port
D
Bit size I/O or Registers access in C
◼ All the I/O ports and registers are bit addressable in
Atmega16
◼ XOR(^)
1010100
◼ 0x54 ^ 0x78 = 0x2C 1111000
1010100 ^ 1111000 = 0101100 0101100
◼ Inverter(~)
◼ ~0x55 = 0xAA
~01010101 = 10101010
Reading or writing a bit in a Register
Consider 8-bit variables ‘x’ and ‘y’
◼ In order to send ‘0’ to a bit 7 of register ‘x’ (remaining
#include<avr/io.h>
unsigned char x;
int main(void)
{
DDRA=0b11110000;
while(1)
{
x = PINA & 0b00001111;
x = x<<4;
PORTA = x & 0b11110000;
}
}
BitOperator.c
Thank You