avr programming in c - saqazi.comsaqazi.com/eee342/fa19_mp_lecture11_20191010_avr...comsats...

29
AVR Programming in C Lecture# 11 Microprocessor System and Interfacing Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 1

Upload: others

Post on 11-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

AVR Programming

in CLecture# 11

Microprocessor System and Interfacing

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

1

Page 2: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

AVR Programming in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

2

Compiler produce hex files to download in AVR

Size of hex file is major concern

Limited on-chip Flash Memory

Assembly Language: Small size of .hex files

C Language: Large Size of .hex files

Benefits:

Easier to write

Less time consuming

Easier to modify

Function libraries

C code is portable

Page 3: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

3

C Data Types

Page 4: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

4

C Data Types – Unsigned Char

Most natural choice – 8bit

range 0 – 255

May be used to set counter values

Use unsigned char (8-bit) instead of int (16-bit) if possible

limited number of registers and RAM locations

int may require more memory space

Page 5: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

5

C Data Types – Signed Char

Most significant bit (D7) is used to represent +ve / -ve

1 is –ve number

0 is +ve number

range -128 -- +127

May be used to represent temperatures

The default is signed char, mention word unsigned if +ve / - ve is

not required

Page 6: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

6

#include <avr/io.h>

int main (void)

{

unsigned char z;

DDRB = 0xFF;

for (z = 0; z <= 255; z++)

{

PORTB = z;

}

return 0;

}

Write a C program to send values 00 – FF to Port B.

The above program never exits the for loop.

Any Reason ?

Page 7: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

7

#include <avr/io.h>

int main (void)

{

char num [ ] = {-4, -3, -2, -1, 0, +1 , +2 , +3, +4};

unsigned char z;

DDRB = 0xFF;

for (z = 0; z <= 8; z++)

{

PORTB = num [ z ];

}

return 0;

}

Write an AVR C program to send values -4 to +4 to Port B.

Page 8: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

8

C Data Types – Unsigned int

16-bit Data type

range 0 – 65,536 (0000 – FFFF)

Can define memory addresses

Counter which require more than 255 time

May not use it unless we have to

Misuse may result

Large .hex files

Slower execution

More memory usage

Page 9: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

9

C Data Types – Signed int

16-bit Data type

D15 is used as signed bit

range -32768 - +32767

Other Data Types

int is limited to values (0 – 65536)

if we need values greater than 16-bits

Use long data types

Page 10: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Class Exercise 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

10

#include <avr/io.h>

int main (void)

{

unsigned char list [ ] = “012345ABCD”;

unsigned char z;

DDRB = 0xFF;

for (z = 0; z <10; z++)

{

PORTB = list [ z ];

}

return 0;

}

Write an AVR C program to send hex values for ASCII characters

of 0, 1, 2, 3, 4, 5, A, B, C and D to Port B.

Page 11: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

11

Time Delay

Three ways to create delays

using a for loop

predefined functions

AVR timers

Crystal

Duration of clock period is function of this crystal

for loop delays

Crystal frequency connected to XTAL1 – XTAL2

Duration of clock period is function of this crystal

Different compilers generate different hex codes

Compiler may omit the loops as it only wastes CPU time

Set level of optimization to zero (none)

Page 12: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Types and Time Delays in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

12

Time Delay

Pre-defined functions

_delay_ms() or _delay_us ()

delay_ms() or delay_us()

Portability issue

different compilers do not use the same name

Use macros and wrapper functions to overcome this issue

Page 13: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

13

#include <avr/io.h>

void delay100ms (void)

{

unsigned int i;

for (i = 0; I < 42150; i++);

}

int main (void)

{

DDRB = 0xFF;

while (1)

{

PORTB = 0xAA;

delay100ms ();

PORTB = 0x55;

delay100ms();

}

return 0;

}

Write an AVR C program to toggle all the bits of Port B

continuously with a 100ms delay. Assume that the system is

Atmega 32 with XTAL = 8MHz.

Page 14: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 4 – Wrapper Functions

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

14

#include <avr/io.h>

#include <util/delay.h>

int main (void)

{

void delay_msec (int d)

{

_delay_ms (d);

}

DDRB = 0xFF;

while (1) {

PORTB = 0xAA;

delay_msec (100);

PORTB = 0x55;

delay_msec (100);

}

return 0;

}

Write an AVR C program to toggle all the bits of Port B

continuously with a 100ms delay. Assume that the system is

Atmega 32 with XTAL = 8MHz.

Page 15: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

IO Programming in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

15

Byte Size I/O

Access PORT register as a byte

Use PORTx to access

x represents name of the port (A, B, C, D)

Access PIN register as a byte

Use PINx to access

x represents name of the port (A, B, C, D)

Page 16: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Class Exercise 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

16

#include <avr/io.h>

int main (void)

{

unsigned char temp;

DDRB = 0x00;

DDRC = 0xFF;

while (1)

{

temp = PINB;

PORTC = temp;

}

return 0;

}

Write and AVR C program to get a byte of data from Port B, and

then send it to Port C.

Page 17: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

IO Programming in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

17

Bit Size I/O

In most of the AVR C compilers, IO ports are bit accessible

to access a single bit

PORTB.x = 1;

where x is 0 – 7

To make code portable, use bit wise AND and OR operations

Page 18: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Logic Operations in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

18

Bit-wise operators in C

Many bit-wise operators are available for AVR C language

AND OR XOR Inverter

A B A&B A|B A^B Y = ~B

0 0

0 1

1 0

1 1

Page 19: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 4

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

19

#include <avr/io.h>

int main (void)

{

DDRB = 0xFF;

while (1)

{

PORTB = PORTB | 0b00010000;

PORTB = PORTB & 0b11101111;

}

return 0;

}

Write an AVR C Program to toggle only bit 4 of Port B continuously

without disturbing the rest of the pins of Port B.

Page 20: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Class Exercise 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

20

#include <avr/io.h>

int main (void)

{

unsigned char msg [] = “ The Earth is

but One Country”

unsigned char z;

DDRB = 0xFF;

DDRC = DDRC | 0b00100000;

The data pins of an LCD are connected to Port B. The information

is latched into the LCD whenever its Enable pin goes from HIGH to

LOW. The enable pin is connected to pin 5 of Port C (6th pin).

Write a C program to send “The Earth is but One Country” to this

LCD.

for (z = 0; z < 28; z++)

{

PORTB = message [z];

PORTC = PORTC | 0b00100000;

PORTC = PORTC & 0b11011111;

}

return 0;

}

Page 21: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Home Task 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

21

Write an AVR C program to read pins 1 and 0 of PORTB and issue

an ASCII character to Port D according to the following table:

pin1 pin0 ASCII Character

0 0 ‘0’

0 1 ‘1’

1 0 ‘2’

1 1 ‘3’

Page 22: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Home Task 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

22

Write an AVR C program to monitor bit 7 of Port B. If it is 1, make

bit 4 of Port B input; otherwise change pin 4 of Port B to output.

Page 23: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Logic Operations in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

23

Compound Assignment Operators

To reduce the coding size we can use compound assignments for

bit-wise operators

AND assignment (a = a & b) - a &= b

OR assignment (a = a | b) - a |= b

Bit-wise shift operation in C

Two bit-wise operations

Shift right - data >> number of bits to be shifted right

0b00010000 >> 3 = 0b00000010

Shift left - data << number of bits to be shifted left

0b00010000 << 3 = 0b10000000

1 << 5 - 0b00100000

Page 24: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Conversion Programs in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

24

Packed BCD to ASCII Conversion

The Real Time Clock (RTC) provides date and time continuously

Data is provided in packed BCD

First convert to unpacked BCD and then to ASCII

Packed BCD Unpacked BCD ASCII

0x29 0x02, 0x09 0x32, 0x39

Page 25: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 5

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

25

#include <avr/io.h>

int main (void)

{

unsigned char x, y;

unsigned char mybyte = 0x29;

DDRB = DDRC = 0xFF; // make PORTB and C output

x = mybyte & 0x0F; // mask upper 4 bits

PORTB = x | 0x30; // make it ASCII

y = mybyte & 0xF0; // mask lower 4 bits

y = y >> 4; // shift it to lower 4 bits

PORTC = y | 0x30; // make it ASCII

return 0;

}

Write an AVR C program to convert packed BCD 0x29 to ASCII

and display the byte on PORTB and PORTC.

Page 26: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Conversion Programs in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

26

Home Reading Task

Checksum Byte in ROM (Page 277)

Example 30

Binary to decimal and ASCII conversion in C

Page 279 and 280 of Book

Example 31

Page 27: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Data Serialization in C

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

27

Sending of Byte one at a time through single port

Two Ways to perform

Using serial port,

Less control over sequence of data

Transfer one bit at a time and control the sequence

Serial version are popular in LCD, ADC and EEPROM

Takes up less space

Other standards are I2C, SPI and CAN

Page 28: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Example 6

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

28

#include <avr/io.h>

#define serpin 3

int main (void)

{

unsigned char conbyte = 0x46;

unsigned char regALSB;

unsigned char x;

regALSB = conbyte;

DDRC |= (1 << serpin)

for (x = 0; x < 8; x ++)

{

if (regALSB & 0x01)

PORTC |= (1<serpin);

else

PORTC &= ~(1<<serpin);

regALSB = regALSB >> 1;

}

return 0;

}

Write an AVR C program to send the value 46H serially one bit at

a time via PORTC, pin 3. The LSB should go first.

Page 29: AVR Programming in C - saqazi.comsaqazi.com/EEE342/FA19_MP_Lecture11_20191010_AVR...COMSATS University Islamabad 23 Compound Assignment Operators To reduce the coding size we can use

Class Exercise 4

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

29

Write an AVR C program to send the value 55H serially one bit at

a time via PORTC, pin 5. The MSB should go first.

#include <avr/io.h>

#define serpin 5

int main (void)

{

unsigned char conbyte = 0x55;

unsigned char regAMSB;

unsigned char x;

regAMSB = conbyte;

DDRC |= (1 << serpin)

for (x = 0; x < 8; x ++)

{

if (regAMSB & 0x80)

PORTC |= (1<serpin);

else

PORTC &= ~(1<<serpin);

regAMSB = regAMSB << 1;

}

return 0;

}