⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 FREESCAL智能车的最简单的初始化程序 包括PWM的初始化和A/D转化
💻 C
字号:
/***************************************
修改日期:2008.05.19
***************************************/

#include "Flash.h"
#include "printp.h"
#include "7219.h"
#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"

#define IN1 PORTK_BIT2
#define IN2 PORTK_BIT3
#define INIT_DATA_TIMER0 0x00
#define SCL_TIMER0 0

#define RIGHT7  MIDDLE-0xE0
#define RIGHT6  MIDDLE-0xC0
#define RIGHT5  MIDDLE-0xA0
#define RIGHT4  MIDDLE-0x80
#define RIGHT3  MIDDLE-0x60
#define RIGHT2  MIDDLE-0x40
#define RIGHT1  MIDDLE-0x20
#define MIDDLE  0x0600
#define LEFT1   MIDDLE+0x20
#define LEFT2   MIDDLE+0x40
#define LEFT3   MIDDLE+0x60
#define LEFT4   MIDDLE+0x80
#define LEFT5   MIDDLE+0xA0
#define LEFT6   MIDDLE+0xC0
#define LEFT7   MIDDLE+0xE0



unsigned char const LED_CODE[]=
{
  0x7E,0x30,0x6D,0x79,//0,1,2,3
  0x33,0x5B,0x5F,0x70,//4,5,6,7
  0x7F,0x7B           //8,9
};

unsigned int ADC_Buf[15];
unsigned int ADC_Buf_white[15];
unsigned int ADC_Buf_black[16];
unsigned int ADC_Buf_current[15];
unsigned int ADC_Buf_middle[15];
unsigned char Flag_start;
dword Total_pulse;

void Get_AD(void);
void Forward_set(void);
void Reverse_set(void);
void Stop_set(void);

#pragma CODE_SEG NON_BANKED
#pragma TRAP_PROC


