?? main._c
字號:
//////////////////////////////
////////////////////////////// 61493為近似180度
#include <iom64v.h>
#include <macros.h>
#define PREVSONG BIT(2)
#define NEXTSONG BIT(3)
#define VOLDOWN BIT(4)
#define VOLUP BIT(5)
#define STOP BIT(6)
#define R_pwm0 BIT(4)
#define R_pwm1 BIT(5)
#define L_pwm0 BIT(6)
#define L_pwm1 BIT(7)
long ADCjohu=0;
int ms=0;
long xx=0;
int adc=0;
#define REF 483
unsigned char lvbo=0;
long lvbo_num=0;
unsigned char pwm_pingjun=100;//平均速度
unsigned char Rpwm0=0;
unsigned char Rpwm1=0;
unsigned char Lpwm0=0;
unsigned char Lpwm1=0;
unsigned char all=0, a=0,b=0,c=0,d=0,e=0,f=0;
#define CIR 1
#define LINE 2
long jiaodu=0,juli=0;
int time=0;
unsigned char sec=0;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
}
//TIMER2 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
TCNT2 = 0x83; //setup
OCR2 = 0x7D;
TCCR2 = 0x03; //start
}
#pragma interrupt_handler timer0_ovf_isr:11
void timer0_ovf_isr(void)
{
TCNT0 = 0x83; //reload counter value
if (time>500){sec++;time=0;}
time++;
ADCjohu=ADCL;
ADCjohu|=ADCH << 8;
///////////////////////////////////////////////////////////////////平均值濾波 1毫秒采樣濾波,10毫秒輸出
if (lvbo==0) {lvbo_num=ADCjohu/2;lvbo++;return;}
if (lvbo<10) {lvbo_num+=ADCjohu/2;lvbo++;return;}
else lvbo=0;/**/
//////////////////////////////////////////////////////////////////////////////
if ( (ADCjohu<REF-6) || (ADCjohu>REF+6))
{
//ADCjohu-=511-10;//-511+31-3.27371 -10
xx=xx+ADCjohu-REF;//
}
ms=1;
//ADCjohu==ADCL;
//PORTD=ADCjohu;
}
//ADC initialize
// Conversion time: 3uS
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x01; //select adc input 0
ACSR = 0x80;
// ADCSRB = 0x00;
ADCSRA = 0xEf;
}
#pragma interrupt_handler adc_isr:22
void adc_isr(void)
{
//conversion complete, read value (int) using...
// value=ADCL; //Read 8 low bits first (important)
// value|=(int)ADCH << 8; //read 2 high bits and shift into top byte
//ADCjohu==ADCL;
//ADCjohu|=(int)ADCH << 8;
//PORTD=ADCjohu;
//adc=1;
PORTA=ADCL;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
timer2_init();
adc_init();uart1_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x40; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//UART1 initialize
// desired baud rate:9600
// actual baud rate:9615 (0.2%)
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = 0x33; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x98;
}
#pragma interrupt_handler uart1_rx_isr:31
//cir+00100
void uart1_rx_isr(void)
{
//uart has received a character in UDR
if (all==CIR)
{
a=b;
b=c;
c=d;
d=e;
e=f;
f=UDR1;
if ((f<'0')||(f>'9'))return;
if (a=='+')//0x2b
{jiaodu=(b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48);
all=0;return;}
else if (a=='-')//0x2d
{jiaodu=-((b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48));
all=0;return;}
if (a!=0) {all=0;return;}
//PORTF=jiaodu;//while(1);
}
else if (all==LINE)
{
a=b;
b=c;
c=d;
d=e;
e=f;
f=UDR1;
if ((f<'0')||(f>'9'))return;
if (a=='+')
{juli=(b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48);
all=0;return;}
else if (a=='-')
{juli=-((b-48)*10000+(c-48)*1000+(d-48)*100+(e-48)*10+(f-48));
all=0;return;}
if (a!=0) {all=0;return;}
//PORTF=a;
}
else if (all==0)//(johu!=3)&&(johu!=5) //5:7-bit 3:8-bit
{
a=b;
b=c;
c=d;
d=UDR1;
if ((a=='c')&&(b=='i')&&(c=='r'))
{
all=CIR;
a=0;b=0;c=0;d=0;e=0;f=0;
return; }//RXlen=4;{johu=3;RXlen=3;return;}
else if ((a=='l')&&(b=='i')&&(c=='n')&&(d=='e'))
{
all=LINE;
a=0;b=0;c=0;d=0;e=0;f=0;
return; }//RXlen=4;{johu=5;RXlen=3;return;}
////////////////////////////////////////////////////////////////////
}
}
/*void number(unsigned char X,unsigned char Y,unsigned char LEN,int NUM,unsigned char BLACK)
{
char code[]={'0','1','2','3','4','5','6','7','8','9',};
if (NUM<0)
{LCD_write_String1(X,Y,"-");NUM=-NUM;}
else
{LCD_write_String1(X,Y,"+");}
X=X+6;
while(LEN--)
{
LCD_write_String1(X+LEN*6,Y,& code[NUM%10]);
NUM=NUM/10;
}
}*/
void number_uart1(long NUM,char huanhang)
{long n=0;
if (NUM<0){n=-NUM; while(!(UCSR1A&(1<<UDRE1))); UDR1='-';}
else {n=NUM;while(!(UCSR1A&(1<<UDRE1))); UDR1='+';}
while(!(UCSR1A&(1<<UDRE1)));
UDR1=(n/10000)%10+48;
while(!(UCSR1A&(1<<UDRE1)));
UDR1=(n/1000)%10+48;
while(!(UCSR1A&(1<<UDRE1)));
UDR1=(n/100)%10+48;
while(!(UCSR1A&(1<<UDRE1)));
UDR1=(n/10)%10+48;
while(!(UCSR1A&(1<<UDRE1)));
UDR1=n%10+48;
if (huanhang)
{
while(!(UCSR1A&(1<<UDRE1)));
UDR1=0x0d;
while(!(UCSR1A&(1<<UDRE1)));
UDR1=0x0a;
}
else
{
while(!(UCSR1A&(1<<UDRE1)));
UDR1=0x20;
while(!(UCSR1A&(1<<UDRE1)));
UDR1=0x20;
}
}
//TIMER0 initialize - prescale:64
// WGM: PWM Fast
// desired value: 1KHz
// actual value: 0.488KHz (-104.8%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 = 0x01; //set count
OCR0 = 0x0f;
TCCR0 = 0x6C; //start timer
}
//TIMER1 initialize - prescale:64
// WGM: 5) PWM 8bit fast, TOP=0x00FF
// desired value: 1KHz
// actual value: 0.488KHz (104.8%)
/*void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x01;
OCR1AH = 0x00;
OCR1AL = 0x0f;
OCR1BH = 0x00;
OCR1BL = 0x0f;
OCR1CH = 0x00;
OCR1CL = 0x0f;
ICR1H = 0x00;
ICR1L = 0xFF;
TCCR1A = 0xA9;
TCCR1B = 0x0B; //start Timer
}*/
//TIMER1 initialize - prescale:8
// WGM: 9) PWM phz freq correct, TOP=OCRnA
// desired value: 40mSec
// actual value: 40.000mSec (0.0%)
/*void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xB1; //setup
TCNT1L = 0xE0;
OCR1AH = 0x4E;
OCR1AL = 0x20;
OCR1BH = 0x4E;
OCR1BL = 0x20;
OCR1CH = 0x4E;
OCR1CL = 0x20;
ICR1H = 0x4E;
ICR1L = 0x20;
TCCR1A = 0x01;
TCCR1B = 0x12; //start Timer
}*/
//TIMER1 initialize - prescale:64
// WGM: 7) PWM 10bit fast, TOP=0x03FF
// desired value: 40mSec
// actual value: 8.192mSec (79.5%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFC; //setup
TCNT1L = 0x01;
OCR1AH = 0x03;
OCR1AL = 0xFF;
OCR1BH = 0x03;
OCR1BL = 0xFF;
OCR1CH = 0x03;
OCR1CL = 0xFF;
ICR1H = 0x03;
ICR1L = 0xFF;
TCCR1A = 0x33;
TCCR1B = 0x0B; //start Timer
}
#define PWM_ON DDRB|= R_pwm0 | R_pwm1 | L_pwm0 | L_pwm1;
#define PWM_OFF DDRB&= ~(R_pwm0 | R_pwm1 | L_pwm0 | L_pwm1);
main()
{int n;
unsigned char n_jiaodu=0,n_juli=0;
unsigned char n_sec=0;
unsigned int pwm_duoji=0;
init_devices();
timer1_init();timer0_init();
//OCR1AH = 0x00;
/* OCR1AL = 64;
//OCR1BH = 0x00;
OCR1BL = 64;
//OCR1CH = 0x00;
OCR1CL = 64;
OCR0=64;*/
//while(1);
DDRE|=0x38;//while(1);
/////////LCD
/* DDRB = 0xBF; //SI輸入,SO,SCK,SS輸出
SPCR = 0x50; //setup SPI
SPSR = 0x01; //setup SPI*/
/*while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';while(!(UCSR0A&(1<<UDRE0)));
UDR0='a';*/
delay_ms(3000);
number_uart1(1010,1);
number_uart1(xx,0);
number_uart1(ADCjohu,1);
delay_ms(3000);
xx=0;
//LCD_Init();
//LCD_clear();
DDRE &= ~0x7c;//if (!(PORTE & STOP)) if (!(PORTE & VOLDOWN)) if (!(PORTE & VOLUP)) if (!(PORTE & NEXTSONG)) if (!(PORTE & PREVSONG))
PORTE |=0x7c;
PORTD=0x01;
//number(0,0,4,-999,1);
/* OCR1AL = pwm_pingjun;
OCR1BL = pwm_pingjun;
OCR1CL = pwm_pingjun;
OCR0=pwm_pingjun;*/
OCR1BH = 0;
OCR1BL = 0;
pwm_duoji=177;
while(1)
{
if (!(PINE & NEXTSONG))
{delay_ms(500);
if (!(PINE & NEXTSONG))
{
//CLI();
if(pwm_duoji<600) pwm_duoji++;//2.5ms
OCR1BH = (pwm_duoji>>8)&0x07;
OCR1BL = pwm_duoji;
/*OCR1BH = 0x03;
OCR1BL = 0xF0;
OCR1CH = 0x03;
OCR1CL = 0xF0;
*/
PWM_ON
//SEI();
}
}
if (!(PINE & PREVSONG))
{delay_ms(500);
if (!(PINE & PREVSONG))
{
//CLI();
if(pwm_duoji>62) pwm_duoji--;//0.5ms
OCR1BH = (pwm_duoji>>8)&0x07;
OCR1BL = pwm_duoji;
/*OCR1BH = 0x03;
OCR1BL = 0xF0;
OCR1CH = 0x03;
OCR1CL = 0xF0;
*/
PWM_ON
//SEI();
}
}
}
while(1)
{ //(unsigned char)jiaodu;
PORTF=juli;
if (!(PINE & STOP))jiaodu=100;//PWM_ON//PORTD=~PORTD;
if (ms==1)
{ //if (jiaodu!=n_jiaodu)//test
if (jiaodu!=0)
{//PORTF=jiaodu;
//if (jiaodu<0-20)//test 旋轉(zhuǎn)
if (ADCjohu<jiaodu-20)
{DDRB |=(R_pwm1 | L_pwm0);
DDRB &=~(R_pwm1 | L_pwm0);
PORTB &=~(R_pwm0 |R_pwm1| L_pwm1 | L_pwm0);
OCR1AL = pwm_pingjun;
OCR1BL = pwm_pingjun;
OCR1CL = 0;
OCR0=0;
PWM_ON
}
//else if (jiaodu>0+20)//test
else if (ADCjohu>jiaodu+20)
{DDRB |=(R_pwm0 | L_pwm1);
DDRB &=~(R_pwm0 | L_pwm1);
PORTB =0xf0;
OCR1AL = 0;
OCR1BL = 0;
OCR1CL = pwm_pingjun;
OCR0=pwm_pingjun;
PWM_ON
}
else PWM_OFF
n_jiaodu=jiaodu;
}
if (juli!=0)
if (sec!=n_sec)
{
if (juli>0)
{DDRB |=(R_pwm1 | L_pwm1);
DDRB &=~(R_pwm1 | L_pwm1);
OCR1AL = 0;
OCR1BL = pwm_pingjun+Rpwm0;
OCR1CL = 0;
OCR0=pwm_pingjun;
PWM_ON
juli--;
}
else if (juli<0)
{DDRB |=(R_pwm0 | L_pwm0);
DDRB &=~(R_pwm0 | L_pwm0);
OCR1AL = pwm_pingjun;
OCR1BL = 0;
OCR1CL = pwm_pingjun;
OCR0=0;
PWM_ON
juli++;
}/**/
else PWM_OFF
n_sec=sec;
}
//number(0,2,5,xx,1);
//number(0,4,5,ADCjohu,1);
if (!(PINE & PREVSONG))xx=0;
if (!(PINE & NEXTSONG))
{
//CLI();
number_uart1(xx,0);
number_uart1(ADCjohu,1);
delay_ms(20);
//SEI();
}
ms=0;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -