#include<iom8v.h>
#include<macros.h>
#include<stdio.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
#define Fosc 8000000
#define BAUD 9600
#define UBRR_i (Fosc/BAUD/16)-1
uchar key_stime_counter=0,hight_votage,timeT_counter=0,key_stime_ok=0;; //舵机的变量
uchar key_stime_counter1=0,hight_votage1,timeT_counter1=0,key_stime_ok1=0; //舵机的变量
void delay_nms(unsigned int n) ;
void delay_1ms(void) ;
void Time0_init(void);
void SteerEngineTurningUp(void);
void SteerEngineTurningDown(void);
void key_scan(void);
void SteerEngineTurningUp0(void);
void SteerEngineTurningDown0(void);
void Time2_init(void);
void USART_Init( unsigned int baud );
unsigned char USART_Receive( void );
uchar Buffer[4] = {0};
void main(void)
{
DDRC|=BIT(6);
PORTC&=~BIT(6);
DDRB|=BIT(0)|BIT(1);
DDRD|=BIT(7)|BIT(6)|BIT(5)|BIT(4)|BIT(3)|BIT(2);//led for checking
PORTD|=BIT(7)|BIT(6)|BIT(5)|BIT(4)|BIT(3)|BIT(2);
USART_Init( UBRR_i );
Time0_init( );
Time2_init( );
while(1)
{
Buffer[0]=USART_Receive( );
//Buffer[1]=Buffer[0]-48;
switch(Buffer[0])
{
case ***:
if(Buffer[0]==***)
{
PORTD&=~BIT(7);
SteerEngineTurningDown();
}
//Buffer[0]=0;
break;
case***:
if(Buffer[0]==***)
{
PORTD&=~BIT(6);
SteerEngineTurningUp();
}
// Buffer[0]=0;
break;
case ***:
if(Buffer[0]==***)
{
PORTD^=~BIT(5);
SteerEngineTurningDown0();
}
//Buffer[0]=0;
break;
case ***:
PORTD^=~BIT(4);
//SteerEngineTurningUp0();
break;
case ***:
if(Buffer[0]==***)
{
PORTD^=~BIT(3);
SteerEngineTurningUp0();
}
break;
case ***:
if(Buffer[0]==***)
{
PORTC|=BIT(6);
}
break;
default:
break;
}
}
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void USART_Init( unsigned int baud )
{
/* 设置波特率*/
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
/* 接收器与发送器使能 */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* 设置帧格式: 8 个数据位, 2个停止位 */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
unsigned char USART_Receive( void )
{
/* 等待接收数据 */
while ( !(UCSRA & (1<<RXC)) )
;
/* 从缓冲器中获取并返回数据 */
return UDR;
}
void Time2_init(void)
{
CLI();
TCNT2=0;
TCCR2=0X01;
PORTB&=~BIT(1);
//SEI();
}
void Time0_init(void)
{
CLI();//关闭中断
TCNT0=0;//装初值
TCCR0=0X01;//设置分频数和工作模式、这里是256分频和普通模式
PORTB&=~BIT(0);
//SEI();//开中断
}
#pragma interrupt_handler timer2:5 //Time2溢出中断
void timer2(void)
{
//TCNT2=0;
if (++key_stime_counter1>=625)
{
key_stime_counter1=0;
PORTB|=BIT(1); // control_signal=1;
key_stime_ok1 = 1; // 20ms到
timeT_counter1=0;
}
if (key_stime_ok1&&(++timeT_counter1>=hight_votage1))
{
key_stime_ok1=0;
timeT_counter1=0;
PORTB&=~BIT(1); // control_signal=0; // hight_votage*0.1ms到
}
}
#pragma interrupt_handler timer0:10 //Time0溢出中断
void timer0(void)
{
//TCNT0=0;
if (++key_stime_counter>=625)
{
key_stime_counter=0;
PORTB|=BIT(0); // control_signal=1;
key_stime_ok = 1; // 20ms到
timeT_counter=0;
}
if (key_stime_ok&&(++timeT_counter>=hight_votage))
{
key_stime_ok=0;
timeT_counter=0;
PORTB&=~BIT(0); // control_signal=0; // hight_votage*0.1ms到
}
}
void SteerEngineTurningUp(void)
{
TIMSK=0X01;//溢出中断使能
SEI();//开中断
delay_nms(5);
hight_votage-=1;
if(hight_votage<16)
hight_votage=16;
TIMSK=0X00;//溢出中断使能
CLI();
}
/********************************************************************
* 名称 :SteerEngineTurningDown()
* 功能 : 舵机向下转。
* 输入 : 无
* 输出 : 无
***********************************************************************/
void SteerEngineTurningDown(void)
{
TIMSK=0X01;//溢出中断使能
SEI();//开中断
delay_nms(5);
hight_votage+=1;
if(hight_votage>78)
hight_votage=78;
TIMSK=0X40;//溢出中断使能
CLI();
}
void SteerEngineTurningUp0(void)
{
TIMSK=0X40;//溢出中断使能
SEI();//开中断
delay_nms(5);
hight_votage1-=1;
if(hight_votage1<16)
hight_votage1=16;
TIMSK=0X00;//溢出中断使能
CLI();
}
/********************************************************************
* 名称 :SteerEngineTurningDown()
* 功能 : 舵机向下转。
* 输入 : 无
* 输出 : 无
***********************************************************************/
void SteerEngineTurningDown0(void)
{
TIMSK=0X40;//溢出中断使能
SEI();//开中断
delay_nms(5);
hight_votage1+=1;
if(hight_votage1>78)
hight_votage1=78;
TIMSK=0X00;//溢出中断使能
CLI();
} |