⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 485_232t.c

📁 DMX512收发程序 基于MEGA8单片机
💻 C
字号:
#include <iom8v.h>
 #include <macros.h>
 #define BYTE unsigned char
 #define DATA 0x20
 #define SCLK 0x40
 #define MAXSIZE 384
 #define REFRESH 0x55
#define CE_High() {PORTC|=0x08;}
#define CE_Low()  {PORTC&=~0x08;}
#define SCK_ONE   {PORTC|=0x20;}
#define SCK_ZERO  {PORTC&=~0x20;}	
//数据发送脚定义
#define DDATA_High  {PORTB|=0x08;}
#define DDATA_Low   {PORTB&=~0x08;}
#define DSCLK_High  {PORTB |= 0x20;}
#define DSCLK_Low   {PORTB &= ~0x20;}
#define DSTB_High   {PORTD |= 0x20;}
#define DSTB_Low    {PORTD &= ~0x20;}

#define SendOne   {PORTB &= ~0x20;PORTB|=0x08;PORTB |= 0x20;}
#define SendZero  {PORTB &= ~0x20;PORTB&=~0x08;PORTB |= 0x20;}
//------------------------
#pragma interrupt_handler UART_RX_interrupt:12
 
 unsigned int Fcount;

 BYTE buf_com[10],Mupdata,revcomd,revFull,comdcnt=0,updata;
 int rdata=0,dflength,dcnt;
 BYTE buf_bg=0x55,swit;
 BYTE datarev=0,display,getaddr,datalen,*dp,buf_offset,checkmark=0;
 unsigned long Dispeed;
 BYTE buf_data[2][MAXSIZE];
 
 
void NormalDisp(BYTE *disp,BYTE BIT1)
{int i;
BYTE BIT2;
BIT2=BIT1<<4;
disp=disp+383;
DSTB_Low 
for(i=0;i<128;i++)
{
SendZero

if((*disp)&BIT1)
{SendOne}
else
{SendZero}
if((*disp)&BIT2)
{SendOne}
else
{SendZero}


disp--;
if((*disp)&BIT1)
{SendOne}
else
{SendZero}


//插入空位
SendZero
if((*disp)&BIT2)
{SendOne}
else
{SendZero}
disp--;
if((*disp)&BIT1)
{SendOne}
else
{SendZero}

if((*disp)&BIT2)
{SendOne}
else
{SendZero}
disp--;
//插入空位
//

}
DSTB_High 
BIT2=BIT1<<4;
BIT2=BIT1<<4;

}

void adjustDisp(BYTE *pt1)
{//int i,j;
//BYTE *dubuf,*zbuf;
NormalDisp(pt1,0x08);
NormalDisp(pt1,0x02);
NormalDisp(pt1,0x08);
NormalDisp(pt1,0x04);
NormalDisp(pt1,0x08);
NormalDisp(pt1,0x04);
NormalDisp(pt1,0x08);
NormalDisp(pt1,0x01);
NormalDisp(pt1,0x08);
NormalDisp(pt1,0x04);
NormalDisp(pt1,0x08);
NormalDisp(pt1,0x02);
NormalDisp(pt1,0x08);

NormalDisp(pt1,0x08);
NormalDisp(pt1,0x04);
}
 
void port_init(void)
 {
  PORTB = 0xFF;
  DDRB  = 0xFF;
  PORTC = 0x3C; //m103 output only
 DDRC  = 0x38;
 PORTD = 0x04;
 DDRD  = 0xFF;	
}

//SPI initialisation
// clock rate: 2000000hz
void spi_init(void)
{
 SPCR = 0x51; //setup SPI
 SPSR = 0x01; //setup SPI
}

//UART0 initialisation
// desired baud rate: 115200
// actual: baud rate:111111 (3.7%)
// char size: 8 bit
// parity: Disabled
 void uart0_init(void)
{	
 BYTE i;
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = 0x86;
DDRC  = 0x3F;
 PORTC = 0xFF;
 DDRC  = 0x00;
 i=PINC&0x10;
 if(i)
 {UBRRL = 0x07; }//set baud rate lo
else
 {UBRRL = 0x03; }
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x9c;
	
}


//call this routine to initialise all peripherals
 void init_devices(void)
{
 //stop errant interrupts until set up
 CLI();//disable all interrupts
 port_init();
 uart0_init();
 //spi_init();
// Read_Cont();
 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI();//re-enable interrupts

 //all peripherals are now initialised
}

/*

BYTE P_updata(void)
{long fadr;
BYTE *buf1;
int i;
 if(updata==1)
 {
 //UDR=0xAA;
 
 dflength=buf_com[4]+buf_com[5]*256;
 //UDR=buf_com[4];
 dflength+=2;
 dp=buf_data[0];
 Mupdata=1;




WREN();
EWSR();
WRSR(0x00);
WRDI();
WREN();
Chip_Erase();
fadr=0;
while(Read_Status_Register()&0x01);
WRDI();
 for(i=0;i<10;i++)
{WREN();
Byte_Program(fadr++,buf_com[i]); 
WRDI();}
fadr=128;
Dispeed=1000+3000*buf_com[6];
UDR=dflength;
UDR=dflength>>8;
//Chip_Erase();
//WRDI();
 while(1)
   {
    if(revFull==1)
 	  {buf1=buf_data[0];
	 for(i=0;i<dflength;i++)
	    {
		WREN();
		Byte_Program(fadr++,*buf1);
		buf1++;
		WRDI();
		//UDR=0xAA;
		}
	// UDR=0x55;
	revFull=0;
	

	 }
  //updata=0;
   if(updata==0)
     { 
	 WREN();
 	 Byte_Program(0x12,buf_com[2]);
	 WRDI(); 
	 WREN();
 	 Byte_Program(0x13,buf_com[3]); 
 	 WRDI();
		 UDR=buf_com[3];
		 UDR=buf_com[2];//UDR=0xff;
     return(1);
     }
   }
 //goto begin
 } 
 return(0);
}    
*/
BYTE getaddrin(viod)
{ BYTE i;
 DDRC  = 0x3F;
 PORTC = 0xFF;
 DDRC  = 0x00;
 i=PINC;
 i=i&0x0f;
 i=(i<<4)+(15-i);
 
 //i=i|0xf0;
 return(i);	
}

void delay(void)
{unsigned long i;
	
 for(i=0;i<Dispeed;i++)
// P_updata();;
	;
}   
//
 void main(void)
{ 

int i,j,k,framecnt;	
unsigned long count=0;

 BYTE *buf;
begin: 
init_devices();
 //initFlashwr();
 Dispeed=1000;
 delay();
//while(count<256)
//{
swit=0;
getaddr=getaddrin();
UDR=getaddr;
while(1)
{
 for(j=14;j<=15;j++)
 {
 buf=buf_data[0];
	//UDR = 0x55;
 for(i=0;i<128;i++)
	
 {*buf++=j<<4;
 *buf++=j;
 *buf++=0;
 } 
 
 for(k=0;k<50;k++)
 adjustDisp(buf_data[0]);
 
}
j=0;
 buf=buf_data[0];
	//UDR = 0x55;
 for(i=0;i<128;i++)
	
 {*buf++=(15-j);
 *buf++=0;
 *buf++=15-j<<4;
 } 
 
 for(k=0;k<50;k++)
 adjustDisp(buf_data[0]);
  buf=buf_data[0];
	//UDR = 0x55;
 for(i=0;i<128;i++)
	
 {*buf++=0;
 *buf++=(15-j)<<4;
 *buf++=15-j;
 } 
 
 for(k=0;k<50;k++)
 adjustDisp(buf_data[0]);
while(1)
{if(swit)
{
adjustDisp(buf_data[1]);
}
else
{ adjustDisp(buf_data[0]);
}
} 
}

}

void UART_RX_interrupt(void)
{BYTE rbuf; 
int i;
rbuf=UCSRB&0x02;
rbuf=UDR;
if(Fcount==0)
dp=buf_data[0];


*dp++=rbuf;
if(++Fcount>=513)
{
UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = 0x86;
 UBRRL = 0x08;//set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x9c;
dp=buf_data[0];
for(i=0;i<513;i++) 
{
UDR=*dp++;
while(!(UCSRA&0x20));
}
 UCSRB = 0x00; //disable while setting baud rate
 UCSRA = 0x00;
 UCSRC = 0x86;
 UBRRL = 0x03;//set baud rate lo
 UBRRH = 0x00; //set baud rate hi
 UCSRB = 0x9c;

Fcount=0;
}

return;
}
//dcnt=0;
//if(checkmark==0xaa)
//Fcount=1;

 //uart has received a character in UDR

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -