Sie sind auf Seite 1von 26

UART.

BUZ.C
#include <avr/io.h>
#define CHECKBIT(x,b) (x&(1<<b))
#define SETBIT(x,b) x|=(1<<b)
#define CLEARBIT(x,b) x&=~(1<<b)
#define TOGGLEBIT(x,b) x^=(1<<b)
void USART1A_INIT()
{
UBRR1H=0X00;
UBRR1L=0X67;
UCSR1C=0X06;
UCSR1B=0X18;
}
unsigned char USART1A_REC()
{
while(!(CHECKBIT(UCSR1A,RXC)));
return UDR1;
}
void main()
{
unsigned char z;
DDRA=0XFF;
USART1A_INIT();
while(1)
{
z=USART1A_REC();
switch(z)
{
case '1':
SETBIT(PORTA,3);
break;
case '2':
CLEARBIT(PORTA,3);
break;
case '3':
TOGGLEBIT(PORTA,3);
break;
case '4':
break;

case '5':
break;
case '6':
break;
case '7':
break;
case '8':
break;
}
}
}
INTLED.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
volatile unsigned char a;
void main()
{
DDRC=0XFF;
UBRR1H=0X00;
UBRR1L=0X67;
UCSR1C=0X06;
UCSR1B=0X98;
PORTC=0X00;
sei();
while(1)
{
PORTC=0X00;
}
}
ISR(USART1_RX_vect)
{
PORTC=0xFF;
a=UDR1;

delay_sec(30000);
}
INTLED2.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
void main()
{
DDRC=0XFF;
UBRR1H=0X00;
UBRR1L=0X67;
UCSR1C=0X06;
UCSR1B=0X98;
PORTC=0X00;
sei();
while(1)
{
PORTC=0X00;
}
}
ISR(USART1_RX_vect)
{
PORTC=0xFF;
delay_sec(1000);
}

PIANO1.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
#define CHECKBIT(x,b) (x&(1<<b))
#define SETBIT(x,b) x|=(1<<b)
#define CLEARBIT(x,b) x&=~(1<<b)
#define TOGGLEBIT(x,b) x^=(1<<b)
volatile unsigned char x;
ISR(USART1_RX_vect)
{

x=UDR1;
}
void USART1A_INIT()
{
UBRR1H=0X00;
UBRR1L=0X67;
UCSR1C=0X06;
UCSR1B=0X18;
}
void main()
{
USART1A_INIT();
SETBIT(UCSR1B,RXCIE);
sei();
while(1)
{
TOGGLEBIT(PORTA,3);
delay_sec(x-48);
}
}
PIANO2.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
#define CHECKBIT(x,b) (x&(1<<b))
#define SETBIT(x,b) x|=(1<<b)
#define CLEARBIT(x,b) x&=~(1<<b)
#define TOGGLEBIT(x,b) x^=(1<<b)
volatile unsigned char x;
ISR(USART1_RX_vect)
{
x=UDR1;
}
void USART1A_INIT()
{
UBRR1H=0X00;
UBRR1L=0X67;
UCSR1C=0X06;
UCSR1B=0X18;
}
void main()

