0% found this document useful (0 votes)
24 views31 pages

7 C Prgramming v21

This document discusses AVR programming in C, including data types, time delays, I/O programming, logic operations, and data serialization. It covers basic C syntax, accessing I/O registers, using bitwise operators for manipulation, different memory types (flash, EEPROM, RAM), and how to access flash and EEPROM memory.

Uploaded by

Pipat Nakaseno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views31 pages

7 C Prgramming v21

This document discusses AVR programming in C, including data types, time delays, I/O programming, logic operations, and data serialization. It covers basic C syntax, accessing I/O registers, using bitwise operators for manipulation, different memory types (flash, EEPROM, RAM), and how to access flash and EEPROM memory.

Uploaded by

Pipat Nakaseno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 31

AVR Programming in C

Chapter 7

Sepehr Naimi

www.NicerLand.com
Topics
 Data Types
 Time Delays
 IO Programming in C
 Logic Operation in C
 Data serialization in C
Languages
 High Level Languages
 Easy to develop and
update
 C Language
 Acceptable performance
High Level Languages
 Easy to develop and (like VB)
update C Language
 Portable
Machine Language
 Low Level Languages
 High performance
 Not portable
A simple C program
 Write a program that calculate the sum of {1,3,
…,13,15}
A simple C program
 Write a program that calculate the sum of {1,3,
…,13,15}

int main ()
{
unsigned int sum;

for (int i = 1; i <= 15; i+=2)


sum += i;

while (1);
return 0;
}
Accessing I/O registers
 Example 1: Write an AVR C program to send
value 0xAA to PORTD.

#include <avr/io.h>

int main ()
{
DDRD = 0xFF;
PORTD = 0xAA;

while (1);
return 0;
}
Accessing I/O registers
 Example 2: Write an AVR C program to calculate
PINB + PINC and send the result to PORTD.
#include <avr/io.h>

int main ()
{
DDRB = 0x00;
DDRC = 0x00;
DDRD = 0xFF;

while (1)
PORTD = PINB + PINC;
return 0;
}
What is happening in machine language?
int Fibonnaci (int n); int Fibonnaci (int n)
{
int main () int a1 = 1;
{ int a2 = 1;
unsigned int sum = 0; int i;
int temp;
for(int j = 0; j < 11; j++)
{ if(n == 0)
sum += Fibonnaci return 0;
(j); else
} if(n <= 2)
return 1;
while(1); else{
return 0; for(i = 2; i < n; i++)
} {
temp = a2;
a2 = a1 + a2;
a1 = temp;
}
return a2;
}
}
Data Types
 Use unsigned whenever you can
 unsigned char instead of unsigned int if you can
Data types (cont.)
 char c; c  long lng; lng

 int a1; a1
 unsigned int a;
a
Data types (cont.)
 char c; c  long lng; lng

 int a1; a1
 unsigned int a;
a

What is the difference between int and unsigned int?


int vs. unsigned int

 int a1 = 5;
int a2 = 3;
int a3 = 9;
b = (a1 * a2) + a3;
int vs. unsigned int

 int a1 = 5;
AVR is an 8-bit. How could we multiply
int a2 = 3; 16-bit numbers?

int a3 = 9;
b = (a1 * a2) + a3;
Multiplications of Big Numbers
23 ab
*49 *cd
27 b*d
18 ad+bc
27
8 ac
Choosing optimized data type
unsigned int sum; unsigned char sum;

for (int i = 1; i <= 15; i+=2) for (char i = 1; i <= 15; i+=2)
sum += i; sum += i;
Accessing I/O registers
#include <avr/io.h>

int main ()
{
DDRD = 0xFF;

while(1)
{
for (unsigned char i = 0; i <= 9; i++)
PORTD = i;
}
return 0;
}
Time Delays in C
 You can use for to make time delay

void delay(void)
{
volatile unsigned int i;
for(i = 0; i < 42150; i++)
{ }
}
Time Delays in C
 You can use for to make time delay

void delay(void)
{
volatile unsigned int i;
for(i = 0; i < 42150; i++)
{ }
}

If you use for loop


 The clock frequency can change your delay duration !

 The compiler has direct effect on delay duration!


Time Delays in C
 You can use predefined functions of compilers to make time
delays

In Atmel Studio:

First you should include:


#define F_CPU 8000000UL
#include <util/delay.h>

and then you can use


_delay_us(200); //200 microseconds
_delay_ms(100); //100 milliseconds

 It is compiler dependant
I/O programming in C
Byte size I/O programming in C

DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}
I/O programming in C
Byte size I/O programming in C

DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}

Different compilers have different syntax for bit


manipulations!
I/O programming in C
Byte size I/O programming in C

DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}

Different compilers have different syntax for bit


manipulations!
I/O programming in C
Byte size I/O programming in C

DDRB = 0xFF;
while (1) {
PORTB = 0xFF;
_delay_ms(500);
PORTB = 0x55;
_delay_ms(500);
}

Different compilers have different syntax for bit


manipulations!
It is better to use
Bit-wise logical operators
Bit-wise logical operators

1110 1111 1110 1111


& 0000 0001 | 0000 0001 ~ 1110 1011
-------------- -------------- --------------
0000 0001 1110 1111 0001 0100
Shift operations in C
 data >> number of bits to be shifted right
 data << number of bits to be shifted left

1110 0000 >> 3 0000 0001 <<2


-------------- --------------
0001 1100 0000 0100
Setting a bit in a Byte to 1
 We can use | operator to set a bit of a byte to 1

xxxx xxxx xxxx xxxx


| 0001 0000 OR | 1 << 4
------------- xxx1 ------------- xxx1
xxxx xxxx

PORTB |= (1<<4); //set bit 4 (5th bit) of PORTB


Clearing a bit in a Byte to 0
 We can use | operator to set a bit of a byte to 1

xxxx xxxx xxxx xxxx


& 1110 1111 OR & ~(1 << 4)
------------- xxx0 ------------- xxx0
xxxx xxxx

PORTB &= ~(1<<4); //clear bit 4 (5th bit) of PORTB


Checking a bit in a Byte
 We can use & operator to see if a bit in a byte is 1 or 0

xxxx xxxx xxxx xxxx


& 0001 0000 OR & (1 << 4)
------------- 000x ------------- 00x0
0000 0000

if( ((PINC & (1<<5)) != 0) //check bit 5 (6th bit)


Memory Types In AVR
 Flash Memory
 Not deleted when power is off
 Big in size
 Suitable for codes, tables and fixed data
 EEPROM
 Not deleted when power is off
 Not very big in size
 Suitable for small data that may be modified but should not be
lost when power is off

 RAM
 deleted when power is off
 Suitable for storing the data we want to manipulate because we
have fast access to read or modify them.
Accessing Flash
 #include <avr/pgmspace.h>
 const unsigned char PROGMEM lookup[] ={5,6,7,4};
ourData = pgm_read_byte(&lookup[i]);

#include <avr/pgmspace.h>

const unsigned char PROGMEM lookup[] ={5,6,7,4};

int main(void)
{
unsigned char a;
a = pgm_read_byte(&lookup[i]);

while (1);
}
Accessing EEPROM

#include <avr/io.h>
#include <avr/eeprom.h>

unsigned char EEMEM myVar; //reserve a location in EEPROM

int main(void)
{
DDRC = 0xFF;

if((PINB&(1<<0)) != 0) //if PB0 is HIGH


eeprom_write_byte(&myVar,'G'); //read from EEPROM
else
PORTC = eeprom_read_byte(&myVar); //write to EEPROM

while (1);
}

You might also like