100% found this document useful (2 votes)
129 views

3 - C Programming

This document discusses programming AVR microcontrollers in C. It covers compilers, data types, statements, I/O registers, and bitwise operations in C. Examples are provided to demonstrate reading from and writing to specific bits of ports. Programming in C is easier but produces larger files than assembly, so understanding data types and operations is important for efficiency.

Uploaded by

komailhaiderz123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
129 views

3 - C Programming

This document discusses programming AVR microcontrollers in C. It covers compilers, data types, statements, I/O registers, and bitwise operations in C. Examples are provided to demonstrate reading from and writing to specific bits of ports. Programming in C is easier but produces larger files than assembly, so understanding data types and operations is important for efficiency.

Uploaded by

komailhaiderz123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

AVR C Programming

LECTURE 3
AVR Programing in C
Programming in C
◼ Compilers produce Hex files that is loaded in the
microcontroller

◼ Size of this Hex file is one of the main concern for


microcontroller programming because microcontrollers
have limited on-chip memory

◼ Assembly language produces Hex file which are much


smaller than C, but programming in Assembly is often
tedious and time consuming

◼ C programming is less time consuming and is much


easier to write but the Hex file produced is much larger
Programming in C
◼ Following are the few main reason for
programming in C rather then in Assembly
◼ It is easier and less time consuming
◼ C is easier to modify and update
◼ You can use codes available in function libraries
◼ C code is portable to other microcontrollers with little
or no modifications
C compilers
◼ Several third party compilers are available for AVR
microcontrollers and anyone of them could be
used
◼ Microchip AVR Studio
◼ Debug Demo
◼ AVR GCC will be used as compiler
DEMO
#include<avr/io.h>
int main(void)
{
unsigned char x=0;
DDRB=0xFF;//Setting Port B as output
while(1)
{
PORTB=0x00;
PORTB=0xAA;
PORTB=0x00;
PORTB=0xFF;
}

}
C data types for AVR C
◼ One goal for AVR programming is to create a smaller Hex
file

◼ In order to achieve that good understanding of C data


types for AVR is important

◼ 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

Data Type Size in Bits Data Range/Usage


unsigned char 8-bit 0-255
char 8-bit -128 to +127
unsigned int 16-bit 0 to 65,535
int 16-bit -32,768 to +32,767
unsigned long 32-bit 0 to 4,294,967,295
long 32-bit -2,147,483,648 to +2,147,483,648
float 32-bit ±1.75e-38 to ±3.402e38
double 32-bit ±1.75e-38 to ±3.402e38
Data Type (unsigned char)
Unsigned Char
◼ It is an 8-bit data type with range from 0 to 255
◼ Since AVR is 8 bit microcontroller, character data type is
the most natural choice for most applications
◼ In many application where there is no need for signed
data this data type should be used
◼ In declaring data type one should be careful to the size
of the data and use unsigned char in place of int.
Microcontrollers have limited set of registers and
memory locations in its RAM, using int in place of char
can lead to use of more memory space
Data Type (char)
char (sign char)
◼ It is an 8-bit data type with most significant bit assigned
to represent the + or – value
◼ There are 7 bits for the magnitude of the of the signed
number so we have a range from -128 to +127
◼ In the situation where + or – is needed to represent a
given quantity such as temperature, the use of signed
char data type is necessary
◼ If the keyword unsigned is not used by default char is
signed character
Data Type (unsigned int)
unsigned int
◼ It is a 16-bit data type that takes value in a range of 0-
65,535
◼ In AVR unsigned int is used to define 16-bit data variables
◼ Since AVR is a 8-bit microcontroller and int data takes two
bytes of RAM, we must not use it until necessary
◼ Because registers and data accesses are in 8-bit chunks so
misuse of int data type will result in larger hex files, slower
execution of program and more memory usage
Data Type (signed int)

int (signed int)


◼ It is a 16-bit data type where the most significant bit
represent the + or – value
◼ There are 15 bits for the magnitude of the data or value
ranging from -32,768 to +32,767
Other data types
◼ AVR C compilers, support long data types, if we
want values greater than 16-bit
◼ To deal with fractions AVR compilers also
support float and double data types
C language Statements
◼ Following are the statements which are most
commonly used while microcontroller
programming in C
◼ For loop
◼ While loop
◼ Do/while
◼ If/else statements
◼ Switch statement
C code
#include<avr/io.h> //All the required header files are declared
int main(void)
{
unsigned char x=0; //they have garbage value in them and not zero
unsigned char y=0; //variable are defined one the variable are
defined
unsigned int z=0;
while(x==1)
{
for(y=2; y<=100; y++)
{
}
}
if (z==3)
{
}
else
{
}
}
Byte size I/O or Register access
Write a program which outputs binary 10101010 on the
port B

#include<avr/io.h> //standard AVR header


int main(void)
{
DDRB=0xFF; //Setting Port B as output
PORTB=0xAA;
}
Example
◼ Write a AVR program to get a byte of data from
Port B, and then send it to Port C.

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

◼ But some AVR compilers such as AVR GCC (WinAVR) does


not support this feature

◼ There are few C compilers which support bit addressability

◼ In order to overcome this problem AND and OR bitwise


operations are used so a single bit can be accessed without
disturbing the rest of the byte
Bit wise operators in C
◼ Following are the bit wise operator in C which
are widely used for embedded systems
programming
◼ AND(&)
◼ OR(|)
◼ EX-OR(^)
◼ Inverter(~)
◼ Shift right(>>)
◼ Shift left(<<)
Bitwise operators in C
◼ AND(&)
◼ It should not be confused with the logical AND(&&)
operator [ True or False ]
0011 0101
◼ 0x35 & 0x0F = 0x05 0000 1111
00110101 & 00001111=00000101 0000 0101
◼ OR(|)
◼ It should not be confused with logical OR(||)
operator
◼ 0x04 | 0x68 = 0x6C 0000 0100
1101 0000
00000100 | 11010000=11010100 1101 0100
Bit wise operators in C

◼ 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

bits to remain unchanged)


we use bit wise AND (&) operator 1010100
x = x & 0b01111111; 0111111
0010100

◼ In order to send ‘1’ to a bit 7 of register


‘x’ we use bit wise OR (|) operator
x = x | 0b10000000; 1010100
1000000
1010100
Example
◼ Write an AVR program to toggle only bit 5 of Port B
continuously without disturbing the rest of the pins of
PORT B

#include<avr/io.h> //standard AVR header


int main(void)
{
DDRB=0xFF; //Setting Port B as output
while(1)
{
PORTB=PORTB | 0b00010000; //set bit 5
PORTB=PORTB & 0b11101111; //clear bit 5
}
return 0;
}
Example
◼ A door sensor is connected to the bit 1 of Port B, and an LED is
connected to bit 7 of Port C. Write an AVR program to monitor
the door sensor and, when it opens (set), turns on the LED

#include<avr/io.h>//standard AVR header


int main(void)
{
DDRB= DDRB & 0b11111101;//Set bit 1 as input
DDRC= DDRC | 0b10000000;//Set bit 7 as output
while(1)
{
if (PNIB & 0b00000010)//Check bit 1 of PNIB
PORTC = PORTC | 0b10000000; //Set bit 7 of PORT C
else
PORTC = PORTC & 0b01111111; //Clear bit 7 of PORT C
}
return 0;
}
Bitwise operator in C (Shift operator)
◼ Shift right
◼ Symbol >>
◼ Format of Shift Operator
data>>number of bit to be shifted
◼ Example
0b00100000 >> 3 = 0b00000100
◼ Shift left
◼ Symbol <<
◼ Format of Shift Operator
data<<number of bit to be shifted
◼ Example
0b00010000 << 3 = 0b10000000
Example
◼ Take input from the first four bits of Port A and output it to last four
bits of Port A.

#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

You might also like