常驻嘉宾
- 积分
- 4940
- 威望
- 705
- 贡献
- 4087
- 兑换币
- 0
- 注册时间
- 2009-6-14
- 在线时间
- 74 小时
|
程序和大家分享,不过为什么我的程序在采集第偶场的时候不能采集,根本就不仅中断呢??
pe2奇偶场信号端。IRQ为行中断。多谢帮忙哦
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
typedef unsigned char INT8U; //无符号8位数
typedef signed char INT8S; //有符号8位数
typedef unsigned int INT16U; //无符号16位数
typedef signed int INT16S; //有符号16位数
typedef unsigned long INT32U; //无符号32位数
typedef signed long INT32S; //有符号32位数
typedef float FP32; //单精度浮点数
typedef double FP64; //双精度浮点数
//------------------------------------------------------------------
#define odd_even PORTE_PE2
#define ROW_START 55// begin to sample from line start
#define ROW_END 280//240 // end flag of sampling
#define ROW_MAX 16//最大取17行
#define LINE_MAX 45// 45
#define JIAN_GE 14
#define PWM_MID 2999 //mid
#define PWM_RIGHT 3798 //max right
#define PWM_LEFT 2420 //max left
int speed,lastspeed;
int steer,laststeer;
int rev_speed,last_rev_speed;
int speed_e,lastspeed_e;
int rev_speed_e,last_rev_speed_e;
//#define high_speed 250 // speed used on straight track
//#define low_speed 100 // speed used on the turn
int speed_MAX;
int speed_set_curve;
unsigned char stand;
//unsigned int speed_vari;
unsigned char K_speed=1,K_steer=1;
unsigned char image_data_0[ROW_MAX][LINE_MAX];
unsigned char image_data_1[ROW_MAX][LINE_MAX];
unsigned char flag_data; // 使用哪一场的标志位
// 0使用 image_data_0[ROW_MAX][LINE_MAX];
// 1使用 image_data_1[ROW_MAX][LINE_MAX];
unsigned char finish_data=1; //0标志数据处理完成
unsigned char row=0;
unsigned int line=0;
unsigned char temp=0;
unsigned int rowcount=0;
//unsigned char rowcount=0;
unsigned char line_temp=0;
/*
//-------------------------------------------OTHER VAR
unsigned char a,b,c,x;
char m,n,i,j,s;
//unsigned char Start_Flag=0,Circle=0,Circle_Count=2,BEGIN=0,STOP=0;
//探测起跑线 ,一圈停车
unsigned char start_count;
unsigned char flag_start,flag_stop,flag_heap;
//起始标志,停止标志,上坡标志
#define circle 2 //run 2 circle
*/
int pa_count;
int speed_set,rev_speed_set;
int Kp=20,Kd=3;
unsigned char i=0,j=0,k=0;
unsigned char time;
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 6 IRQ_ISR()
{ unsigned char time;
rowcount++;
if((rowcount>ROW_START)&&(rowcount%JIAN_GE==0))
{if(flag_data){
ATD0CTL5=0x2F; //启动ATD15位转换
// while(PORTE_PE1!=1);
for(time=0;time<70;time++); //原来为35
for(line=0;line<LINE_MAX;line++)
{
while(!ATD0STAT0_SCF);
image_data_1[row][line]=ATD0DR0L;
}
} else{
ATD0CTL5=0x2F; //启动ATD15位转换
// while(PORTE_PE1!=1);
for(time=0;time<70;time++); //原来为35
for(line=0;line<LINE_MAX;line++)
{
while(!ATD0STAT0_SCF);
image_data_0[row][line]=ATD0DR0L;
}
}
row++;
}
}
#pragma CONST_SEG DEFAULT
//-----------------------------------------------------------------
void init_PORT(void) //端口初始化
{
DDRE_DDRE2=0; //Ph0作为奇偶场信号输入
// IRQCR=0xc0; //外部IRQ使能,行同步,下降沿
DDRB=0xff;
PORTB=0xff;
DDRM=0x00; //键盘
RDRM=0xFF;
DDRP_DDRP2=1; //电机正反转控制
DDRP_DDRP3=1;
}
#pragma CODE_SEG DEFAULT
void delayms(unsigned int ms)
{int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<3338;jj++);//40MHz--1ms
}
/*---------------------------------------------
AD15初始化:
输入;
输出;
---------------------------------------------- */
#pragma CODE_SEG DEFAULT
void AD15_Init(void)
{
ATD0CTL0 = 0x00;
ATD0CTL1 = 0x00; //8位转换
ATD0CTL2 = 0x40; // 0100_0000 AD模块上电, 快速清零, 无等待模式, 禁止外部触发, 中断禁止
ATD0CTL3 = 0x88; //1000_1000 转换长度为一
ATD0CTL4 = 0x01; // fATDCLK=fBUS/[2 × (PRS + 1)]
// ATD0CTL5 = 0x0f; //第15通道转换
ATD0DIEN = 0x00;
}
#pragma CODE_SEG DEFAULT
INT8U AD15_get(void)
{
INT8U temp;
ATD0CTL5 = 0x0F;//启动ATD15位转换
while(!ATD0STAT0_SCF);
temp = ATD0DR0L; //从A/D数据寄存器15中读8位数据,此处为第15通道,video
return temp;
}
#pragma CODE_SEG DEFAULT
void SCI_Init(void)
{ SCI0CR1=0x00; //设置允许SCI,正常码输出,8位数据,无校验
SCI0CR2=0x08; //enable Receive Full Interrupt,RX enable,Tx enable
SCI0BDH=0x01; //busclk 8MHz,19200bps,SCI0BDL=0x1a
SCI0BDL=0x15; //SCI0BDL=busclk/(16*SCI0BDL)
}
void SCISend1(unsigned char o)
{
//判断ReStatusR的第SendTestBit位是否为1,是1可以发送
for(;(SCI0SR1& (1<<7)) == 0;);
SCI0DRL = o;
}
void SetBusCLK_64M(void) //PLLCLK=2*OSCCLK*((SYNR+1)/(REFDV+1))
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR =0xc0 | 0x07;
REFDV=0xc0 | 0x01;
POSTDIV=0x00; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=80MHz;
_asm(nop); //BUS CLOCK=64M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
IRQCR &= 0xbf; //IRQCR.6(IRQEN) =0禁止IRQ中断(默认开)
COPCTL = 0x00; //COPCTL.2-0(cr2:cr0) =000禁止看门狗
}
//------------------------电机启动----------------------------------------*
void MOTOR_PWM01_Init(void)
{
PWMCTL_CON01=1; //通道01级联
//PWME=0X00; //禁止PWM输出
PWMCAE_CAE1=0; //左对齐
PWMPOL_PPOL1=1; //PWM输出起始为高电平
PWMPRCLK=0x02; //ClockA分频=64M/4=16
PWMSCLA=0x04; //ClocksA=20/(5*2) ClocksA为2MHZ
PWMCLK_PCLK1=1; //时钟来源选择ClockA
PWMPER01=2000; //输出周期=(1/2M)*(2000) =1ms
PWMDTY01=40; //高电平时间25us
// PTM =0x01; //PM0=1 PM1=0 电机准备正传
PWME_PWME1=1; //启动PWM输出
}
//------------------------电机启动正反转----------------------------------------*
void MOTOR_zheng(void) { PTP_PTP2=0TP_PTP3=1; }
void MOTOR_fan(void) { PTP_PTP2=1;PTP_PTP3=0; }
#pragma CODE_SEG DEFAULT
//----------------------舵机PWM初始化--------------------------------------*
//功能: *
// 16位PWM45,PWM信号输出引脚PP5, 启动后PP5引脚输出周期20ms高电平时间1.5ms
// 的PWM波 此PWM波应使舵机的转角为0
//参 数:无 *
//返 回:无 *
//-------------------------------------------------------------------------*
void DUOJI_PWM45_Init(void)
{
PWMCTL_CON45=1; //通道45级联
PWMCAE_CAE3=0; //左对齐
PWMPOL_PPOL5=1; //PWM输出起始为高电平
PWMCLK_PCLK5=1; //时钟来源选择ClockSA
PWMPER45=39999; //输出周期=(1/1M)*(20000) =20ms
PWMDTY45=PWM_MID; //高电平持续时间1.5ms,使舵机输出转角为0
PWME_PWME5=1; //启动PWM输出
}
void main(void) {
SetBusCLK_64M();// MCUInit();
init_PORT();
PORTB=0b11111110;
//delayms(1000);
AD15_Init();
PORTB=0b11111101;
//delayms(1000);
SCI_Init();
EnableInterrupts;
PORTB=0b11111011;
//delayms(4000);
EnableInterrupts;
for(;;)
{ PORTB=0b11110111;
//-----------------------------------------------------------
row=0;
rowcount=0;
while(odd_even);
while(!odd_even); //此处向下为高电平
if(odd_even)
{ flag_data=0;//数据放在第0场
while(odd_even); //采集的是低电平的场
IRQCR=0xc0; //打开行中断
while(row<ROW_MAX) { }; //在此处可以进行上一场运算 与标志位相与判断是否完成
IRQCR=0x00;
}
PORTB=0b11101111;
flag_data=1; //数据放在第1场
while(!odd_even);//采集的是高电平的场
IRQCR=0xc0;
while(row<ROW_MAX) { };
IRQCR=0x00;
PORTB=0b11011111;
for(j=0;j<LINE_MAX;j++)
{
SCISend1(j);
}
k=0;
SCISend1(k++);
for(i=0;i<ROW_MAX;i++)
{
for(j=0;j<LINE_MAX;j++)
{
SCISend1(image_data_0[i][j]);
}
SCISend1(k++);
}
PORTB=0b10111111;
delayms(4000);
for(j=0;j<LINE_MAX;j++)
{
SCISend1(j);
}
k=0;
SCISend1(k++);
for(i=0;i<ROW_MAX;i++)
{
for(j=0;j<LINE_MAX;j++)
{
SCISend1(image_data_1[i][j]);
}
SCISend1(k++);
}
PORTB=0b01111111;
}
} |
|