完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
最近在研究MSP430G2553内部AD,下面是程序,AD是单通道多次转换,12864是串口控制,然后还加了两个独立按键的,用来测试按键中断的。发现按键按个10次左右屏幕就花了,不知道为什么,是系统频率的原因吗?因为这个AD程序是考的别人的,不知道他的系统时钟是设置的多少。求助大神啊!!! 主程序: //12864------UART //P1.5采集电压1,P1.3采集电压2 //P2.0,P2.1,P2.2-----12864UART #include #include"LCD12864P.h" #include #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) #define CPU_F ((double)8000000) void ad_ini(); float ad_main(unsigned char x); void KeyAction(); unsigned char count = 0; float result,k = 0; unsigned char flag,KeyChoose = 1; int main(void) { char Buf[18]; float adval=0; WDTCTL = WDTPW + WDTHOLD; //Stop WDT 关闭看门狗 P1DIR = 0XFF; P2DIR = 0xff; P2DIR |= BIT0+BIT1+BIT2; //12864串口的输出P2.0,P2.1,P2.2 P2DIR &= ~BIT3 + ~BIT4; //键盘输入P2.3,P2.4 P2IE |= BIT3 + BIT4; P2IES |= BIT3 + BIT4; //用一个高电平到低电平转换来设定 PxIFGx 标志 P2IFG &= BIT3 + BIT4; P2REN |= BIT3 + BIT4; Ini_Lcd(); ad_ini(); _EINT();//打开全局中断 delay_ms(1); wr_string(0,0,"东南大学CXXY"); wr_string(0,1,"ADC_TEST"); for(;;) { delay_ms(5); adval=ad_main(0); wr_float(0,2,adval); delay_ms(5); adval=ad_main(1); wr_float(0,3,adval); } } void ad_ini() { ADC10CTL1 = CONSEQ_2 ; // 单通道多次转换模式,A1 ADC10CTL0 = ADC10SHT_2+MSC + ADC10ON + ADC10IE; // ADC10ON,中断ENABL } float ad_main(unsigned char x) { flag=1; count=0; result=0; if(x==1) { ADC10AE0 |= BIT5; // P1.5 ADC选项选择 ADC10CTL1 |= INCH_5 ; ADC10CTL0 |= ENC + ADC10SC; // 使能转换和转换开始 } if(x==0) { ADC10AE0 |= BIT3; // P1.3 ADC选项选择 ADC10CTL1 |= INCH_3; ADC10CTL0 |= ENC + ADC10SC; // 使能转换和转换开始 } while(flag); return result; } void KeyAction() { if(KeyChoose == 4) { wr_string(0,1,"按键一动作"); } if(KeyChoose == 3) { wr_string(0,1,"按键二动作"); } } #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { count+=1; result += ADC10MEM; if(count>19) { flag=0; count = 0; result=result/20; //进行了20次转换,取平均 result = result/1024.0*3.55; //设置AD参考电压 ADC10CTL0&=~(ADC10SC+ENC); ADC10CTL1 &= ~(INCH_1+ INCH_5) ; ADC10CTL0 &= ~ADC10IFG; ADC10AE0 = 0x00; } ADC10CTL0 &= ~ADC10IFG; } #pragma vector=PORT2_VECTOR __interrupt void Port2() { if(P2IFG&BIT4) { delay_ms(10); if((P2IFG&BIT4)) { KeyChoose = 4; KeyAction(); P2IFG &= ~BIT4; } } if(P2IFG&BIT3) { delay_ms(10); if(P2IFG&BIT3) { KeyChoose = 3; KeyAction(); P2IFG &= ~BIT3; } } } LCD12864的程序: #include #ifndef LCD12864_H_ #define LCD12864_H_ #define uint unsigned int #define uchar unsigned char extern uchar LCD_table[];//0123456789 //extern void delay_1ms(); extern void delay_nms(uint n); extern void Ini_Lcd(void); extern void wr_string(uchar x,uchar y0, char *p); extern void wr_float(uchar x,uchar y0,float NUM); extern void Send(uchar type,uchar transdata);//0-写控制命令,1-写数据 extern void lcd_pos(uchar x,uchar y);//定位 #endif /* LCD12864_H_ */ #define BIT(x) (1 << (x)) #define uint unsigned int #define uchar unsigned char //static uchar LCD_table[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};//0123456789 void Send(uchar type,uchar transdata); #define cyCS 0 //P2.0,片选信号 #define cySID 1 //P2.1,串行数据 #define cyCLK 2 //P2.2,同步时钟 #define cyPORT P2OUT #define cyDDR P2DIR void delay_1ms() { uint i; for(i=0;i<1140;i++); } void delay_nms(uint n) { uint i = 0; for(i = n;i > 0;i--) delay_1ms(); } void Ini_Lcd(void) { cyDDR |= BIT(cyCLK) + BIT(cySID) + BIT(cyCS); //相应的位端口设置为输出 delay_nms(600); //延时等待液晶完成复位 Send(0,0x30); /*功能设置:一次送8位数据,基本指令集*/ delay_nms(10); Send(0,0x02); /*DDRAM地址归位*/ delay_nms(5); Send(0,0x0c); /*显示设定:开显示,不显示光标,不做当前显示位反白闪动*/ delay_nms(5); Send(0,0x01); /*清屏,将DDRAM的位址计数器调整为“00H”*/ delay_nms(500); Send(0,0x06); /*功能设置,点设定:显示字符/光标从左到右移位,DDRAM地址加1*/ delay_nms(10); } void Send(uchar type,uchar transdata)//0-写控制命令,1-写数据 { uchar firstbyte = 0xf8; uchar temp; uchar i,j = 3; if(type) firstbyte |= 0x02; cyPORT |= BIT(cyCS); cyPORT &= ~BIT(cyCLK); while(j > 0) { if(j == 3) temp = firstbyte; else if(j == 2) temp = transdata&0xf0; else temp = (transdata << 4) & 0xf0; for(i = 8;i > 0;i--) { if(temp & 0x80) cyPORT |= BIT(cySID); else cyPORT &= ~BIT(cySID); cyPORT |= BIT(cyCLK); temp <<= 1; cyPORT &= ~BIT(cyCLK); } //三个字节之间一定要有足够的延时,否则易出现时序问题 if(j == 3) delay_nms(1); else delay_nms(1); j--; } cyPORT &= ~BIT(cySID); cyPORT &= ~BIT(cyCS); } void wr_string(uchar x,uchar y0, char *p) { uchar pos=0;//初始化 uchar cnt=0;//传递过来的是指针 ,cnt为偏移量,然后一个字符一个字符的写入。 switch(y0) { case 0: pos=0x80+x;break; case 1: pos=0x90+x;break; case 2: pos=0x88+x;break; case 3: pos=0x98+x;break; default: break; } Send(0,pos);//写地址 while(*(p+cnt)!=' |