{
USART1A_INIT();
SETBIT(UCSR1B,RXCIE);
sei();
while(1)
{
switch(x)
{
case '1':
SETBIT(PORTA,3);
delay_sec(x-48);
CLEARBIT(PORTA,3);
delay_sec(10-(x-48));
break;
case '2':
SETBIT(PORTA,3);
delay_sec(x-49);
CLEARBIT(PORTA,3);
delay_sec(10-(x-49));
break;
case '3':
SETBIT(PORTA,3);
delay_sec(x-50);
CLEARBIT(PORTA,3);
delay_sec(10-(x-50));
break;
case '4':
SETBIT(PORTA,3);
delay_sec(x-51);
CLEARBIT(PORTA,3);
delay_sec(10-(x-51));
break;
case '5':
SETBIT(PORTA,3);
delay_sec(x-52);
CLEARBIT(PORTA,3);
delay_sec(10-(x-52));
break;
case '6':
SETBIT(PORTA,3);
delay_sec(x-53);
CLEARBIT(PORTA,3);
delay_sec(10-(x-53));
break;
case '7':
SETBIT(PORTA,3);
delay_sec(x-54);
CLEARBIT(PORTA,3);
delay_sec(10-(x-54));

break;
case '8':
SETBIT(PORTA,3);
delay_sec(x-55);
CLEARBIT(PORTA,3);
delay_sec(10-(x-55));
break;
}
}
}
UART.C
#include <avr/io.h>
#define CHECKBIT(x,b) (x&(1<<b))
#define TOGGLEBIT(x,b) x^=(1<<b)
void USART1A_INIT()
{
UBRR1H=0X00;
UBRR1L=0X67;
UCSR1C=0X06;
UCSR1B=0X18;
}
unsigned char USART1A_REC()
{
while(!(CHECKBIT(UCSR1A,RXC)));
return UDR1;
}
void main()
{
unsigned char z;
DDRC=0XFF;
USART1A_INIT();
while(1)
{
z=USART1A_REC();
switch(z)
{
case('0'):
TOGGLEBIT(PORTC,0);
break;
case('1'):
TOGGLEBIT(PORTC,1);
break;
case('2'):

TOGGLEBIT(PORTC,2);
break;
case('3'):
TOGGLEBIT(PORTC,3);
break;
case('4'):
TOGGLEBIT(PORTC,4);
break;
case('5'):
TOGGLEBIT(PORTC,5);
break;
case('6'):
TOGGLEBIT(PORTC,6);
break;
case('7'):
TOGGLEBIT(PORTC,7);
break;
default:
;
}
}
}

TIMER
T1.c
#include <avr/io.h>
volatile unsigned int a;
void main()
{
char k;
TCCR0|=(1<<CS02)|(1<<CS01)|(1<<CS00);
TCNT0=240;
DDRC=0XFF;

while(1)
{
while(!(TIFR&(1<<TOV0)));
a++;
TCNT0=240;
TIFR|=(1<<TOV0);
if((a>0)&&(a<1000))
PORTC=0x00;
if(a>=1000 && a<2000)
PORTC=0xff;
if(a>2000)
a=0;
}
}
T2.c
#include <avr/io.h>
volatile unsigned int a;
void main()
{
char k;
TCCR0|=(1<<CS02)|(1<<CS01)|(1<<CS00);
TCNT0=240;
DDRC=0XFF;
while(1)
{
while(!(TIFR&(1<<TOV0)));
a++;
TCNT0=240;
TIFR|=(1<<TOV0);
if((a>0)&&(a<1000))
PORTC=0x0F;
if(a>=1000 && a<2000)
PORTC=0xF0;
if(a>2000)
a=0;
}
}

T3.c
#include <avr/io.h>
volatile unsigned int a;
void main()
{
TCCR0|=(1<<CS02)|(1<<CS01)|(1<<CS00)|(1<<WGM01);
OCR0=16;
DDRC=0XFF;
while(1)
{
while(!(TIFR&(1<<OCF0)));
a++;
TIFR|=(1<<OCF0);
if((a>0)&&(a<1000))
PORTC=0x55;
if(a>=1000 && a<2000)
PORTC=0xAA;
if(a>2000)
a=0;
}
}

T4.c
#include <avr/io.h>
#include <avr/interrupt.h>
volatile unsigned int a;
ISR(TIMER0_OVF_vect)
{
a++;
TCNT0=240;
}
void main()
{
TCCR0|=(1<<CS02)|(1<<CS01)|(1<<CS00);

TIMSK|=(1<<TOIE0);
sei();
DDRC=0XFF;

// To enable Global interrupts

while(1)
{
if((a>0)&&(a<1000))
PORTC=0xC3;
if(a>=1000 && a<2000)
PORTC=0x3C;
if(a>2000)
a=0;
}
}
T5.c
#include <avr/io.h>
#include <avr/interrupt.h>
volatile unsigned int a;
ISR(TIMER0_COMP_vect)
{
a++;
}
void main()
{
TCCR0|=(1<<CS02)|(1<<CS01)|(1<<CS00)|(1<<WGM01);
TIMSK|=(1<<OCIE0);
sei();
OCR0=16;
DDRC=0XFF;
while(1)
{
if((a>0)&&(a<1000))
PORTC=0x55;
if(a>=1000 && a<2000)
PORTC=0xAA;

if(a>2000)
a=0;
}
}

SPI
SPI1.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#define
#define
#define
#define
#define

DDR_SPI DDRB
DD_MOSI 2
DD_MISO 3
DD_SS 0
DD_SCK 1

void SPI_MasterInit(void)
{
DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS); /* Set MOSI and SCK
output, all others input */
SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR0);
/* Enable SPI, Master, set
clock rate fck/16 */
}

void SPI_MasterTransmit(char cData)


{
SPDR = cData;
while(!(SPSR & (1<<SPIF)));
complete */
}

/* Start transmission */
/* Wait for transmission

int main()
{
lcd_init();
SPI_MasterInit();
while(1)
{
SPI_MasterTransmit('k');
delay_sec(10000);
}
}

SPI2.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#define
#define
#define
#define
#define

DDR_SPI DDRB
DD_MOSI 2
DD_MISO 3
DD_SS 0
DD_SCK 1

void SPI_SlaveInit(void)
{
DDR_SPI|= (1<<DD_MISO);
SPCR|= (1<<SPE);
}
char SPI_SlaveReceive(void)
{

/* Set MISO output, all others input */


/* Enable SPI */

while(!(SPSR & (1<<SPIF)));


*/
return SPDR;
}

/* Wait for reception complete


/* Return data register */

int main()
{
unsigned char temp;
lcd_init();
SPI_SlaveInit();
PORTB|=(1<<DD_SS);
while(1)
{
temp=SPI_SlaveReceive();
lcd_char(temp);
lcd_cmd(0X80);
}
}

SPI3.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#define
#define
#define
#define
#define

DDR_SPI DDRB
DD_MOSI 2
DD_MISO 3
DD_SS 0
DD_SCK 1

void SPI_MasterInit(void)
{
DDR_SPI |= (1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS); /* Set MOSI and SCK
output, all others input */
SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR0);
/* Enable SPI, Master, set
clock rate fck/16 */
}
void SPI_MasterTransmit(char cData)

{
SPDR = cData;
transmission */
while(!(SPSR & (1<<SPIF)));
complete */
}

/* Start
/* Wait for transmission

int main()
{
lcd_init();
SPI_MasterInit();
while(1)
{
SPI_MasterTransmit('K');
lcd_char(SPDR);
SPI_MasterTransmit('T');
lcd_char(SPDR);
delay_sec(10000);
lcd_cmd(0xc0);
lcd_cmd(0X80);
}
}
SPI4.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#define
#define
#define
#define
#define

DDR_SPI DDRB
DD_MOSI 2
DD_MISO 3
DD_SS 0
DD_SCK 1

void SPI_SlaveInit(void)
{
DDR_SPI|= (1<<DD_MISO);
*/
SPCR|= (1<<SPE);
}
char SPI_SlaveReceive(void)

/* Set MISO output, all others input


/* Enable SPI */

{
while(!(SPSR & (1<<SPIF)));
return SPDR;
}

/* Wait for reception complete */


/* Return data register */

int main()
{
unsigned char temp;
lcd_init();
SPI_SlaveInit();
PORTB|=(1<<DD_SS);
while(1)
{
SPDR='R';
temp=SPI_SlaveReceive();
lcd_cmd(0X80);
lcd_char(temp);
delay_sec(10000);
lcd_cmd(0X14);
lcd_char(temp);
delay_sec(10000);
}
}
I2C
/*******************************************************************
FileName
:
I2C_RTC_main.c
Target
:
ATmega128
Project
:
RTC READ
Clock frequency :
16Mhz
Author
:
TEAM
Description
:
It receives the date and time from RTC (DS1307) and
send it to the serial port
*******************************************************************/
#include "I2C_RTC_atm128.h"
#include "Serial.h"
void Initialize_RTC(void);
int main(void)
{
static unsigned char SEC_var = 0;
static unsigned char MIN_var = 0;
static unsigned char HRS_var = 0;

static unsigned char DAY_var = 0;


static unsigned char MONTH_var = 0;
static unsigned char YEAR_var = 0;
volatile unsigned int dlyi;
volatile unsigned int dlyj;
char Debug_str[80];
Serial_Init();
print_string("\n\r START MAIN");
/**************************************
here initialization required for RTC
**************************************/
Initialize_RTC(); /* this is the Intialization for RTC and writes the */
while(1)
{
Update_RTC_variables();
date and time from funcn given below */

/* this is required to getting the

YEAR_var = RTC_getYear();
MONTH_var = RTC_getMonth();
DAY_var = RTC_getDay();
HRS_var = RTC_getHour();
MIN_var = RTC_getMinute();
SEC_var = RTC_getSecond();
sprintf(Debug_str,"\n\r HRS = %02d # MIN = %02d # SEC = %02d
# DAY = %02d # MONTH = %02d # YEAR = %02d
",HRS_var,MIN_var,SEC_var,DAY_var,MONTH_var,YEAR_var);
print_string(Debug_str);
for(dlyj=0;dlyj<200;dlyj++)
for(dlyi=0;dlyi<10000;dlyi++)
asm("nop");
}
}
void Initialize_RTC(void)
{
RTC_Init();
Write_RTC();
/* for this you need to set date and time from
RTC_def_cfg.h */
}

INTERRUPTS.
I1.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
ISR(INT6_vect)
{
PORTC=0X00;
delay_sec(1000);
}
void main()
{
EICRB=0X20;
EIMSK=0X40;
PORTE=0xFF;
DDRC=0XFF;
sei();
while(1)
{
PORTC=0xff;
}
}
I2.C
#include <avr/io.h>
#include <avr/interrupt.h>
#include <sec_delay.h>
ISR(INT7_vect)
{

// To enable Global interrupts

PORTC=0X00;
delay_sec(1000);
}
void main()
{
EICRB=0X00;
EIMSK=0X80;
PORTE=0xFF;
DDRC=0XFF;
sei();
while(1)
{

// To enable Global interrupts

PORTC=0xff;
}
}
EEPROM
#include <avr/io.h>
#include <util/delay.h>
#include "LCD_mega128.h"
void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
while(EECR & (1<<EEWE));
/* Wait for completion of previous write */
EEAR = uiAddress;
/* Set up address register */
EEDR = ucData;
/* Set up data register */
EECR |= (1<<EEMWE);
/* Write logical one to EEMWE */
EECR |= (1<<EEWE);
/* Start eeprom write by setting
EEWE */
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
while(EECR & (1<<EEWE));
/* Wait for completion of previous write */
EEAR = uiAddress;
/* Set up address register */
EECR |= (1<<EERE);
/* Start eeprom read by writing
EERE */
return EEDR;
/* Return data from data register */
}

int main()
{
lcd_init();
unsigned char temp;
EEPROM_write(2,'K');
/*temp=EEPROM_read(2);
lcd_char(temp);*/
}

Adc
Adc1.c
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned char a;
void main()
{
lcd_init();
ADMUX|=((1<<REFS1)|(1<<REFS0))|(1);
ADCSRA|=((1<<ADEN)|(1<<ADFR))|(7);
ADCSRA|=(1<<ADSC);
while(1)
{
lcd_cmd(0x01);
delay_sec(100);
while(!(ADCSRA&(1<<ADIF)));
a=ADCL;

lcd_showvalue(a);
delay_sec(10000);
}
}
Adc2.c
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned char a;
void main()
{
lcd_init();
ADMUX|=((1<<REFS1)|(1<<REFS0))|(0);
ADCSRA|=((1<<ADEN)|(1<<ADFR))|(7);
ADCSRA|=(1<<ADSC);
while(1)
{
lcd_cmd(0x80);
while(!(ADCSRA&(1<<ADIF)));
a=ADCL;
lcd_showvalue(a);
delay_sec(1000);
}
}
ADC3.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned char a;
void main()
{
lcd_init();

ADMUX|=((1<<REFS1)|(1<<REFS0))|(0);
ADCSRA|=((1<<ADEN)|(1<<ADFR))|(7);
ADCSRA|=(1<<ADSC);
while(1)
{
lcd_cmd(0x80);
while(!(ADCSRA&(1<<ADIF)));
a=ADCL;
lcd_showvalue(a);
delay_sec(1000);
}
}
ADC4.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
ISR(ADC_vect)
{
lcd_cmd(0x01);
delay_sec(100);
unsigned int a;
a=ADCH;
lcd_showvalue(a);
delay_sec(10000);
}
volatile unsigned char a;
void main()
{
DDRC=0XFF;
ADMUX=0XE1;
ADCSRA=0XEF;
ADCSRA|=(1<<ADSC);
lcd_init();
sei();
while(1)
{

}
}
ADC5.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
ISR(ADC_vect)
{
lcd_cmd(0x01);
delay_sec(100);
unsigned int a;
a=ADCH;
lcd_showvalue(a);
delay_sec(10000);
}
volatile unsigned char a;
void main()
{
DDRC=0XFF;
ADMUX=0XE0;
ADCSRA=0XEF;
ADCSRA|=(1<<ADSC);
lcd_init();
sei();
while(1)
{
}
}
ADC6.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned int a=0,b=0;
ISR(ADC_vect)

