📄 test_pc10_main.c
字号:
//ICC-AVR application builder : 2005-3-21 10:06:01
// Target : M128
// Crystal: 16.000Mhz
#include <iom128v.h>
#include <macros.h>
#include <stdio.h>
#define BUFF_SIZE 20
#define EN_ADDR 7
#define CLR_ADDR 6
#define CH1 1
#define CH2 2
#define CH3 4
#define DA1 8
#define DA2 10
#define LIGHT_CN 8
#define OK 1
#define PARAMETER_ERR 0
#define HBYTE 1
#define LBYTE 0
#define CNT_BASE 0x270
#define WRB_BASE 0x240
#define FLIGHT_BASE 0x240
#define GYRO_BASE 0x290
union char_int{
unsigned char half[2];
unsigned int i1;
};
union char_int kk;
unsigned char send_buffer[BUFF_SIZE]; //发宋缓冲区
unsigned char rec0; // 接收到的字符
unsigned char char_len=0;
float rob_angle=0.0;
int left,right;
long len;
int len_test=0;
//SPI initialize
// clock rate: 2000000hz
//call this routine to initialize all peripherals
void delay_1us(void) //1us延时函数
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1716;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////串口UCSR0
////////////////////////////////////////////////////////////////////////////////
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
void USART_Transmit( unsigned char data )
{
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0 = data;
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////串口UCSR1
///////////////////////////////////////////////////////////////////////////////////
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00;
UCSR1C = 0x06;
UBRR1L = 0x19; //set baud rate lo
UBRR1H = 0x00; //set baud rate hi
UCSR1B = 0x1C;
}
void serial_send(unsigned char command){
unsigned char i=0;
while( i < char_len){
while ( !( UCSR1A & (1<<UDRE1)) );
UCSR1B &= ~(1<<TXB81);
if ( command & 0x01 )
UCSR1B |= (1<<TXB81);
UDR1 = send_buffer[i];
i++;
}
char_len = 0 ;
return;
}
int scanf_int(void)
{
unsigned char i=0;
while( i < 2){
while ( !(UCSR1A& (1<<RXC1)));
rec0 = UCSR1B;
rec0 = UDR1;
kk.half[1 - i] = rec0;
i++;
}
return kk.i1;
}
/*----------------------------------------------------------------------------
0x01 //清屏,AC自动加1,光标归位,但并不改变设置
0x02 //归位
0x06 //自动加一
0x04 //自动减一
0x0f //开显示,开光标,光标闪烁
0x0e //光标开
0x0d //光标所在字符闪烁
0x0c //光标关
0x18 //光标,字符一起左移
0x1c //光标,字符一起右移
0x38 //8位数据宽度
LCD第一行显示寄存器地址:0X80-0X8f
LCD第二行显示寄存器地址:0XC0-0Xcf
LCD第三行显示寄存器地址:0x90-0x9f
LCD第四行显示寄存器地址:0xd0-0xdf
-----------------------------------------------------------------------------*/
void xy_set(unsigned char xy)
{
unsigned char x,y,address;
x = xy;
x = x & 0x0f;
y = xy;
y = y >> 4;
if (y == 0)
address = 0x80 + x;
else if(y == 1)
address = 0xc0 + x;
else if(y == 2)
address = 0x90 + x;
else if(y == 3)
address = 0xd0 + x;
send_buffer[0] = address;
char_len = 1;
serial_send(1);
return ;
}
void init_lcd ()
{
send_buffer[0] = 0x01;
char_len = 1;
serial_send(1);
return ;
}
void shift_r()
{
send_buffer[0] = 0x1c ;
char_len = 1;
serial_send(1);
return ;
}
void shift_l()
{
send_buffer[0] = 0x18;
char_len = 1;
serial_send(1);
return ;
}
void printf_char(char s){
send_buffer[0] = s;
char_len = 1;
serial_send(0);
return ;
}
void printf_hex(unsigned char s){
unsigned int temp = 0;
temp = hextochar(s);
send_buffer[1] = temp;
temp = temp >> 8;
send_buffer[0] = temp;
char_len = 2;
serial_send(0);
return;
}
void printf_int(int c )
{
char_len = bcd_char( c, send_buffer);
serial_send(0);
return;
}
void printf_str(char *s ){
unsigned char i=0;
while( *s ){
send_buffer[i] = *s ;
i ++;
s++;
}
char_len = i;
serial_send( 0 );
return;
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
////// 104bus io 操作
//////////////////////////////////////////////////////////////////////////////
void outp(unsigned int add,unsigned char data)
{
unsigned char *p;
p = (unsigned char *) (add+0x2000);
*p=data;
}
unsigned char inp(unsigned int add)
{
unsigned char *p;
p = (unsigned char *) (add+0x2000);
return *p;
}
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//
// gyro_sample
//
// 参数 无
//
// 说明 陀螺仪采样函数,首先运行500次,求得零漂平均值,然后处理
// 陀螺仪数据,最终用算出的角度更新全局变量rob_angle
//
// 返回值 无
//
// 创建人 张辉
//
//////////////////////////////////////////////////////////////////////
void init_gyro(){
outp(GYRO_BASE+4,0x00); /*陀螺仪标志位置0*/
}
int gyro_sample()
{
int cmplt;
long angle;
unsigned long high_byte;
unsigned int mid_byte,low_byte;
high_byte = 0;
cmplt=inp(GYRO_BASE+0);
if((cmplt&0x01)==0x01)
{
return 0;
}
outp(GYRO_BASE+4,0x01);
high_byte=inp(GYRO_BASE+1);
printf("%d ",high_byte);
mid_byte=inp(GYRO_BASE+2);
printf("%d ",mid_byte);
low_byte=inp(GYRO_BASE+3);
printf("%d ",low_byte);
outp(GYRO_BASE+4,0x00);
if(high_byte>=0x80)
high_byte |= 0xffffff00;
angle=(long)(((high_byte)<<16)+((mid_byte&0x00ff)<<8)+(low_byte&0x00ff));
rob_angle=angle/2742.9797399;
return 1;
}
///////////////////////////////////////////////////////////////////////////////
void relay_write(unsigned char data)
{
unsigned char *p;
p = (unsigned char *) 0x2250;
*p=data;
}
unsigned char relay_read(void)
{
unsigned char *p;
p = (unsigned char *) 0x2250;
return *p;
}
////////////////////////////////////////////////////////////////////////////
/////////////
///////// pwm
///////
/////////////////////////////////////////////////////////////////////////////
void motor_pwm ( int left,int right )
{
DDRB|=0XE0;
TCCR1A=0xA8;
TCCR1B=0x11;
ICR1 = 0x6000;
OCR1A = left; /////12288 is stop
OCR1B = right; //// range is from 0 to 24576
//OCR1C = 0x3000 + mid;
}
void mainboard_pwm(int pwm1, int pwm2, int pwm3)
{
DDRE |= 0x38;
TCCR3A = 0xA8;
TCCR3B = 0x12;
ICR3 = 0x4E20;
OCR3A = pwm1;
OCR3B = pwm2;
OCR3C = pwm3;
}
void mainboard_pwm1( int frequ)
{
DDRE |= 0x38;
TCCR3A = 0xA8;
TCCR3B = 0x12;
ICR3 = frequ;
OCR3A = frequ/2;
OCR3B = frequ/2;
OCR3C = frequ/2;
}
//////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////////
//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
init_isp_devices();
init_gyro();
uart0_init();
uart1_init();
MCUCR = 0x80;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void test_motor_pwm(void)
{
motor_pwm(12288,12288);
do
{
xy_set(0x01);
printf_str(" inp value :");
delay_nms(100);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
left=scanf_int();
delay_nms(100);
xy_set(0x31);
printf_str(" ");
xy_set(0x31);
delay_nms(100);
printf_int(left);
right=left;
motor_pwm(right,left);
}
while (1);
}
void test_isp_cnt()
{
motor_pwm(12288,12288);
xy_set(0x01);
printf_str(" inp value :");
delay_nms(100);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
left=scanf_int();
delay_nms(100);
xy_set(0x31);
printf_str(" ");
xy_set(0x31);
delay_nms(100);
printf_int(left);
right=left;
motor_pwm(right,left);
do
{
spi_cnt(CH1);
delay_nms(100);
xy_set(0x27);
printf_str(" ");
xy_set(0x27);
len_test=len/7168;
printf_int(len_test);
xy_set(0x01);
printf_hex(inp(0x250));
delay_nms(100);
}
while(1);
}
void gogo(void)
{
int speed = 0;
int gowhere = 0;
int i=0,j=0,k=0;
init_lcd ();
xy_set(0x01);
delay_nms(10);
printf_str(" inp speed :");
delay_nms(10);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
speed = scanf_int();
delay_nms(10);
xy_set(0x31);
printf_str(" ");
xy_set(0x31);
delay_nms(10);
printf_int(speed);
delay_nms(1000);
init_lcd ();
xy_set(0x01);
delay_nms(10);
printf_str(" inp len:");
delay_nms(10);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
gowhere = scanf_int();
delay_nms(10);
xy_set(0x31);
printf_str(" ");
xy_set(0x31);
delay_nms(10);
printf_int(gowhere);
delay_nms(1000);
init_lcd ();
// speed = 13000;
// gowhere = 3;
motor_pwm(speed,speed);
outp(0x250,0xff);
for(i=0;i<5;i++)
spi_cnt(CH1);
j=0;
while (1)
{
spi_cnt(CH1);
spi_cnt(CH1);
len_test = len/7168;
if (len_test >= gowhere)
{
xy_set(0x31);
printf_str(" stop ");
printf_int(len_test);
motor_pwm(12288,12288);
}
else
{
xy_set(0x31);
printf_str(" ");
xy_set(0x31);
printf_int(len_test);
if(k==0)
motor_pwm(speed,speed);
}
if((inp(0x250)&0x01)!=0)
{ delay_nms(1);
if((inp(0x250)&0x01)!=0)
{
xy_set(0x21);
printf_str(" touched ");
motor_pwm(12288,12288);
k=1;
}
}
if ((inp(0x250)&0x80)!=0)
{ delay_nms(1);
if ((inp(0x250)&0x80)!=0)
{
xy_set(0x11);
printf_str(" complete");
outp(0x250,0x00);
j++;
}
}
else
{
delay_nms(1);
if ((inp(0x250)&0x80)==0)
xy_set(0x11);
//printf_str(" ");
delay_nms(50);
//xy_set(0x11);
printf_hex(inp(0x250));
if ((len_test <= gowhere)&(j<777))
outp(0x250,0xff);
}
}
}
void test_relay(void)
{
int key;
outp(0x250,0x00);
while(1)
{
xy_set(0x01);
printf_str(" inp retry:");
delay_nms(100);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
key=scanf_int();
delay_nms(100);
outp(0x250,key);
xy_set(0x31);
printf_hex(inp(0x250));
};
}
void test_wsad()
{
int i;
do{
i=scanf_int();
if((i&0x00ff)==0x88)
{
xy_set(0x01);
printf_str(" W");
}
else if((i&0x00ff)==0x66)
{
xy_set(0x01);
printf_str(" D");
}
else if((i&0x00ff)==0x44)
{
xy_set(0x01);
printf_str(" A");
}
else if((i&0x00ff)==0x22)
{
xy_set(0x01);
printf_str(" S");
}
else if((i&0x00ff)==0x55)
{
xy_set(0x01);
printf_str(" stop");
}
else
{
xy_set(0x21);
printf_str(" got the mun is");
xy_set(0x31);
printf_int(i);
}
}while(1);
}
void test_pwm(void)
{
int frequ;
do{
xy_set(0x01);
delay_nms(100);
printf_str(" inp frequ:");
delay_nms(100);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
delay_nms(100);
frequ=scanf_int();
delay_nms(100);
mainboard_pwm1( frequ);
delay_nms(100);
}
while(1);
}
void test_touluo(void)
{
int touluo;
do{
xy_set(0x01);
delay_nms(100);
printf_str(" inp touluo:");
delay_nms(100);
xy_set(0x21);
printf_str(" ");
xy_set(0x21);
delay_nms(100);
touluo=scanf_int();
delay_nms(100);
mainboard_pwm(touluo,touluo,touluo);
delay_nms(100);
}
while(1);
}
void main()
{
init_devices();
//motor_pwm(12288,12288);
//outp(0x250,0x00);
test_motor_pwm();
test_relay();
// test_wsad();
// test_motor_pwm();
//test_isp_cnt();
//gogo();
//test_isp_cnt();
//test_pwm();
//test_touluo();
// test_isp_cnt();
while (1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -