#include
#define fosc 1000000 //晶振1MHZ
#define baud 4800 //波特率
#define led1 (1<<4)
#define led2 (1<<5)
#define led3 (1<<2)
#define led4 (1<<3)
#define ptr_TXEN (1<<3)
#define send_mode PORTD|=ptr_TXEN
#define rcv_mode PORTD&=~ptr_TXEN
#define ptr_CS (1<<2)
#define ch4339 PORTD&=~ptr_CS
#define ch4343 PORTD|=ptr_CS
void Delay(void);
void port_init(void)
{
PORTB = 0xff;
DDRB = 0xFF;
PORTC = 0x7f; //m103 output on
DDRC = 0x7F;
PORTD = 0xff;
DDRD = 0xFc;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/*****************字符输出函数*****************/
void putchar(unsigned char c)
{
while (!(UCSRA&(1<
}
void uart_init(void)
{
//OSCCAL=0xA1;//在主程序中设置
UCSRB=(1<
UBRRL=(fosc/16/baud-1)%256;
UBRRH=((fosc/16/baud-1)/256)&0b01111111;
UCSRC=(1<
}
void Delay(void)
{unsigned int i,j;
for(i=5;i>0;i--){for(j=5500;j>0;j--);}
}
void ptr_init(void)
{send_mode;
ch4339;
Delay();
}
void main(void)
{unsigned char i=0;Delay();
init_devices();
uart_init();
ptr_init();
while(1)
{putchar(i);
switch (i%4)
{
case 0x00:
{PORTC=(PORTC|0x3c)&(~led1);break;}
case 0x01:
{PORTC=(PORTC|0x3c)&(~led2);break;}
case 0x02:
{PORTC=(PORTC|0x3c)&(~led3);break;}
case 0x03:
{PORTC=(PORTC|0x3c)&(~led4);break;}
default:
{ break;}
}
Delay();i++;
}
}
上一篇:mega8驱动PTR2000接收数据
下一篇:mega8驱动PTR8000接收数据
推荐阅读最新更新时间:2024-03-16 15:17