{
lcd_cmd(0x01);
delay_sec(100);
unsigned int a;
a=ADCH;
lcd_showvalue(a);
delay_sec(10000);
}
volatile unsigned char a;
void main()
{
DDRC=0XFF;
ADMUX=0XE0;
ADCSRA=0XEF;
ADCSRA|=(1<<ADSC);
lcd_init();
sei();
while(1)
{
}
}
ADC7.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned char a,b;
void main()
{
lcd_init();

while(1)
{
ADMUX=(((1<<REFS1)|(1<<REFS0))|(1<<ADLAR))|(1);
ADCSRA|=(1<<ADEN)|(7);

ADCSRA|=(1<<ADSC);
while(!(ADCSRA&(1<<ADIF)));
a=ADCH;
lcd_cmd(0x80);
lcd_string("x axis=");
lcd_showvalue(a);
ADCSRA|=(1<<ADIF);
delay_sec(1000);
ADMUX=(((1<<REFS1)|(1<<REFS0))|(1<<ADLAR))|(2);
ADCSRA|=(1<<ADEN)|(7);
ADCSRA|=(1<<ADSC);
while(!(ADCSRA&(1<<ADIF)));
b=ADCH;
lcd_cmd(0xC0);
lcd_string("y axis=");
lcd_showvalue(b);
ADCSRA|=(1<<ADIF);
delay_sec(1000);
}
}
ADC8.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned char a;
void main()
{
lcd_init();

while(1)
{
ADMUX=(((1<<REFS1)|(1<<REFS0))|(1<<ADLAR))|(1);
ADCSRA|=(1<<ADEN)|(7);

ADCSRA|=(1<<ADSC);
while(!(ADCSRA&(1<<ADIF)));
a=ADCH;
lcd_cmd(0x80);
lcd_string("x axis=");
lcd_showvalue(a);
ADCSRA|=(1<<ADIF);
delay_sec(1000);
}
}
ADC10.C
#include <avr/io.h>
#include <sec_delay.h>
#include "LCD_mega128.h"
#include<avr/interrupt.h>
volatile unsigned char a,b;
unsigned int count=1;
ISR(ADC_vect)
{
if(count)
{
ADMUX=0xe1;
a=ADCH;
count=!count;
}
else
{
ADMUX=0xe2;
b=ADCH;
count=!count;
}
}

void main()

{
//ADMUX=(((1<<REFS1)|(1<<REFS0))|(1<<ADLAR))|(1);
ADMUX=0xe1;
//ADCSRA|=(1<<ADSC)|(1<<ADFR))|(1<<ADIE);
ADCSRA=0xef;
lcd_init();
sei();
while(1)
{
lcd_cmd(0x80);
lcd_string("x axis=");
lcd_showvalue(a);
delay_sec(1000);
lcd_cmd(0xc0);
lcd_string("Y axis=");
lcd_showvalue(b);
delay_sec(1000);
}
}

Das könnte Ihnen auch gefallen