/*
* mega128.c
*
* Created: 2015-8-17 16:23:04
* Author: Acer
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
uchar ledtime=0,flag0=0;;
uchar USART0_R_dat[];
void CPU_Init(void)
{
DDRB|=(1<<7);
PORTB&=0X7F;//4850接收
DDRE&=0XFE;//RXD0输入
DDRE|=0X02;//TXD0输出
PORTE|=0X03;//高电平释放通讯
DDRF|=0X02;//运行指示灯
PORTF|=0X02;
DDRE|=(1<<3)|(1<<5)|(1<<7);
PORTE|=(1<<3)|(1<<5)|(1<<7);
DDRF|=0X01;
PORTF|=0X01;
}
void USMART_Init(void)
{
UBRR0H=0;
UBRR0L=0X2F;//9600bps
UCSR0B=(1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0)|(1<<TXCIE0);//接收、发送使能 接收、发送中断使能
UCSR0C=0x06;//异步 1停止位 无校验 8位长度
TCCR1A = 0;
TCCR1B |= (1<<WGM12); // CTC Mode;
OCR1AH = 0X24; // Compare Value;
OCR1AL=0;
TIMSK |= 1<<OCIE1A; // Enable interrupt;
TCCR1B |= (1<<CS11); // Start Timing;CLK/8;
SREG|=0X80;
}
void led0(uchar a)
{
if(a)
PORTE&=~(1<<7);
else
PORTE|=1<<7;
}
void led1(uchar a)
{
if(a)
PORTE&=~(1<<5);
else
PORTE|=1<<5;
}
void (*pa[])(uchar)={led0,led1};//此处添加函数
int main(void)
{
CPU_Init();
USMART_Init();
while(1)
{
;//TODO:: Please write your application code
}
}
ISR(USART0__RX_vect)
{
static uchar USART0_R_count=0;
USART0_R_dat[USART0_R_count]=UDR0;
USART0_R_count++;
if(USART0_R_count>=2)
{
flag0=1;
USART0_R_count=0;
}
}
ISR(TIMER1_COMPA_vect)//10ms
{
ledtime++;
if(ledtime==70)
PORTF&=~(0X02);
if(ledtime==74)
PORTF|=0X02;
if(ledtime>150)
ledtime=0;
if(flag0)
{
flag0=0;
(*pa[USART0_R_dat[0]])(USART0_R_dat[1]);
}
}
|