单片机 > 单片机程序设计 > 详情

mega8驱动PTR2000发送数据

发布时间:2020-08-24 发布时间:
|
#include

#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 only
 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<  UDR=c;
 }

void uart_init(void)
 {
 //OSCCAL=0xA1;//在主程序中设置
 UCSRB=(1<  //允许接收中断、发送和接收
   UBRRL=(fosc/16/baud-1)%256;
   UBRRH=((fosc/16/baud-1)/256)&0b01111111;
 UCSRC=(1<  //8位数据+1位STOP位
 }
 
 
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++;
}
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM JTAG 调试原理