unsigned int K_timer0;
unsigned int Signal_AD;
unsigned char Dis_led_ADL;
unsigned char Dis_led_ADH;
unsigned int Data_pwm1;
unsigned int Cnter_null;
interrupt void Int_TimerOverFlow(void) 
{
   unsigned int i;
//   i=PACN32;
   i=Total_pulse;
   Total_pulse=0;
//   PACN32=0;
   Write7219(0x01,LED_CODE[(i/1000)%10]);
   Write7219(0x02,LED_CODE[(i/100)%10]);
   Write7219(0x03,LED_CODE[(i/10)%10]);
   Write7219(0x04,LED_CODE[i%10]);
//   TSCR1_TEN  = 0;
   TCNT = INIT_DATA_TIMER0;//25536;
   TFLG2_TOF  = 1;  //clear timer overflow flag  
   K_timer0++;
   if(K_timer0%2)
   {
    PORTB=0x00;
   }
   else
   {
    PORTB=0xFF;
   }
   Get_AD();
   for(i=0,Signal_AD=0,Dis_led_ADL=0,Dis_led_ADH=0;i<15;i++)
   {
      if(ADC_Buf_current[i]<ADC_Buf_middle[i])//判断黑线
      {
          Signal_AD|=1<<i;
          switch(i)
          {
            case 0:
              Dis_led_ADH|=1<<0;
            break;
            case 8:
              Dis_led_ADH|=1<<1;
            break;
            case 1:
              Dis_led_ADH|=1<<2;
            break;
            case 9:
              Dis_led_ADH|=1<<3;
            break;
            case 2:
              Dis_led_ADH|=1<<4;
            break;
            case 10:
              Dis_led_ADH|=1<<5;
            break;
            case 3:
              Dis_led_ADH|=1<<6;
            break;
            
            case 11:
              Dis_led_ADL|=1<<7;
            break;
            case 4:
              Dis_led_ADL|=1<<0;
            break;
            case 12:
              Dis_led_ADL|=1<<1;
            break;
            case 5:
              Dis_led_ADL|=1<<2;
            break;
            case 13:
              Dis_led_ADL|=1<<3;
            break;
            case 6:
              Dis_led_ADL|=1<<4;
            break;
            case 14:
              Dis_led_ADL|=1<<5;
            break;
            case 7:
              Dis_led_ADL|=1<<6;
            break;
          }
//          printp("ADC_Buf_current%d=%d  ",i,ADC_Buf_current[i]);
      }
   }
//   printp("\nSignal_AD=%X\n",Signal_AD);
   Write7219(0x05,Dis_led_ADL);
   Write7219(0x06,Dis_led_ADH);
   switch(Signal_AD)
   {
      case 0x0080:
        Data_pwm1=RIGHT7;
        Cnter_null=0;
      break;
      case 0x4080:
        Data_pwm1=RIGHT7;
        Cnter_null=0;
      break;
      case 0x4000:
        Data_pwm1=RIGHT6;
        Cnter_null=0;
      break;
      case 0x4040:
        Data_pwm1=RIGHT6;
        Cnter_null=0;
      break;
      case 0x0040:
        Data_pwm1=RIGHT5;
        Cnter_null=0;
      break;
      case 0x2040:
        Data_pwm1=RIGHT5;
        Cnter_null=0;
      break;
      case 0x2000:
        Data_pwm1=RIGHT4;
        Cnter_null=0;
      break;
      case 0x2020:
        Data_pwm1=RIGHT4;
        Cnter_null=0;
      break;
      case 0x0020:
        Data_pwm1=RIGHT3;
        Cnter_null=0;
      break;
      case 0x1020:
        Data_pwm1=RIGHT3;
        Cnter_null=0;
      break;
      case 0x1000:
        Data_pwm1=RIGHT2;
        Cnter_null=0;
      break;
      case 0x1010:
        Data_pwm1=RIGHT2;
        Cnter_null=0;
      break;
      case 0x0010:
        Data_pwm1=RIGHT1;
        Cnter_null=0;
      break;
      case 0x0800:
        Data_pwm1=MIDDLE;
        Cnter_null=0;
      break;
      case 0x0008:
        Data_pwm1=LEFT1;
        Cnter_null=0;
      break;
      case 0x0408:
        Data_pwm1=LEFT2;
        Cnter_null=0;
      break;
      case 0x0400:
        Data_pwm1=LEFT2;
        Cnter_null=0;
      break;
      case 0x0404:
        Data_pwm1=LEFT3;
        Cnter_null=0;
      break;
      case 0x0004:
        Data_pwm1=LEFT3;
        Cnter_null=0;
      break;
      case 0x0204:
        Data_pwm1=LEFT4;
        Cnter_null=0;
      break;
      case 0x0200:
        Data_pwm1=LEFT4;
        Cnter_null=0;
      break;
      case 0x0202:
        Data_pwm1=LEFT5;
        Cnter_null=0;
      break;
      case 0x0002:
        Data_pwm1=LEFT5;
        Cnter_null=0;
      break;
      case 0x0102:
        Data_pwm1=LEFT6;
        Cnter_null=0;
      case 0x0100:
        Data_pwm1=LEFT6;
        Cnter_null=0;
      break;
      case 0x0101:
        Data_pwm1=LEFT7;
        Cnter_null=0;
      break;
      case 0x0001:
        Data_pwm1=LEFT7;
        Cnter_null=0;
      break;
      default:
      break;
   }

   PWMDTY3 = (byte)Data_pwm1;
   PWMDTY2 = (byte)(Data_pwm1>>8);
   TSCR1_TEN  = 1;
}




#pragma CODE_SEG DEFAULT

 

void ECT_Init() 
{
   TCNT = INIT_DATA_TIMER0;//25536;
   TSCR2_PR   = SCL_TIMER0;  //prescale factor is 8, bus clock/2=8Mhz/2=4MHz
   TSCR2_TOI  = 1;  //timer overflow interrupt enable
   TSCR1_TEN  = 1;
}

void SCI0Init() {
  SCI0BDL = (byte)((16000000UL /* OSC freq *//2) / 38400 /* baud rate */ / 16 /*factor*/);
  SCI0CR2 = 0x2C;
}

void PORTPInit(void) 
{
  DDRP = 0xEF;    //PORTP4输入
  PERP = 0x10;    //允许上拉或者下拉
  PPSP = 0xFF;    //上升沿触发中断
  PIEP = 0x10;    //中断允许
  DDRK=0b00001100;  //PORTK.2,PORTK.3用于IN1、IN2
  DDRB=0xFF;
  PORTB=0xFF;
  DDRA=0x00;
//  PUCR=1;
}

void AD_Init()
{ //初始化
  ATD0CTL2=0xc0; //AD模块上电, 快速清零, 无等待模式, 禁止外部触发, 中断禁止
  ATD0CTL3=0x40; // 每个序列4次转换, No FIFO, Freeze模式下继续转换
  ATD0CTL4=0x85; // 8位精度, 2个时钟, ATDClock=[BusClock*0.5]/[PRS+1]  ; PRS=3, divider=8
  ATD0CTL5=0xB0; //右对齐无符号,duo通道采样,
  ATD0DIEN=0x00; // 禁止数字输入
  ATD1CTL2=0xc0; //AD模块上电, 快速清零, 无等待模式, 禁止外部触发, 中断禁止
  ATD1CTL3=0x40; // 每个序列4次转换, No FIFO, Freeze模式下继续转换
  ATD1CTL4=0x85; // 8位精度, 2个时钟, ATDClock=[BusClock*0.5]/[PRS+1]  ; PRS=3, divider=8
  ATD1CTL5=0xB0; //右对齐无符号,duo通道采样,
  ATD1DIEN=0x00; // 禁止数字输入
}

void PWM_format(void) 
{
  PWME = 0x00 ; // PWM is disabled
  PWMCTL_CON01 = 1; // PWM01 合并 16 bit
  PWMCTL_CON23 = 1; // PWM23 合并 16 bit
  PWMPRCLK = 0x33; // A=B=8M/8=1M 其中A对应PWM0、1、4、5  B对应PWM2、3、6、7
  PWMSCLA = 100; // SA=A/100=10k 缩放寄存器 频率=分频后的时钟频率/PWMSCLA 
  PWMSCLB = 1; // SB=B/1 =1000k
  PWMCLK = 0b00000100; // PWM0-A PWM2-SB
  PWMPOL = 0b00001010; // 位极性=1 Duty=High Time 
  PWMCAE = 0x00; //   对齐方式-左对齐
  PWMPER2 = 0x27; //  5000 = 0x2710 Frequency=SB/5000=200Hz
  PWMPER3 = 0x10; // 
  PWMDTY2 = MIDDLE>>8; //  1500 = 0x05DC T=1.5ms
  PWMDTY3 = MIDDLE; //
  PWMPER0 = 0x03; //  1000=0x03E8 Frequency=A/1000=1KHz
  PWMPER1 = 0xE8;
  PWMDTY0 = 0x01; //  Duty=200/1000=20%
  PWMDTY1 = 0xC8;
  PWME_PWME3 = 1; // 舵机enable
  PWME_PWME1 = 1; // 电机enable
}



void Dev_init()
{
  ECT_Init();
  PORTPInit();  
  AD_Init();
  PWM_format();
  SCI0Init();
}

void Get_AD()
{
		  
      ADC_Buf_current[0]=ATD0DR0L;
      ADC_Buf_current[1]=ATD0DR1L;
	    ADC_Buf_current[2]=ATD0DR2L;
  	  ADC_Buf_current[3]=ATD0DR3L;
	    ADC_Buf_current[4]=ATD0DR4L;
	    ADC_Buf_current[5]=ATD0DR5L;
  	  ADC_Buf_current[6]=ATD0DR6L;
	    ADC_Buf_current[7]=ATD0DR7L;
	    ADC_Buf_current[8]=ATD1DR0L;
		  ADC_Buf_current[9]=ATD1DR1L;
		  ADC_Buf_current[10]=ATD1DR2L;
	  	ADC_Buf_current[11]=ATD1DR3L;
		  ADC_Buf_current[12]=ATD1DR4L;
	  	ADC_Buf_current[13]=ATD1DR5L;
	  	ADC_Buf_current[14]=ATD1DR6L;
}

void Get_AD_white()
{
		  ADC_Buf_white[0]=ATD0DR0L;
      ADC_Buf_white[1]=ATD0DR1L;
      ADC_Buf_white[2]=ATD0DR2L;
	    ADC_Buf_white[3]=ATD0DR3L;
  	  ADC_Buf_white[4]=ATD0DR4L;
	    ADC_Buf_white[5]=ATD0DR5L;
	    ADC_Buf_white[6]=ATD0DR6L;
  	  ADC_Buf_white[7]=ATD0DR7L;
	    ADC_Buf_white[8]=ATD1DR0L;
	    ADC_Buf_white[9]=ATD1DR1L;
		  ADC_Buf_white[10]=ATD1DR2L;
		  ADC_Buf_white[11]=ATD1DR3L;
	  	ADC_Buf_white[12]=ATD1DR4L;
		  ADC_Buf_white[13]=ATD1DR5L;
	  	ADC_Buf_white[14]=ATD1DR6L;
}

void Get_AD_black()
{
		  ADC_Buf_black[0]=ATD0DR0L;
      ADC_Buf_black[1]=ATD0DR1L;
      ADC_Buf_black[2]=ATD0DR2L;
	    ADC_Buf_black[3]=ATD0DR3L;
  	  ADC_Buf_black[4]=ATD0DR4L;
	    ADC_Buf_black[5]=ATD0DR5L;
	    ADC_Buf_black[6]=ATD0DR6L;
  	  ADC_Buf_black[7]=ATD0DR7L;
	    ADC_Buf_black[8]=ATD1DR0L;
	    ADC_Buf_black[9]=ATD1DR1L;
		  ADC_Buf_black[10]=ATD1DR2L;
		  ADC_Buf_black[11]=ATD1DR3L;
	  	ADC_Buf_black[12]=ATD1DR4L;
		  ADC_Buf_black[13]=ATD1DR5L;
	  	ADC_Buf_black[14]=ATD1DR6L;
	  	ADC_Buf_black[15]=ATD1DR7L;
}
     
void Forward_set()
{
  IN1=0;
  IN2=1;
}

void Reverse_set()
{
  IN1=1;
  IN2=0;
}

void Stop_set()
{
  IN1=0;
  IN2=0;
}

unsigned char Key_in;
  unsigned int bf[5];
  
void main(void) 
{
  unsigned int i;

  /* put your own code here */
  
  Dev_init();
  Init_7219();
  init_falsh();
//  SectorErase(0);
  Stop_set();
  Total_pulse=0;
  Flag_start=0;
//  printp("\n");
  

  {
    Write7219(0x01,0x00);
    Write7219(0x02,0x00);
    Write7219(0x03,0x00);
    Write7219(0x04,0x00);
    Write7219(0x05,0x00);
    Write7219(0x06,0x00);
    Write7219(0x07,0x00);
    Write7219(0x08,0x00);
  }
  if(PORTA==0xE0)
  {
    SectorErase(0);
    while(PORTA!=0xF0)
    Get_AD_black();
    PORTB=0xFE;
    while(PORTA!=0xD0);//按下KEY2
    Get_AD_white();
    PORTB=0xFC;
    for(i=0;i<15;i++)
    {
      ADC_Buf_middle[i]=(ADC_Buf_white[i]+ADC_Buf_black[i])/2;
    }
   
  }
  for(i=0;i<15;i++)
  {
    ADC_Buf_middle[i]=read_flash(i,1);
  }
  while(PORTA!=0xB0);
  EnableInterrupts;
  PORTB=0xF8;
  Flag_start=1;
  Forward_set();
  
  
  

  
  while(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -