Nothing Special   »   [go: up one dir, main page]

AVR Programming Examples

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

‫‪Islamic Azad University‬‬

‫‪Najafabad Branch‬‬
‫‪Computer Department‬‬
‫‪Ahmad M. Shafiee‬‬
‫شماره صفحات ذکر شده در اسالیدها مربوط به کتاب «برنامهریزی و استفاده از میکروکنترلر ‪»AVR‬‬
‫از انتشارات دانشپژوهان میباشد‪.‬‬
7segment

A 7segment
#include <mega16.h>
#include <delay.h>
#define xtal 1000000
eeprom char sseg[10]={0x3f, 0x06, 0x5b, …, 0x6f};
void main(void) {
DDRA=255;
while(1) {
for(int i=0; i<10; i++) {
PORTA=sseg[i];
delay_ms(1000); }
}
}
}
#include <mega16.h>
#include <delay.h>
#define xtal 1000000
eeprom char sseg[10]={0x3f, 0x06, 0x5b, …, 0x6f};
void main(void) {
DDRA=255;
DDRB=0xff;
DDRC=0b11111111;
DDRD=0; PORTD=255;
while(1) {
bool reset=0;
for(int i=0; i<10; i++) {
PORTA=sseg[i];
for(int j=0; j<6; j++) {
PORTB=sseg[j];
for(int k=0; k<10; k++) {
PORTC=sseg[k];
while(PIND.1==1)
if(PIND.0==0) {reset=1; break;}
if(reset==0) delay_ms(1000);
else break; }
if(reset==1) break;}
if(reset==1) break; } } }
C
C A 7segment

#include <mega32.h>
#include <delay.h>
#define xtal 2000000
eeprom char sseg[10]={0x3f, 0x06, 0x5b, …, 0x6f};
void main(void) {
DDRA=255;
DDRC=0; PORTC=255;
int counter=0;
while(1) {
PORTA=sseg[counter];
while(PINC.0==1); //falling edge
delay_ms(20); // debounce
if(PINC.1==1) { counter++;
if(counter==10) counter=0;
PORTA=sseg[counter]; }
while(PINC.0==0); //rising edge
delay_ms(20); // debounce
if(PINC.1==0) { counter++;
counter %= 10; } } }
LCD UART
BCD
A LCD
#include <mega32.h>
#include <stdio.h>
#include <lcd.h>
#define xtal 2000000 // quartz crystal frequency (Hz)
#define baud 9600 // Baud rate (bps)
#asm( .equ __lcd_port = 0x1b ); // PORTA
void main(void) {
char buffer[6];
char k;
UBRR = xtal/16/baud-1; // initialize the baudrate
UCR = 0x10; //(1<<RXEN)|(0<<TXEN)
DDRA=255;
lcd_init(16);
while(1) {
k = getchar();
k = ( k>>4 ) + ( k&0x0f );
lcd_clear();
lcd_gotoxy(0, 0);
sprintf(buffer, “sum=%d”, k);
lcd_puts(buffer); }}
7segment
A 4*4
7segment
D C
#include <mega32.h>
#include <delay.h>
#define xtal 2000000
unsigned char scan_key(void); //Function Prototype
eeprom unsigned char svseg[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //7segment lookup table
void main(void) {
unsigned char key1, key2;
DDRC=0b11111111; DDRD=0b11111111;
PORTC=0xff; PORTD=0xbf; // 0b10111111
while(1) {
do{ key1=scan_key(); } while(key1>9);
delay_ms(20);
PORTD=0xff;
PORTC=svseg[key1];
while(scan_key()!=10);
delay_ms(20);
PORTC=0b10111001;
key2=255;
while(key2>9) key2=scan_key();
delay_ms(20);
PORTC=svseg[key2];
while(scan_key()!=11);
delay_ms(20);
PORTC=svseg[(key1+key2)%10];
PORTD=svseg[(key1+key2)/10]; } }
7segment
4*4
scan_key A
D C 7segment
unsigned char scan_key(void)
{
eeprom unsigned char code[4][4]={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}}; //Keyboard
lookup table
unsigned char row, columnStatus, temp;
DDRA=0xF0;
temp=0x7F;
for(signed char row = 3; row >= 0; row--)
{
PORTA = temp;
delay_us(5);
columnStatus = PINA & 0x0f; 00
switch(columnStatus) { 11
22
case 0x07: return code[0][row];
33
case 0x0B: return code[1][row]; 44
case 0x0D: return code[2][row]; 55
case 0x0E: return code[3][row]; 66
77
}
88
temp = ((temp>>1) | 0x80); 99
} 10+
return 0xff; // Error code } 11=
7segment
4*4
scan_key A
D C 7segment
unsigned char scan_key(void)
{
unsigned char butnum, num_key;
eeprom unsigned char keytbl[16]={0xe7, 0xeb, 0xed, 0xee, 0xd7, 0xdb, 0xdd, 0xde, 0xb7, 0xbb, 0xbd,
0xbe, 0x77, 0x7b, 0x7d, 0x7e}; //Keyboard lookup table
DDRA = 0x0f;
PORTA = 0xf0;
delay_us(5);
num_key = PINA;
DDRA = 0xf0;
PORTA = 0x0f;
delay_us(5); 00
num_key = num_key | PINA; 11
22
for (butnum=0; butnum<16; butnum++)
33
{ 44
if (keytbl[butnum] == num_key) 55
return butnum; 66
77
}
88
return 255; 99
} 10+
11=
2’s complement
dot matrix 8*8
D C 2’s complement
C
#include <mega32.h>
#include <delay.h>
B A dot matrix
#define xtal 4000000
flash unsigned char DotM[10][8]={{0x18, 0x3C, 0x66, 0x66, 0x66, 0x66, 0x3C, 0x18}, {0x0C, 0x1C, 0x3C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C}, {0x18, 0x3C, 0x66, 0x06, 0x0C, 0x18, 0x7F, 0x7F}, …};
void main(void)
{
unsigned char row, MaxDig;
signed int i;
DDRA=0xFF;
DDRB=0xFF;
DDRC=0x00;
DDRD=0x00;
while (1){
i = ((signed int) PIND << 8) | PINC;
if(i<0) i = -i;
MaxDig=0;
while(i != 0) {
if( (i%10)>MaxDig ) MaxDig=i%10;
i /= 10; }
for(row = 0; row <= 7; row++){
PORTA = DotM[MaxDig][row];
PORTB = ~(1<<row);
delay_ms(2);
PORTB=0xFF; } } }

You might also like