1、智能车源程序#include /* common defines and macros */#include /* derivative information */#include math.h#pragma LINK_INFO DERIVATIVE mc9s12xs128/*DEFINE*/#define WHITE 50#define BLACK 40 #define KD 0 #define EnableSCIReInt SCI0CR2 |= 0x20 #define DisableSCIReInt SCI0CR2 &= 0xDF#define RTI_SWITCH_ON CRGINT
2、_RTIE=1#define RTI_SWITCH_OFF CRGINT_RTIE=0#define IC_SWITCH_ON TSCR1_TEN=1 #define IC_SWITCH_OFF TSCR1_TEN=0/-define key-/ #define KEY_RUN_OR_LCD PORTK_PK0 #define KEY_START_RUN PORTA_PA1 #define KEY_UP PORTA_PA2#define KEY_DOWN PORTA_PA3#define KEY_ENTER PORTA_PA4/-part of lcd-/#define LINE1 0 #de
3、fine LINE2 1 #define LINE1_HEAD 0x80 #define LINE2_HEAD 0xC0 #define DATA_MODE 0x38 / 8 bit data transmission mode, double line #define CLR 0x01 #define BUSY 0x80 / LCD Busy Tag #define CURSOR_RESET 0x02 #define INPUTMODE_CUR_R 0x06 #define INPUTMODE_CUR_L 0x04 #define INPUTMODE_ALL_E 0x05 #define I
4、NPUTMODE_ALL_D 0x04 #define SCREEN_OPEN 0x0C #define SCREEN_OPEN_CUR 0x02 #define SCREEN_OPEN_FLASH 0x01 #define SCSHIFT_CUR_L 0x10 / Screen or Cursor shift #define SCSHIFT_CUR_R 0x14 #define SCSHIFT_SCR_L 0x18 #define SCSHIFT_SCR_R 0x1C /-define lcd pins-/#define RS PORTA_PA5 /Data Command Pin 1 da
5、ta 0 command #define RW PORTA_PA6 /Read Write Pin 1 read 0 write #define EN PORTA_PA7 /LCD Enable Signal #define LCD_DATA PTH #define LCD_DATA_DIR DDRH #define LCD_DIR_IN 0x00 #define LCD_DIR_OUT 0xff /*END*/*HARDWARE INIT FUN DECLARE*/void uart_init(void);extern void uart_PutChar(unsigned char);uns
6、igned char TERMIO_GetChar(void);void printp( char * , .); void CPU_Init(void);void IO_Init(void);void SciTx(unsigned char text);char SciRx(void); void RTI_Init(void);void IC_Init(void);void OC7_Init(void);void PTI0_Init(void);void PWM_Init(void);void AD_Init(void);/-part of lcd-/void LcdCommand(unsi
7、gned char command,unsigned char BusyC);void LcdData(unsigned char temp,unsigned char BusyC); void LcdInit(void); unsigned char ReadStatus(void); void GotoXY( unsigned char x, unsigned char y ); void Print(unsigned char *str);void Lcd_Print_Str(unsigned char x,unsigned char y,char *s); void Lcd_Print
8、_Int( unsigned char x,unsigned char y,unsigned int data);void Lcd_Print_Char( unsigned char x,unsigned char y,unsigned char data);void Lcd_Print_uChar( unsigned char x,unsigned char y,char data);/*END*/ /*SOFTWARE FUN DECLARE*/void light_flip(void);void AD_DATA_HIGHT(void);void AD_DATA_LOW(void);voi
9、d VOL_Normaliz(void);void err_calculate_hight(void);void err_calculate_low(void);signed int PID(void);void key_scan_deal(void);void LCD_DISPLAY(void);uint err_Std(void);void check_line(void);void LcdDelay(byte k);void Dly_ms(int ms);void delay(unsigned int ref);/*END*/*DEFINE VAR*/-sensor data var-/
10、 uchar THRESHOLD_black=40; uchar max_vol9,min_vol9=255,255,255,255,255,255,255,255,255;uchar WHITE_MIN=240,WHITE_MAX=250;uchar percent9,data9,det_vol9,flip_data9;schar err100,err_e3;uchar L_times=130;byte flip=0,max_num_p=0;/-sensor_2 data var-/ uchar data_25,THRESHOLD_2=200,flip_data_25,err_e23;/-s
11、peed var-/ byte load_bit=0;uchar pluse_1=0,pluse_2=0,get_speed=0,set_speed=30,now_speed=0;uchar MAX_SPEED=30;/-check var- /uint check_pluse=0;uchar start_line=0,stop=0,stop_delay=0,slope_time=0,slope_timing=0;byte check=0,check_bit=0,line_num=0,triangle_status=0,slope_bit=0,black_bit=0;/-key&lcd var
12、-/byte key_r_o_l=0,lcd_print=1;uchar menu=1,menu_option=1,bits=0;/- calc var-/uchar KP=8,period_1=0,period_2=0;char std_err10;int temp_std;/*END*/void main() DisableInterrupts; CPU_Init(); IO_Init(); uart_init(); /EnableSCIReInt; LcdInit(); PWM_Init(); AD_Init(); VOL_Normaliz(); Dly_ms(3000); Dly_ms
13、(2000); RTI_Init(); IC_Init(); for(;) EnableInterrupts; /err_calculate_hight(); key_scan_deal(); /-/ HARDWARE FUN START /-void CPU_Init() CLKSEL=0X00; PLLCTL_PLLON=1; REFDV=0x01; /BUS_CLOCK = 40M SYNR=0x04; _asm(nop); _asm(nop); while(!(CRGFLG_LOCK=1); CLKSEL_PLLSEL=0x01; FCLKDIV=0x0F;/Flash Clock D
14、ivide Factor 40M/16=?M void SciTx(unsigned char text) unsigned char temp; temp=SCI0SR1; /*clear flag*/ while (!(SCI0SR1&0x80); /* wait for output buffer empty */ SCI0DRH=0; SCI0DRL=text;char SciRx(void) unsigned char result,temp; temp=SCI0SR1; /*clear flag*/ while(!(SCI0SR1&0x20); result=SCI0DRL; re
15、turn result; void RTI_Init() RTICTL=0b10010011; /0010000=2*103,10010011=8*103=0.5ms,10001111=16*103=1ms,11000111=160x103=10ms ,11101111=1.6*106=100ms /CRGINT_RTIE=1; void IC_Init() DisableInterrupts; TIOS_IOS0=0; /channel 0 input capture TCTL4_EDG0B=1; TCTL4_EDG0A=1; TIE_C0I=1; /channel 0 interrupt
16、enable TSCR2=0x03; /TIMEclock = 40M / 8 =0.2us TFLG1 = 0xFF; TFLG2 = 0x80; /TSCR1_TEN=1; /Timer enable EnableInterrupts;void OC7_Init() TSCR2_TCRE=1; TSCR2_PR=0; TIOS_IOS7=1; TC7=0x5dc0; /24000,1ms TCTL1=0x00; TIE_C7I=1; TSCR1_TEN = 1; /*void PTI0_Init() PITCFLMT=0x00; PITCE_PCE0=1; PITMUX_PMUX0=0;
17、PITMTLD0=0x01; PITLD0=0x0001; /2x2x25ns=1us; PITINTE_PINTE0=1; PITCFLMT_PITE=1;*/void PWM_Init() PWME=0xaa; /enable c_1,c_3,c_5,c_7 PWMPOL=0xaa; PWMCAE=0X00; PWMCTL=0xf0; /com 0&1 2&3 4&5 PWMPRCLK=0x00; PWMCLK_PCLK1=1; /0&1-SA PWMSCLA=0x04; /SA = 5M PWMPER01=50000; /舵机: 5000000/50000=100Hz ; PWMDTY0
18、1=7100; PWMPER23=5000; /电机: 24000000/12000=2000Hz ;24000000/2400=10000Hz ;40000000/5000=8000Hz PWMDTY23=2500; PWMPER45=5000; PWMDTY45=2500;void AD_Init() /3:0-WRAP /ATD0CTL0=0x00; /7-ETRIGSEL, 6:5-Resolution Select, 4-Discharge, 3:0-External Trigger Channel Select ATD0CTL1=0x00; /6-AFFC;5-ICLKSTP; 4
19、:3-External Trigger Edge/Polarity; 2-ETRIGE; /1-ASCIE,Sequence Complete Interrupt Enable; 0-ACMPIE,Compare Interrupt Enable ATD0CTL2=0x40; /7-DJM,Data Justification; 6:3-S8C,S4C,S2C,S1C,Sequence Length;2-FIFO; /1:0-Background Debug Freeze Enable; ATD0CTL3=0x98; /7:5-SMP,Sample Time Select; 4:0-PRS,f
20、ATDCLK=fBUS/2/(PRS + 1) ATD0CTL4=0x01; /6-SC5,Special Channel Conversion; 5-SCAN,Continuous Conversion Sequence Mode /4-MULT,Multi-Channel Sample Mode; 3:0-Analog Input Channel Select Code CD,CC,CB,CA /ATD0CTL5=0x01; /ATD0CMPE_CMPE0=1; /ATD0CMPHT_CMPHT0=0; /AD0 /ATD0DR0L=0x000f; ATD0DIEN=0x00; void
21、IO_Init() / SENSOR DDRB=0xff; PORTB=0x00; DDRA=0x01; PORTA_PA0=0; / KEY DEFINE DDRK_DDRK0 = 0; DDRA_DDRA1 = 0; DDRA_DDRA2 = 0; DDRA_DDRA3 = 0; DDRA_DDRA4 = 0; DDRK_DDRK1 = 0; DDRK_DDRK2 = 0; DDRK_DDRK3 = 0; / LCD CONTORL DDRA_DDRA5 = 1; DDRA_DDRA6 = 1; DDRA_DDRA7 = 1; /LED DDRK_DDRK4 = 1; DDRK_DDRK5
22、 = 1; PORTK_PK4 = 0; PORTK_PK5 = 0; /sensor_2 DDRK_DDRK7 = 1; PORTK_PK7 = 0; void Dly_ms(int ms) int ii,jj; if (ms1) ms=1; for(ii=0;iims;ii+) for(jj=0;jj2670;jj+); /busclk:16MHz-1ms void delay(unsigned int ref) uint kk; for(kk=0;kkref;kk+) asm nop; /*LCD drive start*/void LcdInit() LCD_DATA_DIR = LC
23、D_DIR_OUT; LcdDelay(21); LcdCommand(DATA_MODE,1); /set 8 bit data transmission mode LcdDelay(7); LcdCommand(DATA_MODE,1); /set 8 bit data transmission mode LcdDelay(7); LcdCommand(DATA_MODE,1); /set 8 bit data transmission mode LcdCommand(CLR,0); /clear screen LcdCommand(DATA_MODE,1); /set 8 bit dat
24、a transmission mode LcdCommand(INPUTMODE_CUR_R | INPUTMODE_ALL_D, 1); / cursor right, disable moving LcdCommand(SCREEN_OPEN, 1); /open display (enable lcd display) LcdCommand(LINE1_HEAD,1); /set lcd first display address LcdCommand(CLR,1); /clear screen unsigned char ReadStatus(void) unsigned char c
25、Rtn; LCD_DATA_DIR = LCD_DIR_IN; RS = 0; RW = 1; EN = 0; delay(500); EN = 1; while (LCD_DATA & BUSY); /Test Busy State cRtn = LCD_DATA_DIR; / if Not save the port value, it should be change LCD_DATA_DIR = LCD_DIR_OUT; return(cRtn); void LcdCommand(unsigned char command,unsigned char BusyC) if (BusyC)
26、 ReadStatus(); /Test it busy or not LCD_DATA=command; RS=0; RW=0; EN=0; delay(500); EN=1; void LcdData(unsigned char dat,unsigned char BusyC) if (BusyC) ReadStatus(); /Test it busy or not LCD_DATA=dat; RS=1; RW=0; EN=0; delay(500); EN=1; void LcdDelay(byte k) /k=7=5ms uint i, j; for (i = 0; i k; i+) for(j = 0; j 4780; j+); void GotoXY( unsigned char x, unsigned char y ) unsigned char address; if (y = LINE1) address = LINE1_HEAD + x; else address = LINE2_HEAD + x; LcdCommand(addres
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1