сдесь будут помещаться рзличные примеры кода и прочего обсуждаемого в других темах.. также скидывайте сюда любую полезную информацию по авр микроконтроллерам.
Avr, все про авр микрокотроллеры
Сообщений 1 страница 3 из 3
Поделиться22007-12-25 15:06:39
Вот вам один кодок, для начала. Он 100% рабочий
Это пример работы USART на МК Atmega16 при частоте кварца 11,059000
код:
#include <mega16.h> //обычный хидер
#include <m8_128.h> //хидер для битов
#include <delay.h>
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// Буфер - USART Receiver buffer
#define RX_BUFFER_SIZE 100
char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
rx_wr_index ++; // инкремент индекса
if (rx_wr_index == RX_BUFFER_SIZE)
{
rx_wr_index=0;
};
++rx_counter;
if (rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
// взять символ из созданного буфера USART
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Глобальные переменные
void main(void)
{
// Локальные переменные
PORTD=0x00;
DDRD=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// Global enable interrupts
#asm("sei")
delay_ms(100);//пауза
printf("Hello world\r\n"); //отправляем строчку и переходим на новую строчку, и возвращаем калитку.
delay_ms(100);//пауза
printf("-=gga=-\r\n");
}
Народ, по активнее будьте, не жалейте коды!!!!!
Поделиться32007-12-25 15:09:16
Здравствуйте. Вот код написал для управления матрйчным-LCD(Программа реализована на CodeVisionAVR).
Некоторые блоки программы закоментированны, взависимости какие функции управления LCD требуются.
код:
#include <mega32.h>
#include <delay.h>
#include <mem.h>
#include <stdlib.h>
//--------------------------------------------------------------------------------------
unsigned char l_display_array[4][122];
unsigned char l_mask_array[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
/*struct IMG_DEF
{
unsigned char width_in_pixels;
unsigned char height_in_pixels;
unsigned char *char_table;
}; */
//--------------------------------------------------------------------------------------
void intn()
{
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0x07;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
}
//--------------------------------------------------------------------------------------
void lcd_strobe_e(unsigned char ncontr)
{
delay_us(2);
if ( ncontr & 0x01 )
PORTB.1 &= 0;
if ( ncontr & 0x02 )
PORTB.2 &= 0;
delay_us(2);
PORTB.1 |= 1;
PORTB.2 |= 1;
}
//--------------------------------------------------------------------------------------
void lcd_out_ctl(unsigned char cmd, unsigned char ncontr)
{
PORTB.0 &= 0;
PORTA = cmd;
lcd_strobe_e(ncontr);
}
//--------------------------------------------------------------------------------------
void lcd_out_dat(unsigned char dat, unsigned char ncontr)
{
PORTB.0 |= 1;
PORTA = dat;
lcd_strobe_e(ncontr);
}
//-----------------------------------------------------------------
void lcd_init1(void)
{
lcd_out_ctl(0,3);
lcd_out_ctl(0xE2,3);
delay_ms(100);
lcd_out_ctl(0xAF,3);
lcd_out_ctl(0xA1,3);
lcd_out_ctl(0xC0,3);
lcd_out_ctl(0xB8,3);
lcd_out_ctl(0x00+19,3);
}
//--------------------------------------------------------------------------------------
void lcd_update(unsigned char top, unsigned char bottom)
{
unsigned char x, y, yt, yb, *colptr;
yb=bottom>>3;
yt=top>>3;
for(y=yt;y<=yb;y++)
{
lcd_out_ctl(0xB8+y,3);
// lcd_out_ctl(LCD_SET_COL+LCD_STARTCOL_REVERSE,3);
lcd_out_ctl(0x00+0,3);
colptr=&(l_display_array[y][0]);
for (x=0;x<122;x++)
{
if (x<61)
lcd_out_dat(*colptr++,1);
else
lcd_out_dat(*colptr++,2);
}
}
}
//--------------------------------------------------------------------------------------
void lcd_update_all(void)
{
lcd_update(0,31);
}
//--------------------------------------------------------------------------------------
void lcd_fill(unsigned char pattern)
{
unsigned char page, col;
for (page=0; page<4; page++)
{
for (col=0; col<122; col++)
l_display_array[page][col]=pattern;
}
lcd_update_all();
}
//--------------------------------------------------------------------------------------
void lcd_erase(void)
{
lcd_fill(0x00);
lcd_update_all();
}
//--------------------------------------------------------------------------------------
void lcd_dot(unsigned char x, unsigned char y, unsigned char mode)
{
unsigned char bitnum, bitmask, yByte;
unsigned char *pBuffer;
if ( ( x > 121 ) || ( y > 31 ) ) return;
yByte = y >> 3;
bitnum = y & 0x07;
bitmask = l_mask_array[bitnum];
pBuffer = &(l_display_array[yByte][x]);
switch (mode)
{
case 1:
*pBuffer |= bitmask;
break;
case 0:
*pBuffer &= ~bitmask;
break;
case 2:
*pBuffer ^= bitmask;
break;
default: break;
}
}
//--------------------------------------------------------------------------------------
/*void lcd_line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int mode )
{
unsigned int length, xTmp, yTmp, i, y, yAlt;
int m;
if(x1 == x2)
{
if(y1 > y2)
{
xTmp = x1;
yTmp = y1;
x1 = x2;
y1 = y2;
x2 = xTmp;
y2 = yTmp;
}
length = y2-y1;
for(i=0; i<=length; i++)
lcd_dot(x1, y1+i, mode);
}
else
if(y1 == y2)
{
if(x1 > x2)
{
xTmp = x1;
yTmp = y1;
x1 = x2;
y1 = y2;
x2 = xTmp;
y2 = yTmp;
}
length = x2-x1;
for(i=0; i<=length; i++)
lcd_dot(x1+i, y1, mode);
}
else
{
if(x1 > x2)
{
xTmp = x1;
yTmp = y1;
x1 = x2;
y1 = y2;
x2 = xTmp;
y2 = yTmp;
}
if((y2-y1) >= (x2-x1) || (y1-y2) >= (x2-x1))
{
length = x2-x1;
m = ((y2-y1)*200)/length;
yAlt = y1;
for(i=0; i<=length; i++)
{
y = ((m*i)/200)+y1;
if((m*i)%200 >= 100)
y++;
else if((m*i)%200 <= -100)
y--;
lcd_line(x1+i, yAlt, x1+i, y, mode );
if(length<=(y2-y1) && y1 < y2)
yAlt = y+1;
else if(length<=(y1-y2) && y1 > y2)
yAlt = y-1;
else
yAlt = y;
}
}
else
{
if(y1 > y2)
{
xTmp = x1;
yTmp = y1;
x1 = x2;
y1 = y2;
x2 = xTmp;
y2 = yTmp;
}
length = y2-y1;
m = ((x2-x1)*200)/length;
yAlt = x1;
for(i=0; i<=length; i++)
{
y = ((m*i)/200)+x1;
if((m*i)%200 >= 100)
y++;
else if((m*i)%200 <= -100)
y--;
lcd_line(yAlt, y1+i, y, y1+i, mode);
if(length<=(x2-x1) && x1 < x2)
yAlt = y+1;
else if(length<=(x1-x2) && x1 > x2)
yAlt = y-1;
else
yAlt = y;
}
}
}
}*/
//--------------------------------------------------------------------------------------
void lcd_circle(unsigned int xCenter, unsigned int yCenter, unsigned int radius, unsigned int mode)
{
int tSwitch, y, x = 0;
unsigned int d;
d = yCenter - xCenter;
y = radius;
tSwitch = 3 - 2 * radius;
while (x <= y)
{
lcd_dot(xCenter + x, yCenter + y, mode);
lcd_dot(xCenter + x, yCenter - y, mode);
lcd_dot(xCenter - x, yCenter + y, mode);
lcd_dot(xCenter - x, yCenter - y, mode);
lcd_dot(yCenter + y - d, yCenter + x, mode);
lcd_dot(yCenter + y - d, yCenter - x, mode);
lcd_dot(yCenter - y - d, yCenter + x, mode);
lcd_dot(yCenter - y - d, yCenter - x, mode);
if (tSwitch < 0)
tSwitch += (4 * x + 6);
else
{
tSwitch += (4 * (x - y) + 10);
y--;
}
x++;
}
}
//--------------------------------------------------------------------------------------
/*void lcd_bitmap(unsigned int left, unsigned int top, struct IMG_DEF *img_ptr, unsigned int mode)
{
unsigned int width, heigth, h, w, pattern, mask;
unsigned int* ptable;
width = peekb(&(img_ptr->width_in_pixels));
heigth = peekb(&(img_ptr->height_in_pixels));
ptable = (unsigned int*) peekw(&(img_ptr->char_table));
for ( h=0; h<heigth; h++ )
{
mask = 0x80;
pattern = peekb( ptable );
ptable++;
for ( w=0; w<width; w++ )
{
if ( pattern & mask )
lcd_dot(w+left, h+top, mode);
mask >>= 1;
if ( mask == 0 )
{
mask = 0x80;
pattern = peekb( ptable );
ptable++;
}
}
}
} */
//--------------------------------------------------------------------------------------
/*void lcd_glyph(unsigned int left, unsigned int top, unsigned int width, unsigned int height, unsigned int *glyph_ptr, unsigned int store_width)
{
unsigned int bit_pos;
unsigned int byte_offset;
unsigned int y_bits;
unsigned int remaining_bits;
unsigned int mask;
unsigned int char_mask;
unsigned int x;
unsigned int *glyph_scan;
unsigned int glyph_offset;
bit_pos = top & 0x07;
glyph_offset = 0;
char_mask = 0x80;
for (x = left; x < (left + width); x++)
{
byte_offset = top >> 3;
y_bits = height;
remaining_bits = 8 - bit_pos;
mask = l_mask_array[bit_pos];
glyph_scan = glyph_ptr + glyph_offset;
while((y_bits) && (byte_offset < 4))
{
if(peekb(glyph_scan) & char_mask)
l_display_array[byte_offset][x] |= mask;
else
l_display_array[byte_offset][x] &= ~mask;
if(l_mask_array[0] & 0x80)
mask >>= 1;
else
mask <<= 1;
y_bits--;
remaining_bits--;
if(remaining_bits == 0)
{
remaining_bits = 8;
byte_offset++;
mask = l_mask_array[0];
}
glyph_scan += store_width;
}
char_mask >>= 1;
if(char_mask == 0)
{
char_mask = 0x80;
glyph_offset++;
}
}
}*/
//--------------------------------------------------------------------------------------
/*void lcd_box(unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned int mode)
{
unsigned int i;
if (!width) return;
width--;
for (i=y;i<y+height;i++)
lcd_line(x, i, x+width, i, mode);
}*/
//--------------------------------------------------------------------------------------
/*void lcd_text_intern(unsigned int left, unsigned int top, unsigned int font, char *str, unsigned int inprogmem)
{
unsigned int x = left;
unsigned int glyph;
unsigned int width;
unsigned int height, defaultheight;
unsigned int store_width;
unsigned int *glyph_ptr;
unsigned int *width_table_ptr;
unsigned int *glyph_table_ptr;
unsigned int glyph_beg, glyph_end;
unsigned int fixedwidth;
defaultheight = peekb ( &(fonts[font].glyph_height) );
store_width = peekb ( &(fonts[font].store_width) );
width_table_ptr = (unsigned int*) pgm_read_word( &(fonts[font].width_table) );
glyph_table_ptr = (unsigned int*) pgm_read_word( &(fonts[font].glyph_table) );
glyph_beg = peekb( &(fonts[font].glyph_beg) );
glyph_end = peekb( &(fonts[font].glyph_end) );
fixedwidth = peekb( &(fonts[font].fixed_width) );
if (inprogmem)
glyph = peekb(str);
else
glyph = (unsigned int)*str;
while(glyph != 0x00) // while(*str != 0x00)
{
if((glyph < glyph_beg) || (glyph > glyph_end))
glyph = peekb( &(fonts[font].glyph_def) );
glyph -= glyph_beg;
if(fixedwidth == 0)
// width=fonts[font].width_table[glyph];
width=peekb(width_table_ptr+glyph);
else
width = fixedwidth;
height = defaultheight;
//glyph_ptr = fonts[font].glyph_table + ((unsigned int)glyph * (unsigned int)store_width * (unsigned int)height);
glyph_ptr = glyph_table_ptr + ((unsigned int)glyph * (unsigned int)store_width * (unsigned int)height);
if(x > 121)
x = 121;
if((x + width) > 121+1)
width = 121 - x + 1;
if(top > 31)
top = 31;
if((top + height) > 31+1)
height = 31 - top + 1;
lcd_glyph(x,top,width,height,glyph_ptr,store_width);
x += width;
str++;
if (inprogmem)
glyph = peekb(str);
else
glyph = (unsigned int)*str;
}
}
//--------------------------------------------------------------------------------------
void lcd_text(unsigned int left, unsigned int top, unsigned int font, const char *str)
{
lcd_text_intern(left, top, font, str, 0);
}
//--------------------------------------------------------------------------------------
void lcd_text_p(unsigned int left, unsigned int top, unsigned int font, const char *str)
{
lcd_text_intern(left, top, font, str, 1);
}*/
//--------------------------------------------------------------------------------------
void main(void)
{
intn();
lcd_init1();
while (1)
{
lcd_erase();
/* lcd_line(121,31,1,31,1);
lcd_line(121,1,1,1,1);
lcd_line(121,1,121,31,1);
lcd_line(1,1,1,31,1);
lcd_line(119,29,3,29,1);
lcd_line(119,3,3,3,1);
lcd_line(119,3,119,29,1);
lcd_line(3,3,3,29,1);*/
lcd_circle(90,20,15,1);
lcd_circle(20,10,5,1);
lcd_update_all();
delay_ms(2000);
/*
lcd_text(5,4,FONT_SIX_DOT,"#abcdefghijkl");
lcd_text(5,20,FONT_SEVEN_DOT,"mnopqrstuwxyvz");*/
/* lcd_box(2,2,118,28,2);
/* lcd_erase();
lcd_text(4,7,FONT_EIGHTEEN_DOT,"12:34:56");*/
/* lcd_erase();
lcd_text(5,0,FONT_NINE_DOT,"EW12A03GLY");
lcd_text(5,16,FONT_SEVEN_DOT,"Proteus VSM");
lcd_box(0,0,121+1,31+1,LCD_MODE_XOR);
lcd_box(0,0,121+1,31+1,LCD_MODE_XOR);*/
/* lcd_bitmap(90, 1, &frame_bmp, 1);
lcd_erase(); */
/*
lcd_text_p(6,3,FONT_NINE_DOT,PSTR("Labcenter"));
lcd_text_P(6,15,FONT_SEVEN_DOT,"Electronics");*/
};
}
эта программа написана под дисплей SED1520. Но переписать эту программу под любой другой дисплей не составляет ни какой сложности. И вот ещё что эта программа находится в разработке, и если кто хочет помочь в написании программы очень буду рад.