Demo entry 6730942

draw_rect

   

Submitted by anonymous on Apr 09, 2018 at 15:19
Language: C. Code size: 3.0 kB.

#include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义          
#include <math.h>
#include "delay.h"

void InitUART  (void)
{

    SCON  = 0x50;		        // SCON: 模式 1, 8-bit UART, 使能接收  
    TMOD |= 0x20;               // TMOD: timer 1, mode 2, 8-bit 重装
    TH1   = 0xFD;               // TH1:  重装值 9600 波特率 晶振 11.0592MHz  
    TR1   = 1;                  // TR1:  timer 1 打开                         
    EA    = 1;                  //打开总中断
    ES    = 1;                  //打开串口中断
}


int posy=0,posx=0;

#include <stdio.h>

sbit xENABLE=P0^1; 
sbit xTRIG=P0^2;
sbit xDATA=P0^3;
sbit yENABLE=P0^4; 
sbit yTRIG=P0^5;
sbit yDATA=P0^6;

 int step=0,simstep=0;
 int iSendCount=0;
 void xSend(int c){
 	xENABLE=0;
 	xENABLE=1;
	
	for(iSendCount=0;iSendCount<16;iSendCount++){
		xTRIG=1;
		xDATA=c&0x8000;
		c=c<<1;
		xTRIG=0;
		DelayMs(20);
	}
		 
		DelayMs(20);
	xENABLE=0;
	//DelayMs(200);
 }
 void ySend(int c){

 	yENABLE=0;
 	yENABLE=1;
	
	for(iSendCount=0;iSendCount<16;iSendCount++){
		yTRIG=1;
		yDATA=c&0x8000;
		c=c<<1;
		yTRIG=0;
		DelayMs(20);
	}
		 
		DelayMs(20);
	yENABLE=0;
	//DelayMs(200);
 }


//自己才实现的常用预处理数学函数
#define tabs(x) ((float)((x>0)?x:-x))
#define tmax(x,y) ((x>y)?(x):(y))

void TDelayMs(int ms){
	while(ms--)DelayMs(100);
}
int px=0,py=0,gap;
//局部量化控制
float simi=0;
//贝赛尔曲线路径计算函数,啊
float CalculateBezierPointForCubic(float t, float p0, float p1, float p2, float p3) {
	float temp = 1 - t;
	return p0 * temp * temp * temp + 3 * p1 * t * temp * temp + 3 * p2 * t * t * temp + p3 * t * t * t;
}


int circlex=300;
int circley=300;

#define TYPE 0
#define PI 3.1415296

void main (void)
{
	InitUART();
			step=1;simstep=1;
	while (1)                       
	{
		int k=1;
#if TYPE==2
		//通过贝塞尔曲线和sin函数绘制椭圆,
		switch(step){
			case 1: 
				simi+=0.1;
				posx=circlex*sin(simi*PI);
				posy=simi*circley;
				//posx = CalculateBezierPointForCubic(simi,k*circlex[0],k*circlex[1],k*circlex[2],k*circlex[3]);
				//posy = CalculateBezierPointForCubic(simi,k*circley[0],k*circley[1],k*circley[2],k*circley[3]);
				xSend(posx);ySend(posy);
				TDelayMs(10);
				if(simi>=1){simi=0;step++;}
				break;
			case 2: 
				simi+=0.1;
				posx=circley*sin(simi*PI);
				posy=-simi*circley;
				//posx = CalculateBezierPointForCubic(simi,k*circlex[3],k*circlex[4],k*circlex[5],k*circlex[6]);
				//posy = CalculateBezierPointForCubic(simi,k*circley[3],k*circley[4],k*circley[5],k*circley[6]);
				xSend(posx);ySend(posy);TDelayMs(10);
				if(simi>=1){simi=0;step++;}
				break;
			case 3: step=0;break;
			}
		step++;
#else
		
		switch(step){
			case 1: posy=1000;xSend(posx);ySend(posy);TDelayMs(10);break;
			case 2: posx=1000;xSend(posx);ySend(posy);TDelayMs(230);break;
			case 3: posy=0;xSend(posx);ySend(posy);TDelayMs(10);break;
			case 4: posx=0;xSend(posx);ySend(posy);TDelayMs(230);break;
			case 5: step=0;xSend(posx);ySend(posy);TDelayMs(10);break;
		}	step++;
#endif
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).